noUseBeforeDeclareRule.js 4.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 ts = require("typescript");
  21. var Lint = require("../index");
  22. var Rule = /** @class */ (function (_super) {
  23. tslib_1.__extends(Rule, _super);
  24. function Rule() {
  25. return _super !== null && _super.apply(this, arguments) || this;
  26. }
  27. /* tslint:enable:object-literal-sort-keys */
  28. Rule.FAILURE_STRING = function (name) {
  29. return "variable '" + name + "' used before declaration";
  30. };
  31. Rule.prototype.applyWithProgram = function (sourceFile, program) {
  32. return this.applyWithFunction(sourceFile, walk, undefined, program.getTypeChecker());
  33. };
  34. /* tslint:disable:object-literal-sort-keys */
  35. Rule.metadata = {
  36. ruleName: "no-use-before-declare",
  37. description: "Disallows usage of variables before their declaration.",
  38. 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 "]))),
  39. optionsDescription: "Not configurable.",
  40. options: null,
  41. optionExamples: [true],
  42. type: "functionality",
  43. typescriptOnly: false,
  44. requiresTypeInfo: true,
  45. };
  46. return Rule;
  47. }(Lint.Rules.TypedRule));
  48. exports.Rule = Rule;
  49. function walk(ctx, checker) {
  50. return ts.forEachChild(ctx.sourceFile, function recur(node) {
  51. switch (node.kind) {
  52. case ts.SyntaxKind.TypeReference:
  53. // Ignore types.
  54. return;
  55. case ts.SyntaxKind.PropertyAccessExpression:
  56. // Ignore `y` in `x.y`, but recurse to `x`.
  57. return recur(node.expression);
  58. case ts.SyntaxKind.Identifier:
  59. return checkIdentifier(node, checker.getSymbolAtLocation(node));
  60. case ts.SyntaxKind.ExportSpecifier:
  61. return checkIdentifier(node.name, checker.getExportSpecifierLocalTargetSymbol(node));
  62. default:
  63. return ts.forEachChild(node, recur);
  64. }
  65. });
  66. function checkIdentifier(node, symbol) {
  67. var declarations = symbol === undefined ? undefined : symbol.declarations;
  68. if (declarations === undefined || declarations.length === 0) {
  69. return;
  70. }
  71. var declaredBefore = declarations.some(function (decl) {
  72. switch (decl.kind) {
  73. case ts.SyntaxKind.FunctionDeclaration:
  74. // Functions may be declared later.
  75. return true;
  76. default:
  77. // Use `<=` in case this *is* the declaration.
  78. // If it's a global declared in a different file, OK.
  79. return decl.pos <= node.pos || decl.getSourceFile() !== ctx.sourceFile;
  80. }
  81. });
  82. if (!declaredBefore) {
  83. ctx.addFailureAtNode(node, Rule.FAILURE_STRING(node.text));
  84. }
  85. }
  86. }
  87. var templateObject_1;