From 1b668a35c57c4999291e225bdaa8a1e09add0bf7 Mon Sep 17 00:00:00 2001 From: Viktor Mitev Date: Tue, 1 Sep 2015 01:10:18 +0300 Subject: [PATCH] added test cases for 'no-internal-module' option and excluded errors raised for nested declarations with the dot-syntax --- src/rules/noInternalModuleRule.ts | 19 ++++++++--- test/files/rules/nointernalmodule.test.ts | 39 +++++++++++++++++++++++ test/rules/noInternalModuleTests.ts | 9 +++++- 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/src/rules/noInternalModuleRule.ts b/src/rules/noInternalModuleRule.ts index dc47e01219d..29824e00dff 100644 --- a/src/rules/noInternalModuleRule.ts +++ b/src/rules/noInternalModuleRule.ts @@ -24,12 +24,23 @@ export class Rule extends Lint.Rules.AbstractRule { class NoInternalModuleWalker extends Lint.RuleWalker { public visitModuleDeclaration(node: ts.ModuleDeclaration) { - if (Lint.isNodeFlagSet(node, ts.NodeFlags.Namespace)) { - // ok namespace - } else if (node.name.kind === ts.SyntaxKind.Identifier) { - // for external modules, node.name will be a LiteralExpression instead of Identifier + if (this.isInternalModuleDeclaration(node)) { this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING)); } super.visitModuleDeclaration(node); } + + private isInternalModuleDeclaration(node: ts.ModuleDeclaration) { + // an internal module declaration is not a namespace or a nested declaration + // for external modules, node.name.kind will be a LiteralExpression instead of Identifier + return !Lint.isNodeFlagSet(node, ts.NodeFlags.Namespace) + && !this.isNestedDeclaration(node) + && node.name.kind === ts.SyntaxKind.Identifier; + } + + private isNestedDeclaration(node: ts.ModuleDeclaration) { + // in a declaration expression like 'module a.b.c' - 'a' is the top level module declaration node and 'b' and 'c' are nested + // therefore we can depend that a node's position will only match with its name's position for nested nodes + return node.name.pos === node.pos; + } } diff --git a/test/files/rules/nointernalmodule.test.ts b/test/files/rules/nointernalmodule.test.ts index 727789b3fb5..02841ffcc14 100644 --- a/test/files/rules/nointernalmodule.test.ts +++ b/test/files/rules/nointernalmodule.test.ts @@ -11,3 +11,42 @@ declare module "hoge" { } declare module 'fuga' { } + +namespace foo.bar { +} +namespace foo.bar.baz { +} +namespace foo { + namespace bar.baz { + } +} + +namespace foo.bar { + module baz { + namespace buzz { + } + } +} + +module foo.bar { + namespace baz { + module buzz { + } + } +} + +namespace name.namespace { +} +namespace namespace.name { +} + +// intentionally malformed for test cases, do not format +declare module declare +.dec{} +declare module dec . declare { +} + +module mod.module{} +module module.mod +{ +} diff --git a/test/rules/noInternalModuleTests.ts b/test/rules/noInternalModuleTests.ts index 0eddd77e63d..08c0c0ee9d8 100644 --- a/test/rules/noInternalModuleTests.ts +++ b/test/rules/noInternalModuleTests.ts @@ -23,7 +23,14 @@ describe("", () => { const actualFailures = Lint.Test.applyRuleOnFile(fileName, NoInternalModule); const expectedFailures = [ Lint.Test.createFailure(fileName, [4, 1], [4, 15], failureString), - Lint.Test.createFailure(fileName, [7, 1], [7, 24], failureString) + Lint.Test.createFailure(fileName, [7, 1], [7, 24], failureString), + Lint.Test.createFailure(fileName, [25, 5], [28, 6], failureString), + Lint.Test.createFailure(fileName, [31, 1], [36, 2], failureString), + Lint.Test.createFailure(fileName, [33, 9], [33, 32], failureString), + Lint.Test.createFailure(fileName, [44, 1], [44, 30], failureString), + Lint.Test.createFailure(fileName, [46, 1], [46, 35], failureString), + Lint.Test.createFailure(fileName, [49, 1], [49, 21], failureString), + Lint.Test.createFailure(fileName, [50, 1], [50, 22], failureString) ]; Lint.Test.assertFailuresEqual(actualFailures, expectedFailures);