Skip to content

Commit 19191e9

Browse files
refactor: exit node
1 parent 6e63a00 commit 19191e9

File tree

10 files changed

+200
-57
lines changed

10 files changed

+200
-57
lines changed

src/ast.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,18 +323,15 @@ AST.prototype.resolvePrecedence = function(result, parser) {
323323
result = buffer;
324324
}
325325
}
326-
} else if (
327-
result.kind === "silent" &&
328-
!result.expr.parenthesizedExpression
329-
) {
330-
if (result.expr.kind === 'assign') return result;
326+
} else if (result.kind === "silent" && !result.expr.parenthesizedExpression) {
327+
if (result.expr.kind === "assign") return result;
331328
// overall least precedence
332329
if (result.expr.right) {
333330
buffer = result.expr;
334331
result.expr = buffer.left;
335332
buffer.left = result;
336333
this.swapLocations(buffer, buffer.left, buffer.right, parser);
337-
result = buffer;
334+
result = buffer;
338335
}
339336
}
340337
return result;

src/ast/exit.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ const KIND = "exit";
1212
* Defines an exit / die call
1313
* @constructor Exit
1414
* @extends {Expression}
15-
* @property {Node|null} status
15+
* @property {Node|null} expression
1616
* @property {Boolean} useDie
1717
*/
1818
module.exports = Expression.extends(KIND, function Exit(
19-
status,
19+
expression,
2020
useDie,
2121
docs,
2222
location
2323
) {
2424
Expression.apply(this, [KIND, docs, location]);
25-
this.status = status;
25+
this.expression = expression;
2626
this.useDie = useDie;
2727
});

src/parser/expr.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,18 +281,18 @@ module.exports = {
281281
case this.tok.T_EXIT: {
282282
const useDie = this.lexer.yytext.toLowerCase() === "die";
283283
result = this.node("exit");
284-
let status = null;
284+
let expression = null;
285285
if (this.next().token === "(") {
286286
if (this.next().token !== ")") {
287-
status = this.read_expr();
287+
expression = this.read_expr();
288288
if (this.expect(")")) {
289289
this.next();
290290
}
291291
} else {
292292
this.next();
293293
}
294294
}
295-
return result(status, useDie);
295+
return result(expression, useDie);
296296
}
297297

298298
case this.tok.T_PRINT:

test/snapshot/__snapshots__/acid.test.js.snap

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7524,24 +7524,7 @@ BAZ
75247524
},
75257525
ExpressionStatement {
75267526
"expression": Exit {
7527-
"kind": "exit",
7528-
"loc": Location {
7529-
"end": Position {
7530-
"column": 3,
7531-
"line": 149,
7532-
"offset": 3119,
7533-
},
7534-
"source": "die($foo(<<<'BAR'
7535-
$foo + $bar
7536-
BAR
7537-
));",
7538-
"start": Position {
7539-
"column": 2,
7540-
"line": 146,
7541-
"offset": 3080,
7542-
},
7543-
},
7544-
"status": Call {
7527+
"expression": Call {
75457528
"arguments": Array [
75467529
Nowdoc {
75477530
"kind": "nowdoc",
@@ -7614,6 +7597,23 @@ BAR
76147597
"name": "foo",
76157598
},
76167599
},
7600+
"kind": "exit",
7601+
"loc": Location {
7602+
"end": Position {
7603+
"column": 3,
7604+
"line": 149,
7605+
"offset": 3119,
7606+
},
7607+
"source": "die($foo(<<<'BAR'
7608+
$foo + $bar
7609+
BAR
7610+
));",
7611+
"start": Position {
7612+
"column": 2,
7613+
"line": 146,
7614+
"offset": 3080,
7615+
},
7616+
},
76177617
"useDie": true,
76187618
},
76197619
"kind": "expressionstatement",

test/snapshot/__snapshots__/ast.test.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,29 +221,29 @@ Program {
221221
"children": Array [
222222
ExpressionStatement {
223223
"expression": Exit {
224-
"kind": "exit",
225-
"status": String {
224+
"expression": String {
226225
"isDoubleQuote": true,
227226
"kind": "string",
228227
"raw": "\\"bye\\"",
229228
"unicode": false,
230229
"value": "bye",
231230
},
231+
"kind": "exit",
232232
"useDie": true,
233233
},
234234
"kind": "expressionstatement",
235235
},
236236
ExpressionStatement {
237237
"expression": Exit {
238-
"kind": "exit",
239-
"status": Unary {
238+
"expression": Unary {
240239
"kind": "unary",
241240
"type": "-",
242241
"what": Number {
243242
"kind": "number",
244243
"value": "1",
245244
},
246245
},
246+
"kind": "exit",
247247
"useDie": false,
248248
},
249249
"kind": "expressionstatement",

test/snapshot/__snapshots__/exit.test.js.snap

Lines changed: 130 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ Program {
55
"children": Array [
66
ExpressionStatement {
77
"expression": Exit {
8-
"kind": "exit",
9-
"status": Variable {
8+
"expression": Variable {
109
"curly": false,
1110
"kind": "variable",
1211
"name": "var",
1312
},
13+
"kind": "exit",
1414
"useDie": false,
1515
},
1616
"kind": "expressionstatement",
@@ -26,8 +26,71 @@ Program {
2626
"children": Array [
2727
ExpressionStatement {
2828
"expression": Exit {
29+
"expression": null,
30+
"kind": "exit",
31+
"useDie": true,
32+
},
33+
"kind": "expressionstatement",
34+
},
35+
],
36+
"errors": Array [],
37+
"kind": "program",
38+
}
39+
`;
40+
41+
exports[`exit die with empty expression 1`] = `
42+
Program {
43+
"children": Array [
44+
ExpressionStatement {
45+
"expression": Exit {
46+
"expression": null,
47+
"kind": "exit",
48+
"useDie": true,
49+
},
50+
"kind": "expressionstatement",
51+
},
52+
],
53+
"errors": Array [],
54+
"kind": "program",
55+
}
56+
`;
57+
58+
exports[`exit die with expression 1`] = `
59+
Program {
60+
"children": Array [
61+
ExpressionStatement {
62+
"expression": Exit {
63+
"expression": Bin {
64+
"kind": "bin",
65+
"left": Number {
66+
"kind": "number",
67+
"value": "10",
68+
},
69+
"right": Variable {
70+
"curly": false,
71+
"kind": "variable",
72+
"name": "var",
73+
},
74+
"type": "+",
75+
},
76+
"kind": "exit",
77+
"useDie": true,
78+
},
79+
"kind": "expressionstatement",
80+
},
81+
],
82+
"errors": Array [],
83+
"kind": "program",
84+
}
85+
`;
86+
87+
exports[`exit die without expression 1`] = `
88+
Program {
89+
"children": Array [
90+
ExpressionStatement {
91+
"expression": Exit {
92+
"expression": null,
2993
"kind": "exit",
30-
"status": null,
3194
"useDie": true,
3295
},
3396
"kind": "expressionstatement",
@@ -38,13 +101,76 @@ Program {
38101
}
39102
`;
40103

104+
exports[`exit exit with empty expression 1`] = `
105+
Program {
106+
"children": Array [
107+
ExpressionStatement {
108+
"expression": Exit {
109+
"expression": null,
110+
"kind": "exit",
111+
"useDie": false,
112+
},
113+
"kind": "expressionstatement",
114+
},
115+
],
116+
"errors": Array [],
117+
"kind": "program",
118+
}
119+
`;
120+
121+
exports[`exit exit with expression 1`] = `
122+
Program {
123+
"children": Array [
124+
ExpressionStatement {
125+
"expression": Exit {
126+
"expression": Bin {
127+
"kind": "bin",
128+
"left": Number {
129+
"kind": "number",
130+
"value": "10",
131+
},
132+
"right": Variable {
133+
"curly": false,
134+
"kind": "variable",
135+
"name": "var",
136+
},
137+
"type": "+",
138+
},
139+
"kind": "exit",
140+
"useDie": false,
141+
},
142+
"kind": "expressionstatement",
143+
},
144+
],
145+
"errors": Array [],
146+
"kind": "program",
147+
}
148+
`;
149+
150+
exports[`exit exit without expression 1`] = `
151+
Program {
152+
"children": Array [
153+
ExpressionStatement {
154+
"expression": Exit {
155+
"expression": null,
156+
"kind": "exit",
157+
"useDie": false,
158+
},
159+
"kind": "expressionstatement",
160+
},
161+
],
162+
"errors": Array [],
163+
"kind": "program",
164+
}
165+
`;
166+
41167
exports[`exit simple 1`] = `
42168
Program {
43169
"children": Array [
44170
ExpressionStatement {
45171
"expression": Exit {
172+
"expression": null,
46173
"kind": "exit",
47-
"status": null,
48174
"useDie": false,
49175
},
50176
"kind": "expressionstatement",

test/snapshot/__snapshots__/expr.test.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,27 +1259,27 @@ Program {
12591259
"children": Array [
12601260
ExpressionStatement {
12611261
"expression": Exit {
1262-
"kind": "exit",
1263-
"status": Number {
1262+
"expression": Number {
12641263
"kind": "number",
12651264
"value": "1",
12661265
},
1266+
"kind": "exit",
12671267
"useDie": false,
12681268
},
12691269
"kind": "expressionstatement",
12701270
},
12711271
ExpressionStatement {
12721272
"expression": Exit {
1273+
"expression": null,
12731274
"kind": "exit",
1274-
"status": null,
12751275
"useDie": true,
12761276
},
12771277
"kind": "expressionstatement",
12781278
},
12791279
ExpressionStatement {
12801280
"expression": Exit {
1281+
"expression": null,
12811282
"kind": "exit",
1282-
"status": null,
12831283
"useDie": false,
12841284
},
12851285
"kind": "expressionstatement",

test/snapshot/__snapshots__/location.test.js.snap

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4743,21 +4743,7 @@ Program {
47434743
"children": Array [
47444744
ExpressionStatement {
47454745
"expression": Exit {
4746-
"kind": "exit",
4747-
"loc": Location {
4748-
"end": Position {
4749-
"column": 8,
4750-
"line": 1,
4751-
"offset": 8,
4752-
},
4753-
"source": "exit(1);",
4754-
"start": Position {
4755-
"column": 0,
4756-
"line": 1,
4757-
"offset": 0,
4758-
},
4759-
},
4760-
"status": Number {
4746+
"expression": Number {
47614747
"kind": "number",
47624748
"loc": Location {
47634749
"end": Position {
@@ -4774,6 +4760,20 @@ Program {
47744760
},
47754761
"value": "1",
47764762
},
4763+
"kind": "exit",
4764+
"loc": Location {
4765+
"end": Position {
4766+
"column": 8,
4767+
"line": 1,
4768+
"offset": 8,
4769+
},
4770+
"source": "exit(1);",
4771+
"start": Position {
4772+
"column": 0,
4773+
"line": 1,
4774+
"offset": 0,
4775+
},
4776+
},
47774777
"useDie": false,
47784778
},
47794779
"kind": "expressionstatement",

test/snapshot/array.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,6 @@ describe('Array without keys', () => {
6262
it("single and empty (short form)", () => {
6363
expect(parser.parseEval("[]")).toMatchSnapshot();
6464
});
65+
66+
6567
});

0 commit comments

Comments
 (0)