Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [no-namespace] allow namespaces in nested declarations with allowDeclarations #2238

Merged
merged 5 commits into from Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/eslint-plugin/docs/rules/no-namespace.md
Expand Up @@ -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:
Expand Down
10 changes: 9 additions & 1 deletion packages/eslint-plugin/src/rules/no-namespace.ts
Expand Up @@ -50,6 +50,14 @@ export default util.createRule<Options, MessageIds>({
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,
Expand All @@ -58,7 +66,7 @@ export default util.createRule<Options, MessageIds>({
(node.parent &&
node.parent.type === AST_NODE_TYPES.TSModuleDeclaration) ||
(allowDefinitionFiles && util.isDefinitionFile(filename)) ||
(allowDeclarations && node.declare === true)
(allowDeclarations && isDeclaration(node))
) {
return;
}
Expand Down
58 changes: 58 additions & 0 deletions packages/eslint-plugin/tests/rules/no-namespace.test.ts
Expand Up @@ -17,6 +17,42 @@ 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 }],
},
{
code: `
declare namespace foo {
namespace bar {
namespace baz {}
}
}
`,
options: [{ allowDeclarations: true }],
},
{
filename: 'test.d.ts',
code: 'namespace foo {}',
Expand Down Expand Up @@ -71,6 +107,28 @@ ruleTester.run('no-namespace', rule, {
},
],
},
{
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: [
Expand Down