4 * Copyright 2018 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 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);
27 return _super !== null && _super.apply(this, arguments) || this;
29 Rule.prototype.apply = function (sourceFile) {
30 return this.applyWithFunction(sourceFile, walk, {
31 allowDeclarations: this.ruleArguments.indexOf(OPTION_ALLOW_DECLARATIONS) !== -1,
34 /* tslint:disable:object-literal-sort-keys */
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),
45 enum: [OPTION_ALLOW_DECLARATIONS],
50 optionExamples: [true, [true, OPTION_ALLOW_DECLARATIONS]],
54 /* tslint:enable:object-literal-sort-keys */
55 Rule.FAILURE_STRING = "'namespace' and 'module' are disallowed";
57 }(Lint.Rules.AbstractRule));
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) {
65 for (var _i = 0, _a = ctx.sourceFile.statements; _i < _a.length; _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 ||
71 !tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.DeclareKeyword))) {
72 ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
77 var templateObject_1, templateObject_2;