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 ts = require("typescript");
21 var Lint = require("../index");
22 var Rule = /** @class */ (function (_super) {
23 tslib_1.__extends(Rule, _super);
25 return _super !== null && _super.apply(this, arguments) || this;
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 + ".";
31 Rule.prototype.apply = function (sourceFile) {
32 return this.applyWithWalker(new Walker(sourceFile, this.ruleName, undefined));
34 /* tslint:disable:object-literal-sort-keys */
36 ruleName: "no-mergeable-namespace",
37 description: "Disallows mergeable namespaces in the same file.",
38 optionsDescription: "Not configurable.",
40 optionExamples: [true],
41 type: "maintainability",
45 }(Lint.Rules.AbstractRule));
47 var Walker = /** @class */ (function (_super) {
48 tslib_1.__extends(Walker, _super);
50 return _super !== null && _super.apply(this, arguments) || this;
52 Walker.prototype.walk = function (node) {
53 return this.checkStatements(node.statements);
55 Walker.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) {
62 var name = statement.name;
63 if (name.kind === ts.SyntaxKind.Identifier) {
65 var prev = seen.get(text);
66 if (prev !== undefined) {
67 this.addFailureAtNode(name, Rule.failureStringFactory(text, this.getLineOfNode(prev.name)));
69 seen.set(text, statement);
71 // Recursively check in all module declarations
72 this.checkModuleDeclaration(statement);
75 Walker.prototype.checkModuleDeclaration = function (decl) {
77 if (body === undefined) {
81 case ts.SyntaxKind.ModuleBlock:
82 this.checkStatements(body.statements);
84 case ts.SyntaxKind.ModuleDeclaration:
85 this.checkModuleDeclaration(body);
88 Walker.prototype.getLineOfNode = function (node) {
89 return ts.getLineAndCharacterOfPosition(this.sourceFile, node.pos).line + 1;
92 }(Lint.AbstractWalker));