|
| 1 | +'use strict'; |
| 2 | +const path = require('node:path'); |
| 3 | +const readPkgUp = require('read-pkg-up'); |
| 4 | +const coreJsCompat = require('core-js-compat'); |
| 5 | +const {camelCase} = require('lodash'); |
| 6 | +const isStaticRequire = require('./ast/is-static-require.js'); |
| 7 | + |
| 8 | +const {data: compatData, entries: coreJsEntries} = coreJsCompat; |
| 9 | + |
| 10 | +const MESSAGE_ID_POLYFILL = 'unnecessaryPolyfill'; |
| 11 | +const MESSAGE_ID_CORE_JS = 'unnecessaryCoreJsModule'; |
| 12 | +const messages = { |
| 13 | + [MESSAGE_ID_POLYFILL]: 'Use built-in instead.', |
| 14 | + [MESSAGE_ID_CORE_JS]: |
| 15 | + 'All polyfilled features imported from `{{coreJsModule}}` are available as built-ins. Use the built-ins instead.', |
| 16 | +}; |
| 17 | + |
| 18 | +const additionalPolyfillPatterns = { |
| 19 | + 'es.promise.finally': '|(p-finally)', |
| 20 | + 'es.object.set-prototype-of': '|(setprototypeof)', |
| 21 | + 'es.string.code-point-at': '|(code-point-at)', |
| 22 | +}; |
| 23 | + |
| 24 | +const prefixes = '(mdn-polyfills/|polyfill-)'; |
| 25 | +const suffixes = '(-polyfill)'; |
| 26 | +const delimiter = '(\\.|-|\\.prototype\\.|/)?'; |
| 27 | + |
| 28 | +const polyfills = Object.keys(compatData).map(feature => { |
| 29 | + let [ecmaVersion, constructorName, methodName = ''] = feature.split('.'); |
| 30 | + |
| 31 | + if (ecmaVersion === 'es') { |
| 32 | + ecmaVersion = '(es\\d*)'; |
| 33 | + } |
| 34 | + |
| 35 | + constructorName = `(${constructorName}|${camelCase(constructorName)})`; |
| 36 | + methodName &&= `(${methodName}|${camelCase(methodName)})`; |
| 37 | + |
| 38 | + const methodOrConstructor = methodName || constructorName; |
| 39 | + |
| 40 | + const patterns = [ |
| 41 | + `^((${prefixes}?(`, |
| 42 | + methodName && `(${ecmaVersion}${delimiter}${constructorName}${delimiter}${methodName})|`, // Ex: es6-array-copy-within |
| 43 | + methodName && `(${constructorName}${delimiter}${methodName})|`, // Ex: array-copy-within |
| 44 | + `(${ecmaVersion}${delimiter}${constructorName}))`, // Ex: es6-array |
| 45 | + `${suffixes}?)|`, |
| 46 | + `(${prefixes}${methodOrConstructor}|${methodOrConstructor}${suffixes})`, // Ex: polyfill-copy-within / polyfill-promise |
| 47 | + `${additionalPolyfillPatterns[feature] || ''})$`, |
| 48 | + ]; |
| 49 | + |
| 50 | + return { |
| 51 | + feature, |
| 52 | + pattern: new RegExp(patterns.join(''), 'i'), |
| 53 | + }; |
| 54 | +}); |
| 55 | + |
| 56 | +function getTargets(options, dirname) { |
| 57 | + if (options?.targets) { |
| 58 | + return options.targets; |
| 59 | + } |
| 60 | + |
| 61 | + /** @type {readPkgUp.ReadResult | undefined} */ |
| 62 | + let packageResult; |
| 63 | + try { |
| 64 | + // It can fail if, for example, the package.json file has comments. |
| 65 | + packageResult = readPkgUp.sync({normalize: false, cwd: dirname}); |
| 66 | + } catch {} |
| 67 | + |
| 68 | + if (!packageResult) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + const {browserlist, engines} = packageResult.packageJson; |
| 73 | + return browserlist ?? engines; |
| 74 | +} |
| 75 | + |
| 76 | +function create(context) { |
| 77 | + const targets = getTargets(context.options[0], path.dirname(context.filename)); |
| 78 | + if (!targets) { |
| 79 | + return {}; |
| 80 | + } |
| 81 | + |
| 82 | + let unavailableFeatures; |
| 83 | + try { |
| 84 | + unavailableFeatures = coreJsCompat({targets}).list; |
| 85 | + } catch { |
| 86 | + // This can happen if the targets are invalid or use unsupported syntax like `{node:'*'}`. |
| 87 | + return {}; |
| 88 | + } |
| 89 | + |
| 90 | + const checkFeatures = features => !features.every(feature => unavailableFeatures.includes(feature)); |
| 91 | + |
| 92 | + return { |
| 93 | + Literal(node) { |
| 94 | + if ( |
| 95 | + !( |
| 96 | + (['ImportDeclaration', 'ImportExpression'].includes(node.parent.type) && node.parent.source === node) |
| 97 | + || (isStaticRequire(node.parent) && node.parent.arguments[0] === node) |
| 98 | + ) |
| 99 | + ) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + const importedModule = node.value; |
| 104 | + if (typeof importedModule !== 'string' || ['.', '/'].includes(importedModule[0])) { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + const coreJsModuleFeatures = coreJsEntries[importedModule.replace('core-js-pure', 'core-js')]; |
| 109 | + |
| 110 | + if (coreJsModuleFeatures) { |
| 111 | + if (coreJsModuleFeatures.length > 1) { |
| 112 | + if (checkFeatures(coreJsModuleFeatures)) { |
| 113 | + return { |
| 114 | + node, |
| 115 | + messageId: MESSAGE_ID_CORE_JS, |
| 116 | + data: { |
| 117 | + coreJsModule: importedModule, |
| 118 | + }, |
| 119 | + }; |
| 120 | + } |
| 121 | + } else if (!unavailableFeatures.includes(coreJsModuleFeatures[0])) { |
| 122 | + return {node, messageId: MESSAGE_ID_POLYFILL}; |
| 123 | + } |
| 124 | + |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + const polyfill = polyfills.find(({pattern}) => pattern.test(importedModule)); |
| 129 | + if (polyfill) { |
| 130 | + const [, namespace, method = ''] = polyfill.feature.split('.'); |
| 131 | + const [, features] = Object.entries(coreJsEntries).find( |
| 132 | + entry => entry[0] === `core-js/full/${namespace}${method && '/'}${method}`, |
| 133 | + ); |
| 134 | + if (checkFeatures(features)) { |
| 135 | + return {node, messageId: MESSAGE_ID_POLYFILL}; |
| 136 | + } |
| 137 | + } |
| 138 | + }, |
| 139 | + }; |
| 140 | +} |
| 141 | + |
| 142 | +const schema = [ |
| 143 | + { |
| 144 | + type: 'object', |
| 145 | + additionalProperties: false, |
| 146 | + required: ['targets'], |
| 147 | + properties: { |
| 148 | + targets: { |
| 149 | + oneOf: [ |
| 150 | + { |
| 151 | + type: 'string', |
| 152 | + }, |
| 153 | + { |
| 154 | + type: 'array', |
| 155 | + }, |
| 156 | + { |
| 157 | + type: 'object', |
| 158 | + }, |
| 159 | + ], |
| 160 | + }, |
| 161 | + }, |
| 162 | + }, |
| 163 | +]; |
| 164 | + |
| 165 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 166 | +module.exports = { |
| 167 | + create, |
| 168 | + meta: { |
| 169 | + type: 'suggestion', |
| 170 | + docs: { |
| 171 | + description: 'Enforce the use of built-in methods instead of unnecessary polyfills.', |
| 172 | + }, |
| 173 | + schema, |
| 174 | + messages, |
| 175 | + }, |
| 176 | +}; |
0 commit comments