gkz / grasp

JavaScript structural search, replace, and refactor
http://graspjs.com
MIT License
1.28k stars 33 forks source link

getRaw returns extra characters if unicode #130

Open ainthek opened 5 years ago

ainthek commented 5 years ago

when grasp is using flow parser , the AST returns ranges and getRaw returns incorrect match. See test case. This is eather bug in flow-parser or bad interpretation of range in grasp implementation of getRaw. Any hints ? Thanx

const grasp = require("grasp"); var raw; var replacer = grasp.replace('equery', 'var INPUT=$x;', function(getRaw, node, query, named) { raw = getRaw(node); return ""; }); replacer("var INPUT={test:'aa'};var UNEXPECTED;"); console.assert(raw==="var INPUT={test:'aa'};");

replacer("var INPUT={test:'až'};var UNEXPECTED;"); console.log(raw); //contains extra char, range from AST is wrong console.assert(raw==="var INPUT={test:'až'};"); //FAILS

ainthek commented 5 years ago

Just a note: On the other hand, it seems that -o option works , because formatResult function does not work with node.range but works with node.loc.

Please can you explain also parser options range:true and false ? I tried to switch it to false for flow-parser but it seems to have no effect and node.rage is still calculated (and incorrectly) interpreted in replaceFunction (getRaw)

parser: ['flow-parser', { loc: true, sourceType: 'module', range: "false" } ],