Skip to content

Commit 898a759

Browse files
authored
fix: respect herecomments when computing function bounds (#162)
Fixes decaffeinate/decaffeinate#687 Previously, the bounds of a function body were shrunk to only include semantic tokens, which could cause a block comment to get moved outside the body and be improperly indented after the normalize step. Now, we include the comments as part of the body. This is a small change that gets this case to work, but it might be that in the future we'll want to avoid all AST location adjustment and/or put block comments in the AST.
1 parent c28a27a commit 898a759

File tree

7 files changed

+175
-11
lines changed

7 files changed

+175
-11
lines changed

src/mappers/mapBase.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ export default function mapBase(context: ParseContext, node: Base): Node {
3030
return (
3131
token.end <= end &&
3232
token.type !== SourceType.NEWLINE &&
33-
token.type !== SourceType.COMMENT &&
34-
token.type !== SourceType.HERECOMMENT
33+
token.type !== SourceType.COMMENT
3534
);
3635
}, context.sourceTokens.indexOfTokenNearSourceIndex(end));
3736

src/nodes.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,8 +1171,7 @@ export function makeRealNode(context: ParseContext, type: string, loc: LocationD
11711171
return (
11721172
token.end <= result.range[1] &&
11731173
token.type !== SourceType.NEWLINE &&
1174-
token.type !== SourceType.COMMENT &&
1175-
token.type !== SourceType.HERECOMMENT
1174+
token.type !== SourceType.COMMENT
11761175
);
11771176
}, context.sourceTokens.indexOfTokenNearSourceIndex(result.range[1]));
11781177

test/examples/block-comment-in-function/output.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"column": 1,
1414
"range": [
1515
0,
16-
2
16+
18
1717
],
1818
"statements": [
1919
{
@@ -22,24 +22,24 @@
2222
"column": 1,
2323
"range": [
2424
0,
25-
2
25+
18
2626
],
2727
"body": {
2828
"type": "Block",
2929
"line": 2,
3030
"column": 3,
3131
"range": [
3232
5,
33-
2
33+
18
3434
],
3535
"statements": [],
36-
"raw": "",
36+
"raw": "###\n a\n ###",
3737
"inline": false
3838
},
3939
"parameters": [],
40-
"raw": "->"
40+
"raw": "->\n ###\n a\n ###"
4141
}
4242
],
43-
"raw": "->"
43+
"raw": "->\n ###\n a\n ###"
4444
}
45-
}
45+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
a ->
2+
b
3+
### c ###
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{
2+
"type": "Program",
3+
"line": 1,
4+
"column": 1,
5+
"range": [
6+
0,
7+
21
8+
],
9+
"raw": "a ->\n b\n ### c ###\n",
10+
"body": {
11+
"type": "Block",
12+
"line": 1,
13+
"column": 1,
14+
"range": [
15+
0,
16+
20
17+
],
18+
"statements": [
19+
{
20+
"type": "FunctionApplication",
21+
"line": 1,
22+
"column": 1,
23+
"range": [
24+
0,
25+
20
26+
],
27+
"function": {
28+
"type": "Identifier",
29+
"line": 1,
30+
"column": 1,
31+
"raw": "a",
32+
"range": [
33+
0,
34+
1
35+
],
36+
"data": "a"
37+
},
38+
"arguments": [
39+
{
40+
"type": "Function",
41+
"line": 1,
42+
"column": 3,
43+
"range": [
44+
2,
45+
20
46+
],
47+
"body": {
48+
"type": "Block",
49+
"line": 2,
50+
"column": 3,
51+
"range": [
52+
7,
53+
20
54+
],
55+
"statements": [
56+
{
57+
"type": "Identifier",
58+
"line": 2,
59+
"column": 3,
60+
"raw": "b",
61+
"range": [
62+
7,
63+
8
64+
],
65+
"data": "b"
66+
}
67+
],
68+
"raw": "b\n ### c ###",
69+
"inline": false
70+
},
71+
"parameters": [],
72+
"raw": "->\n b\n ### c ###"
73+
}
74+
],
75+
"raw": "a ->\n b\n ### c ###"
76+
}
77+
],
78+
"raw": "a ->\n b\n ### c ###"
79+
}
80+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
a ->
2+
b
3+
### c ###
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{
2+
"type": "Program",
3+
"line": 1,
4+
"column": 1,
5+
"range": [
6+
0,
7+
19
8+
],
9+
"raw": "a ->\n b\n### c ###\n",
10+
"body": {
11+
"type": "Block",
12+
"line": 1,
13+
"column": 1,
14+
"range": [
15+
0,
16+
18
17+
],
18+
"statements": [
19+
{
20+
"type": "FunctionApplication",
21+
"line": 1,
22+
"column": 1,
23+
"range": [
24+
0,
25+
8
26+
],
27+
"function": {
28+
"type": "Identifier",
29+
"line": 1,
30+
"column": 1,
31+
"raw": "a",
32+
"range": [
33+
0,
34+
1
35+
],
36+
"data": "a"
37+
},
38+
"arguments": [
39+
{
40+
"type": "Function",
41+
"line": 1,
42+
"column": 3,
43+
"range": [
44+
2,
45+
8
46+
],
47+
"body": {
48+
"type": "Block",
49+
"line": 2,
50+
"column": 3,
51+
"range": [
52+
7,
53+
8
54+
],
55+
"statements": [
56+
{
57+
"type": "Identifier",
58+
"line": 2,
59+
"column": 3,
60+
"raw": "b",
61+
"range": [
62+
7,
63+
8
64+
],
65+
"data": "b"
66+
}
67+
],
68+
"raw": "b",
69+
"inline": false
70+
},
71+
"parameters": [],
72+
"raw": "->\n b"
73+
}
74+
],
75+
"raw": "a ->\n b"
76+
}
77+
],
78+
"raw": "a ->\n b\n### c ###"
79+
}
80+
}

0 commit comments

Comments
 (0)