Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c1df669

Browse files
fix(eslint-plugin): [no-namespace] allow namespaces in nested declarations with allowDeclarations (typescript-eslint#2238)
1 parent 16667b1 commit c1df669

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

packages/eslint-plugin/docs/rules/no-namespace.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ Examples of **correct** code for the `{ "allowDeclarations": true }` option:
5151
declare module 'foo' {}
5252
declare module foo {}
5353
declare namespace foo {}
54+
55+
declare global {
56+
namespace foo {}
57+
}
58+
59+
declare module foo {
60+
namespace foo {}
61+
}
5462
```
5563

5664
Examples of **incorrect** code for the `{ "allowDeclarations": false }` option:

packages/eslint-plugin/src/rules/no-namespace.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ export default util.createRule<Options, MessageIds>({
5050
create(context, [{ allowDeclarations, allowDefinitionFiles }]) {
5151
const filename = context.getFilename();
5252

53+
function isDeclaration(node: TSESTree.TSModuleDeclaration): boolean {
54+
return (
55+
node.declare === true ||
56+
(node.parent!.parent?.type === AST_NODE_TYPES.TSModuleDeclaration &&
57+
isDeclaration(node.parent!.parent))
58+
);
59+
}
60+
5361
return {
5462
"TSModuleDeclaration[global!=true][id.type='Identifier']"(
5563
node: TSESTree.TSModuleDeclaration,
@@ -58,7 +66,7 @@ export default util.createRule<Options, MessageIds>({
5866
(node.parent &&
5967
node.parent.type === AST_NODE_TYPES.TSModuleDeclaration) ||
6068
(allowDefinitionFiles && util.isDefinitionFile(filename)) ||
61-
(allowDeclarations && node.declare === true)
69+
(allowDeclarations && isDeclaration(node))
6270
) {
6371
return;
6472
}

packages/eslint-plugin/tests/rules/no-namespace.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,42 @@ ruleTester.run('no-namespace', rule, {
1717
code: 'declare namespace foo {}',
1818
options: [{ allowDeclarations: true }],
1919
},
20+
{
21+
code: `
22+
declare global {
23+
namespace foo {}
24+
}
25+
`,
26+
options: [{ allowDeclarations: true }],
27+
},
28+
{
29+
code: `
30+
declare module foo {
31+
namespace bar {}
32+
}
33+
`,
34+
options: [{ allowDeclarations: true }],
35+
},
36+
{
37+
code: `
38+
declare global {
39+
namespace foo {
40+
namespace bar {}
41+
}
42+
}
43+
`,
44+
options: [{ allowDeclarations: true }],
45+
},
46+
{
47+
code: `
48+
declare namespace foo {
49+
namespace bar {
50+
namespace baz {}
51+
}
52+
}
53+
`,
54+
options: [{ allowDeclarations: true }],
55+
},
2056
{
2157
filename: 'test.d.ts',
2258
code: 'namespace foo {}',
@@ -71,6 +107,28 @@ ruleTester.run('no-namespace', rule, {
71107
},
72108
],
73109
},
110+
{
111+
code: 'module foo {}',
112+
options: [{ allowDeclarations: true }],
113+
errors: [
114+
{
115+
messageId: 'moduleSyntaxIsPreferred',
116+
line: 1,
117+
column: 1,
118+
},
119+
],
120+
},
121+
{
122+
code: 'namespace foo {}',
123+
options: [{ allowDeclarations: true }],
124+
errors: [
125+
{
126+
messageId: 'moduleSyntaxIsPreferred',
127+
line: 1,
128+
column: 1,
129+
},
130+
],
131+
},
74132
{
75133
code: 'declare module foo {}',
76134
errors: [

0 commit comments

Comments
 (0)