Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add JS AST querying with XPath #409

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
Binary file added website/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"acorn-to-esprima": "^1.0.2",
"astexplorer-refmt": "^1.0.1",
"astexplorer-syn": "^0.15.30",
"astq": "^2.6.2",
"babel-core": "^6.24.0",
"babel-eslint": "^7.2.3",
"babel-eslint8": "file:packages/babel-eslint8",
Expand Down
Binary file added website/src/.DS_Store
Binary file not shown.
Binary file added website/src/parsers/.DS_Store
Binary file not shown.
Binary file added website/src/parsers/js/.DS_Store
Binary file not shown.
Binary file added website/src/parsers/js/transformers/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions website/src/parsers/js/transformers/astq/codeExample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// VariableDeclaration [@kind == 'const']
43 changes: 43 additions & 0 deletions website/src/parsers/js/transformers/astq/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import pkg from 'astq/package.json';

const ID = 'astq';
const name = 'ASTQ';

export default {
id: ID,
displayName: name,
version: pkg.version,
homepage: pkg.homepage,

defaultParserID: 'esprima',

loadTransformer (callback) {
require(['esprima', 'astq'], (esprima, astq) =>
callback({ esprima, astq }));
},

transform ({ esprima, astq }, xPathQuery, code) {
const ASTQ = new astq();
ASTQ.adapter('mozast');

let results = {
matches: 0,
nodes: [],
};
if (!xPathQuery || xPathQuery === '') {
return JSON.stringify(results, null, 2);
}

try {
const ast = esprima.parseScript(code);
const nodes = ASTQ.query(ast, xPathQuery);
results = {
matches: nodes.length,
nodes,
};
return JSON.stringify(results, null, 2);
} catch ({ message }) {
return message;
}
},
};
186 changes: 173 additions & 13 deletions website/yarn.lock

Large diffs are not rendered by default.