4 * Copyright 2014 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 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);
26 return _super !== null && _super.apply(this, arguments) || this;
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.";
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) {
39 return this.applyWithFunction(sourceFile, walk, bannedGlobals, program.getTypeChecker());
42 /* tslint:disable:object-literal-sort-keys */
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 "]))),
51 items: { type: "string" },
53 optionExamples: [[true, "name", "length", "event"]],
54 type: "functionality",
55 typescriptOnly: false,
56 requiresTypeInfo: true,
59 }(Lint.Rules.TypedRule));
61 function walk(ctx, checker) {
62 return ts.forEachChild(ctx.sourceFile, function recur(node) {
63 if (node == undefined) {
66 // Handles `const { bar, length: { x: y = () => event } } = foo`
67 if (tsutils_1.isBindingElement(node)) {
68 recur(node.initializer);
70 if (node.propertyName != undefined && tsutils_1.isComputedPropertyName(node.propertyName)) {
71 recur(node.propertyName);
74 else if (tsutils_1.isPropertyAccessExpression(node)) {
75 // Ignore `y` in `x.y`, but recurse to `x`.
76 recur(node.expression);
78 else if (tsutils_1.isIdentifier(node)) {
79 checkIdentifier(node);
82 ts.forEachChild(node, recur);
85 function checkIdentifier(node) {
86 if (!ctx.options.has(node.text)) {
89 var symbol = checker.getSymbolAtLocation(node);
90 var declarations = symbol === undefined ? undefined : symbol.declarations;
91 if (declarations === undefined || declarations.length === 0) {
94 var isAmbientGlobal = declarations.some(function (decl) { return decl.getSourceFile().isDeclarationFile; });
95 if (isAmbientGlobal) {
96 ctx.addFailureAtNode(node, Rule.FAILURE_STRING(node.text));
100 var templateObject_1, templateObject_2, templateObject_3;