Skip to content

Commit c28a27a

Browse files
authored
fix: use Constructor node even for external constructors (#163)
Progress toward #830 Constructors are allowed to be arbitrary expressions in CoffeeScript, so I think the right approach is for the parser to use the `Constructor` node even if the value isn't a function, and decaffeinate will figure out the right way to handle it.
1 parent 3c4f328 commit c28a27a

File tree

4 files changed

+172
-3
lines changed

4 files changed

+172
-3
lines changed

src/mappers/mapClass.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Assign, Class as CoffeeClass, Comment, Obj, Value } from 'decaffeinate-coffeescript/lib/coffee-script/nodes';
22
import { inspect } from 'util';
33
import {
4-
AssignOp, Block, BoundFunction, BoundGeneratorFunction, Class, ClassProtoAssignOp, Constructor, Function,
4+
AssignOp, Block, BoundFunction, BoundGeneratorFunction, Class, ClassProtoAssignOp, Constructor,
55
Identifier, MemberAccessOp, Node, This
66
} from '../nodes';
77
import ParseContext from '../util/ParseContext';
@@ -29,7 +29,7 @@ export default function mapClass(context: ParseContext, node: CoffeeClass): Clas
2929
let value = mapAny(context, property.value);
3030
let Node = ClassProtoAssignOp;
3131

32-
if (key instanceof Identifier && key.data === 'constructor' && value instanceof Function) {
32+
if (key instanceof Identifier && key.data === 'constructor') {
3333
Node = Constructor;
3434
} else if (key instanceof MemberAccessOp && key.expression instanceof This) {
3535
Node = AssignOp;

src/nodes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,7 @@ export class Constructor extends BaseAssignOp {
991991
end: number,
992992
raw: string,
993993
assignee: Node,
994-
expression: BaseFunction
994+
expression: Node
995995
) {
996996
super('Constructor', line, column, start, end, raw, assignee, expression);
997997
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
f = ->
2+
class A
3+
constructor: f
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
{
2+
"type": "Program",
3+
"line": 1,
4+
"column": 1,
5+
"range": [
6+
0,
7+
32
8+
],
9+
"raw": "f = ->\nclass A\n constructor: f\n",
10+
"body": {
11+
"type": "Block",
12+
"line": 1,
13+
"column": 1,
14+
"range": [
15+
0,
16+
31
17+
],
18+
"statements": [
19+
{
20+
"type": "AssignOp",
21+
"line": 1,
22+
"column": 1,
23+
"range": [
24+
0,
25+
6
26+
],
27+
"assignee": {
28+
"type": "Identifier",
29+
"line": 1,
30+
"column": 1,
31+
"raw": "f",
32+
"range": [
33+
0,
34+
1
35+
],
36+
"data": "f"
37+
},
38+
"expression": {
39+
"type": "Function",
40+
"line": 1,
41+
"column": 5,
42+
"range": [
43+
4,
44+
6
45+
],
46+
"body": null,
47+
"parameters": [],
48+
"raw": "->"
49+
},
50+
"raw": "f = ->"
51+
},
52+
{
53+
"type": "Class",
54+
"line": 2,
55+
"column": 1,
56+
"range": [
57+
7,
58+
31
59+
],
60+
"name": {
61+
"type": "Identifier",
62+
"line": 2,
63+
"column": 7,
64+
"raw": "A",
65+
"range": [
66+
13,
67+
14
68+
],
69+
"data": "A"
70+
},
71+
"nameAssignee": {
72+
"type": "Identifier",
73+
"line": 2,
74+
"column": 7,
75+
"raw": "A",
76+
"range": [
77+
13,
78+
14
79+
],
80+
"data": "A"
81+
},
82+
"body": {
83+
"type": "Block",
84+
"line": 3,
85+
"column": 3,
86+
"range": [
87+
17,
88+
31
89+
],
90+
"statements": [
91+
{
92+
"type": "Constructor",
93+
"line": 3,
94+
"column": 3,
95+
"range": [
96+
17,
97+
31
98+
],
99+
"assignee": {
100+
"type": "Identifier",
101+
"line": 3,
102+
"column": 3,
103+
"raw": "constructor",
104+
"range": [
105+
17,
106+
28
107+
],
108+
"data": "constructor"
109+
},
110+
"expression": {
111+
"type": "Identifier",
112+
"line": 3,
113+
"column": 16,
114+
"raw": "f",
115+
"range": [
116+
30,
117+
31
118+
],
119+
"data": "f"
120+
},
121+
"raw": "constructor: f"
122+
}
123+
],
124+
"inline": false,
125+
"raw": "constructor: f"
126+
},
127+
"boundMembers": [],
128+
"parent": null,
129+
"ctor": {
130+
"type": "Constructor",
131+
"line": 3,
132+
"column": 3,
133+
"range": [
134+
17,
135+
31
136+
],
137+
"assignee": {
138+
"type": "Identifier",
139+
"line": 3,
140+
"column": 3,
141+
"raw": "constructor",
142+
"range": [
143+
17,
144+
28
145+
],
146+
"data": "constructor"
147+
},
148+
"expression": {
149+
"type": "Identifier",
150+
"line": 3,
151+
"column": 16,
152+
"raw": "f",
153+
"range": [
154+
30,
155+
31
156+
],
157+
"data": "f"
158+
},
159+
"raw": "constructor: f"
160+
},
161+
"raw": "class A\n constructor: f"
162+
}
163+
],
164+
"raw": "f = ->\nclass A\n constructor: f"
165+
}
166+
}

0 commit comments

Comments
 (0)