Skip to content

Commit 7fcfc6a

Browse files
committed
Allow commaless fn params and obj/array patterns
1 parent 8159e93 commit 7fcfc6a

File tree

18 files changed

+1050
-17
lines changed

18 files changed

+1050
-17
lines changed

src/parser/expression.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,11 @@ pp.parseParenAndDistinguishExpression = function (startPos, startLoc, canBeArrow
805805
if (first) {
806806
first = false;
807807
} else {
808-
this.expect(tt.comma, refNeedsArrowPos.start || null);
808+
if (this.hasPlugin("lightscript")) {
809+
this.expectCommaOrLineBreak(refNeedsArrowPos.start || null);
810+
} else {
811+
this.expect(tt.comma, refNeedsArrowPos.start || null);
812+
}
809813
if (this.match(tt.parenR)) {
810814
optionalCommaStart = this.state.start;
811815
break;

src/parser/lval.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,11 @@ pp.parseBindingList = function (close, allowEmpty) {
151151
if (first) {
152152
first = false;
153153
} else {
154-
this.expect(tt.comma);
154+
if (this.hasPlugin("lightscript")) {
155+
this.expectCommaOrLineBreak();
156+
} else {
157+
this.expect(tt.comma);
158+
}
155159
}
156160
if (allowEmpty && this.match(tt.comma)) {
157161
elts.push(null);

src/plugins/lightscript.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ pp.parseWhiteBlock = function (isExpression?) {
170170
return this.parseMultilineWhiteBlock(node, indentLevel);
171171
};
172172

173-
pp.expectCommaOrLineBreak = function () {
173+
pp.expectCommaOrLineBreak = function (loc = null) {
174174
// TODO: consider error message like "Missing comma or newline."
175-
if (!(this.eat(tt.comma) || this.isLineBreak())) this.unexpected(null, tt.comma);
175+
if (!(this.eat(tt.comma) || this.isLineBreak())) this.unexpected(loc, tt.comma);
176176
};
177177

178178
// lightscript only allows plain space (ascii-32), \r\n, and \n.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
a
3+
[
4+
b
5+
c
6+
]
7+
d
8+
] = []
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
{
2+
"type": "File",
3+
"start": 0,
4+
"end": 36,
5+
"loc": {
6+
"start": {
7+
"line": 1,
8+
"column": 0
9+
},
10+
"end": {
11+
"line": 8,
12+
"column": 6
13+
}
14+
},
15+
"program": {
16+
"type": "Program",
17+
"start": 0,
18+
"end": 36,
19+
"loc": {
20+
"start": {
21+
"line": 1,
22+
"column": 0
23+
},
24+
"end": {
25+
"line": 8,
26+
"column": 6
27+
}
28+
},
29+
"sourceType": "script",
30+
"body": [
31+
{
32+
"type": "VariableDeclaration",
33+
"start": 0,
34+
"end": 36,
35+
"loc": {
36+
"start": {
37+
"line": 1,
38+
"column": 0
39+
},
40+
"end": {
41+
"line": 8,
42+
"column": 6
43+
}
44+
},
45+
"kind": "const",
46+
"extra": {
47+
"implicit": true
48+
},
49+
"declarations": [
50+
{
51+
"type": "VariableDeclarator",
52+
"start": 0,
53+
"end": 36,
54+
"loc": {
55+
"start": {
56+
"line": 1,
57+
"column": 0
58+
},
59+
"end": {
60+
"line": 8,
61+
"column": 6
62+
}
63+
},
64+
"id": {
65+
"type": "ArrayPattern",
66+
"start": 0,
67+
"end": 31,
68+
"loc": {
69+
"start": {
70+
"line": 1,
71+
"column": 0
72+
},
73+
"end": {
74+
"line": 8,
75+
"column": 1
76+
}
77+
},
78+
"elements": [
79+
{
80+
"type": "Identifier",
81+
"start": 4,
82+
"end": 5,
83+
"loc": {
84+
"start": {
85+
"line": 2,
86+
"column": 2
87+
},
88+
"end": {
89+
"line": 2,
90+
"column": 3
91+
},
92+
"identifierName": "a"
93+
},
94+
"name": "a"
95+
},
96+
{
97+
"type": "ArrayPattern",
98+
"start": 8,
99+
"end": 25,
100+
"loc": {
101+
"start": {
102+
"line": 3,
103+
"column": 2
104+
},
105+
"end": {
106+
"line": 6,
107+
"column": 3
108+
}
109+
},
110+
"elements": [
111+
{
112+
"type": "Identifier",
113+
"start": 14,
114+
"end": 15,
115+
"loc": {
116+
"start": {
117+
"line": 4,
118+
"column": 4
119+
},
120+
"end": {
121+
"line": 4,
122+
"column": 5
123+
},
124+
"identifierName": "b"
125+
},
126+
"name": "b"
127+
},
128+
{
129+
"type": "Identifier",
130+
"start": 20,
131+
"end": 21,
132+
"loc": {
133+
"start": {
134+
"line": 5,
135+
"column": 4
136+
},
137+
"end": {
138+
"line": 5,
139+
"column": 5
140+
},
141+
"identifierName": "c"
142+
},
143+
"name": "c"
144+
}
145+
]
146+
},
147+
{
148+
"type": "Identifier",
149+
"start": 28,
150+
"end": 29,
151+
"loc": {
152+
"start": {
153+
"line": 7,
154+
"column": 2
155+
},
156+
"end": {
157+
"line": 7,
158+
"column": 3
159+
},
160+
"identifierName": "d"
161+
},
162+
"name": "d"
163+
}
164+
]
165+
},
166+
"init": {
167+
"type": "ArrayExpression",
168+
"start": 34,
169+
"end": 36,
170+
"loc": {
171+
"start": {
172+
"line": 8,
173+
"column": 4
174+
},
175+
"end": {
176+
"line": 8,
177+
"column": 6
178+
}
179+
},
180+
"elements": []
181+
}
182+
}
183+
]
184+
}
185+
],
186+
"directives": []
187+
}
188+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(
2+
a
3+
b
4+
) => {}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
{
2+
"type": "File",
3+
"start": 0,
4+
"end": 17,
5+
"loc": {
6+
"start": {
7+
"line": 1,
8+
"column": 0
9+
},
10+
"end": {
11+
"line": 4,
12+
"column": 7
13+
}
14+
},
15+
"program": {
16+
"type": "Program",
17+
"start": 0,
18+
"end": 17,
19+
"loc": {
20+
"start": {
21+
"line": 1,
22+
"column": 0
23+
},
24+
"end": {
25+
"line": 4,
26+
"column": 7
27+
}
28+
},
29+
"sourceType": "script",
30+
"body": [
31+
{
32+
"type": "ExpressionStatement",
33+
"start": 0,
34+
"end": 17,
35+
"loc": {
36+
"start": {
37+
"line": 1,
38+
"column": 0
39+
},
40+
"end": {
41+
"line": 4,
42+
"column": 7
43+
}
44+
},
45+
"expression": {
46+
"type": "ArrowFunctionExpression",
47+
"start": 0,
48+
"end": 17,
49+
"loc": {
50+
"start": {
51+
"line": 1,
52+
"column": 0
53+
},
54+
"end": {
55+
"line": 4,
56+
"column": 7
57+
}
58+
},
59+
"id": null,
60+
"generator": false,
61+
"expression": false,
62+
"async": false,
63+
"params": [
64+
{
65+
"type": "Identifier",
66+
"start": 4,
67+
"end": 5,
68+
"loc": {
69+
"start": {
70+
"line": 2,
71+
"column": 2
72+
},
73+
"end": {
74+
"line": 2,
75+
"column": 3
76+
},
77+
"identifierName": "a"
78+
},
79+
"name": "a"
80+
},
81+
{
82+
"type": "Identifier",
83+
"start": 8,
84+
"end": 9,
85+
"loc": {
86+
"start": {
87+
"line": 3,
88+
"column": 2
89+
},
90+
"end": {
91+
"line": 3,
92+
"column": 3
93+
},
94+
"identifierName": "b"
95+
},
96+
"name": "b"
97+
}
98+
],
99+
"skinny": false,
100+
"body": {
101+
"type": "BlockStatement",
102+
"start": 15,
103+
"end": 17,
104+
"loc": {
105+
"start": {
106+
"line": 4,
107+
"column": 5
108+
},
109+
"end": {
110+
"line": 4,
111+
"column": 7
112+
}
113+
},
114+
"body": [],
115+
"directives": [],
116+
"extra": {
117+
"curly": true
118+
}
119+
}
120+
}
121+
}
122+
],
123+
"directives": []
124+
}
125+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function x (
2+
a
3+
b
4+
) {}

0 commit comments

Comments
 (0)