-
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.
refactor: make SourceParser class heritable + create and use JsSource…
…Parser in … (#215) * refactor: make SourceParser class heritable + create and use JsSourceParser in runASTAnalysis * chore: kParsingOptions is only for the JS parser
- Loading branch information
1 parent
d26eafc
commit 83aa3d5
Showing
5 changed files
with
96 additions
and
85 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,61 @@ | ||
// Import Third-party Dependencies | ||
import * as meriyah from "meriyah"; | ||
|
||
// Import Internal Dependencies | ||
import { SourceParser } from "./SourceParser.js"; | ||
|
||
// CONSTANTS | ||
const kParsingOptions = { | ||
next: true, | ||
loc: true, | ||
raw: true, | ||
jsx: true | ||
}; | ||
|
||
export class JsSourceParser extends SourceParser { | ||
/** | ||
* @param {object} options | ||
* @param {boolean} options.isEcmaScriptModule | ||
*/ | ||
parseScript(options = {}) { | ||
const { | ||
isEcmaScriptModule | ||
} = options; | ||
|
||
try { | ||
const { body } = meriyah.parseScript( | ||
this.source, | ||
{ | ||
...kParsingOptions, | ||
module: isEcmaScriptModule, | ||
globalReturn: !isEcmaScriptModule | ||
} | ||
); | ||
|
||
return body; | ||
} | ||
catch (error) { | ||
const isIllegalReturn = error.description.includes("Illegal return statement"); | ||
|
||
if (error.name === "SyntaxError" && ( | ||
error.description.includes("The import keyword") || | ||
error.description.includes("The export keyword") || | ||
isIllegalReturn | ||
)) { | ||
const { body } = meriyah.parseScript( | ||
this.source, | ||
{ | ||
...kParsingOptions, | ||
module: true, | ||
globalReturn: isIllegalReturn | ||
} | ||
); | ||
|
||
return body; | ||
} | ||
|
||
throw error; | ||
} | ||
} | ||
} | ||
|
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,33 @@ | ||
// Import Node.js Dependencies | ||
import { describe, it } from "node:test"; | ||
|
||
// Import Internal Dependencies | ||
import { JsSourceParser } from "../src/JsSourceParser.js"; | ||
|
||
describe("JsSourceParser", () => { | ||
describe("parseScript", () => { | ||
it("should not crash even if isEcmaScriptModule 'false' is provided (import keyword)", () => { | ||
new JsSourceParser("import * as foo from \"foo\";").parseScript({ | ||
isEcmaScriptModule: false | ||
}); | ||
}); | ||
|
||
it("should not crash even if isEcmaScriptModule 'false' is provided (export keyword)", () => { | ||
new JsSourceParser("export const foo = 5;").parseScript({ | ||
isEcmaScriptModule: false | ||
}); | ||
}); | ||
|
||
it("should not crash with a source code containing JSX", () => { | ||
const code = `const Dropzone = forwardRef(({ children, ...params }, ref) => { | ||
const { open, ...props } = useDropzone(params); | ||
useImperativeHandle(ref, () => ({ open }), [open]); | ||
return <Fragment>{children({ ...props, open })}</Fragment>; | ||
});`; | ||
|
||
new JsSourceParser(code).parseScript({ | ||
isEcmaScriptModule: false | ||
}); | ||
}); | ||
}); | ||
}); |
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