From 2b1332eea54d460115e7a61622a6b58a4031d597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Gomes?= Date: Thu, 21 Feb 2019 11:36:19 +0000 Subject: [PATCH] Ensure there are no links from unknown timezones. The plugin could generate packed files containing links from timezones that are themselves links. This is not well supported by moment-timezone and would result in runtime errors. --- .eslintrc.json | 2 +- src/index.js | 9 +++++++-- test/index.test.js | 11 +++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 04b143e..f6b5229 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,7 +1,7 @@ { "extends": "eslint:recommended", "parserOptions": { - "ecmaVersion": "2017" + "ecmaVersion": 2018 }, "env": { "es6": true, diff --git a/src/index.js b/src/index.js index c50780d..7503e7b 100644 --- a/src/index.js +++ b/src/index.js @@ -28,8 +28,13 @@ function filterData(tzdata, config, file) { const filteredData = moment.tz.filterLinkPack( { version: tzdata.version, - zones: newZonesData, - links: newLinksData.map(link => link.join('|')), + zones: newLinksData.reduce((zones, link) => { + const newEntry = { ...newZonesData.find(z => z.name === link[0]) }; + newEntry.name = link[1]; + zones.push(newEntry); + return zones; + }, newZonesData ), + links: [], }, startYear, endYear diff --git a/test/index.test.js b/test/index.test.js index cb98fc8..c35bfa5 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -212,6 +212,17 @@ describe('usage', () => { const zoneCount = data.zones.length + data.links.length; assert(zoneCount === moment.tz.names().length); }); + + it("does not include links from undefined timezones", async () => { + const { data } = await buildWebpack({ + startYear: 2000 + }); + for (const link of data.links) { + const from = link.split('|')[0]; + const matchingZone = data.zones.find(zone => zone.startsWith(from)); + assert.notEqual(matchingZone, undefined, `'${from}' not in zones`); + } + }); }); describe('caching', () => {