Skip to content

Commit 0df5a49

Browse files
authored
Merge pull request #390 from glayzzle/refactor-isset-variables
refactor: isset variables
2 parents ae9b5b8 + c167015 commit 0df5a49

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
lines changed

src/parser/expr.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,20 @@ module.exports = {
116116
return this.node("cast")(type, this.text(), this.next().read_expr());
117117
},
118118

119+
/**
120+
* Read a isset variable
121+
*/
122+
read_isset_variable: function() {
123+
return this.read_expr();
124+
},
125+
126+
/**
127+
* Reads isset variables
128+
*/
129+
read_isset_variables: function () {
130+
return this.read_function_list(this.read_isset_variable, ",");
131+
},
132+
119133
/**
120134
* ```ebnf
121135
* Reads an expression
@@ -215,7 +229,7 @@ module.exports = {
215229
if (this.next().expect("(")) {
216230
this.next();
217231
}
218-
const variables = this.read_function_list(this.read_expr, ",");
232+
const variables = this.read_isset_variables();
219233
if (this.expect(")")) {
220234
this.next();
221235
}

test/snapshot/__snapshots__/isset.test.js.snap

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,48 @@ Program {
8585
}
8686
`;
8787

88+
exports[`isset test errors 1`] = `
89+
Program {
90+
"children": Array [
91+
ExpressionStatement {
92+
"expression": Assign {
93+
"kind": "assign",
94+
"left": Variable {
95+
"curly": false,
96+
"kind": "variable",
97+
"name": "var",
98+
},
99+
"operator": "=",
100+
"right": Isset {
101+
"kind": "isset",
102+
"variables": Array [
103+
undefined,
104+
],
105+
},
106+
},
107+
"kind": "expressionstatement",
108+
},
109+
],
110+
"errors": Array [
111+
Error {
112+
"expected": "EXPR",
113+
"kind": "error",
114+
"line": 1,
115+
"message": "Parse Error : syntax error, unexpected ')' on line 1",
116+
"token": "')'",
117+
},
118+
Error {
119+
"expected": ")",
120+
"kind": "error",
121+
"line": 1,
122+
"message": "Parse Error : syntax error, unexpected ';', expecting ')' on line 1",
123+
"token": "';'",
124+
},
125+
],
126+
"kind": "program",
127+
}
128+
`;
129+
88130
exports[`isset trailing comma #2 1`] = `
89131
Program {
90132
"children": Array [

test/snapshot/isset.test.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1-
const parser = require('../main');
1+
const parser = require("../main");
22

33
describe("isset", function() {
44
it("simple", function() {
5-
expect(parser.parseEval('isset($var);')).toMatchSnapshot();
5+
expect(parser.parseEval("isset($var);")).toMatchSnapshot();
66
});
77
it("multiple", function() {
8-
expect(parser.parseEval('isset($var, $var, $var);')).toMatchSnapshot();
8+
expect(parser.parseEval("isset($var, $var, $var);")).toMatchSnapshot();
99
});
1010
it("assign", function() {
11-
expect(parser.parseEval('$var = isset($var);')).toMatchSnapshot();
11+
expect(parser.parseEval("$var = isset($var);")).toMatchSnapshot();
1212
});
1313
it("trailing comma", function() {
14-
expect(parser.parseEval('isset($foo,);')).toMatchSnapshot();
14+
expect(parser.parseEval("isset($foo,);")).toMatchSnapshot();
1515
});
1616
it("trailing comma #2", function() {
17-
expect(parser.parseEval('isset($foo, $bar,);')).toMatchSnapshot();
17+
expect(parser.parseEval("isset($foo, $bar,);")).toMatchSnapshot();
18+
});
19+
it("test errors", function() {
20+
const errAst = parser.parseEval("$var = isset();", {
21+
parser: { suppressErrors: true }
22+
});
23+
expect(errAst).toMatchSnapshot();
24+
expect(errAst.errors.length).not.toBe(0);
1825
});
1926
});

0 commit comments

Comments
 (0)