Skip to content

Commit 39e785c

Browse files
authored
fix: adjust block location to include a following semicolon (#229)
Fixes decaffeinate/decaffeinate#1099 Due to some complicated issues in the CoffeeScript lexer, the outdent token for blocks can sometimes end up earlier than the semicolon ending the block. This means that the block position ends before the semicolon, so decaffeinate ends up making some wrong decisions because it thinks the semicolon is after the block. I tried updating the CoffeeScript lexer to handle this but that broke other things, so as sort of a hack, I'm just updating fixLocations to handle this case. Also update `yarn.lock` after running `yarn`.
1 parent 2d9b2ff commit 39e785c

File tree

6 files changed

+127
-1
lines changed

6 files changed

+127
-1
lines changed

src/util/fixLocations.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,24 @@ export default function fixLocations(context: ParseContext, node: Base) {
133133
lastChild.locationData
134134
);
135135
}
136+
// Blocks can sometimes end one index before their terminating semicolon
137+
// when really they should end exactly at that semicolon.
138+
let blockEnd = linesAndColumns.indexForLocation({
139+
line: node.locationData.last_line,
140+
column: node.locationData.last_column,
141+
});
142+
if (blockEnd === null) {
143+
throw new Error('Expected to find index for block end.');
144+
}
145+
if (source[blockEnd + 1] === ';') {
146+
blockEnd++;
147+
let loc = linesAndColumns.locationForIndex(blockEnd);
148+
if (loc === null) {
149+
throw new Error('Expected to find location for block end.');
150+
}
151+
node.locationData.last_line = loc.line;
152+
node.locationData.last_column = loc.column;
153+
}
136154
}
137155

138156
if (node instanceof If) {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
x = if a
2+
;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"body": {
3+
"column": 1,
4+
"inline": false,
5+
"line": 1,
6+
"range": [
7+
0,
8+
12
9+
],
10+
"raw": "x = if a\n ;",
11+
"statements": [
12+
{
13+
"assignee": {
14+
"column": 1,
15+
"data": "x",
16+
"line": 1,
17+
"range": [
18+
0,
19+
1
20+
],
21+
"raw": "x",
22+
"type": "Identifier"
23+
},
24+
"column": 1,
25+
"expression": {
26+
"alternate": null,
27+
"column": 5,
28+
"condition": {
29+
"column": 8,
30+
"data": "a",
31+
"line": 1,
32+
"range": [
33+
7,
34+
8
35+
],
36+
"raw": "a",
37+
"type": "Identifier"
38+
},
39+
"consequent": null,
40+
"isUnless": false,
41+
"line": 1,
42+
"range": [
43+
4,
44+
12
45+
],
46+
"raw": "if a\n ;",
47+
"type": "Conditional"
48+
},
49+
"line": 1,
50+
"range": [
51+
0,
52+
12
53+
],
54+
"raw": "x = if a\n ;",
55+
"type": "AssignOp"
56+
}
57+
],
58+
"type": "Block"
59+
},
60+
"column": 1,
61+
"line": 1,
62+
"range": [
63+
0,
64+
13
65+
],
66+
"raw": "x = if a\n ;\n",
67+
"type": "Program"
68+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
->
2+
;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"body": {
3+
"column": 1,
4+
"inline": false,
5+
"line": 1,
6+
"range": [
7+
0,
8+
6
9+
],
10+
"raw": "->\n ;",
11+
"statements": [
12+
{
13+
"body": null,
14+
"column": 1,
15+
"line": 1,
16+
"parameters": [
17+
],
18+
"range": [
19+
0,
20+
6
21+
],
22+
"raw": "->\n ;",
23+
"type": "Function"
24+
}
25+
],
26+
"type": "Block"
27+
},
28+
"column": 1,
29+
"line": 1,
30+
"range": [
31+
0,
32+
7
33+
],
34+
"raw": "->\n ;\n",
35+
"type": "Program"
36+
}

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ eslint-scope@^3.7.1:
962962
esrecurse "^4.1.0"
963963
estraverse "^4.1.1"
964964

965-
eslint@4.1.0:
965+
eslint@^4.1.0:
966966
version "4.1.0"
967967
resolved "https://registry.npmjs.org/eslint/-/eslint-4.1.0.tgz#bbb55a28220ee08b69da9554d45a6b2ebfd7d913"
968968
dependencies:

0 commit comments

Comments
 (0)