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 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);
28 return _super !== null && _super.apply(this, arguments) || this;
30 /* tslint:enable:object-literal-sort-keys */
31 Rule.FAILURE_STRING = function (name) {
32 return "variable '" + name + "' used before declaration";
34 Rule.prototype.applyWithProgram = function (sourceFile, program) {
35 return this.applyWithFunction(sourceFile, walk, undefined, program.getTypeChecker());
37 /* tslint:disable:object-literal-sort-keys */
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.",
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."
54 }(Lint.Rules.TypedRule));
56 function walk(ctx, checker) {
57 return ts.forEachChild(ctx.sourceFile, function recur(node) {
59 case ts.SyntaxKind.TypeReference:
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)) {
69 return checkIdentifier(node, checker.getSymbolAtLocation(node));
70 case ts.SyntaxKind.ExportSpecifier:
71 return checkIdentifier(node.name, checker.getExportSpecifierLocalTargetSymbol(node));
73 return ts.forEachChild(node, recur);
76 function checkIdentifier(node, symbol) {
77 var declarations = symbol === undefined ? undefined : symbol.declarations;
78 if (declarations === undefined || declarations.length === 0) {
81 var declaredBefore = declarations.some(function (decl) {
83 case ts.SyntaxKind.FunctionDeclaration:
84 // Functions may be declared later.
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;
92 if (!declaredBefore) {
93 ctx.addFailureAtNode(node, Rule.FAILURE_STRING(node.text));
97 * Destructured vars/args w/ rename are declared later in the source.
98 * var { x: y } = { x: 43 };
100 function isPropNameInBinding(node) {
101 return (node.parent !== undefined &&
102 tsutils_1.isBindingElement(node.parent) &&
103 node.parent.propertyName === node);
106 var templateObject_1;