.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noUnnecessaryQualifierRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2017 Palantir Technologies, Inc.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
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);
25     function Rule() {
26         return _super !== null && _super.apply(this, arguments) || this;
27     }
28     /* tslint:enable:object-literal-sort-keys */
29     Rule.FAILURE_STRING = function (name) {
30         return "Qualifier is unnecessary since '" + name + "' is in scope.";
31     };
32     Rule.prototype.applyWithProgram = function (sourceFile, program) {
33         return this.applyWithFunction(sourceFile, walk, undefined, program.getTypeChecker());
34     };
35     /* tslint:disable:object-literal-sort-keys */
36     Rule.metadata = {
37         ruleName: "no-unnecessary-qualifier",
38         description: "Warns when a namespace qualifier (`A.x`) is unnecessary.",
39         hasFix: true,
40         optionsDescription: "Not configurable.",
41         options: null,
42         optionExamples: [true],
43         type: "style",
44         typescriptOnly: true,
45         requiresTypeInfo: true,
46     };
47     return Rule;
48 }(Lint.Rules.TypedRule));
49 exports.Rule = Rule;
50 function walk(ctx, checker) {
51     var namespacesInScope = [];
52     ts.forEachChild(ctx.sourceFile, cb);
53     function cb(node) {
54         switch (node.kind) {
55             case ts.SyntaxKind.ModuleDeclaration:
56             case ts.SyntaxKind.EnumDeclaration:
57                 namespacesInScope.push(node);
58                 ts.forEachChild(node, cb);
59                 namespacesInScope.pop();
60                 break;
61             case ts.SyntaxKind.QualifiedName:
62                 var _a = node, left = _a.left, right = _a.right;
63                 visitNamespaceAccess(node, left, right);
64                 break;
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);
69                     break;
70                 }
71             // falls through
72             default:
73                 ts.forEachChild(node, cb);
74         }
75     }
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);
80         }
81         else {
82             // Only look for nested qualifier errors if we didn't already fail on the outer qualifier.
83             ts.forEachChild(node, cb);
84         }
85     }
86     function qualifierIsUnnecessary(qualifier, name) {
87         var namespaceSymbol = checker.getSymbolAtLocation(qualifier);
88         if (namespaceSymbol === undefined || !symbolIsNamespaceInScope(namespaceSymbol)) {
89             return false;
90         }
91         var accessedSymbol = checker.getSymbolAtLocation(name);
92         if (accessedSymbol === undefined) {
93             return false;
94         }
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);
98     }
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; });
103     }
104     function symbolIsNamespaceInScope(symbol) {
105         var symbolDeclarations = symbol.getDeclarations();
106         if (symbolDeclarations === undefined) {
107             return false;
108         }
109         else if (symbolDeclarations.some(function (decl) { return namespacesInScope.some(function (ns) { return ns === decl; }); })) {
110             return true;
111         }
112         var alias = tryGetAliasedSymbol(symbol, checker);
113         return alias !== undefined && symbolIsNamespaceInScope(alias);
114     }
115     function symbolsAreEqual(accessed, inScope) {
116         if (checker.getExportSymbolOfSymbol !== undefined) {
117             inScope = checker.getExportSymbolOfSymbol(inScope);
118             return accessed === inScope;
119         }
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; }));
123     }
124 }
125 function tryGetAliasedSymbol(symbol, checker) {
126     return utils.isSymbolFlagSet(symbol, ts.SymbolFlags.Alias)
127         ? checker.getAliasedSymbol(symbol)
128         : undefined;
129 }