diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index ef7403f..0000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,290 +0,0 @@ -## 0.0.39 (2018-08-29) - -* 0.0.39 ([8abba86](https://github.com/wessberg/browserslist-generator/commit/8abba86)) -* Bumped deps ([279aaf3](https://github.com/wessberg/browserslist-generator/commit/279aaf3)) -* Bumped version ([28657ac](https://github.com/wessberg/browserslist-generator/commit/28657ac)) - - - -## 0.0.38 (2018-07-30) - -* 0.0.38 ([d1ab3b6](https://github.com/wessberg/browserslist-generator/commit/d1ab3b6)) -* Bumped deps ([999dc31](https://github.com/wessberg/browserslist-generator/commit/999dc31)) -* Bumped version ([7c439f3](https://github.com/wessberg/browserslist-generator/commit/7c439f3)) - - - -## 0.0.37 (2018-06-15) - -* 0.0.37 ([7344be4](https://github.com/wessberg/browserslist-generator/commit/7344be4)) -* Bumped version ([03b4e8e](https://github.com/wessberg/browserslist-generator/commit/03b4e8e)) -* Fixed a bug ([c72972c](https://github.com/wessberg/browserslist-generator/commit/c72972c)) - - - -## 0.0.36 (2018-06-13) - -* 0.0.36 ([b9aef38](https://github.com/wessberg/browserslist-generator/commit/b9aef38)) -* Added additional memory caching ([35b6bbc](https://github.com/wessberg/browserslist-generator/commit/35b6bbc)) -* Bumped version ([5fe4c07](https://github.com/wessberg/browserslist-generator/commit/5fe4c07)) - - - -## 0.0.35 (2018-06-13) - -* 0.0.35 ([1b57d01](https://github.com/wessberg/browserslist-generator/commit/1b57d01)) -* Added more feature corrections ([7c1a948](https://github.com/wessberg/browserslist-generator/commit/7c1a948)) -* Bumped version ([932de4f](https://github.com/wessberg/browserslist-generator/commit/932de4f)) - - - -## 0.0.34 (2018-06-13) - -* 0.0.34 ([1779931](https://github.com/wessberg/browserslist-generator/commit/1779931)) -* Bumped version ([f29cf0e](https://github.com/wessberg/browserslist-generator/commit/f29cf0e)) -* Made it possible to pass both Caniuse and MDN features to the same functions to allow for mixing and ([97fe6a9](https://github.com/wessberg/browserslist-generator/commit/97fe6a9)) - - - -## 0.0.33 (2018-06-13) - -* 0.0.33 ([8192260](https://github.com/wessberg/browserslist-generator/commit/8192260)) -* Bumped version ([56874a5](https://github.com/wessberg/browserslist-generator/commit/56874a5)) -* Fixed a bug ([c069736](https://github.com/wessberg/browserslist-generator/commit/c069736)) - - - -## 0.0.32 (2018-06-13) - -* 0.0.32 ([6e59464](https://github.com/wessberg/browserslist-generator/commit/6e59464)) -* Added support for working with MDN features as well ([353fe28](https://github.com/wessberg/browserslist-generator/commit/353fe28)) -* Bumped deps ([35dadbd](https://github.com/wessberg/browserslist-generator/commit/35dadbd)) -* Bumped version ([fd23ee2](https://github.com/wessberg/browserslist-generator/commit/fd23ee2)) - - - -## 0.0.31 (2018-06-12) - -* 0.0.31 ([d3498d2](https://github.com/wessberg/browserslist-generator/commit/d3498d2)) -* Bumped version ([de47080](https://github.com/wessberg/browserslist-generator/commit/de47080)) -* Updated IE xhr2 support ([b3d007e](https://github.com/wessberg/browserslist-generator/commit/b3d007e)) - - - -## 0.0.30 (2018-06-07) - -* 0.0.30 ([fb47a3c](https://github.com/wessberg/browserslist-generator/commit/fb47a3c)) -* Bumped version ([8e0dfac](https://github.com/wessberg/browserslist-generator/commit/8e0dfac)) -* Fixed tests ([fef8c65](https://github.com/wessberg/browserslist-generator/commit/fef8c65)) - - - -## 0.0.29 (2018-06-07) - -* 0.0.29 ([624a70b](https://github.com/wessberg/browserslist-generator/commit/624a70b)) -* Bumped version ([71c74a8](https://github.com/wessberg/browserslist-generator/commit/71c74a8)) -* Made and_ff report as desktop firefox ([f33982c](https://github.com/wessberg/browserslist-generator/commit/f33982c)) - - - -## 0.0.28 (2018-06-07) - -* 0.0.28 ([2f9f936](https://github.com/wessberg/browserslist-generator/commit/2f9f936)) -* Bumped version ([32b7c75](https://github.com/wessberg/browserslist-generator/commit/32b7c75)) -* Made Android Chrome report as desktop chrome ([6f3c759](https://github.com/wessberg/browserslist-generator/commit/6f3c759)) - - - -## 0.0.27 (2018-05-23) - -* 0.0.27 ([1c3c0d4](https://github.com/wessberg/browserslist-generator/commit/1c3c0d4)) -* Added a 'normalizeBrowserslist' function ([74abdd2](https://github.com/wessberg/browserslist-generator/commit/74abdd2)) -* Bumped deps ([0aef003](https://github.com/wessberg/browserslist-generator/commit/0aef003)) -* Bumped version ([528acdc](https://github.com/wessberg/browserslist-generator/commit/528acdc)) - - - -## 0.0.26 (2018-05-18) - -* 0.0.26 ([f8e890f](https://github.com/wessberg/browserslist-generator/commit/f8e890f)) -* Bumped deps. Added Patreon to README ([1f3e277](https://github.com/wessberg/browserslist-generator/commit/1f3e277)) -* Bumped version ([0e917f4](https://github.com/wessberg/browserslist-generator/commit/0e917f4)) - - - -## 0.0.25 (2018-05-08) - -* 0.0.25 ([ba1946d](https://github.com/wessberg/browserslist-generator/commit/ba1946d)) -* Bumped version ([76debb1](https://github.com/wessberg/browserslist-generator/commit/76debb1)) -* Fixed a bug ([97749d3](https://github.com/wessberg/browserslist-generator/commit/97749d3)) - - - -## 0.0.24 (2018-05-07) - -* 0.0.24 ([7d2af3f](https://github.com/wessberg/browserslist-generator/commit/7d2af3f)) -* Added a nnew helper: 'userAgentSupportsFeatures' ([4cb7d8b](https://github.com/wessberg/browserslist-generator/commit/4cb7d8b)) -* Bumped version ([9f32fec](https://github.com/wessberg/browserslist-generator/commit/9f32fec)) - - - -## 0.0.23 (2018-05-07) - -* 0.0.23 ([a04682a](https://github.com/wessberg/browserslist-generator/commit/a04682a)) -* Bumped version ([11aba67](https://github.com/wessberg/browserslist-generator/commit/11aba67)) -* Removed an unwanted console.log ([3f5323a](https://github.com/wessberg/browserslist-generator/commit/3f5323a)) - - - -## 0.0.22 (2018-05-07) - -* 0.0.22 ([c62d2a7](https://github.com/wessberg/browserslist-generator/commit/c62d2a7)) -* BBug fixes ([72156dd](https://github.com/wessberg/browserslist-generator/commit/72156dd)) -* Bumped version ([70e686c](https://github.com/wessberg/browserslist-generator/commit/70e686c)) -* Improvements ([83c42e1](https://github.com/wessberg/browserslist-generator/commit/83c42e1)) - - - -## 0.0.21 (2018-05-07) - -* 0.0.21 ([e344488](https://github.com/wessberg/browserslist-generator/commit/e344488)) -* Bumped version ([ec3e712](https://github.com/wessberg/browserslist-generator/commit/ec3e712)) -* Made sure to normalize the browserslists returned by 'browsersWithSupportForFeatures' and 'browsersW ([cd6809a](https://github.com/wessberg/browserslist-generator/commit/cd6809a)) - - - -## 0.0.20 (2018-05-06) - -* 0.0.20 ([4e44851](https://github.com/wessberg/browserslist-generator/commit/4e44851)) -* Bumped version ([b80ab99](https://github.com/wessberg/browserslist-generator/commit/b80ab99)) -* Fixed a bug ([213a490](https://github.com/wessberg/browserslist-generator/commit/213a490)) - - - -## 0.0.19 (2018-05-06) - -* 0.0.19 ([b7df01e](https://github.com/wessberg/browserslist-generator/commit/b7df01e)) -* Bumped version ([26679fc](https://github.com/wessberg/browserslist-generator/commit/26679fc)) -* Fixed a few bugs ([0f1d126](https://github.com/wessberg/browserslist-generator/commit/0f1d126)) - - - -## 0.0.18 (2018-05-06) - -* 0.0.18 ([643f11a](https://github.com/wessberg/browserslist-generator/commit/643f11a)) -* Bumped version ([76e209b](https://github.com/wessberg/browserslist-generator/commit/76e209b)) -* Fixed a few bugs ([c19b747](https://github.com/wessberg/browserslist-generator/commit/c19b747)) - - - -## 0.0.17 (2018-05-05) - -* 0.0.17 ([5e6dc24](https://github.com/wessberg/browserslist-generator/commit/5e6dc24)) -* Bumped version ([0178a19](https://github.com/wessberg/browserslist-generator/commit/0178a19)) -* Reffinements and testing ([0f6be0f](https://github.com/wessberg/browserslist-generator/commit/0f6be0f)) -* Removed dependency on external library and rewrote logic for matching a useragent on a browserslist ([18ae325](https://github.com/wessberg/browserslist-generator/commit/18ae325)) - - - -## 0.0.16 (2018-05-05) - -* 0.0.16 ([237beaa](https://github.com/wessberg/browserslist-generator/commit/237beaa)) -* Bumped version ([e1e07be](https://github.com/wessberg/browserslist-generator/commit/e1e07be)) -* Updated keywords ([e7aae8a](https://github.com/wessberg/browserslist-generator/commit/e7aae8a)) - - - -## 0.0.15 (2018-05-05) - -* 0.0.15 ([9a53ef7](https://github.com/wessberg/browserslist-generator/commit/9a53ef7)) -* Bumped version ([949a34a](https://github.com/wessberg/browserslist-generator/commit/949a34a)) -* Updated README ([6feb72d](https://github.com/wessberg/browserslist-generator/commit/6feb72d)) - - - -## 0.0.14 (2018-05-05) - -* 0.0.14 ([87ee3fb](https://github.com/wessberg/browserslist-generator/commit/87ee3fb)) -* Bumped version ([778c6a6](https://github.com/wessberg/browserslist-generator/commit/778c6a6)) -* Updated README examples ([a44d1cb](https://github.com/wessberg/browserslist-generator/commit/a44d1cb)) - - - -## 0.0.13 (2018-05-05) - -* 0.0.13 ([af7bbca](https://github.com/wessberg/browserslist-generator/commit/af7bbca)) -* Bumped version ([b3e5f03](https://github.com/wessberg/browserslist-generator/commit/b3e5f03)) -* Fixed a bug ([5c20350](https://github.com/wessberg/browserslist-generator/commit/5c20350)) - - - -## 0.0.12 (2018-05-05) - -* 0.0.12 ([d7697e9](https://github.com/wessberg/browserslist-generator/commit/d7697e9)) -* Bumped version ([86fd978](https://github.com/wessberg/browserslist-generator/commit/86fd978)) -* Fixed a bug ([012c9be](https://github.com/wessberg/browserslist-generator/commit/012c9be)) - - - -## 0.0.11 (2018-05-05) - -* 0.0.11 ([998d5a4](https://github.com/wessberg/browserslist-generator/commit/998d5a4)) -* Bug fixes and improvements ([f9540d0](https://github.com/wessberg/browserslist-generator/commit/f9540d0)) -* Bumped version ([05f1105](https://github.com/wessberg/browserslist-generator/commit/05f1105)) - - - -## 0.0.10 (2018-05-05) - -* 0.0.10 ([7803078](https://github.com/wessberg/browserslist-generator/commit/7803078)) -* Added a 'browserslistSupportsFeatures' function that returns true if the given browserslist support ([1c13395](https://github.com/wessberg/browserslist-generator/commit/1c13395)) -* Bumped version ([2c381fe](https://github.com/wessberg/browserslist-generator/commit/2c381fe)) - - - -## 0.0.9 (2018-05-05) - -* 0.0.9 ([8bb7932](https://github.com/wessberg/browserslist-generator/commit/8bb7932)) -* Added a function that generating a browserslist based on browsers that - doesn't - support specific ([fc8c47c](https://github.com/wessberg/browserslist-generator/commit/fc8c47c)) -* Bumped version ([b80ae05](https://github.com/wessberg/browserslist-generator/commit/b80ae05)) - - - -## 0.0.8 (2018-05-05) - -* 0.0.8 ([d806aca](https://github.com/wessberg/browserslist-generator/commit/d806aca)) -* Bumped version ([031d80d](https://github.com/wessberg/browserslist-generator/commit/031d80d)) -* Fixed a few bugs ([adc38e5](https://github.com/wessberg/browserslist-generator/commit/adc38e5)) - - - -## 0.0.7 (2018-05-01) - -* 0.0.7 ([69b2c46](https://github.com/wessberg/browserslist-generator/commit/69b2c46)) -* Added support for matching a browserslist query on a user agent ([69db5c0](https://github.com/wessberg/browserslist-generator/commit/69db5c0)) -* Bumped version ([fb05ae7](https://github.com/wessberg/browserslist-generator/commit/fb05ae7)) - - - -## 0.0.6 (2018-04-30) - -* 0.0.6 ([0af214f](https://github.com/wessberg/browserslist-generator/commit/0af214f)) -* Added changelog ([1442788](https://github.com/wessberg/browserslist-generator/commit/1442788)) -* Fixed a few issues ([dcf5a88](https://github.com/wessberg/browserslist-generator/commit/dcf5a88)) - - - -## 0.0.5 (2018-04-30) - -* 0.0.5 ([bb70540](https://github.com/wessberg/browserslist-generator/commit/bb70540)) -* Bumped version ([fab76c6](https://github.com/wessberg/browserslist-generator/commit/fab76c6)) - - - -## 0.0.4 (2018-04-30) - -* 0.0.4 ([f4b6558](https://github.com/wessberg/browserslist-generator/commit/f4b6558)) -* First commit ([1342ad1](https://github.com/wessberg/browserslist-generator/commit/1342ad1)) - - - diff --git a/README.md b/README.md index 44c38ee..b443fa9 100644 --- a/README.md +++ b/README.md @@ -116,16 +116,39 @@ MIT © [Frederik Wessberg](https://github.com/wessberg) ##### `browsersWithSupportForFeatures (...features: string[]): string[]` -Takes any amount of [caniuse](https://caniuse.com/) or [MDN](https://github.com/mdn/browser-compat-data) features and generates a browserslist that targets all browsers that support these features +Takes any amount of [caniuse](https://caniuse.com/) or [MDN](https://github.com/mdn/browser-compat-data) features and generates a [browserslist](https://github.com/browserslist/browserslist) that targets all browsers that support these features ##### `browsersWithoutSupportForFeatures (...features: string[]): string[]` -Takes any amount of [caniuse](https://caniuse.com/) or [MDN](https://github.com/mdn/browser-compat-data) features and generates a browserslist that targets all browsers that _doesn't_ support these features +Takes any amount of [caniuse](https://caniuse.com/) or [MDN](https://github.com/mdn/browser-compat-data) features and generates a [browserslist](https://github.com/browserslist/browserslist) that targets all browsers that _doesn't_ support these features + +##### `browsersWithSupportForEcmaVersion (version: EcmaVersion): string[]` + +Generates a [browserslist](https://github.com/browserslist/browserslist) that targets all the browsers that support the given Ecma version. + +Possible Ecma versions are: + +- `es3` +- `es5` +- `es2015` +- `es2016` +- `es2017` +- `es2018` ##### `browserslistSupportsFeatures (browserslist: string[], ...features: string[]): boolean` Returns true if the given [browserslist](https://github.com/browserslist/browserslist) supports all of the given [caniuse](https://caniuse.com/) or [MDN](https://github.com/mdn/browser-compat-data) features +##### `browserslistSupportsEcmaVersion (browserslist: string[], version: EcmaVersion): boolean` + +Returns true if the given [browserslist](https://github.com/browserslist/browserslist) supports the given Ecma version. +See [this section](#browserswithsupportforecmaversion-version-ecmaversion-string) for an overview of all supported Ecma versions. + +##### `getAppropriateEcmaVersionForBrowserslist (browserslist: string[]): EcmaVersion` + +Gets the Ecma version that is most appropriate for the given [browserslist](https://github.com/browserslist/browserslist). +See [this section](#browserswithsupportforecmaversion-version-ecmaversion-string) for an overview of all supported Ecma versions. + #### `matchBrowserslistOnUserAgent (userAgent: string, browserslist: string[]): boolean` Will check if the given user agent string matches the given browserslist and return true if so. diff --git a/package.json b/package.json index 08a1c58..a79eb61 100644 --- a/package.json +++ b/package.json @@ -52,17 +52,17 @@ "@wessberg/ts-config": "0.0.30", "ava": "^0.25.0", "conventional-changelog-cli": "^2.0.5", - "husky": "^1.0.0-rc.15", + "husky": "^1.1.1", "tslint": "^5.11.0", - "typescript": "^3.0.3" + "typescript": "^3.1.2" }, "dependencies": { "@types/object-path": "^0.9.29", "@types/semver": "^5.5.0", "@types/ua-parser-js": "^0.7.32", - "browserslist": "^4.1.1", - "caniuse-lite": "^1.0.30000887", - "mdn-browser-compat-data": "0.0.51", + "browserslist": "^4.2.0", + "caniuse-lite": "^1.0.30000890", + "mdn-browser-compat-data": "0.0.53", "object-path": "^0.11.4", "semver": "^5.5.1", "tslib": "^1.9.3", diff --git a/src/browserslist-generator/browserslist-generator.ts b/src/browserslist-generator/browserslist-generator.ts index 09c46cd..d60ad21 100644 --- a/src/browserslist-generator/browserslist-generator.ts +++ b/src/browserslist-generator/browserslist-generator.ts @@ -10,6 +10,7 @@ import {UAParser} from "ua-parser-js"; import {getLatestVersionOfBrowser, getNextVersionOfBrowser, getOldestVersionOfBrowser, getPreviousVersionOfBrowser, getSortedBrowserVersions} from "./browser-version"; import {compareVersions} from "./compare-versions"; import {ComparisonOperator} from "./comparison-operator"; +import {EcmaVersion, ES2015_FEATURES, ES2016_FEATURES, ES2017_FEATURES, ES2018_FEATURES, ES5_FEATURES} from "./ecma-version"; import {IBrowserSupportForFeaturesCommonResult} from "./i-browser-support-for-features-common-result"; import {CaniuseBrowser, CaniuseStats, CaniuseStatsNormalized, CaniuseSupportKind, ICaniuseBrowserCorrection, ICaniuseDataCorrection, ICaniuseFeature} from "./i-caniuse"; import {IMdn, MdnBrowserName} from "./i-mdn"; @@ -46,7 +47,7 @@ const userAgentWithFeaturesToSupportCache: Map = new Map(); * A Map between features and browsers that has partial support for them but should be allowed anyway * @type {Map} */ -const PARTIAL_SUPPORT_ALLOWANCES = > new Map([ +const PARTIAL_SUPPORT_ALLOWANCES = > new Map([ [ "shadowdomv1", "*" @@ -92,46 +93,28 @@ function rangeCorrection (browser: CaniuseBrowser, supportKind: CaniuseSupportKi if (start == null && end == null) { shouldSet = true; - } - - else if (start != null && end == null) { + } else if (start != null && end == null) { if (version === "TP") { shouldSet = true; - } - - else if (version === "all") { + } else if (version === "all") { shouldSet = true; - } - - else { + } else { shouldSet = gte(coerceVersion(version), coerceVersion(start)); } - } - - else if (start == null && end != null) { + } else if (start == null && end != null) { if (version === "TP") { shouldSet = end === "TP"; - } - - else if (version === "all") { + } else if (version === "all") { shouldSet = true; - } - - else { + } else { shouldSet = lte(coerceVersion(version), coerceVersion(end)); } - } - - else if (start != null && end != null) { + } else if (start != null && end != null) { if (version === "TP") { shouldSet = end === "TP"; - } - - else if (version === "all") { + } else if (version === "all") { shouldSet = true; - } - - else { + } else { shouldSet = gte(coerceVersion(version), coerceVersion(start)) && lte(coerceVersion(version), coerceVersion(end)); } } @@ -401,6 +384,71 @@ export function browsersWithSupportForFeatures (...features: string[]): string[] return extendQueryWithUnreleasedVersions(query, browsers); } +/** + * Returns true if the given Browserslist supports the given EcmaVersion + * @param browserslist + * @param version + */ +export function browserslistSupportsEcmaVersion (browserslist: string[], version: EcmaVersion): boolean { + switch (version) { + case "es3": + // ES3 is the lowest possible target and will always be treated as supported + return true; + + case "es5": + return browserslistSupportsFeatures(browserslist, ...ES5_FEATURES); + + case "es2015": + return browserslistSupportsFeatures(browserslist, ...ES2015_FEATURES); + + case "es2016": + return browserslistSupportsFeatures(browserslist, ...ES2016_FEATURES); + + case "es2017": + return browserslistSupportsFeatures(browserslist, ...ES2017_FEATURES); + + case "es2018": + return browserslistSupportsFeatures(browserslist, ...ES2018_FEATURES); + } +} + +/** + * Returns the appropriate Ecma version for the given Browserslist + * @param {string[]} browserslist + * @returns {EcmaVersion} + */ +export function getAppropriateEcmaVersionForBrowserslist (browserslist: string[]): EcmaVersion { + if (browserslistSupportsEcmaVersion(browserslist, "es2018")) return "es2018"; + else if (browserslistSupportsEcmaVersion(browserslist, "es2017")) return "es2017"; + else if (browserslistSupportsEcmaVersion(browserslist, "es2016")) return "es2016"; + else if (browserslistSupportsEcmaVersion(browserslist, "es2015")) return "es2015"; + else if (browserslistSupportsEcmaVersion(browserslist, "es5")) return "es5"; + else return "es3"; +} + +/** + * Generates a Browserslist based on browser support for the given ECMA version + * @param {EcmaVersion} version + * @returns {string[]} + */ +export function browsersWithSupportForEcmaVersion (version: EcmaVersion): string[] { + + switch (version) { + case "es3": + return browsersWithoutSupportForFeatures(...ES5_FEATURES); + case "es5": + return browsersWithSupportForFeatures(...ES5_FEATURES); + case "es2015": + return browsersWithSupportForFeatures(...ES2015_FEATURES); + case "es2016": + return browsersWithSupportForFeatures(...ES2016_FEATURES); + case "es2017": + return browsersWithSupportForFeatures(...ES2017_FEATURES); + case "es2018": + return browsersWithSupportForFeatures(...ES2018_FEATURES); + } +} + /** * Returns true if the given browserslist support all of the given features * @param {string[]} browserslist @@ -472,20 +520,13 @@ function getCaniuseLiteFeatureNormalized (stats: CaniuseStats, featureName: stri if (support === CaniuseSupportKind.AVAILABLE || support === CaniuseSupportKind.UNAVAILABLE || support === CaniuseSupportKind.PARTIAL_SUPPORT || support === CaniuseSupportKind.PREFIXED) { supportKind = support; - } - - else if (support.startsWith("y")) { + } else if (support.startsWith("y")) { supportKind = CaniuseSupportKind.AVAILABLE; - } - - else if (support.startsWith("n")) { + } else if (support.startsWith("n")) { supportKind = CaniuseSupportKind.UNAVAILABLE; - } - - else if (support.startsWith("a")) { + } else if (support.startsWith("a")) { supportKind = CaniuseSupportKind.PARTIAL_SUPPORT; - } - else { + } else { supportKind = CaniuseSupportKind.PREFIXED; } @@ -605,9 +646,7 @@ function getMdnFeatureSupport (feature: string): CaniuseStatsNormalized { // If the features has never been supported, mark the feature as unavailable if (supportedSince == null) { dict[version] = CaniuseSupportKind.UNAVAILABLE; - } - - else { + } else { dict[version] = version === "TP" || version === "all" || gte(coerceVersion(version), coerceVersion(supportedSince)) ? CaniuseSupportKind.AVAILABLE : CaniuseSupportKind.UNAVAILABLE; } }); @@ -980,9 +1019,7 @@ function getCaniuseVersionForUseragentVersion (browser: CaniuseBrowser, version: // Always use 'all' with Opera Mini if (browser === "op_mini") { return "all"; - } - - else if (browser === "safari") { + } else if (browser === "safari") { // Check if there is a newer version of the browser const nextBrowserVersion = getNextVersionOfBrowser(browser, version); @@ -1059,13 +1096,9 @@ function getCaniuseVersionForUseragentVersion (browser: CaniuseBrowser, version: // Up to version 4.4.4, these could include patch releases. After that, only use major versions if (major < 4) { return buildSemverVersion(major, minor); - } - - else if (major === 4) { + } else if (major === 4) { return buildSemverVersion(major, minor, patch); - } - - else { + } else { return buildSemverVersion(major); } diff --git a/src/browserslist-generator/ecma-version.ts b/src/browserslist-generator/ecma-version.ts new file mode 100644 index 0000000..3a85320 --- /dev/null +++ b/src/browserslist-generator/ecma-version.ts @@ -0,0 +1,46 @@ +export type EcmaVersion = + "es3" + | "es5" + | "es2015" + | "es2016" + | "es2017" + | "es2018"; + +export const ES5_FEATURES: string[] = [ + "es5" +]; + +export const ES2015_FEATURES: string[] = [ + "es6-class", + "const", + "let", + "arrow-functions", + "rest-parameters", + "template-literals", + "javascript.operators.destructuring", + "javascript.operators.spread.spread_in_arrays", + "javascript.functions.default_parameters", + "javascript.builtins.RegExp.sticky", + "javascript.operators.object_initializer.shorthand_property_names", + "javascript.operators.object_initializer.computed_property_names", + "javascript.operators.object_initializer.shorthand_method_names" +]; + +export const ES2016_FEATURES: string[] = [ + "javascript.operators.arithmetic.exponentiation", + "javascript.builtins.Array.includes" +]; + +export const ES2017_FEATURES: string[] = [ + "async-functions", + "javascript.builtins.Object.values", + "javascript.builtins.Object.entries", + "javascript.builtins.Object.getOwnPropertyDescriptors", + "javascript.builtins.String.padStart", + "javascript.builtins.String.padEnd" +]; + +export const ES2018_FEATURES: string[] = [ + "javascript.operators.spread.spread_in_object_literals", + "javascript.builtins.Promise.finally" +]; \ No newline at end of file diff --git a/test/browserslist-generator/browserslist-generator.test.ts b/test/browserslist-generator/browserslist-generator.test.ts index dd8d80b..fddcfe4 100644 --- a/test/browserslist-generator/browserslist-generator.test.ts +++ b/test/browserslist-generator/browserslist-generator.test.ts @@ -1,10 +1,12 @@ import test from "ava"; // @ts-ignore import {chrome, edge, firefox, ie, safari} from "useragent-generator"; -import {browserslistSupportsFeatures, browsersWithoutSupportForFeatures, browsersWithSupportForFeatures, getFirstVersionsWithFullSupport, matchBrowserslistOnUserAgent, userAgentSupportsFeatures, generateBrowserslistFromUseragent} from "../../src/browserslist-generator/browserslist-generator"; +import {browserslistSupportsFeatures, browsersWithoutSupportForFeatures, browsersWithSupportForFeatures, getFirstVersionsWithFullSupport, matchBrowserslistOnUserAgent, userAgentSupportsFeatures, generateBrowserslistFromUseragent, browsersWithSupportForEcmaVersion, browserslistSupportsEcmaVersion, getAppropriateEcmaVersionForBrowserslist} from "../../src/browserslist-generator/browserslist-generator"; // tslint:disable:no-duplicate-string +// tslint:disable:no-identical-functions + const ES_MODULE_FEATURE_NAME = "es6-module"; const SHADOW_DOM_FEATURE_NAME = "shadowdomv1"; const CUSTOM_ELEMENTS_FEATURE_NAME = "custom-elementsv1"; @@ -176,4 +178,118 @@ test("generateBrowserslistFromUseragent() => Will fall back to the latest known test("userAgentSupportsFeatures() => Correctly determines that Chrome 70 supports Web Animations (even though support is partial) #1", t => { t.true(userAgentSupportsFeatures(chrome("70"), "web-animation")); +}); + +test("browsersWithSupportForEcmaVersion() => Correctly determines that a Browserslist generated for targeting ES3 doesn't support ES5 features #1", t => { + t.false( + browserslistSupportsFeatures(browsersWithSupportForEcmaVersion("es3"), "es5") + ); +}); + +test("browsersWithSupportForEcmaVersion() => Correctly determines that a Browserslist generated for targeting ES5 doesn't support ES2015 features #1", t => { + t.false( + browserslistSupportsFeatures(browsersWithSupportForEcmaVersion("es5"), "es6-class") + ); +}); + +test("browsersWithSupportForEcmaVersion() => Correctly determines that a Browserslist generated for targeting ES2015 doesn't support ES2016 features #1", t => { + t.false( + browserslistSupportsFeatures(browsersWithSupportForEcmaVersion("es2015"), "javascript.operators.arithmetic.exponentiation") + ); +}); + +test("browsersWithSupportForEcmaVersion() => Correctly determines that a Browserslist generated for targeting ES2016 doesn't support ES2017 features #1", t => { + t.false( + browserslistSupportsFeatures(browsersWithSupportForEcmaVersion("es2016"), "async-functions") + ); +}); + +test("browsersWithSupportForEcmaVersion() => Correctly determines that a Browserslist generated for targeting ES2017 doesn't support ES2018 features #1", t => { + t.false( + browserslistSupportsFeatures(browsersWithSupportForEcmaVersion("es2017"), "javascript.operators.spread.spread_in_object_literals") + ); +}); + +test("browsersWithSupportForEcmaVersion() => Correctly determines that a Browserslist generated for targeting ES2018 does support LATEST features #1", t => { + t.true( + browserslistSupportsFeatures(browsersWithSupportForEcmaVersion("es2018"), "javascript.operators.spread.spread_in_object_literals") + ); +}); + +test("browserslistSupportsEcmaVersion() => Correctly determines that a Browserslist generated for targeting ES3 doesn't support ES5 features #1", t => { + t.false( + browserslistSupportsEcmaVersion(browsersWithSupportForEcmaVersion("es3"), "es5") + ); +}); + +test("browserslistSupportsEcmaVersion() => Correctly determines that a Browserslist generated for targeting ES5 doesn't support ES2015 features #1", t => { + t.false( + browserslistSupportsEcmaVersion(browsersWithSupportForEcmaVersion("es5"), "es2015") + ); +}); + +test("browserslistSupportsEcmaVersion() => Correctly determines that a Browserslist generated for targeting ES2015 doesn't support ES2016 features #1", t => { + t.false( + browserslistSupportsEcmaVersion(browsersWithSupportForEcmaVersion("es2015"), "es2016") + ); +}); + +test("browserslistSupportsEcmaVersion() => Correctly determines that a Browserslist generated for targeting ES2016 doesn't support ES2017 features #1", t => { + t.false( + browserslistSupportsEcmaVersion(browsersWithSupportForEcmaVersion("es2016"), "es2017") + ); +}); + +test("browserslistSupportsEcmaVersion() => Correctly determines that a Browserslist generated for targeting ES2017 doesn't support ES2018 features #1", t => { + t.false( + browserslistSupportsEcmaVersion(browsersWithSupportForEcmaVersion("es2017"), "es2018") + ); +}); + +test("browserslistSupportsEcmaVersion() => Correctly determines that a Browserslist generated for targeting ES2018 does support LATEST features #1", t => { + t.true( + browserslistSupportsEcmaVersion(browsersWithSupportForEcmaVersion("es2018"), "es2018") + ); +}); + +test("getAppropriateEcmaVersionForBrowserslist() => Correctly determines that the most appropriate Ecma version for a Browserslist targeting browsers only compatible with ES3 is indeed ES3 #1", t => { + t.deepEqual( + getAppropriateEcmaVersionForBrowserslist(browsersWithSupportForEcmaVersion("es3")), + "es3" + ); +}); + +test("getAppropriateEcmaVersionForBrowserslist() => Correctly determines that the most appropriate Ecma version for a Browserslist targeting browsers only compatible with ES5 is indeed ES5 #1", t => { + t.deepEqual( + getAppropriateEcmaVersionForBrowserslist(browsersWithSupportForEcmaVersion("es5")), + "es5" + ); +}); + +test("getAppropriateEcmaVersionForBrowserslist() => Correctly determines that the most appropriate Ecma version for a Browserslist targeting browsers only compatible with ES2015 is indeed ES2015 #1", t => { + t.deepEqual( + getAppropriateEcmaVersionForBrowserslist(browsersWithSupportForEcmaVersion("es2015")), + "es2015" + ); +}); + +test("getAppropriateEcmaVersionForBrowserslist() => Correctly determines that the most appropriate Ecma version for a Browserslist targeting browsers only compatible with ES2016 is indeed ES2016 #1", t => { + t.deepEqual( + getAppropriateEcmaVersionForBrowserslist(browsersWithSupportForEcmaVersion("es2016")), + "es2016" + ); +}); + +test("getAppropriateEcmaVersionForBrowserslist() => Correctly determines that the most appropriate Ecma version for a Browserslist targeting browsers only compatible with ES2017 is indeed ES2017 #1", t => { + t.deepEqual( + getAppropriateEcmaVersionForBrowserslist(browsersWithSupportForEcmaVersion("es2017")), + "es2017" + ); +}); + +test("getAppropriateEcmaVersionForBrowserslist() => Correctly determines that the most appropriate Ecma version for a Browserslist targeting browsers only compatible with ES2018 is indeed ES2018 #1", t => { + t.deepEqual( + getAppropriateEcmaVersionForBrowserslist(browsersWithSupportForEcmaVersion("es2018")), + "es2018" + ); }); \ No newline at end of file