.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noRestrictedGlobalsRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2014 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 tsutils_1 = 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 "Unexpected global variable '" + name + "'. Use a local parameter or variable instead.";
31     };
32     Rule.prototype.applyWithProgram = function (sourceFile, program) {
33         var bannedList = this.ruleArguments.length > 0 ? this.ruleArguments : ["event", "name", "length"];
34         var bannedGlobals = new Set(bannedList);
35         if (sourceFile.isDeclarationFile) {
36             return [];
37         }
38         else {
39             return this.applyWithFunction(sourceFile, walk, bannedGlobals, program.getTypeChecker());
40         }
41     };
42     /* tslint:disable:object-literal-sort-keys */
43     Rule.metadata = {
44         ruleName: "no-restricted-globals",
45         description: "Disallow specific global variables.",
46         rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            ```ts\n            function broken(evt: Event) {\n                // Meant to do something with `evt` but typed it incorrectly.\n                Event.target;  // compiler error\n                event.target;  // should be a lint failure\n            }\n\n            Early Internet Explorer versions exposed the current DOM event as a global variable 'event',\n            but using this variable has been considered a bad practice for a long time.\n            Restricting this will make sure this variable isn\u2019t used in browser code.\n            ```\n        "], ["\n            \\`\\`\\`ts\n            function broken(evt: Event) {\n                // Meant to do something with \\`evt\\` but typed it incorrectly.\n                Event.target;  // compiler error\n                event.target;  // should be a lint failure\n            }\n\n            Early Internet Explorer versions exposed the current DOM event as a global variable 'event',\n            but using this variable has been considered a bad practice for a long time.\n            Restricting this will make sure this variable isn\u2019t used in browser code.\n            \\`\\`\\`\n        "]))),
47         descriptionDetails: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n            Disallowing usage of specific global variables can be useful if you want to allow\n            a set of global variables by enabling an environment, but still want to disallow\n            some of those.\n        "], ["\n            Disallowing usage of specific global variables can be useful if you want to allow\n            a set of global variables by enabling an environment, but still want to disallow\n            some of those.\n        "]))),
48         optionsDescription: Lint.Utils.dedent(templateObject_3 || (templateObject_3 = tslib_1.__makeTemplateObject(["\n            This rule takes a list of strings, where each string is a global to be restricted.\n            `event`, `name` and `length` are restricted by default.\n        "], ["\n            This rule takes a list of strings, where each string is a global to be restricted.\n            \\`event\\`, \\`name\\` and \\`length\\` are restricted by default.\n        "]))),
49         options: {
50             type: "list",
51             items: { type: "string" },
52         },
53         optionExamples: [[true, "name", "length", "event"]],
54         type: "functionality",
55         typescriptOnly: false,
56         requiresTypeInfo: true,
57     };
58     return Rule;
59 }(Lint.Rules.TypedRule));
60 exports.Rule = Rule;
61 function walk(ctx, checker) {
62     return ts.forEachChild(ctx.sourceFile, function recur(node) {
63         if (node == undefined) {
64             return;
65         }
66         // Handles `const { bar, length: { x: y = () => event } } = foo`
67         if (tsutils_1.isBindingElement(node)) {
68             recur(node.initializer);
69             recur(node.name);
70             if (node.propertyName != undefined && tsutils_1.isComputedPropertyName(node.propertyName)) {
71                 recur(node.propertyName);
72             }
73         }
74         else if (tsutils_1.isPropertyAccessExpression(node)) {
75             // Ignore `y` in `x.y`, but recurse to `x`.
76             recur(node.expression);
77         }
78         else if (tsutils_1.isIdentifier(node)) {
79             checkIdentifier(node);
80         }
81         else {
82             ts.forEachChild(node, recur);
83         }
84     });
85     function checkIdentifier(node) {
86         if (!ctx.options.has(node.text)) {
87             return;
88         }
89         var symbol = checker.getSymbolAtLocation(node);
90         var declarations = symbol === undefined ? undefined : symbol.declarations;
91         if (declarations === undefined || declarations.length === 0) {
92             return;
93         }
94         var isAmbientGlobal = declarations.some(function (decl) { return decl.getSourceFile().isDeclarationFile; });
95         if (isAmbientGlobal) {
96             ctx.addFailureAtNode(node, Rule.FAILURE_STRING(node.text));
97         }
98     }
99 }
100 var templateObject_1, templateObject_2, templateObject_3;