noInternalModuleRule.js 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2013 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 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);
  25. function Rule() {
  26. return _super !== null && _super.apply(this, arguments) || this;
  27. }
  28. Rule.prototype.apply = function (sourceFile) {
  29. return this.applyWithWalker(new NoInternalModuleWalker(sourceFile, this.ruleName, undefined));
  30. };
  31. /* tslint:disable:object-literal-sort-keys */
  32. Rule.metadata = {
  33. ruleName: "no-internal-module",
  34. description: "Disallows internal `module`",
  35. rationale: "Using `module` leads to a confusion of concepts with external modules. Use the newer `namespace` keyword instead.",
  36. optionsDescription: "Not configurable.",
  37. options: null,
  38. optionExamples: [true],
  39. type: "typescript",
  40. typescriptOnly: true,
  41. hasFix: true,
  42. };
  43. /* tslint:enable:object-literal-sort-keys */
  44. Rule.FAILURE_STRING = "The internal 'module' syntax is deprecated, use the 'namespace' keyword instead.";
  45. return Rule;
  46. }(Lint.Rules.AbstractRule));
  47. exports.Rule = Rule;
  48. var NoInternalModuleWalker = /** @class */ (function (_super) {
  49. tslib_1.__extends(NoInternalModuleWalker, _super);
  50. function NoInternalModuleWalker() {
  51. return _super !== null && _super.apply(this, arguments) || this;
  52. }
  53. NoInternalModuleWalker.prototype.walk = function (sourceFile) {
  54. return this.checkStatements(sourceFile.statements);
  55. };
  56. NoInternalModuleWalker.prototype.checkStatements = function (statements) {
  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. this.checkModuleDeclaration(statement);
  61. }
  62. }
  63. };
  64. NoInternalModuleWalker.prototype.checkModuleDeclaration = function (node, nested) {
  65. if (!nested &&
  66. node.name.kind === ts.SyntaxKind.Identifier &&
  67. !tsutils_1.isNodeFlagSet(node, ts.NodeFlags.Namespace) &&
  68. // augmenting global uses a special syntax that is allowed
  69. // see https://github.com/Microsoft/TypeScript/pull/6213
  70. !tsutils_1.isNodeFlagSet(node, ts.NodeFlags.GlobalAugmentation)) {
  71. var end = node.name.pos;
  72. var start = end - "module".length;
  73. this.addFailure(start, end, Rule.FAILURE_STRING, Lint.Replacement.replaceFromTo(start, end, "namespace"));
  74. }
  75. if (node.body !== undefined) {
  76. switch (node.body.kind) {
  77. case ts.SyntaxKind.ModuleBlock:
  78. return this.checkStatements(node.body.statements);
  79. case ts.SyntaxKind.ModuleDeclaration:
  80. return this.checkModuleDeclaration(node.body, true);
  81. }
  82. }
  83. };
  84. return NoInternalModuleWalker;
  85. }(Lint.AbstractWalker));