|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const emberSourceVersion = require('../utils/ember-source-version'); |
| 4 | + |
| 5 | +//------------------------------------------------------------------------------ |
| 6 | +// Mapping from tracked-built-ins exports to @ember/reactive/collections exports |
| 7 | +//------------------------------------------------------------------------------ |
| 8 | + |
| 9 | +const TRACKED_BUILT_INS_MAPPING = { |
| 10 | + TrackedArray: 'trackedArray', |
| 11 | + TrackedObject: 'trackedObject', |
| 12 | + TrackedMap: 'trackedMap', |
| 13 | + TrackedWeakMap: 'trackedWeakMap', |
| 14 | + TrackedSet: 'trackedSet', |
| 15 | + TrackedWeakSet: 'trackedWeakSet', |
| 16 | +}; |
| 17 | + |
| 18 | +const TRACKED_BUILT_INS_MODULE = 'tracked-built-ins'; |
| 19 | +const EMBER_REACTIVE_MODULE = '@ember/reactive/collections'; |
| 20 | + |
| 21 | +const ERROR_MESSAGE_IMPORT = |
| 22 | + 'Use imports from `@ember/reactive/collections` instead of `tracked-built-ins`.'; |
| 23 | + |
| 24 | +//------------------------------------------------------------------------------ |
| 25 | +// Rule Definition |
| 26 | +//------------------------------------------------------------------------------ |
| 27 | + |
| 28 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 29 | +module.exports = { |
| 30 | + meta: { |
| 31 | + type: 'suggestion', |
| 32 | + docs: { |
| 33 | + description: |
| 34 | + 'enforce usage of `@ember/reactive/collections` imports instead of `tracked-built-ins`', |
| 35 | + category: 'Ember Octane', |
| 36 | + recommended: false, |
| 37 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/no-tracked-built-ins.md', |
| 38 | + }, |
| 39 | + fixable: 'code', |
| 40 | + schema: [], |
| 41 | + messages: { |
| 42 | + import: ERROR_MESSAGE_IMPORT, |
| 43 | + newExpression: |
| 44 | + 'Use `{{newName}}(...)` instead of `new {{oldName}}(...)`. The `@ember/reactive/collections` utilities do not use `new`.', |
| 45 | + }, |
| 46 | + }, |
| 47 | + |
| 48 | + ERROR_MESSAGE_IMPORT, |
| 49 | + |
| 50 | + create(context) { |
| 51 | + // Only report when ember-source >= 6.8 (which provides @ember/reactive/collections) |
| 52 | + if (!emberSourceVersion.isEmberSourceVersionAtLeast(6, 8)) { |
| 53 | + return {}; |
| 54 | + } |
| 55 | + |
| 56 | + // Track which imported identifiers map to tracked-built-ins classes |
| 57 | + // so we can fix `new TrackedArray(...)` → `trackedArray(...)` |
| 58 | + const trackedIdentifiers = new Map(); |
| 59 | + |
| 60 | + return { |
| 61 | + ImportDeclaration(node) { |
| 62 | + if (node.source.value !== TRACKED_BUILT_INS_MODULE) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + context.report({ |
| 67 | + node, |
| 68 | + messageId: 'import', |
| 69 | + fix(fixer) { |
| 70 | + const specifiers = node.specifiers; |
| 71 | + |
| 72 | + // Only autofix named imports we know how to map |
| 73 | + const namedSpecifiers = specifiers.filter( |
| 74 | + (s) => s.type === 'ImportSpecifier' && s.imported.name in TRACKED_BUILT_INS_MAPPING |
| 75 | + ); |
| 76 | + |
| 77 | + // If there's a default import or unknown named imports, we can't fully autofix |
| 78 | + const hasDefault = specifiers.some((s) => s.type === 'ImportDefaultSpecifier'); |
| 79 | + const unknownNamed = specifiers.filter( |
| 80 | + (s) => s.type === 'ImportSpecifier' && !(s.imported.name in TRACKED_BUILT_INS_MAPPING) |
| 81 | + ); |
| 82 | + |
| 83 | + if (hasDefault || unknownNamed.length > 0 || namedSpecifiers.length === 0) { |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + const newSpecifiers = namedSpecifiers.map((s) => { |
| 88 | + const newName = TRACKED_BUILT_INS_MAPPING[s.imported.name]; |
| 89 | + if (s.local.name !== s.imported.name) { |
| 90 | + // Has alias: `import { TrackedArray as TA }` → `import { trackedArray as TA }` |
| 91 | + return `${newName} as ${s.local.name}`; |
| 92 | + } |
| 93 | + return newName; |
| 94 | + }); |
| 95 | + |
| 96 | + const newImport = `import { ${newSpecifiers.join(', ')} } from '${EMBER_REACTIVE_MODULE}';`; |
| 97 | + return fixer.replaceText(node, newImport); |
| 98 | + }, |
| 99 | + }); |
| 100 | + |
| 101 | + // Register the local names for NewExpression tracking |
| 102 | + for (const specifier of node.specifiers) { |
| 103 | + if ( |
| 104 | + specifier.type === 'ImportSpecifier' && |
| 105 | + specifier.imported.name in TRACKED_BUILT_INS_MAPPING |
| 106 | + ) { |
| 107 | + const isAliased = specifier.local.name !== specifier.imported.name; |
| 108 | + trackedIdentifiers.set(specifier.local.name, { |
| 109 | + newName: TRACKED_BUILT_INS_MAPPING[specifier.imported.name], |
| 110 | + isAliased, |
| 111 | + }); |
| 112 | + } |
| 113 | + } |
| 114 | + }, |
| 115 | + |
| 116 | + NewExpression(node) { |
| 117 | + if (node.callee.type === 'Identifier' && trackedIdentifiers.has(node.callee.name)) { |
| 118 | + const oldName = node.callee.name; |
| 119 | + const { newName, isAliased } = trackedIdentifiers.get(oldName); |
| 120 | + |
| 121 | + context.report({ |
| 122 | + node, |
| 123 | + messageId: 'newExpression', |
| 124 | + data: { oldName, newName }, |
| 125 | + fix(fixer) { |
| 126 | + const sourceCode = context.getSourceCode(); |
| 127 | + const newKeyword = sourceCode.getFirstToken(node); |
| 128 | + const calleeToken = sourceCode.getTokenAfter(newKeyword); |
| 129 | + const fixes = [ |
| 130 | + // Remove the `new` keyword and any whitespace up to the callee |
| 131 | + fixer.removeRange([newKeyword.range[0], calleeToken.range[0]]), |
| 132 | + ]; |
| 133 | + // Only rename the callee if it's not aliased |
| 134 | + if (!isAliased) { |
| 135 | + fixes.push(fixer.replaceText(node.callee, newName)); |
| 136 | + } |
| 137 | + return fixes; |
| 138 | + }, |
| 139 | + }); |
| 140 | + } |
| 141 | + }, |
| 142 | + }; |
| 143 | + }, |
| 144 | +}; |
0 commit comments