|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const tsutils = require('ts-api-utils'); |
| 4 | +const ts = require('typescript'); |
| 5 | + |
| 6 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 7 | +module.exports = { |
| 8 | + meta: { |
| 9 | + type: 'problem', |
| 10 | + docs: { |
| 11 | + description: |
| 12 | + 'disallow using deprecated Glimmer components, helpers, and modifiers in templates', |
| 13 | + category: 'Ember Octane', |
| 14 | + recommendedGts: true, |
| 15 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-deprecated.md', |
| 16 | + }, |
| 17 | + requiresTypeChecking: true, |
| 18 | + schema: [], |
| 19 | + messages: { |
| 20 | + deprecated: '`{{name}}` is deprecated.', |
| 21 | + deprecatedWithReason: '`{{name}}` is deprecated. {{reason}}', |
| 22 | + }, |
| 23 | + }, |
| 24 | + |
| 25 | + create(context) { |
| 26 | + const services = context.sourceCode.parserServices ?? context.parserServices; |
| 27 | + if (!services?.program) { |
| 28 | + return {}; |
| 29 | + } |
| 30 | + |
| 31 | + const checker = services.program.getTypeChecker(); |
| 32 | + const sourceCode = context.sourceCode; |
| 33 | + |
| 34 | + function getJsDocDeprecation(symbol) { |
| 35 | + let jsDocTags; |
| 36 | + try { |
| 37 | + jsDocTags = symbol?.getJsDocTags(checker); |
| 38 | + } catch { |
| 39 | + // workaround for https://github.com/microsoft/TypeScript/issues/60024 |
| 40 | + return undefined; |
| 41 | + } |
| 42 | + const tag = jsDocTags?.find((t) => t.name === 'deprecated'); |
| 43 | + if (!tag) { |
| 44 | + return undefined; |
| 45 | + } |
| 46 | + const displayParts = tag.text; |
| 47 | + return displayParts ? ts.displayPartsToString(displayParts) : ''; |
| 48 | + } |
| 49 | + |
| 50 | + function searchForDeprecationInAliasesChain(symbol, checkAliasedSymbol) { |
| 51 | + if (!symbol || !tsutils.isSymbolFlagSet(symbol, ts.SymbolFlags.Alias)) { |
| 52 | + return checkAliasedSymbol ? getJsDocDeprecation(symbol) : undefined; |
| 53 | + } |
| 54 | + const targetSymbol = checker.getAliasedSymbol(symbol); |
| 55 | + while (tsutils.isSymbolFlagSet(symbol, ts.SymbolFlags.Alias)) { |
| 56 | + const reason = getJsDocDeprecation(symbol); |
| 57 | + if (reason != null) { |
| 58 | + return reason; |
| 59 | + } |
| 60 | + const immediateAliasedSymbol = |
| 61 | + symbol.getDeclarations() && checker.getImmediateAliasedSymbol(symbol); |
| 62 | + if (!immediateAliasedSymbol) { |
| 63 | + break; |
| 64 | + } |
| 65 | + symbol = immediateAliasedSymbol; |
| 66 | + if (checkAliasedSymbol && symbol === targetSymbol) { |
| 67 | + return getJsDocDeprecation(symbol); |
| 68 | + } |
| 69 | + } |
| 70 | + return undefined; |
| 71 | + } |
| 72 | + |
| 73 | + function checkDeprecatedIdentifier(identifierNode, scope) { |
| 74 | + const ref = scope.references.find((v) => v.identifier === identifierNode); |
| 75 | + const variable = ref?.resolved; |
| 76 | + const def = variable?.defs[0]; |
| 77 | + |
| 78 | + if (!def || def.type !== 'ImportBinding') { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + const tsNode = services.esTreeNodeToTSNodeMap.get(def.node); |
| 83 | + if (!tsNode) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + // ImportClause and ImportSpecifier require .name for getSymbolAtLocation |
| 88 | + const tsIdentifier = tsNode.name ?? tsNode; |
| 89 | + const symbol = checker.getSymbolAtLocation(tsIdentifier); |
| 90 | + if (!symbol) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + const reason = searchForDeprecationInAliasesChain(symbol, true); |
| 95 | + if (reason == null) { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + if (reason === '') { |
| 100 | + context.report({ |
| 101 | + node: identifierNode, |
| 102 | + messageId: 'deprecated', |
| 103 | + data: { name: identifierNode.name }, |
| 104 | + }); |
| 105 | + } else { |
| 106 | + context.report({ |
| 107 | + node: identifierNode, |
| 108 | + messageId: 'deprecatedWithReason', |
| 109 | + data: { name: identifierNode.name, reason }, |
| 110 | + }); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return { |
| 115 | + GlimmerPathExpression(node) { |
| 116 | + checkDeprecatedIdentifier(node.head, sourceCode.getScope(node)); |
| 117 | + }, |
| 118 | + |
| 119 | + GlimmerElementNode(node) { |
| 120 | + // GlimmerElementNode is in its own scope; get the outer scope |
| 121 | + const scope = sourceCode.getScope(node.parent); |
| 122 | + checkDeprecatedIdentifier(node.parts[0], scope); |
| 123 | + }, |
| 124 | + }; |
| 125 | + }, |
| 126 | +}; |
0 commit comments