From 2de1973c847b4ea315429b38fdbb66b224bb19f8 Mon Sep 17 00:00:00 2001 From: David Leyland Date: Fri, 24 May 2024 10:18:32 +0100 Subject: [PATCH] Fix issue with ignoreLocallyUsed (#300) --- features/ignore-locally-used.feature | 23 +++++++++++++++++++++++ src/parser/nodeProcessor.ts | 7 +------ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/features/ignore-locally-used.feature b/features/ignore-locally-used.feature index 8758b0c..6f66786 100644 --- a/features/ignore-locally-used.feature +++ b/features/ignore-locally-used.feature @@ -40,3 +40,26 @@ Scenario: ignoreLocallyUsed works with more complex file extensions """ When running ts-unused-exports "tsconfig.json" --ignoreLocallyUsed Then the CLI result at status is 0 + +Scenario: ignoreLocallyUsed works with template literals + Given file "local.test.ts" is + """ + export const a = 1; + const b = `text ${a} some more text`; + """ + When running ts-unused-exports "tsconfig.json" --ignoreLocallyUsed + Then the CLI result at status is 0 + +Scenario: ignoreLocallyUsed works with objects + Given file "local.test.ts" is + """ + export const a = 1; + export const b = 2; + const c = { + a + }; + c['b'] = b; + """ + When running ts-unused-exports "tsconfig.json" --ignoreLocallyUsed + Then the CLI result at status is 0 + \ No newline at end of file diff --git a/src/parser/nodeProcessor.ts b/src/parser/nodeProcessor.ts index ed7a7ce..2730a9f 100644 --- a/src/parser/nodeProcessor.ts +++ b/src/parser/nodeProcessor.ts @@ -161,12 +161,7 @@ export const processNode = ( addImportsFromNamespace(node, imports, addImport); } - if ( - extraOptions?.ignoreLocallyUsed && - kind === ts.SyntaxKind.Identifier && - node.parent.kind !== ts.SyntaxKind.VariableDeclaration && - node.parent.kind !== ts.SyntaxKind.FunctionDeclaration - ) { + if (extraOptions?.ignoreLocallyUsed && kind === ts.SyntaxKind.Identifier) { addImport({ from: removeTsFileExtension(node.getSourceFile().fileName), what: [node.getText()],