From 051e20591a3d12f6ccd064a1cc4d9abb0c5418fc Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Mon, 16 Aug 2021 15:08:19 +0800 Subject: [PATCH] Lint dist files with `eslint-plugin-compat` (#11245) --- cspell.json | 2 ++ package.json | 11 ++++++++-- scripts/build/build.mjs | 1 + scripts/build/bundler.mjs | 12 ++++------- scripts/bundle-eslint-config.js | 26 ++++++++++++++++++++++ yarn.lock | 38 ++++++++++++++++++++++++++++++++- 6 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 scripts/bundle-eslint-config.js diff --git a/cspell.json b/cspell.json index a55cc8d69a35..2d617833f2e7 100644 --- a/cspell.json +++ b/cspell.json @@ -41,6 +41,7 @@ "Bopomofo", "Breakell", "Brevik", + "browserslist", "builtins", "callees", "camelcase", @@ -59,6 +60,7 @@ "codemods", "combinator", "commonmark", + "compat", "concating", "cond", "corejs", diff --git a/package.json b/package.json index 480cd284bca0..692fc40e3ab1 100644 --- a/package.json +++ b/package.json @@ -109,6 +109,7 @@ "eslint": "7.32.0", "eslint-config-prettier": "8.3.0", "eslint-formatter-friendly": "7.0.0", + "eslint-plugin-compat": "3.11.1", "eslint-plugin-import": "2.24.0", "eslint-plugin-jest": "24.4.0", "eslint-plugin-prettier-internal-rules": "2.0.1", @@ -153,7 +154,7 @@ "lint:eslint": "cross-env EFF_NO_LINK_RULES=true eslint . --format friendly", "lint:changelog": "node ./scripts/lint-changelog.mjs", "lint:prettier": "prettier . \"!test*\" --check", - "lint:dist": "eslint --no-eslintrc --no-ignore --no-inline-config --env=es6,browser --parser-options=ecmaVersion:2019 \"dist/!(bin-prettier|index|third-party).js\"", + "lint:dist": "eslint --no-eslintrc --no-ignore --no-inline-config --config=./scripts/bundle-eslint-config.js \"dist/!(bin-prettier|index|third-party).js\"", "lint:spellcheck": "cspell --no-progress --relative", "lint:deps": "node ./scripts/check-deps.mjs", "lint:actionlint": "node-actionlint", @@ -162,5 +163,11 @@ "fix:prettier": "yarn lint:prettier --write", "build": "node --max-old-space-size=3072 ./scripts/build/build.mjs", "build-docs": "node ./scripts/build-docs.mjs" - } + }, + "browserslist": [ + ">0.5%", + "not ie 11", + "not safari 5.1", + "not op_mini all" + ] } diff --git a/scripts/build/build.mjs b/scripts/build/build.mjs index 4bcf898123b3..1fa3f5a12859 100644 --- a/scripts/build/build.mjs +++ b/scripts/build/build.mjs @@ -137,6 +137,7 @@ async function preparePackage() { pkg.engines.node = ">=10.13.0"; delete pkg.dependencies; delete pkg.devDependencies; + delete pkg.browserslist; pkg.scripts = { prepublishOnly: "node -e \"assert.equal(require('.').version, require('..').version)\"", diff --git a/scripts/build/bundler.mjs b/scripts/build/bundler.mjs index b54eeb4763fc..b6f402556626 100644 --- a/scripts/build/bundler.mjs +++ b/scripts/build/bundler.mjs @@ -18,8 +18,9 @@ import rollupPluginEvaluate from "./rollup-plugins/evaluate.mjs"; import rollupPluginReplaceModule from "./rollup-plugins/replace-module.mjs"; import bundles from "./config.mjs"; -const { __dirname, require } = createEsmUtils(import.meta); +const { __dirname, require, json } = createEsmUtils(import.meta); const PROJECT_ROOT = path.join(__dirname, "../.."); +const packageJson = json.loadSync("../../package.json"); const entries = [ // Force using the CJS file, instead of ESM; i.e. get the file @@ -79,12 +80,7 @@ function getBabelConfig(bundle) { }; const targets = { node: "10" }; if (bundle.target === "universal") { - targets.browsers = [ - ">0.5%", - "not ie 11", - "not safari 5.1", - "not op_mini all", - ]; + targets.browsers = packageJson.browserslist; } config.presets = [ [ @@ -189,7 +185,7 @@ function getRollupConfig(bundle) { // Replace package.json with `{version: "{VERSION}"}` replaceModule[path.join(PROJECT_ROOT, "package.json")] = { code: `export default ${JSON.stringify({ - version: require("../../package.json").version, + version: packageJson.version, })};`, }; diff --git a/scripts/bundle-eslint-config.js b/scripts/bundle-eslint-config.js new file mode 100644 index 000000000000..01e9edda13b1 --- /dev/null +++ b/scripts/bundle-eslint-config.js @@ -0,0 +1,26 @@ +"use strict"; + +const targets = require("../package.json").browserslist; + +module.exports = { + root: true, + env: { + browser: true, + }, + parserOptions: { + ecmaVersion: "latest", + }, + settings: { + targets, + lintAllEsApis: true, + polyfills: [ + // These are not really polyfilled, but seems safe to use in target browsers + "BigInt", + "Symbol.asyncIterator", + ], + }, + plugins: ["compat"], + rules: { + "compat/compat": "error", + }, +}; diff --git a/yarn.lock b/yarn.lock index 97af580b088e..a7eb3fdec948 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1464,6 +1464,11 @@ "@types/yargs" "^16.0.0" chalk "^4.0.0" +"@mdn/browser-compat-data@^3.3.11": + version "3.3.14" + resolved "https://registry.yarnpkg.com/@mdn/browser-compat-data/-/browser-compat-data-3.3.14.tgz#b72a37c654e598f9ae6f8335faaee182bebc6b28" + integrity sha512-n2RC9d6XatVbWFdHLimzzUJxJ1KY8LdjqrW6YvGPiRmsHkhOUx74/Ct10x5Yo7bC/Jvqx7cDEW8IMPv/+vwEzA== + "@nodelib/fs.scandir@2.1.5": version "2.1.5" resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5" @@ -2123,6 +2128,13 @@ array.prototype.flatmap@^1.2.4: es-abstract "^1.18.0-next.1" function-bind "^1.1.1" +ast-metadata-inferer@^0.5.1: + version "0.5.1" + resolved "https://registry.yarnpkg.com/ast-metadata-inferer/-/ast-metadata-inferer-0.5.1.tgz#8268d68b20c03ff3dbce97c15dfa00dcac70fa5c" + integrity sha512-fj+QuB47ODy18p5gJ4BFnpenk992o7gx7oPid6oUK9+Uy/F3/5cvZ13harpQPN5Y8MlcjYf0y1LwgOV1J31k+A== + dependencies: + "@mdn/browser-compat-data" "^3.3.11" + astral-regex@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-2.0.0.tgz#483143c567aeed4785759c0865786dc77d7d2e31" @@ -2334,6 +2346,11 @@ caniuse-lite@^1.0.30001219: resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001237.tgz#4b7783661515b8e7151fc6376cfd97f0e427b9e5" integrity sha512-pDHgRndit6p1NR2GhzMbQ6CkRrp4VKuSsqbcLeOQppYPKOYkKT/6ZvZDvKJUqcmtyWIAHuZq3SVS2vc1egCZzw== +caniuse-lite@^1.0.30001245: + version "1.0.30001251" + resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001251.tgz#6853a606ec50893115db660f82c094d18f096d85" + integrity sha512-HOe1r+9VkU4TFmnU70z+r7OLmtR+/chB1rdcJUeQlAinjEeb0cKL20tlAtOagNZhbrtLnCvV19B4FmF1rgzl6A== + ccount@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/ccount/-/ccount-1.1.0.tgz#246687debb6014735131be8abab2d93898f8d043" @@ -2547,7 +2564,7 @@ core-js-compat@^3.16.0, core-js-compat@^3.9.1: browserslist "^4.16.6" semver "7.0.0" -core-js@3.16.1: +core-js@3.16.1, core-js@^3.15.2: version "3.16.1" resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.16.1.tgz#f4485ce5c9f3c6a7cb18fa80488e08d362097249" integrity sha512-AAkP8i35EbefU+JddyWi12AWE9f2N/qr/pwnDtWz4nyUIBGMJPX99ANFFRSw6FefM374lDujdtLDyhN2A/btHw== @@ -2979,6 +2996,20 @@ eslint-module-utils@^2.6.2: debug "^3.2.7" pkg-dir "^2.0.0" +eslint-plugin-compat@3.11.1: + version "3.11.1" + resolved "https://registry.yarnpkg.com/eslint-plugin-compat/-/eslint-plugin-compat-3.11.1.tgz#f95d7da1cdbb7c05c81d75fa2e1ecee266ccf4aa" + integrity sha512-iJyltnaVN9g/MYL3WGb6GFyJs+4mMkumq2E5srxsQIfPqQh14HEE0dtQC/HKDWze+hkwQtSo5DvC3IW5Gmxdtw== + dependencies: + "@mdn/browser-compat-data" "^3.3.11" + ast-metadata-inferer "^0.5.1" + browserslist "^4.16.6" + caniuse-lite "^1.0.30001245" + core-js "^3.15.2" + find-up "^5.0.0" + lodash.memoize "4.1.2" + semver "7.3.5" + eslint-plugin-import@2.24.0: version "2.24.0" resolved "https://registry.yarnpkg.com/eslint-plugin-import/-/eslint-plugin-import-2.24.0.tgz#697ffd263e24da5e84e03b282f5fb62251777177" @@ -4780,6 +4811,11 @@ lodash.debounce@^4.0.8: resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af" integrity sha1-gteb/zCmfEAF/9XiUVMArZyk168= +lodash.memoize@4.1.2: + version "4.1.2" + resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe" + integrity sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4= + lodash.merge@^4.6.2: version "4.6.2" resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"