{"action":"created","issue":{"url":"https://api.github.com/repos/benjamn/recast/issues/159","labels_url":"https://api.github.com/repos/benjamn/recast/issues/159/labels{/name}","comments_url":"https://api.github.com/repos/benjamn/recast/issues/159/comments","events_url":"https://api.github.com/repos/benjamn/recast/issues/159/events","html_url":"https://github.com/benjamn/recast/issues/159","id":58942569,"number":159,"title":"use attachComment option in esprima","user":{"login":"jamestalmage","id":4082216,"avatar_url":"https://avatars.githubusercontent.com/u/4082216?v=3","gravatar_id":"","url":"https://api.github.com/users/jamestalmage","html_url":"https://github.com/jamestalmage","followers_url":"https://api.github.com/users/jamestalmage/followers","following_url":"https://api.github.com/users/jamestalmage/following{/other_user}","gists_url":"https://api.github.com/users/jamestalmage/gists{/gist_id}","starred_url":"https://api.github.com/users/jamestalmage/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jamestalmage/subscriptions","organizations_url":"https://api.github.com/users/jamestalmage/orgs","repos_url":"https://api.github.com/users/jamestalmage/repos","events_url":"https://api.github.com/users/jamestalmage/events{/privacy}","received_events_url":"https://api.github.com/users/jamestalmage/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-02-25T17:52:52Z","updated_at":"2015-02-25T19:21:11Z","closed_at":"2015-02-25T18:37:46Z","body":"Is there a reason you can't use esprima.parse(code, {attachComment:true})?\r\n\r\nI see you pass {`comment:true`} to esprima and then run your own algorithm to attach. Their attachment is pretty nice. It gives you both a `leadingComments`, and `trailingComments` property. For example, in the code below, `// comment 2` would be attached to `trailingComments` for `var a;`, and to`leadingComments` for `var b;`. Also it seems to do a better job of attaching nodes where I expect them (see notes on how `//comment1` is handled below).\r\n\r\n\r\n```javascript\r\nvar code = [\r\n  \"// comment1\",\r\n  \"var a;\",\r\n  \"// comment2\",\r\n  \"var b;\",\r\n  \"if(true) {\",\r\n  \"  // comment3\",\r\n  \"  var c;\",\r\n  \"}\"\r\n].join('\\n');\r\n\r\n// comments is the property name on the node where comments will be stored.\r\nvar wrapper, ast, comments, commentHack = false;\r\n\r\nif(commentHack){\r\n  wrapper = require('recast').parse(code, {esprima:{\r\n    parse:function(code,options){\r\n      options.attachComment = true;\r\n      return require('esprima-fb').parse(code, options);\r\n    }\r\n  }});\r\n  ast = wrapper.program;\r\n  comments = \"leadingComments\";\r\n}\r\nelse {\r\n  wrapper = require('recast').parse(code);\r\n  ast = wrapper.program;\r\n  comments = \"comments\";\r\n}\r\n\r\nfunction log(message,code){\r\n  console.log('----------'+ message + '---------');\r\n  console.log(code);\r\n  console.log('\\n');\r\n}\r\n\r\nlog('comment 1 - on \"var a;\"',               ast.body[0][comments]);\r\n// without hack: undefined \r\n// with hack: \"comment1\" is in leading node\r\n\r\nlog('comment 1 - on program',                ast[comments]);\r\n// without hack: \"comment1\", leading = true\r\n// with hack: undefinded\r\n\r\nlog('comment 2 - attaches as expected',      ast.body[1][comments]);\r\n// outputs:  \"comment2\", leading = true\r\n\r\nlog('comment 3 - attaches as expected',      ast.body[2].consequent.body[0][comments]);\r\n// \"comment3\", leading = true\r\n```"},"comment":{"url":"https://api.github.com/repos/benjamn/recast/issues/comments/76035102","html_url":"https://github.com/benjamn/recast/issues/159#issuecomment-76035102","issue_url":"https://api.github.com/repos/benjamn/recast/issues/159","id":76035102,"user":{"login":"jamestalmage","id":4082216,"avatar_url":"https://avatars.githubusercontent.com/u/4082216?v=3","gravatar_id":"","url":"https://api.github.com/users/jamestalmage","html_url":"https://github.com/jamestalmage","followers_url":"https://api.github.com/users/jamestalmage/followers","following_url":"https://api.github.com/users/jamestalmage/following{/other_user}","gists_url":"https://api.github.com/users/jamestalmage/gists{/gist_id}","starred_url":"https://api.github.com/users/jamestalmage/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jamestalmage/subscriptions","organizations_url":"https://api.github.com/users/jamestalmage/orgs","repos_url":"https://api.github.com/users/jamestalmage/repos","events_url":"https://api.github.com/users/jamestalmage/events{/privacy}","received_events_url":"https://api.github.com/users/jamestalmage/received_events","type":"User","site_admin":false},"created_at":"2015-02-25T19:21:11Z","updated_at":"2015-02-25T19:21:11Z","body":"I just had another thought. One advantage to keeping separate `leading` and `trailing` comment arrays is keeping things consistent as you add nodes. If you push a new node on a list of nodes, would you rip out the trailing comments from what used to be the last node, and set them as leading comments to the last node? It seems that gets solved by using two separate arrays for leading and trailing and having any two siblings point to the same array that lies between them (i.e. `body[2].trailingComments === body[3].leadingComments`)."}}