Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30,729 changes: 23,403 additions & 7,326 deletions package-lock.json

Large diffs are not rendered by default.

26 changes: 20 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,30 @@
"description": "Builds static call graph for JavaScript with a field-based algorithm.",
"main": "index.js",
"dependencies": {
"@babel/core": "^7.14.6",
"@babel/preset-flow": "^7.14.5",
"@babel/core": "^7.18.2",
"@babel/plugin-proposal-class-properties": "^7.1.0",
"@babel/plugin-proposal-optional-chaining": "^7.17.12",
"@babel/plugin-transform-react-jsx": "^7.17.12",
"@babel/preset-env": "^7.18.2",
"@babel/preset-flow": "^7.0.0",
"@babel/preset-typescript": "^7.1.0",
"@typescript-eslint/typescript-estree": "^5.37.0",
"argparse": "^2.0.1",
"babel-core": "^6.26.3",
"amdefine": "^1.0.1",
"argparse": "^1.0.10",
"body-parser": "^1.18.2",
"esprima": "^4.0.0",
"express": "^4.16.3",
"JSONStream": "1.3.3",
"npm": "^5.10.0",
"sloc": "^0.2.0",
"source-map": "^0.7.3",
"typescript": "^4.8.3",
"underscore": "^1.13.1"
"underscore": "^1.13.1",
"vue-parser": "^1.1.6"
},
"devDependencies": {
"jest": "^27.0.5"
"@babel/cli": "^7.17.10",
"jest": "^23.1.0"
},
"scripts": {
"test": "jest && python3 ./tests/test.py"
Expand Down
4 changes: 3 additions & 1 deletion src/astutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ function parse(src,jsxCheck) {
return tsEstree.parse(src, {
loc: true,
range: true,
jsx: jsxCheck
jsx: false
});
}

Expand All @@ -302,6 +302,8 @@ function buildProgram(fname, src) {

// trim hashbang
src = prep.trimHashbangPrep(src);
// Transform JSX
src = prep.jsxPrep(src);
// extract script from .vue file
try {
if (fname.endsWith('.vue')) {
Expand Down
18 changes: 18 additions & 0 deletions src/srcPreprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
const babel = require('@babel/core');

const nameToFunc = {
'jsx': jsxPrep,
'flow': stripFlowPrep,
'hashbang': trimHashbangPrep
};
Expand Down Expand Up @@ -51,6 +52,22 @@ function stripFlowPrep(src) {
}).code;
}

function jsxPrep(src) {
src = babel.transform(src, {
presets: ["@babel/preset-env"],
plugins: [
['@babel/plugin-transform-react-jsx',
{
pragma: 'callGraphCreateElement',
pragmaFrag: 'Fragment',
}],
],
retainLines: true,
parserOpts: {strictMode: false}
}).code;
return src.replace(/callGraphCreateElement\(([^,]+), /g, '$1(')
}

/* Trim hashbang to avoid the parser blowing up
Example:
#!/usr/bin/env node
Expand All @@ -71,4 +88,5 @@ function trimHashbangPrep(src) {

module.exports.applyPreps = applyPreps;
module.exports.stripFlowPrep = stripFlowPrep;
module.exports.jsxPrep = jsxPrep;
module.exports.trimHashbangPrep = trimHashbangPrep;
7 changes: 7 additions & 0 deletions tests/jsx/nested.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const SubComponent = () => null;
const InternalComponent = () => null;
const Component = () => <SubComponent />;

const Root = () => <Component> <InternalComponent /> </Component>;

Root();
4 changes: 4 additions & 0 deletions tests/jsx/nested.truth
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'Component' (nested.js@3:108-126) -> 'SubComponent' (nested.js@1:21-31)
'Root' (nested.js@5:195-218) -> 'InternalComponent' (nested.js@2:59-69)
'Root' (nested.js@5:161-224) -> 'Component' (nested.js@3:89-126)
'global' (nested.js@7:227-233) -> 'Root' (nested.js@5:142-224)
5 changes: 5 additions & 0 deletions tests/jsx/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const Component = () => null;

const Root = () => <Component />;

Root();
2 changes: 2 additions & 0 deletions tests/jsx/simple.truth
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'Root' (simple.js@3:63-78) -> 'Component' (simple.js@1:18-28)
'global' (simple.js@5:81-87) -> 'Root' (simple.js@3:44-78)