Skip to content

Commit

Permalink
fix: don't shrink loop bounds by assuming the body is at the end (#74)
Browse files Browse the repository at this point in the history
Fixes decaffeinate/decaffeinate#333
Fixes decaffeinate/decaffeinate#502

The code to fix CoffeeScript location data was incorrectly assuming that the
last node of a loop is the loop body, but that isn't true in postfix loops, so
it was causing the end location to be artificially early. Probably, the ideal
behavior (assuming location data actually needs to be fixed here) is to take the
max of all nodes, but that didn't seem trivial. Instead, an easy fix is to just
do the same thing we do for the `if` case: just use the body to extend the
bounds if necessary, but never shrink it. This also seems more robust in
general, probably. At the very least, it's really simple and doesn't break any
decaffeinate tests.
  • Loading branch information
alangpierce committed Nov 13, 2016
1 parent 2229f42 commit 16b0596
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ function convert(context) {
case 'While':
{
let lastChild = node.body;
node.locationData = locationWithLastPosition(
node.locationData = mergeLocations(
node.locationData,
lastChild.locationData
);
Expand Down
1 change: 1 addition & 0 deletions test/examples/post-for/input.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a for a in b
81 changes: 81 additions & 0 deletions test/examples/post-for/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"type": "Program",
"line": 1,
"column": 1,
"range": [
0,
13
],
"raw": "a for a in b\n",
"body": {
"type": "Block",
"line": 1,
"column": 1,
"range": [
0,
12
],
"statements": [
{
"type": "ForIn",
"line": 1,
"column": 1,
"range": [
0,
12
],
"keyAssignee": null,
"valAssignee": {
"type": "Identifier",
"line": 1,
"column": 7,
"range": [
6,
7
],
"data": "a",
"raw": "a"
},
"body": {
"type": "Block",
"line": 1,
"column": 1,
"range": [
0,
1
],
"statements": [
{
"type": "Identifier",
"line": 1,
"column": 1,
"range": [
0,
1
],
"data": "a",
"raw": "a"
}
],
"raw": "a",
"inline": false
},
"target": {
"type": "Identifier",
"line": 1,
"column": 12,
"range": [
11,
12
],
"data": "b",
"raw": "b"
},
"filter": null,
"step": null,
"raw": "a for a in b"
}
],
"raw": "a for a in b"
}
}

0 comments on commit 16b0596

Please sign in to comment.