-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: implement isESMExport probe to consider export stmts as dependen…
…cies
- Loading branch information
Showing
5 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* @description Search for ESM Export | ||
* | ||
* @example | ||
* export { bar } from "./foo.js"; | ||
* export * from "./bar.js"; | ||
*/ | ||
function validateNode(node) { | ||
return [ | ||
/** | ||
* We must be sure that the source property is a Literal to not fall in a trap | ||
* export const foo = "bar"; | ||
*/ | ||
(node.type === "ExportNamedDeclaration" && node.source?.type === "Literal") || | ||
node.type === "ExportAllDeclaration" | ||
]; | ||
} | ||
|
||
function main(node, { sourceFile }) { | ||
sourceFile.addDependency( | ||
node.source.value, | ||
node.loc | ||
); | ||
} | ||
|
||
export default { | ||
name: "isESMExport", | ||
validateNode, | ||
main, | ||
breakOnMatch: true | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./shared.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Import Node.js Dependencies | ||
import { describe, it } from "node:test"; | ||
import assert from "node:assert"; | ||
|
||
// Import Internal Dependencies | ||
import { AstAnalyser } from "../../index.js"; | ||
|
||
describe("probe: isESMExport", () => { | ||
it("should detect ExportNamedDeclaration statement with a Literal source as dependency", () => { | ||
const code = ` | ||
export { foo } from "./bar.js"; | ||
export const bar = "foo"; | ||
`; | ||
const { dependencies } = new AstAnalyser().analyse(code); | ||
|
||
assert.deepEqual( | ||
[...dependencies.keys()], | ||
["./bar.js"] | ||
); | ||
}); | ||
|
||
it("should detect ExportAllDeclaration statement as dependency", () => { | ||
const code = ` | ||
export * from "./bar.js"; | ||
`; | ||
const { dependencies } = new AstAnalyser().analyse(code); | ||
|
||
assert.deepEqual( | ||
[...dependencies.keys()], | ||
["./bar.js"] | ||
); | ||
}); | ||
}); | ||
|