noMergeableNamespaceRule.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2016 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.failureStringFactory = function (name, seenBeforeLine) {
  29. return "Mergeable namespace '" + name + "' found. Merge its contents with the namespace on line " + seenBeforeLine + ".";
  30. };
  31. Rule.prototype.apply = function (sourceFile) {
  32. return this.applyWithWalker(new Walker(sourceFile, this.ruleName, undefined));
  33. };
  34. /* tslint:disable:object-literal-sort-keys */
  35. Rule.metadata = {
  36. ruleName: "no-mergeable-namespace",
  37. description: "Disallows mergeable namespaces in the same file.",
  38. optionsDescription: "Not configurable.",
  39. options: null,
  40. optionExamples: [true],
  41. type: "maintainability",
  42. typescriptOnly: true,
  43. };
  44. return Rule;
  45. }(Lint.Rules.AbstractRule));
  46. exports.Rule = Rule;
  47. var Walker = /** @class */ (function (_super) {
  48. tslib_1.__extends(Walker, _super);
  49. function Walker() {
  50. return _super !== null && _super.apply(this, arguments) || this;
  51. }
  52. Walker.prototype.walk = function (node) {
  53. return this.checkStatements(node.statements);
  54. };
  55. Walker.prototype.checkStatements = function (statements) {
  56. var seen = new Map();
  57. for (var _i = 0, statements_1 = statements; _i < statements_1.length; _i++) {
  58. var statement = statements_1[_i];
  59. if (statement.kind !== ts.SyntaxKind.ModuleDeclaration) {
  60. continue;
  61. }
  62. var name = statement.name;
  63. if (name.kind === ts.SyntaxKind.Identifier) {
  64. var text = name.text;
  65. var prev = seen.get(text);
  66. if (prev !== undefined) {
  67. this.addFailureAtNode(name, Rule.failureStringFactory(text, this.getLineOfNode(prev.name)));
  68. }
  69. seen.set(text, statement);
  70. }
  71. // Recursively check in all module declarations
  72. this.checkModuleDeclaration(statement);
  73. }
  74. };
  75. Walker.prototype.checkModuleDeclaration = function (decl) {
  76. var body = decl.body;
  77. if (body === undefined) {
  78. return;
  79. }
  80. switch (body.kind) {
  81. case ts.SyntaxKind.ModuleBlock:
  82. this.checkStatements(body.statements);
  83. break;
  84. case ts.SyntaxKind.ModuleDeclaration:
  85. this.checkModuleDeclaration(body);
  86. }
  87. };
  88. Walker.prototype.getLineOfNode = function (node) {
  89. return ts.getLineAndCharacterOfPosition(this.sourceFile, node.pos).line + 1;
  90. };
  91. return Walker;
  92. }(Lint.AbstractWalker));