-
-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes: #36
- Loading branch information
Showing
6 changed files
with
391 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Enforce the use of built-in methods instead of unnecessary polyfills | ||
|
||
<!-- Do not manually modify RULE_NOTICE part. Run: `npm run generate-rule-notices` --> | ||
<!-- RULE_NOTICE --> | ||
✅ *This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.* | ||
<!-- /RULE_NOTICE --> | ||
|
||
This rules helps to use existing methods instead of using extra polyfills. | ||
|
||
## Fail | ||
|
||
package.json | ||
|
||
```json | ||
{ | ||
"engines": { | ||
"node": ">=8" | ||
} | ||
} | ||
``` | ||
|
||
```js | ||
const assign = require('object-assign'); | ||
``` | ||
|
||
## Pass | ||
|
||
package.json | ||
|
||
```json | ||
{ | ||
"engines": { | ||
"node": "4" | ||
} | ||
} | ||
``` | ||
|
||
```js | ||
const assign = require('object-assign'); // passes as Object.assign is not supported | ||
``` | ||
|
||
## Options | ||
|
||
Type: `object` | ||
|
||
### nodeVersion | ||
|
||
Type: `string` | ||
|
||
By default, the target version of Node.js is taken from the `package.json`, in the `engines.node` field. If you want to override this, you can set the `nodeVersion` option. | ||
|
||
**Note:** The SemVer syntax is fully supported in both the `package.json` and this option. | ||
|
||
```js | ||
"unicorn/no-unnecessary-polyfills": ["error", { "nodeVersion": ">=12" }] | ||
``` | ||
|
||
```js | ||
"unicorn/no-unnecessary-polyfills": ["error", { "nodeVersion": "14.1.0" }] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
'use strict'; | ||
const semver = require('semver'); | ||
const readPkgUp = require('read-pkg-up'); | ||
const {data: compatData, entries: coreJsEntries} = require('core-js-compat'); | ||
const {camelCase, upperFirst} = require('lodash'); | ||
|
||
const MESSAGE_ID_POLYFILL = 'unnecessaryPolyfill'; | ||
const MESSAGE_ID_CORE_JS = 'unnecessaryCoreJsModule'; | ||
const messages = { | ||
[MESSAGE_ID_POLYFILL]: 'Use the built-in `{{featureName}}`.', | ||
[MESSAGE_ID_CORE_JS]: 'All polyfilled features imported from `{{coreJsModule}}` are disponible as built-ins. Use the built-ins instead.', | ||
}; | ||
|
||
function getNodeEngineVersion(cwd) { | ||
const result = readPkgUp.sync({cwd}); | ||
return result && result.pkg && result.pkg.engines && result.pkg.engines.node; | ||
} | ||
|
||
// Hard-coded compat data | ||
compatData['node.util.promisify'] = {node: '8'}; | ||
|
||
const constructorCaseExceptions = { | ||
regexp: 'RegExp', | ||
util: 'util', | ||
}; | ||
function constructorCase(name) { | ||
return constructorCaseExceptions[name] || upperFirst(camelCase(name)); | ||
} | ||
|
||
const additionalPolyfillPatterns = { | ||
'es.promise.finally': '|(p-finally)', | ||
'es.object.set-prototype-of': '|(setprototypeof)', | ||
'es.string.code-point-at': '|(code-point-at)', | ||
}; | ||
|
||
const prefixes = '(mdn-polyfills|polyfill-)'; | ||
const suffixes = '(-polyfill)'; | ||
const delimiter = '(\\.|-|\\.prototype\\.|/)?'; | ||
|
||
const polyfills = Object.keys(compatData).map(feature => { | ||
let [ecmaVersion, constructorName, methodName = ''] = feature.split('.'); | ||
|
||
if (ecmaVersion === 'es') { | ||
ecmaVersion = `(${ecmaVersion}\\d*)`; | ||
} | ||
|
||
constructorName = `(${constructorName}|${camelCase(constructorName)})`; | ||
if (methodName) { | ||
methodName = `(${methodName}|${camelCase(methodName)})`; | ||
} | ||
|
||
const methodOrConstructor = methodName || constructorName; | ||
|
||
return { | ||
feature, | ||
pattern: new RegExp( | ||
`^((${prefixes}?` | ||
+ `(${ecmaVersion}${delimiter}${constructorName}${delimiter}${methodName}|` // Ex: es6-array-copy-within | ||
+ `${constructorName}${delimiter}${methodName}|` // Ex: array-copy-within | ||
+ `${ecmaVersion}${delimiter}${constructorName})` // Ex: es6-array | ||
+ `${suffixes}?)|` | ||
+ `(${prefixes}${methodOrConstructor}|${methodOrConstructor}${suffixes})` // Ex: polyfill-copy-within / polyfill-promise | ||
+ `${additionalPolyfillPatterns[feature] || ''})$`, | ||
'i', | ||
), | ||
}; | ||
}); | ||
|
||
function checkVersion(nodeVersion, featureVersion) { | ||
const supportedNodeVersion = semver.coerce(featureVersion); | ||
const validTargetVersion = semver.minVersion(nodeVersion); | ||
return validTargetVersion | ||
? semver.lte(supportedNodeVersion, validTargetVersion) | ||
: semver.ltr(supportedNodeVersion, nodeVersion); | ||
} | ||
|
||
function report(context, node, feature) { | ||
let [ecmaVersion, namespace, method = ''] = feature.split('.'); | ||
if (namespace === 'typed-array' && method.endsWith('-array')) { | ||
namespace = method; | ||
method = ''; | ||
} | ||
|
||
const delimiter = method && (ecmaVersion === 'node' ? '.' : '#'); | ||
|
||
context.report({ | ||
node, | ||
messageId: MESSAGE_ID_POLYFILL, | ||
data: { | ||
featureName: `${constructorCase(namespace)}${delimiter}${camelCase(method)}`, | ||
}, | ||
}); | ||
} | ||
|
||
function processRule(context, node, moduleName, nodeVersion) { | ||
if (!moduleName || typeof moduleName !== 'string') { | ||
return; | ||
} | ||
|
||
const importedModule = moduleName.replace(/\.[^/.]+$/, ''); | ||
const coreJsModuleFeatures = coreJsEntries[importedModule.replace('core-js-pure', 'core-js')]; | ||
|
||
if (coreJsModuleFeatures) { | ||
if (coreJsModuleFeatures.length === 1) { | ||
if (checkVersion(nodeVersion, compatData[coreJsModuleFeatures[0]].node)) { | ||
report(context, node, coreJsModuleFeatures[0]); | ||
} | ||
} else if (coreJsModuleFeatures.every(feature => compatData[feature].node && checkVersion(nodeVersion, compatData[feature].node))) { | ||
context.report({ | ||
node, | ||
messageId: MESSAGE_ID_CORE_JS, | ||
data: { | ||
coreJsModule: moduleName, | ||
}, | ||
}); | ||
} | ||
return; | ||
} | ||
|
||
const polyfill = polyfills.find(({pattern}) => pattern.test(moduleName)); | ||
if (polyfill && checkVersion(nodeVersion, compatData[polyfill.feature].node)) { | ||
report(context, node, polyfill.feature); | ||
} | ||
} | ||
|
||
function create(context) { | ||
const options = context.options[0]; | ||
const nodeVersion = (options && options.nodeVersion) || getNodeEngineVersion(context.getFilename()); | ||
|
||
if (!nodeVersion) { | ||
return {}; | ||
} | ||
|
||
return { | ||
'CallExpression[callee.name="require"]'(node) { | ||
processRule(context, node, node.arguments[0].value, nodeVersion); | ||
}, | ||
'ImportDeclaration, ImportExpression'(node) { | ||
processRule(context, node, node.source.value, nodeVersion); | ||
}, | ||
}; | ||
} | ||
|
||
const schema = [ | ||
{ | ||
type: 'object', | ||
additionalProperties: false, | ||
properties: { | ||
nodeVersion: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
]; | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Enforce the use of built-in methods instead of unnecessary polyfills.', | ||
}, | ||
schema, | ||
messages, | ||
}, | ||
}; |
Oops, something went wrong.