|
| 1 | +'use strict'; |
| 2 | +const semver = require('semver'); |
| 3 | +const readPkgUp = require('read-pkg-up'); |
| 4 | +const coreJsCompat = require('core-js-compat'); |
| 5 | +const {camelCase, upperFirst} = require('lodash'); |
| 6 | + |
| 7 | +const {data: compatData, entries: coreJsEntries} = coreJsCompat; |
| 8 | + |
| 9 | +const MESSAGE_ID_POLYFILL = 'unnecessaryPolyfill'; |
| 10 | +const MESSAGE_ID_CORE_JS = 'unnecessaryCoreJsModule'; |
| 11 | +const messages = { |
| 12 | + [MESSAGE_ID_POLYFILL]: 'Use the built-in `{{featureName}}`.', |
| 13 | + [MESSAGE_ID_CORE_JS]: 'All polyfilled features imported from `{{coreJsModule}}` are disponible as built-ins. Use the built-ins instead.', |
| 14 | +}; |
| 15 | + |
| 16 | +function getTargetsFromPkg(cwd, useBrowserlistFieldByDefault) { |
| 17 | + const result = readPkgUp.sync({cwd}); |
| 18 | + if (!result || !result.pkg) { |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + const {browserlist} = result.pkg; |
| 23 | + const nodeEngine = result.pkg.engines && result.pkg.engines.node && new SemverNodeVersion(result.pkg.engines.node); |
| 24 | + return useBrowserlistFieldByDefault ? browserlist || nodeEngine : nodeEngine || browserlist; |
| 25 | +} |
| 26 | + |
| 27 | +const constructorCaseExceptions = { |
| 28 | + regexp: 'RegExp', |
| 29 | +}; |
| 30 | +function constructorCase(name) { |
| 31 | + return constructorCaseExceptions[name] || upperFirst(camelCase(name)); |
| 32 | +} |
| 33 | + |
| 34 | +const additionalPolyfillPatterns = { |
| 35 | + 'es.promise.finally': '|(p-finally)', |
| 36 | + 'es.object.set-prototype-of': '|(setprototypeof)', |
| 37 | + 'es.string.code-point-at': '|(code-point-at)', |
| 38 | +}; |
| 39 | + |
| 40 | +const prefixes = '(mdn-polyfills|polyfill-)'; |
| 41 | +const suffixes = '(-polyfill)'; |
| 42 | +const delimiter = '(\\.|-|\\.prototype\\.|/)?'; |
| 43 | + |
| 44 | +const polyfills = Object.keys(compatData).map(feature => { |
| 45 | + let [ecmaVersion, constructorName, methodName = ''] = feature.split('.'); |
| 46 | + |
| 47 | + if (ecmaVersion === 'es') { |
| 48 | + ecmaVersion = `(${ecmaVersion}\\d*)`; |
| 49 | + } |
| 50 | + |
| 51 | + constructorName = `(${constructorName}|${camelCase(constructorName)})`; |
| 52 | + if (methodName) { |
| 53 | + methodName = `(${methodName}|${camelCase(methodName)})`; |
| 54 | + } |
| 55 | + |
| 56 | + const methodOrConstructor = methodName || constructorName; |
| 57 | + |
| 58 | + return { |
| 59 | + feature, |
| 60 | + pattern: new RegExp( |
| 61 | + `^((${prefixes}?` |
| 62 | + + `(${ecmaVersion}${delimiter}${constructorName}${delimiter}${methodName}|` // Ex: es6-array-copy-within |
| 63 | + + `${constructorName}${delimiter}${methodName}|` // Ex: array-copy-within |
| 64 | + + `${ecmaVersion}${delimiter}${constructorName})` // Ex: es6-array |
| 65 | + + `${suffixes}?)|` |
| 66 | + + `(${prefixes}${methodOrConstructor}|${methodOrConstructor}${suffixes})` // Ex: polyfill-copy-within / polyfill-promise |
| 67 | + + `${additionalPolyfillPatterns[feature] || ''})$`, |
| 68 | + 'i', |
| 69 | + ), |
| 70 | + }; |
| 71 | +}); |
| 72 | + |
| 73 | +function report(context, node, feature) { |
| 74 | + let [ecmaVersion, namespace, method = ''] = feature.split('.'); |
| 75 | + if (namespace === 'typed-array' && method.endsWith('-array')) { |
| 76 | + namespace = method; |
| 77 | + method = ''; |
| 78 | + } |
| 79 | + |
| 80 | + const delimiter = method && (ecmaVersion === 'node' ? '.' : '#'); |
| 81 | + |
| 82 | + context.report({ |
| 83 | + node, |
| 84 | + messageId: MESSAGE_ID_POLYFILL, |
| 85 | + data: { |
| 86 | + featureName: `${constructorCase(namespace)}${delimiter}${camelCase(method)}`, |
| 87 | + }, |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +class SemverNodeVersion { |
| 92 | + constructor(nodeVersion) { |
| 93 | + this.nodeVersion = nodeVersion; |
| 94 | + this.validNodeVersion = semver.coerce(nodeVersion); |
| 95 | + } |
| 96 | + |
| 97 | + compare(featureVersion) { |
| 98 | + const supportedNodeVersion = semver.coerce(featureVersion); |
| 99 | + return this.validNodeVersion |
| 100 | + ? semver.lte(supportedNodeVersion, this.validNodeVersion) |
| 101 | + : semver.ltr(supportedNodeVersion, this.nodeVersion); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +function processRule(context, node, moduleName, targets) { |
| 106 | + if (!moduleName || typeof moduleName !== 'string') { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + const nodeVersion = targets[0] instanceof SemverNodeVersion && targets[0]; |
| 111 | + const importedModule = moduleName.replace(/([/\\].+?)\.[^.]+$/, '$1'); |
| 112 | + |
| 113 | + const unavailableFeatures = coreJsCompat({targets}).list; |
| 114 | + const coreJsModuleFeatures = coreJsEntries[importedModule.replace('core-js-pure', 'core-js')]; |
| 115 | + |
| 116 | + const checkFeatures = features => nodeVersion |
| 117 | + ? features.every(feature => compatData[feature].node && nodeVersion.compare(compatData[feature].node)) |
| 118 | + : !features.every(feature => unavailableFeatures.includes(feature)); |
| 119 | + |
| 120 | + if (coreJsModuleFeatures) { |
| 121 | + if (coreJsModuleFeatures.length === 1) { |
| 122 | + if (nodeVersion ? nodeVersion.compare(compatData[coreJsModuleFeatures[0]].node) : !unavailableFeatures.includes(coreJsModuleFeatures[0])) { |
| 123 | + report(context, node, coreJsModuleFeatures[0]); |
| 124 | + } |
| 125 | + } else if (checkFeatures(coreJsModuleFeatures)) { |
| 126 | + context.report({ |
| 127 | + node, |
| 128 | + messageId: MESSAGE_ID_CORE_JS, |
| 129 | + data: { |
| 130 | + coreJsModule: moduleName, |
| 131 | + }, |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + const polyfill = polyfills.find(({pattern}) => pattern.test(importedModule)); |
| 139 | + if (polyfill) { |
| 140 | + const [, namespace, method = ''] = polyfill.feature.split('.'); |
| 141 | + const [, features] = Object.entries(coreJsEntries).find(it => it[0] === `core-js/actual/${namespace}${method && '/'}${method}`); |
| 142 | + if (checkFeatures(features)) { |
| 143 | + report(context, node, polyfill.feature); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +function create(context) { |
| 149 | + const options = context.options[0]; |
| 150 | + let targets = options && options.targets; |
| 151 | + if (!targets) { |
| 152 | + getTargetsFromPkg(context.getFilename(), options && options.useBrowserlistFieldByDefault); |
| 153 | + } else if (options.treatsTargetsAsSemver) { |
| 154 | + targets = new SemverNodeVersion(targets); |
| 155 | + } |
| 156 | + |
| 157 | + if (!targets) { |
| 158 | + return {}; |
| 159 | + } |
| 160 | + |
| 161 | + return { |
| 162 | + 'CallExpression[callee.name="require"]'(node) { |
| 163 | + processRule(context, node, node.arguments[0].value, targets); |
| 164 | + }, |
| 165 | + 'ImportDeclaration, ImportExpression'(node) { |
| 166 | + processRule(context, node, node.source.value, targets); |
| 167 | + }, |
| 168 | + }; |
| 169 | +} |
| 170 | + |
| 171 | +const schema = [ |
| 172 | + { |
| 173 | + type: 'object', |
| 174 | + additionalProperties: false, |
| 175 | + required: ['targets'], |
| 176 | + properties: { |
| 177 | + useBrowserlistFieldByDefault: {type: 'boolean'}, |
| 178 | + treatsTargetsAsSemver: {type: 'boolean'}, |
| 179 | + targets: { |
| 180 | + oneOf: [ |
| 181 | + { |
| 182 | + type: 'string', |
| 183 | + minLength: 1, |
| 184 | + }, |
| 185 | + { |
| 186 | + type: 'array', |
| 187 | + minItems: 1, |
| 188 | + items: { |
| 189 | + type: 'string', |
| 190 | + }, |
| 191 | + }, |
| 192 | + { |
| 193 | + type: 'object', |
| 194 | + minProperties: 1, |
| 195 | + properties: { |
| 196 | + android: {type: 'string'}, |
| 197 | + chrome: {type: 'string'}, |
| 198 | + deno: {type: 'string'}, |
| 199 | + edge: {type: 'string'}, |
| 200 | + electron: {type: 'string'}, |
| 201 | + firefox: {type: 'string'}, |
| 202 | + ie: {type: 'string'}, |
| 203 | + ios: {type: 'string'}, |
| 204 | + node: {type: 'string'}, |
| 205 | + opera: {type: 'string'}, |
| 206 | + // eslint-disable-next-line camelcase |
| 207 | + opera_mobile: {type: 'string'}, |
| 208 | + phantom: {type: 'string'}, |
| 209 | + rhino: {type: 'string'}, |
| 210 | + safari: {type: 'string'}, |
| 211 | + samsung: {type: 'string'}, |
| 212 | + esmodules: {type: 'boolean'}, |
| 213 | + browsers: { |
| 214 | + oneOf: [ |
| 215 | + { |
| 216 | + type: 'string', |
| 217 | + minLength: 1, |
| 218 | + }, |
| 219 | + { |
| 220 | + type: 'array', |
| 221 | + minItems: 1, |
| 222 | + items: { |
| 223 | + type: 'string', |
| 224 | + }, |
| 225 | + }, |
| 226 | + { |
| 227 | + type: 'object', |
| 228 | + minProperties: 1, |
| 229 | + }, |
| 230 | + ], |
| 231 | + }, |
| 232 | + }, |
| 233 | + }, |
| 234 | + ], |
| 235 | + }, |
| 236 | + }, |
| 237 | + }, |
| 238 | +]; |
| 239 | + |
| 240 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 241 | +module.exports = { |
| 242 | + create, |
| 243 | + meta: { |
| 244 | + type: 'suggestion', |
| 245 | + docs: { |
| 246 | + description: 'Enforce the use of built-in methods instead of unnecessary polyfills.', |
| 247 | + }, |
| 248 | + schema, |
| 249 | + messages, |
| 250 | + }, |
| 251 | +}; |
0 commit comments