.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noUseBeforeDeclareRule.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 semver = require("semver");
21 var tsutils_1 = require("tsutils");
22 var ts = require("typescript");
23 var Lint = require("../index");
24 var noUseBeforeDeclare_examples_1 = require("./code-examples/noUseBeforeDeclare.examples");
25 var Rule = /** @class */ (function (_super) {
26     tslib_1.__extends(Rule, _super);
27     function Rule() {
28         return _super !== null && _super.apply(this, arguments) || this;
29     }
30     /* tslint:enable:object-literal-sort-keys */
31     Rule.FAILURE_STRING = function (name) {
32         return "variable '" + name + "' used before declaration";
33     };
34     Rule.prototype.applyWithProgram = function (sourceFile, program) {
35         return this.applyWithFunction(sourceFile, walk, undefined, program.getTypeChecker());
36     };
37     /* tslint:disable:object-literal-sort-keys */
38     Rule.metadata = {
39         ruleName: "no-use-before-declare",
40         description: "Disallows usage of variables before their declaration.",
41         descriptionDetails: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            This rule is primarily useful when using the `var` keyword since the compiler will\n            automatically detect if a block-scoped `let` and `const` variable is used before\n            declaration. Since most modern TypeScript doesn't use `var`, this rule is generally\n            discouraged and is kept around for legacy purposes. It is slow to compute, is not\n            enabled in the built-in configuration presets, and should not be used to inform TSLint\n            design decisions.\n        "], ["\n            This rule is primarily useful when using the \\`var\\` keyword since the compiler will\n            automatically detect if a block-scoped \\`let\\` and \\`const\\` variable is used before\n            declaration. Since most modern TypeScript doesn't use \\`var\\`, this rule is generally\n            discouraged and is kept around for legacy purposes. It is slow to compute, is not\n            enabled in the built-in configuration presets, and should not be used to inform TSLint\n            design decisions.\n        "]))),
42         optionsDescription: "Not configurable.",
43         options: null,
44         optionExamples: [true],
45         type: "functionality",
46         typescriptOnly: false,
47         requiresTypeInfo: true,
48         codeExamples: noUseBeforeDeclare_examples_1.codeExamples,
49         deprecationMessage: semver.gte(ts.version, "2.9.0-dev.0")
50             ? "Since TypeScript 2.9. Please use the built-in compiler checks instead."
51             : undefined,
52     };
53     return Rule;
54 }(Lint.Rules.TypedRule));
55 exports.Rule = Rule;
56 function walk(ctx, checker) {
57     return ts.forEachChild(ctx.sourceFile, function recur(node) {
58         switch (node.kind) {
59             case ts.SyntaxKind.TypeReference:
60                 // Ignore types.
61                 return;
62             case ts.SyntaxKind.PropertyAccessExpression:
63                 // Ignore `y` in `x.y`, but recurse to `x`.
64                 return recur(node.expression);
65             case ts.SyntaxKind.Identifier:
66                 if (isPropNameInBinding(node)) {
67                     return;
68                 }
69                 return checkIdentifier(node, checker.getSymbolAtLocation(node));
70             case ts.SyntaxKind.ExportSpecifier:
71                 return checkIdentifier(node.name, checker.getExportSpecifierLocalTargetSymbol(node));
72             default:
73                 return ts.forEachChild(node, recur);
74         }
75     });
76     function checkIdentifier(node, symbol) {
77         var declarations = symbol === undefined ? undefined : symbol.declarations;
78         if (declarations === undefined || declarations.length === 0) {
79             return;
80         }
81         var declaredBefore = declarations.some(function (decl) {
82             switch (decl.kind) {
83                 case ts.SyntaxKind.FunctionDeclaration:
84                     // Functions may be declared later.
85                     return true;
86                 default:
87                     // Use `<=` in case this *is* the declaration.
88                     // If it's a global declared in a different file, OK.
89                     return decl.pos <= node.pos || decl.getSourceFile() !== ctx.sourceFile;
90             }
91         });
92         if (!declaredBefore) {
93             ctx.addFailureAtNode(node, Rule.FAILURE_STRING(node.text));
94         }
95     }
96     /**
97      * Destructured vars/args w/ rename are declared later in the source.
98      * var { x: y } = { x: 43 };
99      */
100     function isPropNameInBinding(node) {
101         return (node.parent !== undefined &&
102             tsutils_1.isBindingElement(node.parent) &&
103             node.parent.propertyName === node);
104     }
105 }
106 var templateObject_1;