Skip to content

Commit

Permalink
provide location information for shift-parser (#584)
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkot authored May 29, 2021
1 parent 658d581 commit 1d15068
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
18 changes: 10 additions & 8 deletions website/src/parsers/js/shift.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import pkg from 'shift-parser/package.json';

const ID = 'shift';

let lastParsedLocations;

export default {
...defaultParserInterface,

Expand All @@ -18,13 +20,16 @@ export default {

parse(shift, code, options) {
const parseMethod = options.sourceType === 'module' ?
'parseModule' :
'parseScript';
return shift[parseMethod](code, options);
'parseModuleWithLocation' :
'parseScriptWithLocation';
const { tree, locations } = shift[parseMethod](code, options);
lastParsedLocations = locations;
return tree;
},

nodeToRange({ loc }) {
if (loc) {
nodeToRange(node) {
if (lastParsedLocations && lastParsedLocations.has(node)) {
let loc = lastParsedLocations.get(node);
return [loc.start.offset, loc.end.offset];
}
},
Expand All @@ -42,7 +47,6 @@ export default {

getDefaultOptions() {
return {
loc: true,
earlyErrors: false,
sourceType: 'module',
};
Expand All @@ -52,10 +56,8 @@ export default {
return {
fields: [
['sourceType', ['script', 'module']],
'loc',
'earlyErrors',
],
required: new Set(['loc']),
};
},

Expand Down
2 changes: 1 addition & 1 deletion website/src/parsers/utils/defaultParserInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default {
/**
* A generator to iterate over each "property" of the node. Overwriting this
* function allows a parser to expose information from a node if the node
* is not implemnted as plain JavaScript object.
* is not implemented as plain JavaScript object.
*/
*forEachProperty(node) {
if (node && typeof node === 'object') {
Expand Down

0 comments on commit 1d15068

Please sign in to comment.