gkz / grasp

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

Bug when replaced variable is used more than three times on one line #9

Closed dougmiller closed 10 years ago

dougmiller commented 10 years ago

When replacing a variable used three time or more on one line, the third and subsequent replacements get a bit confused.

file.js
a(searchVal);
a(searchVal, searchVal);
a(searchVal, searchVal, searchVal);
a(searchVal, searchVal, searchVal, searchVal);
a(searchVal, searchVal, searchVal, searchVal, searchVal);
a(searchVal, searchVal, searchVal, searchVal, searchVal, searchVal);
a(searchVal, searchVal, searchVal, searchVal, searchVal, searchVal, searchVal);
a(searchVal,
    searchVal,
    searchVal,
    searchVal,
    searchVal,
    searchVal,
    searchVal);
grasp -i '#searchVal' -R n file.js

results in:

a(n);
a(n, n);
a(n, n, searchVan
a(n, n, searchVanVal);n
a(n, n, searchVanVal, searcnn
a(n, n, searchVanVal, searcnrchVal);nn
a(n, n, searchVanVal, searcnrchVal, sennn
a(n,
    n,
    n,
    n,
    n,
    n,
    n);
dougmiller commented 10 years ago

Acorn looks to pare this OK so looks to be somewhere in grasp itself.

acorn file.js

{ "type": "Program", "start": 0, "end": 432, "body": [ { "type": "ExpressionStatement", "start": 0, "end": 13, "expression": { "type": "CallExpression", "start": 0, "end": 12, "callee": { "type": "Identifier", "start": 0, "end": 1, "name": "a" }, "arguments": [ { "type": "Identifier", "start": 2, "end": 11, "name": "searchVal" } ] } }, { "type": "ExpressionStatement", "start": 14, "end": 38, "expression": { "type": "CallExpression", "start": 14, "end": 37, "callee": { "type": "Identifier", "start": 14, "end": 15, "name": "a" }, "arguments": [ { "type": "Identifier", "start": 16, "end": 25, "name": "searchVal" }, { "type": "Identifier", "start": 27, "end": 36, "name": "searchVal" } ] } }, { "type": "ExpressionStatement", "start": 39, "end": 74, "expression": { "type": "CallExpression", "start": 39, "end": 73, "callee": { "type": "Identifier", "start": 39, "end": 40, "name": "a" }, "arguments": [ { "type": "Identifier", "start": 41, "end": 50, "name": "searchVal" }, { "type": "Identifier", "start": 52, "end": 61, "name": "searchVal" }, { "type": "Identifier", "start": 63, "end": 72, "name": "searchVal" } ] } }, { "type": "ExpressionStatement", "start": 75, "end": 121, "expression": { "type": "CallExpression", "start": 75, "end": 120, "callee": { "type": "Identifier", "start": 75, "end": 76, "name": "a" }, "arguments": [ { "type": "Identifier", "start": 77, "end": 86, "name": "searchVal" }, { "type": "Identifier", "start": 88, "end": 97, "name": "searchVal" }, { "type": "Identifier", "start": 99, "end": 108, "name": "searchVal" }, { "type": "Identifier", "start": 110, "end": 119, "name": "searchVal" } ] } }, { "type": "ExpressionStatement", "start": 122, "end": 179, "expression": { "type": "CallExpression", "start": 122, "end": 178, "callee": { "type": "Identifier", "start": 122, "end": 123, "name": "a" }, "arguments": [ { "type": "Identifier", "start": 124, "end": 133, "name": "searchVal" }, { "type": "Identifier", "start": 135, "end": 144, "name": "searchVal" }, { "type": "Identifier", "start": 146, "end": 155, "name": "searchVal" }, { "type": "Identifier", "start": 157, "end": 166, "name": "searchVal" }, { "type": "Identifier", "start": 168, "end": 177, "name": "searchVal" } ] } }, { "type": "ExpressionStatement", "start": 180, "end": 248, "expression": { "type": "CallExpression", "start": 180, "end": 247, "callee": { "type": "Identifier", "start": 180, "end": 181, "name": "a" }, "arguments": [ { "type": "Identifier", "start": 182, "end": 191, "name": "searchVal" }, { "type": "Identifier", "start": 193, "end": 202, "name": "searchVal" }, { "type": "Identifier", "start": 204, "end": 213, "name": "searchVal" }, { "type": "Identifier", "start": 215, "end": 224, "name": "searchVal" }, { "type": "Identifier", "start": 226, "end": 235, "name": "searchVal" }, { "type": "Identifier", "start": 237, "end": 246, "name": "searchVal" } ] } }, { "type": "ExpressionStatement", "start": 249, "end": 328, "expression": { "type": "CallExpression", "start": 249, "end": 327, "callee": { "type": "Identifier", "start": 249, "end": 250, "name": "a" }, "arguments": [ { "type": "Identifier", "start": 251, "end": 260, "name": "searchVal" }, { "type": "Identifier", "start": 262, "end": 271, "name": "searchVal" }, { "type": "Identifier", "start": 273, "end": 282, "name": "searchVal" }, { "type": "Identifier", "start": 284, "end": 293, "name": "searchVal" }, { "type": "Identifier", "start": 295, "end": 304, "name": "searchVal" }, { "type": "Identifier", "start": 306, "end": 315, "name": "searchVal" }, { "type": "Identifier", "start": 317, "end": 326, "name": "searchVal" } ] } }, { "type": "ExpressionStatement", "start": 329, "end": 432, "expression": { "type": "CallExpression", "start": 329, "end": 431, "callee": { "type": "Identifier", "start": 329, "end": 330, "name": "a" }, "arguments": [ { "type": "Identifier", "start": 331, "end": 340, "name": "searchVal" }, { "type": "Identifier", "start": 346, "end": 355, "name": "searchVal" }, { "type": "Identifier", "start": 361, "end": 370, "name": "searchVal" }, { "type": "Identifier", "start": 376, "end": 385, "name": "searchVal" }, { "type": "Identifier", "start": 391, "end": 400, "name": "searchVal" }, { "type": "Identifier", "start": 406, "end": 415, "name": "searchVal" }, { "type": "Identifier", "start": 421, "end": 430, "name": "searchVal" } ] } } ] }

gkz commented 10 years ago

Yes, thanks for the report! I'll take a look.