From a6357e7bcfa66a5a1c8a3b0bdab214a381e9aeeb Mon Sep 17 00:00:00 2001 From: David Burles Date: Wed, 24 Jul 2024 16:00:09 +1000 Subject: [PATCH 1/5] support .jsx --- scanModuleCode.mjs | 21 +++++++++++---------- scanModuleCode.test.mjs | 10 ++++++++++ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/scanModuleCode.mjs b/scanModuleCode.mjs index 06a0d7b..23ca83b 100644 --- a/scanModuleCode.mjs +++ b/scanModuleCode.mjs @@ -52,20 +52,21 @@ export default async function scanModuleCode(code, path) { ["decorators", { decoratorsBeforeExport: false }], ]; - if ( - path && + if (path) { // Path is a TypeScript module. - (path.endsWith(".mts") || + if ( + path.endsWith(".mts") || path.endsWith(".cts") || path.endsWith(".ts") || - path.endsWith(".tsx")) - ) { - // Allow parsing code containing TypeScript syntax. - plugins.push("typescript"); - - if (path.endsWith(".tsx")) - // Allow parsing code containing JSX syntax. + path.endsWith(".tsx") + ) { + // Allow parsing code containing TypeScript syntax. + plugins.push("typescript"); + } + // Allow parsing code containing JSX syntax. + if (path.endsWith(".tsx") || path.endsWith(".jsx")) { plugins.push("jsx"); + } } const ast = await babel.parseAsync(code, { diff --git a/scanModuleCode.test.mjs b/scanModuleCode.test.mjs index 6cd8be2..ebfbce2 100644 --- a/scanModuleCode.test.mjs +++ b/scanModuleCode.test.mjs @@ -58,6 +58,16 @@ describe("Function `scanModuleCode`.", { concurrency: true }, () => { }, ); }); + + it("`.jsx` file, JavaScript and JSX syntax.", async () => { + deepStrictEqual( + await scanModuleCode("let a; const b =
;", "a.jsx"), + { + imports: {}, + exports: new Set(), + }, + ); + }); }); it("No imports or exports.", async () => { From 7aa0908aca88adbfc2a2fa48587a3fad36ae83b5 Mon Sep 17 00:00:00 2001 From: David Burles Date: Wed, 24 Jul 2024 16:12:32 +1000 Subject: [PATCH 2/5] update changelog --- changelog.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/changelog.md b/changelog.md index c3760a6..396965b 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,11 @@ # find-unused-exports changelog +## Next + +### Minor + +- Enable JSX syntax when parsing `.jsx` files with Babel, even when the project has no Babel config for JSX. + ## 6.0.0 ### Major From 72d9d198c7b79b223a2c27b271bf701c74b9405e Mon Sep 17 00:00:00 2001 From: Jayden Seric Date: Thu, 8 Aug 2024 13:03:25 +1000 Subject: [PATCH 3/5] Add the PR link to the changelog entry. --- changelog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 396965b..201051f 100644 --- a/changelog.md +++ b/changelog.md @@ -4,7 +4,7 @@ ### Minor -- Enable JSX syntax when parsing `.jsx` files with Babel, even when the project has no Babel config for JSX. +- Enable JSX syntax when parsing `.jsx` files with Babel, even when the project has no Babel config for JSX, via [#8](https://github.com/jaydenseric/find-unused-exports/pull/8). ## 6.0.0 From 9f8fc38c9b8c170f555642120994b7814aed3469 Mon Sep 17 00:00:00 2001 From: Jayden Seric Date: Thu, 8 Aug 2024 13:09:00 +1000 Subject: [PATCH 4/5] Polish formatting. --- scanModuleCode.mjs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/scanModuleCode.mjs b/scanModuleCode.mjs index 23ca83b..977e48f 100644 --- a/scanModuleCode.mjs +++ b/scanModuleCode.mjs @@ -59,14 +59,12 @@ export default async function scanModuleCode(code, path) { path.endsWith(".cts") || path.endsWith(".ts") || path.endsWith(".tsx") - ) { + ) // Allow parsing code containing TypeScript syntax. plugins.push("typescript"); - } + // Allow parsing code containing JSX syntax. - if (path.endsWith(".tsx") || path.endsWith(".jsx")) { - plugins.push("jsx"); - } + if (path.endsWith(".tsx") || path.endsWith(".jsx")) plugins.push("jsx"); } const ast = await babel.parseAsync(code, { From 9243f9b6657d9782011a317c588fb0c0722f8712 Mon Sep 17 00:00:00 2001 From: Jayden Seric Date: Thu, 8 Aug 2024 13:09:13 +1000 Subject: [PATCH 5/5] Simplify a test assertion. --- scanModuleCode.test.mjs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/scanModuleCode.test.mjs b/scanModuleCode.test.mjs index ebfbce2..373bbd5 100644 --- a/scanModuleCode.test.mjs +++ b/scanModuleCode.test.mjs @@ -60,13 +60,10 @@ describe("Function `scanModuleCode`.", { concurrency: true }, () => { }); it("`.jsx` file, JavaScript and JSX syntax.", async () => { - deepStrictEqual( - await scanModuleCode("let a; const b =
;", "a.jsx"), - { - imports: {}, - exports: new Set(), - }, - ); + deepStrictEqual(await scanModuleCode("const a =
;", "a.jsx"), { + imports: {}, + exports: new Set(), + }); }); });