|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// ts.SymbolFlags.Alias = 2097152 (1 << 21). |
| 4 | +// Hardcoded to avoid adding a direct `typescript` dependency. This value has |
| 5 | +// been stable since TypeScript was open-sourced (~2014) but is not formally |
| 6 | +// guaranteed. If it ever changes, this rule will need to require the user's |
| 7 | +// installed TypeScript and read ts.SymbolFlags.Alias at runtime. |
| 8 | +const TS_ALIAS_FLAG = 2_097_152; |
| 9 | + |
| 10 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 11 | +module.exports = { |
| 12 | + meta: { |
| 13 | + type: 'problem', |
| 14 | + docs: { |
| 15 | + description: |
| 16 | + 'disallow using deprecated Glimmer components, helpers, and modifiers in templates', |
| 17 | + category: 'Ember Octane', |
| 18 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-deprecated.md', |
| 19 | + }, |
| 20 | + schema: [], |
| 21 | + messages: { |
| 22 | + deprecated: '`{{name}}` is deprecated.', |
| 23 | + deprecatedWithReason: '`{{name}}` is deprecated. {{reason}}', |
| 24 | + }, |
| 25 | + }, |
| 26 | + |
| 27 | + create(context) { |
| 28 | + const services = context.sourceCode.parserServices ?? context.parserServices; |
| 29 | + if (!services?.program) { |
| 30 | + return {}; |
| 31 | + } |
| 32 | + |
| 33 | + const checker = services.program.getTypeChecker(); |
| 34 | + const sourceCode = context.sourceCode; |
| 35 | + |
| 36 | + // Cache component class symbol → Args object type (null = no Args) per lint run. |
| 37 | + const argsTypeCache = new Map(); |
| 38 | + |
| 39 | + function getComponentArgsType(classSymbol) { |
| 40 | + if (argsTypeCache.has(classSymbol)) { |
| 41 | + return argsTypeCache.get(classSymbol); |
| 42 | + } |
| 43 | + let result = null; |
| 44 | + try { |
| 45 | + const declaredType = checker.getDeclaredTypeOfSymbol(classSymbol); |
| 46 | + const baseTypes = checker.getBaseTypes(declaredType); |
| 47 | + outer: for (const base of baseTypes) { |
| 48 | + for (const arg of checker.getTypeArguments(base) ?? []) { |
| 49 | + const argsSymbol = arg.getProperty('Args'); |
| 50 | + if (argsSymbol) { |
| 51 | + result = checker.getTypeOfSymbol(argsSymbol); |
| 52 | + break outer; |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } catch { |
| 57 | + result = null; |
| 58 | + } |
| 59 | + argsTypeCache.set(classSymbol, result); |
| 60 | + return result; |
| 61 | + } |
| 62 | + |
| 63 | + function getJsDocDeprecation(symbol) { |
| 64 | + let jsDocTags; |
| 65 | + try { |
| 66 | + jsDocTags = symbol?.getJsDocTags(checker); |
| 67 | + } catch { |
| 68 | + // workaround for https://github.com/microsoft/TypeScript/issues/60024 |
| 69 | + return undefined; |
| 70 | + } |
| 71 | + const tag = jsDocTags?.find((t) => t.name === 'deprecated'); |
| 72 | + if (!tag) { |
| 73 | + return undefined; |
| 74 | + } |
| 75 | + const displayParts = tag.text; |
| 76 | + return displayParts ? displayParts.map((p) => p.text).join('') : ''; |
| 77 | + } |
| 78 | + |
| 79 | + function searchForDeprecationInAliasesChain(symbol, checkAliasedSymbol) { |
| 80 | + // eslint-disable-next-line no-bitwise |
| 81 | + if (!symbol || !(symbol.flags & TS_ALIAS_FLAG)) { |
| 82 | + return checkAliasedSymbol ? getJsDocDeprecation(symbol) : undefined; |
| 83 | + } |
| 84 | + const targetSymbol = checker.getAliasedSymbol(symbol); |
| 85 | + let current = symbol; |
| 86 | + // eslint-disable-next-line no-bitwise |
| 87 | + while (current.flags & TS_ALIAS_FLAG) { |
| 88 | + const reason = getJsDocDeprecation(current); |
| 89 | + if (reason !== undefined) { |
| 90 | + return reason; |
| 91 | + } |
| 92 | + const immediateAliasedSymbol = |
| 93 | + current.getDeclarations() && checker.getImmediateAliasedSymbol(current); |
| 94 | + if (!immediateAliasedSymbol) { |
| 95 | + break; |
| 96 | + } |
| 97 | + current = immediateAliasedSymbol; |
| 98 | + if (checkAliasedSymbol && current === targetSymbol) { |
| 99 | + return getJsDocDeprecation(current); |
| 100 | + } |
| 101 | + } |
| 102 | + return undefined; |
| 103 | + } |
| 104 | + |
| 105 | + function checkDeprecatedIdentifier(identifierNode, scope) { |
| 106 | + const ref = scope.references.find((v) => v.identifier === identifierNode); |
| 107 | + const variable = ref?.resolved; |
| 108 | + const def = variable?.defs[0]; |
| 109 | + |
| 110 | + if (!def || def.type !== 'ImportBinding') { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + const tsNode = services.esTreeNodeToTSNodeMap.get(def.node); |
| 115 | + if (!tsNode) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + // ImportClause and ImportSpecifier require .name for getSymbolAtLocation |
| 120 | + const tsIdentifier = tsNode.name ?? tsNode; |
| 121 | + const symbol = checker.getSymbolAtLocation(tsIdentifier); |
| 122 | + if (!symbol) { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + const reason = searchForDeprecationInAliasesChain(symbol, true); |
| 127 | + if (reason === undefined) { |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + if (reason === '') { |
| 132 | + context.report({ |
| 133 | + node: identifierNode, |
| 134 | + messageId: 'deprecated', |
| 135 | + data: { name: identifierNode.name }, |
| 136 | + }); |
| 137 | + } else { |
| 138 | + context.report({ |
| 139 | + node: identifierNode, |
| 140 | + messageId: 'deprecatedWithReason', |
| 141 | + data: { name: identifierNode.name, reason }, |
| 142 | + }); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return { |
| 147 | + GlimmerPathExpression(node) { |
| 148 | + checkDeprecatedIdentifier(node.head, sourceCode.getScope(node)); |
| 149 | + }, |
| 150 | + |
| 151 | + GlimmerElementNode(node) { |
| 152 | + // GlimmerElementNode is in its own scope; get the outer scope |
| 153 | + const scope = sourceCode.getScope(node.parent); |
| 154 | + checkDeprecatedIdentifier(node.parts[0], scope); |
| 155 | + }, |
| 156 | + |
| 157 | + GlimmerAttrNode(node) { |
| 158 | + if (!node.name.startsWith('@')) { |
| 159 | + return; |
| 160 | + } |
| 161 | + |
| 162 | + // Resolve the component import binding from the parent element |
| 163 | + const elementNode = node.parent; |
| 164 | + const scope = sourceCode.getScope(elementNode.parent); |
| 165 | + const ref = scope.references.find((v) => v.identifier === elementNode.parts[0]); |
| 166 | + const def = ref?.resolved?.defs[0]; |
| 167 | + if (!def || def.type !== 'ImportBinding') { |
| 168 | + return; |
| 169 | + } |
| 170 | + |
| 171 | + const tsNode = services.esTreeNodeToTSNodeMap.get(def.node); |
| 172 | + if (!tsNode) { |
| 173 | + return; |
| 174 | + } |
| 175 | + |
| 176 | + const tsIdentifier = tsNode.name ?? tsNode; |
| 177 | + const importSymbol = checker.getSymbolAtLocation(tsIdentifier); |
| 178 | + if (!importSymbol) { |
| 179 | + return; |
| 180 | + } |
| 181 | + |
| 182 | + // Resolve alias to the class symbol |
| 183 | + // eslint-disable-next-line no-bitwise |
| 184 | + const classSymbol = |
| 185 | + importSymbol.flags & TS_ALIAS_FLAG |
| 186 | + ? checker.getAliasedSymbol(importSymbol) |
| 187 | + : importSymbol; |
| 188 | + |
| 189 | + const argsType = getComponentArgsType(classSymbol); |
| 190 | + if (!argsType) { |
| 191 | + return; |
| 192 | + } |
| 193 | + |
| 194 | + const argName = node.name.slice(1); // strip leading '@' |
| 195 | + const argSymbol = argsType.getProperty(argName); |
| 196 | + const reason = getJsDocDeprecation(argSymbol); |
| 197 | + if (reason === undefined) { |
| 198 | + return; |
| 199 | + } |
| 200 | + |
| 201 | + if (reason === '') { |
| 202 | + context.report({ |
| 203 | + node, |
| 204 | + messageId: 'deprecated', |
| 205 | + data: { name: node.name }, |
| 206 | + }); |
| 207 | + } else { |
| 208 | + context.report({ |
| 209 | + node, |
| 210 | + messageId: 'deprecatedWithReason', |
| 211 | + data: { name: node.name, reason }, |
| 212 | + }); |
| 213 | + } |
| 214 | + }, |
| 215 | + }; |
| 216 | + }, |
| 217 | +}; |
0 commit comments