From 34bff378b31cef2e4e09934e1162c3b8b6707ca1 Mon Sep 17 00:00:00 2001 From: cherryblossom <31467609+cherryblossom000@users.noreply.github.com> Date: Mon, 22 Jun 2020 23:48:23 +1000 Subject: [PATCH 1/3] fix(eslint-plugin): [no-namespace] allow namespaces in nested declarations with `allowDeclarations` (fixes #2237) --- .../eslint-plugin/docs/rules/no-namespace.md | 8 ++++++ .../eslint-plugin/src/rules/no-namespace.ts | 10 ++++++- .../tests/rules/no-namespace.test.ts | 26 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/docs/rules/no-namespace.md b/packages/eslint-plugin/docs/rules/no-namespace.md index d82ad724b7b..b155c2aeb30 100644 --- a/packages/eslint-plugin/docs/rules/no-namespace.md +++ b/packages/eslint-plugin/docs/rules/no-namespace.md @@ -51,6 +51,14 @@ Examples of **correct** code for the `{ "allowDeclarations": true }` option: declare module 'foo' {} declare module foo {} declare namespace foo {} + +declare global { + namespace foo {} +} + +declare module foo { + namespace foo {} +} ``` Examples of **incorrect** code for the `{ "allowDeclarations": false }` option: diff --git a/packages/eslint-plugin/src/rules/no-namespace.ts b/packages/eslint-plugin/src/rules/no-namespace.ts index 3698b4f941b..f8cc05b1047 100644 --- a/packages/eslint-plugin/src/rules/no-namespace.ts +++ b/packages/eslint-plugin/src/rules/no-namespace.ts @@ -50,6 +50,14 @@ export default util.createRule({ create(context, [{ allowDeclarations, allowDefinitionFiles }]) { const filename = context.getFilename(); + function isDeclaration(node: TSESTree.TSModuleDeclaration): boolean { + return ( + node.declare === true || + (node.parent!.parent?.type === AST_NODE_TYPES.TSModuleDeclaration && + isDeclaration(node.parent!.parent)) + ); + } + return { "TSModuleDeclaration[global!=true][id.type='Identifier']"( node: TSESTree.TSModuleDeclaration, @@ -58,7 +66,7 @@ export default util.createRule({ (node.parent && node.parent.type === AST_NODE_TYPES.TSModuleDeclaration) || (allowDefinitionFiles && util.isDefinitionFile(filename)) || - (allowDeclarations && node.declare === true) + (allowDeclarations && isDeclaration(node)) ) { return; } diff --git a/packages/eslint-plugin/tests/rules/no-namespace.test.ts b/packages/eslint-plugin/tests/rules/no-namespace.test.ts index e175f5895df..901bea7354b 100644 --- a/packages/eslint-plugin/tests/rules/no-namespace.test.ts +++ b/packages/eslint-plugin/tests/rules/no-namespace.test.ts @@ -17,6 +17,32 @@ ruleTester.run('no-namespace', rule, { code: 'declare namespace foo {}', options: [{ allowDeclarations: true }], }, + { + code: ` +declare global { + namespace foo {} +} + `, + options: [{ allowDeclarations: true }], + }, + { + code: ` +declare module foo { + namespace bar {} +} + `, + options: [{ allowDeclarations: true }], + }, + { + code: ` +declare global { + namespace foo { + namespace bar {} + } +} + `, + options: [{ allowDeclarations: true }], + }, { filename: 'test.d.ts', code: 'namespace foo {}', From c532a95a7407d4eda20375d370ee46a3d8ade6cf Mon Sep 17 00:00:00 2001 From: cherryblossom <31467609+cherryblossom000@users.noreply.github.com> Date: Tue, 23 Jun 2020 00:17:34 +1000 Subject: [PATCH 2/3] test(eslint-plugin): [no-namespace] add some more tests --- .../tests/rules/no-namespace.test.ts | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/packages/eslint-plugin/tests/rules/no-namespace.test.ts b/packages/eslint-plugin/tests/rules/no-namespace.test.ts index 901bea7354b..80abf5dcf96 100644 --- a/packages/eslint-plugin/tests/rules/no-namespace.test.ts +++ b/packages/eslint-plugin/tests/rules/no-namespace.test.ts @@ -97,6 +97,28 @@ declare global { }, ], }, + { + code: 'module foo {}', + options: [{ allowDeclarations: true }], + errors: [ + { + messageId: 'moduleSyntaxIsPreferred', + line: 1, + column: 1, + }, + ], + }, + { + code: 'namespace foo {}', + options: [{ allowDeclarations: true }], + errors: [ + { + messageId: 'moduleSyntaxIsPreferred', + line: 1, + column: 1, + }, + ], + }, { code: 'declare module foo {}', errors: [ From 6d950947e9d7fd560fb887ffc0bd4cf497bf016d Mon Sep 17 00:00:00 2001 From: cherryblossom <31467609+cherryblossom000@users.noreply.github.com> Date: Tue, 23 Jun 2020 00:20:00 +1000 Subject: [PATCH 3/3] test(eslint-plugin): [no-namespace] add another test --- .../eslint-plugin/tests/rules/no-namespace.test.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/eslint-plugin/tests/rules/no-namespace.test.ts b/packages/eslint-plugin/tests/rules/no-namespace.test.ts index 80abf5dcf96..3bb9079b3fc 100644 --- a/packages/eslint-plugin/tests/rules/no-namespace.test.ts +++ b/packages/eslint-plugin/tests/rules/no-namespace.test.ts @@ -39,6 +39,16 @@ declare global { namespace foo { namespace bar {} } +} + `, + options: [{ allowDeclarations: true }], + }, + { + code: ` +declare namespace foo { + namespace bar { + namespace baz {} + } } `, options: [{ allowDeclarations: true }],