Skip to content

Commit

Permalink
Merge pull request #8 from dburles/jsx-support
Browse files Browse the repository at this point in the history
Allow JSX syntax when parsing `.jsx` files, PR: #8 .
  • Loading branch information
jaydenseric authored Aug 8, 2024
2 parents e6eaedb + 9243f9b commit 3eb268b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -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, via [#8](https://github.com/jaydenseric/find-unused-exports/pull/8).

## 6.0.0

### Major
Expand Down
21 changes: 10 additions & 11 deletions scanModuleCode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,19 @@ 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.
plugins.push("jsx");
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, {
Expand Down
7 changes: 7 additions & 0 deletions scanModuleCode.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ describe("Function `scanModuleCode`.", { concurrency: true }, () => {
},
);
});

it("`.jsx` file, JavaScript and JSX syntax.", async () => {
deepStrictEqual(await scanModuleCode("const a = <div />;", "a.jsx"), {
imports: {},
exports: new Set(),
});
});
});

it("No imports or exports.", async () => {
Expand Down

0 comments on commit 3eb268b

Please sign in to comment.