|
| 1 | +/** |
| 2 | + * @fileoverview Warns when a namespace qualifier is unnecessary. |
| 3 | + * @author Benjamin Lichtman |
| 4 | + */ |
| 5 | + |
| 6 | +import { TSESTree } from '@typescript-eslint/typescript-estree'; |
| 7 | +import ts from 'typescript'; |
| 8 | +import * as tsutils from 'tsutils'; |
| 9 | +import * as util from '../util'; |
| 10 | + |
| 11 | +//------------------------------------------------------------------------------ |
| 12 | +// Rule Definition |
| 13 | +//------------------------------------------------------------------------------ |
| 14 | + |
| 15 | +export default util.createRule({ |
| 16 | + name: 'no-unnecessary-qualifier', |
| 17 | + meta: { |
| 18 | + docs: { |
| 19 | + category: 'Best Practices', |
| 20 | + description: 'Warns when a namespace qualifier is unnecessary.', |
| 21 | + recommended: false, |
| 22 | + tslintName: 'no-unnecessary-qualifier' |
| 23 | + }, |
| 24 | + fixable: 'code', |
| 25 | + messages: { |
| 26 | + unnecessaryQualifier: |
| 27 | + "Qualifier is unnecessary since '{{ name }}' is in scope." |
| 28 | + }, |
| 29 | + schema: [], |
| 30 | + type: 'suggestion' |
| 31 | + }, |
| 32 | + defaultOptions: [], |
| 33 | + create(context) { |
| 34 | + const namespacesInScope: ts.Node[] = []; |
| 35 | + let currentFailedNamespaceExpression: TSESTree.Node | null = null; |
| 36 | + const parserServices = util.getParserServices(context); |
| 37 | + const esTreeNodeToTSNodeMap = parserServices.esTreeNodeToTSNodeMap; |
| 38 | + const program = parserServices.program; |
| 39 | + const checker = program.getTypeChecker(); |
| 40 | + const sourceCode = context.getSourceCode(); |
| 41 | + |
| 42 | + //---------------------------------------------------------------------- |
| 43 | + // Helpers |
| 44 | + //---------------------------------------------------------------------- |
| 45 | + |
| 46 | + function tryGetAliasedSymbol( |
| 47 | + symbol: ts.Symbol, |
| 48 | + checker: ts.TypeChecker |
| 49 | + ): ts.Symbol | null { |
| 50 | + return tsutils.isSymbolFlagSet(symbol, ts.SymbolFlags.Alias) |
| 51 | + ? checker.getAliasedSymbol(symbol) |
| 52 | + : null; |
| 53 | + } |
| 54 | + |
| 55 | + function symbolIsNamespaceInScope(symbol: ts.Symbol): boolean { |
| 56 | + const symbolDeclarations = symbol.getDeclarations() || []; |
| 57 | + |
| 58 | + if ( |
| 59 | + symbolDeclarations.some(decl => |
| 60 | + namespacesInScope.some(ns => ns === decl) |
| 61 | + ) |
| 62 | + ) { |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + const alias = tryGetAliasedSymbol(symbol, checker); |
| 67 | + |
| 68 | + return alias !== null && symbolIsNamespaceInScope(alias); |
| 69 | + } |
| 70 | + |
| 71 | + function getSymbolInScope( |
| 72 | + node: ts.Node, |
| 73 | + flags: ts.SymbolFlags, |
| 74 | + name: string |
| 75 | + ): ts.Symbol | undefined { |
| 76 | + // TODO:PERF `getSymbolsInScope` gets a long list. Is there a better way? |
| 77 | + const scope = checker.getSymbolsInScope(node, flags); |
| 78 | + |
| 79 | + return scope.find(scopeSymbol => scopeSymbol.name === name); |
| 80 | + } |
| 81 | + |
| 82 | + function symbolsAreEqual(accessed: ts.Symbol, inScope: ts.Symbol): boolean { |
| 83 | + return accessed === checker.getExportSymbolOfSymbol(inScope); |
| 84 | + } |
| 85 | + |
| 86 | + function qualifierIsUnnecessary( |
| 87 | + qualifier: TSESTree.Node, |
| 88 | + name: TSESTree.Identifier |
| 89 | + ): boolean { |
| 90 | + const tsQualifier = esTreeNodeToTSNodeMap.get(qualifier); |
| 91 | + const tsName = esTreeNodeToTSNodeMap.get(name); |
| 92 | + |
| 93 | + if (!(tsQualifier && tsName)) return false; // TODO: throw error? |
| 94 | + |
| 95 | + const namespaceSymbol = checker.getSymbolAtLocation(tsQualifier); |
| 96 | + |
| 97 | + if ( |
| 98 | + typeof namespaceSymbol === 'undefined' || |
| 99 | + !symbolIsNamespaceInScope(namespaceSymbol) |
| 100 | + ) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + const accessedSymbol = checker.getSymbolAtLocation(tsName); |
| 105 | + |
| 106 | + if (typeof accessedSymbol === 'undefined') { |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + // If the symbol in scope is different, the qualifier is necessary. |
| 111 | + const fromScope = getSymbolInScope( |
| 112 | + tsQualifier, |
| 113 | + accessedSymbol.flags, |
| 114 | + sourceCode.getText(name) |
| 115 | + ); |
| 116 | + |
| 117 | + return ( |
| 118 | + typeof fromScope === 'undefined' || |
| 119 | + symbolsAreEqual(accessedSymbol, fromScope) |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + function visitNamespaceAccess( |
| 124 | + node: TSESTree.Node, |
| 125 | + qualifier: TSESTree.Node, |
| 126 | + name: TSESTree.Identifier |
| 127 | + ): void { |
| 128 | + // Only look for nested qualifier errors if we didn't already fail on the outer qualifier. |
| 129 | + if ( |
| 130 | + !currentFailedNamespaceExpression && |
| 131 | + qualifierIsUnnecessary(qualifier, name) |
| 132 | + ) { |
| 133 | + currentFailedNamespaceExpression = node; |
| 134 | + context.report({ |
| 135 | + node: qualifier, |
| 136 | + messageId: 'unnecessaryQualifier', |
| 137 | + data: { |
| 138 | + name: sourceCode.getText(name) |
| 139 | + }, |
| 140 | + fix(fixer) { |
| 141 | + return fixer.removeRange([qualifier.range[0], name.range[0]]); |
| 142 | + } |
| 143 | + }); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + function enterDeclaration(node: TSESTree.Node): void { |
| 148 | + const tsDeclaration = esTreeNodeToTSNodeMap.get(node); |
| 149 | + if (tsDeclaration) { |
| 150 | + namespacesInScope.push(tsDeclaration); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + function exitDeclaration(node: TSESTree.Node) { |
| 155 | + if (esTreeNodeToTSNodeMap.has(node)) { |
| 156 | + namespacesInScope.pop(); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + function resetCurrentNamespaceExpression(node: TSESTree.Node): void { |
| 161 | + if (node === currentFailedNamespaceExpression) { |
| 162 | + currentFailedNamespaceExpression = null; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + function isPropertyAccessExpression( |
| 167 | + node: TSESTree.Node |
| 168 | + ): node is TSESTree.MemberExpression { |
| 169 | + return node.type === 'MemberExpression' && !node.computed; |
| 170 | + } |
| 171 | + |
| 172 | + function isEntityNameExpression(node: TSESTree.Node): boolean { |
| 173 | + return ( |
| 174 | + node.type === 'Identifier' || |
| 175 | + (isPropertyAccessExpression(node) && |
| 176 | + isEntityNameExpression(node.object)) |
| 177 | + ); |
| 178 | + } |
| 179 | + |
| 180 | + //---------------------------------------------------------------------- |
| 181 | + // Public |
| 182 | + //---------------------------------------------------------------------- |
| 183 | + |
| 184 | + return { |
| 185 | + TSModuleDeclaration: enterDeclaration, |
| 186 | + TSEnumDeclaration: enterDeclaration, |
| 187 | + 'ExportNamedDeclaration[declaration.type="TSModuleDeclaration"]': enterDeclaration, |
| 188 | + 'ExportNamedDeclaration[declaration.type="TSEnumDeclaration"]': enterDeclaration, |
| 189 | + 'TSModuleDeclaration:exit': exitDeclaration, |
| 190 | + 'TSEnumDeclaration:exit': exitDeclaration, |
| 191 | + 'ExportNamedDeclaration[declaration.type="TSModuleDeclaration"]:exit': exitDeclaration, |
| 192 | + 'ExportNamedDeclaration[declaration.type="TSEnumDeclaration"]:exit': exitDeclaration, |
| 193 | + TSQualifiedName(node: TSESTree.TSQualifiedName): void { |
| 194 | + visitNamespaceAccess(node, node.left, node.right); |
| 195 | + }, |
| 196 | + 'MemberExpression[computed=false]': function( |
| 197 | + node: TSESTree.MemberExpression |
| 198 | + ): void { |
| 199 | + const property = node.property as TSESTree.Identifier; |
| 200 | + if (isEntityNameExpression(node.object)) { |
| 201 | + visitNamespaceAccess(node, node.object, property); |
| 202 | + } |
| 203 | + }, |
| 204 | + 'TSQualifiedName:exit': resetCurrentNamespaceExpression, |
| 205 | + 'MemberExpression:exit': resetCurrentNamespaceExpression |
| 206 | + }; |
| 207 | + } |
| 208 | +}); |
0 commit comments