noNamespaceRule.js 3.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 tsutils_1 = require("tsutils");
  21. var ts = require("typescript");
  22. var Lint = require("../index");
  23. var OPTION_ALLOW_DECLARATIONS = "allow-declarations";
  24. var Rule = /** @class */ (function (_super) {
  25. tslib_1.__extends(Rule, _super);
  26. function Rule() {
  27. return _super !== null && _super.apply(this, arguments) || this;
  28. }
  29. Rule.prototype.apply = function (sourceFile) {
  30. return this.applyWithFunction(sourceFile, walk, {
  31. allowDeclarations: this.ruleArguments.indexOf(OPTION_ALLOW_DECLARATIONS) !== -1,
  32. });
  33. };
  34. /* tslint:disable:object-literal-sort-keys */
  35. Rule.metadata = {
  36. ruleName: "no-namespace",
  37. description: "Disallows use of internal \`module\`s and \`namespace\`s.",
  38. descriptionDetails: "This rule still allows the use of `declare module ... {}`",
  39. rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n ES6-style external modules are the standard way to modularize code.\n Using `module {}` and `namespace {}` are outdated ways to organize TypeScript code."], ["\n ES6-style external modules are the standard way to modularize code.\n Using \\`module {}\\` and \\`namespace {}\\` are outdated ways to organize TypeScript code."]))),
  40. optionsDescription: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n One argument may be optionally provided:\n\n * `", "` allows `declare namespace ... {}` to describe external APIs."], ["\n One argument may be optionally provided:\n\n * \\`", "\\` allows \\`declare namespace ... {}\\` to describe external APIs."])), OPTION_ALLOW_DECLARATIONS),
  41. options: {
  42. type: "array",
  43. items: {
  44. type: "string",
  45. enum: [OPTION_ALLOW_DECLARATIONS],
  46. },
  47. minLength: 0,
  48. maxLength: 1,
  49. },
  50. optionExamples: [true, [true, OPTION_ALLOW_DECLARATIONS]],
  51. type: "typescript",
  52. typescriptOnly: true,
  53. };
  54. /* tslint:enable:object-literal-sort-keys */
  55. Rule.FAILURE_STRING = "'namespace' and 'module' are disallowed";
  56. return Rule;
  57. }(Lint.Rules.AbstractRule));
  58. exports.Rule = Rule;
  59. function walk(ctx) {
  60. // Ignore all .d.ts files by returning and not walking their ASTs.
  61. // .d.ts declarations do not have the Ambient flag set, but are still declarations.
  62. if (ctx.sourceFile.isDeclarationFile && ctx.options.allowDeclarations) {
  63. return;
  64. }
  65. for (var _i = 0, _a = ctx.sourceFile.statements; _i < _a.length; _i++) {
  66. var node = _a[_i];
  67. if (node.kind === ts.SyntaxKind.ModuleDeclaration) {
  68. if (node.name.kind !== ts.SyntaxKind.StringLiteral &&
  69. !tsutils_1.isNodeFlagSet(node, ts.NodeFlags.GlobalAugmentation) &&
  70. (!ctx.options.allowDeclarations || !tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.DeclareKeyword))) {
  71. ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
  72. }
  73. }
  74. }
  75. }
  76. var templateObject_1, templateObject_2;