gkz / grasp

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

Problem parsing anonymous function #8

Closed mkramb closed 10 years ago

mkramb commented 10 years ago

Example file:

Test = function () {};

function buildDialog() {
    var tt = "11";
}

And then running:

grasp -d 'func[id=#buildDialog]' test2.js

Returns:

options:
{ debug: true,
  extensions: [ 'js' ],
  parser: [ 'acorn', { locations: true } ],
  lineNumber: true,
  color: true,
  _: [ 'func[id=#buildDialog]', 'test2.js' ] }
parse-selector: 2ms
parsed-selector:
{"type":"compound","selectors":[{"type":"matches","selectors":[{"type":"identifier","value":"FunctionDeclaration"},{"type":"identifier","value":"FunctionExpression"}]},{"type":"attribute","name":"id","operator":"=","valType":"complex","value":{"type":"compound","selectors":[{"type":"identifier","value":"Identifier"},{"type":"attribute","name":"name","operator":"=","valType":"primitive","value":{"type":"literal","value":"buildDialog"}}]}}]}
parse-input:test2.js: 2ms
Cannot read property 'type' of null
everything: 11ms

The problem is in definition of Test variable, if I define first function with a name, then it works. For example:

Test = function a() {};

function buildDialog() {
    var tt = "11";
}

Great tool! Is this a problem of acorn parser or grasp itself? Thanks for any info ...