4 * Copyright 2017 Palantir Technologies, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
20 var utils = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var Rule = /** @class */ (function (_super) {
24 tslib_1.__extends(Rule, _super);
26 return _super !== null && _super.apply(this, arguments) || this;
28 /* tslint:enable:object-literal-sort-keys */
29 Rule.FAILURE_STRING = function (name) {
30 return "Qualifier is unnecessary since '" + name + "' is in scope.";
32 Rule.prototype.applyWithProgram = function (sourceFile, program) {
33 return this.applyWithFunction(sourceFile, walk, undefined, program.getTypeChecker());
35 /* tslint:disable:object-literal-sort-keys */
37 ruleName: "no-unnecessary-qualifier",
38 description: "Warns when a namespace qualifier (`A.x`) is unnecessary.",
40 optionsDescription: "Not configurable.",
42 optionExamples: [true],
45 requiresTypeInfo: true,
48 }(Lint.Rules.TypedRule));
50 function walk(ctx, checker) {
51 var namespacesInScope = [];
52 ts.forEachChild(ctx.sourceFile, cb);
55 case ts.SyntaxKind.ModuleDeclaration:
56 case ts.SyntaxKind.EnumDeclaration:
57 namespacesInScope.push(node);
58 ts.forEachChild(node, cb);
59 namespacesInScope.pop();
61 case ts.SyntaxKind.QualifiedName:
62 var _a = node, left = _a.left, right = _a.right;
63 visitNamespaceAccess(node, left, right);
65 case ts.SyntaxKind.PropertyAccessExpression:
66 var _b = node, expression = _b.expression, name = _b.name;
67 if (utils.isEntityNameExpression(expression)) {
68 visitNamespaceAccess(node, expression, name);
73 ts.forEachChild(node, cb);
76 function visitNamespaceAccess(node, qualifier, name) {
77 if (qualifierIsUnnecessary(qualifier, name)) {
78 var fix = Lint.Replacement.deleteFromTo(qualifier.getStart(), name.getStart());
79 ctx.addFailureAtNode(qualifier, Rule.FAILURE_STRING(qualifier.getText()), fix);
82 // Only look for nested qualifier errors if we didn't already fail on the outer qualifier.
83 ts.forEachChild(node, cb);
86 function qualifierIsUnnecessary(qualifier, name) {
87 var namespaceSymbol = checker.getSymbolAtLocation(qualifier);
88 if (namespaceSymbol === undefined || !symbolIsNamespaceInScope(namespaceSymbol)) {
91 var accessedSymbol = checker.getSymbolAtLocation(name);
92 if (accessedSymbol === undefined) {
95 // If the symbol in scope is different, the qualifier is necessary.
96 var fromScope = getSymbolInScope(qualifier, accessedSymbol.flags, name.text);
97 return fromScope === undefined || symbolsAreEqual(accessedSymbol, fromScope);
99 function getSymbolInScope(node, flags, name) {
100 // TODO:PERF `getSymbolsInScope` gets a long list. Is there a better way?
101 var scope = checker.getSymbolsInScope(node, flags);
102 return scope.find(function (scopeSymbol) { return scopeSymbol.name === name; });
104 function symbolIsNamespaceInScope(symbol) {
105 var symbolDeclarations = symbol.getDeclarations();
106 if (symbolDeclarations === undefined) {
109 else if (symbolDeclarations.some(function (decl) { return namespacesInScope.some(function (ns) { return ns === decl; }); })) {
112 var alias = tryGetAliasedSymbol(symbol, checker);
113 return alias !== undefined && symbolIsNamespaceInScope(alias);
115 function symbolsAreEqual(accessed, inScope) {
116 if (checker.getExportSymbolOfSymbol !== undefined) {
117 inScope = checker.getExportSymbolOfSymbol(inScope);
118 return accessed === inScope;
120 return (accessed === inScope ||
121 // For compatibility with typescript@2.5: compare declarations because the symbols don't have the same reference
122 Lint.Utils.arraysAreEqual(accessed.declarations, inScope.declarations, function (a, b) { return a === b; }));
125 function tryGetAliasedSymbol(symbol, checker) {
126 return utils.isSymbolFlagSet(symbol, ts.SymbolFlags.Alias)
127 ? checker.getAliasedSymbol(symbol)