Skip to content

Commit

Permalink
fix(eslint-plugin): [no-namespace] fix false positive for exported na…
Browse files Browse the repository at this point in the history
…mespaces when allowDeclarations=true (#4844)

* Add failing test

* Fix isDeclaration to look recursively

* Add more tests
  • Loading branch information
zachkirsch committed Apr 22, 2022
1 parent 88ed9ec commit 4e7c9be
Show file tree
Hide file tree
Showing 2 changed files with 321 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/eslint-plugin/src/rules/no-namespace.ts
Expand Up @@ -46,12 +46,15 @@ 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))
);
function isDeclaration(node: TSESTree.Node): boolean {
if (
node.type === AST_NODE_TYPES.TSModuleDeclaration &&
node.declare === true
) {
return true;
}

return node.parent != null && isDeclaration(node.parent);
}

return {
Expand Down
312 changes: 312 additions & 0 deletions packages/eslint-plugin/tests/rules/no-namespace.test.ts
Expand Up @@ -49,6 +49,16 @@ declare namespace foo {
namespace bar {
namespace baz {}
}
}
`,
options: [{ allowDeclarations: true }],
},
{
code: `
export declare namespace foo {
export namespace bar {
namespace baz {}
}
}
`,
options: [{ allowDeclarations: true }],
Expand Down Expand Up @@ -251,5 +261,307 @@ namespace Foo.Bar {
},
],
},
{
code: `
namespace A {
namespace B {
declare namespace C {}
}
}
`,
errors: [
{
messageId: 'moduleSyntaxIsPreferred',
line: 2,
column: 1,
},
{
messageId: 'moduleSyntaxIsPreferred',
line: 3,
column: 3,
},
],
options: [{ allowDeclarations: true }],
},
{
code: `
namespace A {
namespace B {
export declare namespace C {}
}
}
`,
errors: [
{
messageId: 'moduleSyntaxIsPreferred',
line: 2,
column: 1,
},
{
messageId: 'moduleSyntaxIsPreferred',
line: 3,
column: 3,
},
],
options: [{ allowDeclarations: true }],
},
{
code: `
namespace A {
declare namespace B {
namespace C {}
}
}
`,
errors: [
{
messageId: 'moduleSyntaxIsPreferred',
line: 2,
column: 1,
},
],
options: [{ allowDeclarations: true }],
},
{
code: `
namespace A {
export declare namespace B {
namespace C {}
}
}
`,
errors: [
{
messageId: 'moduleSyntaxIsPreferred',
line: 2,
column: 1,
},
],
options: [{ allowDeclarations: true }],
},
{
code: `
namespace A {
export declare namespace B {
declare namespace C {}
}
}
`,
errors: [
{
messageId: 'moduleSyntaxIsPreferred',
line: 2,
column: 1,
},
],
options: [{ allowDeclarations: true }],
},
{
code: `
namespace A {
export declare namespace B {
export declare namespace C {}
}
}
`,
errors: [
{
messageId: 'moduleSyntaxIsPreferred',
line: 2,
column: 1,
},
],
options: [{ allowDeclarations: true }],
},
{
code: `
namespace A {
declare namespace B {
export declare namespace C {}
}
}
`,
errors: [
{
messageId: 'moduleSyntaxIsPreferred',
line: 2,
column: 1,
},
],
options: [{ allowDeclarations: true }],
},
{
code: `
namespace A {
export namespace B {
export declare namespace C {}
}
}
`,
errors: [
{
messageId: 'moduleSyntaxIsPreferred',
line: 2,
column: 1,
},
{
messageId: 'moduleSyntaxIsPreferred',
line: 3,
column: 10,
},
],
options: [{ allowDeclarations: true }],
},
{
code: `
export namespace A {
namespace B {
declare namespace C {}
}
}
`,
errors: [
{
messageId: 'moduleSyntaxIsPreferred',
line: 2,
column: 8,
},
{
messageId: 'moduleSyntaxIsPreferred',
line: 3,
column: 3,
},
],
options: [{ allowDeclarations: true }],
},
{
code: `
export namespace A {
namespace B {
export declare namespace C {}
}
}
`,
errors: [
{
messageId: 'moduleSyntaxIsPreferred',
line: 2,
column: 8,
},
{
messageId: 'moduleSyntaxIsPreferred',
line: 3,
column: 3,
},
],
options: [{ allowDeclarations: true }],
},
{
code: `
export namespace A {
declare namespace B {
namespace C {}
}
}
`,
errors: [
{
messageId: 'moduleSyntaxIsPreferred',
line: 2,
column: 8,
},
],
options: [{ allowDeclarations: true }],
},
{
code: `
export namespace A {
export declare namespace B {
namespace C {}
}
}
`,
errors: [
{
messageId: 'moduleSyntaxIsPreferred',
line: 2,
column: 8,
},
],
options: [{ allowDeclarations: true }],
},
{
code: `
export namespace A {
export declare namespace B {
declare namespace C {}
}
}
`,
errors: [
{
messageId: 'moduleSyntaxIsPreferred',
line: 2,
column: 8,
},
],
options: [{ allowDeclarations: true }],
},
{
code: `
export namespace A {
export declare namespace B {
export declare namespace C {}
}
}
`,
errors: [
{
messageId: 'moduleSyntaxIsPreferred',
line: 2,
column: 8,
},
],
options: [{ allowDeclarations: true }],
},
{
code: `
export namespace A {
declare namespace B {
export declare namespace C {}
}
}
`,
errors: [
{
messageId: 'moduleSyntaxIsPreferred',
line: 2,
column: 8,
},
],
options: [{ allowDeclarations: true }],
},
{
code: `
export namespace A {
export namespace B {
export declare namespace C {}
}
}
`,
errors: [
{
messageId: 'moduleSyntaxIsPreferred',
line: 2,
column: 8,
},
{
messageId: 'moduleSyntaxIsPreferred',
line: 3,
column: 10,
},
],
options: [{ allowDeclarations: true }],
},
],
});

0 comments on commit 4e7c9be

Please sign in to comment.