Skip to content

Commit 16b0596

Browse files
authored
fix: don't shrink loop bounds by assuming the body is at the end (#74)
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.
1 parent 2229f42 commit 16b0596

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

src/parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ function convert(context) {
260260
case 'While':
261261
{
262262
let lastChild = node.body;
263-
node.locationData = locationWithLastPosition(
263+
node.locationData = mergeLocations(
264264
node.locationData,
265265
lastChild.locationData
266266
);

test/examples/post-for/input.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a for a in b

test/examples/post-for/output.json

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"type": "Program",
3+
"line": 1,
4+
"column": 1,
5+
"range": [
6+
0,
7+
13
8+
],
9+
"raw": "a for a in b\n",
10+
"body": {
11+
"type": "Block",
12+
"line": 1,
13+
"column": 1,
14+
"range": [
15+
0,
16+
12
17+
],
18+
"statements": [
19+
{
20+
"type": "ForIn",
21+
"line": 1,
22+
"column": 1,
23+
"range": [
24+
0,
25+
12
26+
],
27+
"keyAssignee": null,
28+
"valAssignee": {
29+
"type": "Identifier",
30+
"line": 1,
31+
"column": 7,
32+
"range": [
33+
6,
34+
7
35+
],
36+
"data": "a",
37+
"raw": "a"
38+
},
39+
"body": {
40+
"type": "Block",
41+
"line": 1,
42+
"column": 1,
43+
"range": [
44+
0,
45+
1
46+
],
47+
"statements": [
48+
{
49+
"type": "Identifier",
50+
"line": 1,
51+
"column": 1,
52+
"range": [
53+
0,
54+
1
55+
],
56+
"data": "a",
57+
"raw": "a"
58+
}
59+
],
60+
"raw": "a",
61+
"inline": false
62+
},
63+
"target": {
64+
"type": "Identifier",
65+
"line": 1,
66+
"column": 12,
67+
"range": [
68+
11,
69+
12
70+
],
71+
"data": "b",
72+
"raw": "b"
73+
},
74+
"filter": null,
75+
"step": null,
76+
"raw": "a for a in b"
77+
}
78+
],
79+
"raw": "a for a in b"
80+
}
81+
}

0 commit comments

Comments
 (0)