From b0bdc45966b8846a4103a5624fc37f427aa42339 Mon Sep 17 00:00:00 2001 From: lonyele Date: Sat, 12 Feb 2022 21:00:03 +0900 Subject: [PATCH 1/2] feat: find if ancestor namespace is declared --- .../src/rules/init-declarations.ts | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/eslint-plugin/src/rules/init-declarations.ts b/packages/eslint-plugin/src/rules/init-declarations.ts index 611687b0e9f..a10035c0691 100644 --- a/packages/eslint-plugin/src/rules/init-declarations.ts +++ b/packages/eslint-plugin/src/rules/init-declarations.ts @@ -35,11 +35,7 @@ export default createRule({ if (node.declare) { return; } - if ( - node.parent?.type === AST_NODE_TYPES.TSModuleBlock && - node.parent.parent?.type === AST_NODE_TYPES.TSModuleDeclaration && - node.parent.parent?.declare - ) { + if (isAncestorNamespaceDeclared(node)) { return; } } @@ -47,5 +43,24 @@ export default createRule({ rules['VariableDeclaration:exit'](node); }, }; + + function isAncestorNamespaceDeclared( + node: TSESTree.VariableDeclaration, + ): boolean { + let ancestor = node.parent; + + while (ancestor) { + if ( + ancestor.type === AST_NODE_TYPES.TSModuleDeclaration && + ancestor.declare + ) { + return true; + } + + ancestor = ancestor.parent; + } + + return false; + } }, }); From 7c2b112f0f527dc37f5d315cf2cbb54a670851e0 Mon Sep 17 00:00:00 2001 From: lonyele Date: Sat, 12 Feb 2022 21:00:28 +0900 Subject: [PATCH 2/2] test: add nested namespace cases --- .../tests/rules/init-declarations.test.ts | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/packages/eslint-plugin/tests/rules/init-declarations.test.ts b/packages/eslint-plugin/tests/rules/init-declarations.test.ts index 95d9a08799a..29e9025f734 100644 --- a/packages/eslint-plugin/tests/rules/init-declarations.test.ts +++ b/packages/eslint-plugin/tests/rules/init-declarations.test.ts @@ -341,6 +341,35 @@ namespace myLib { `, options: ['always'], }, + { + code: ` +declare namespace myLib1 { + const foo: number; + namespace myLib2 { + let bar: string; + namespace myLib3 { + let baz: object; + } + } +} + `, + options: ['always'], + }, + + { + code: ` +declare namespace myLib1 { + const foo: number; + namespace myLib2 { + let bar: string; + namespace myLib3 { + let baz: object; + } + } +} + `, + options: ['never'], + }, ], invalid: [ // checking compatibility with base rule @@ -724,5 +753,36 @@ namespace myLib { }, ], }, + { + code: ` +namespace myLib1 { + const foo: number; + namespace myLib2 { + let bar: string; + namespace myLib3 { + let baz: object; + } + } +} + `, + options: ['always'], + errors: [ + { + messageId: 'initialized', + data: { idName: 'foo' }, + type: AST_NODE_TYPES.VariableDeclarator, + }, + { + messageId: 'initialized', + data: { idName: 'bar' }, + type: AST_NODE_TYPES.VariableDeclarator, + }, + { + messageId: 'initialized', + data: { idName: 'baz' }, + type: AST_NODE_TYPES.VariableDeclarator, + }, + ], + }, ], });