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] fix false positive for exported namespaces when allowDeclarations=true #4844

Merged
merged 4 commits into from Apr 22, 2022
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
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);
armano2 marked this conversation as resolved.
Show resolved Hide resolved
}

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 {}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are few more edge cases that should be tested around declared namespaces inside a non-declared one, both exported and not:

namespace Outer {
  declare namespace Declared {
    /* export */ namespace InsideDeclared {
       // and maybe more namespaces in here
    }
  }
}

}
`,
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 }],
},
],
});