Skip to content

Commit eeb00b8

Browse files
committed
fix empty arrays
1 parent f8e0455 commit eeb00b8

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

src/parser/array.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ module.exports = {
2020
var items = [];
2121
var result = this.node(ArrayExpr);
2222

23-
this.expect([this.tok.T_ARRAY, '[']);
24-
25-
if (this.token == this.tok.T_ARRAY) {
23+
if (this.token === this.tok.T_ARRAY) {
2624
this.next().expect('(');
25+
expect = ')';
2726
} else {
2827
shortForm = true;
28+
expect = ']';
2929
}
30+
3031
if (this.next().token != expect) {
3132
while(this.token != this.EOF) {
3233
items.push(this.read_array_pair_list());
@@ -38,7 +39,7 @@ module.exports = {
3839
} else break;
3940
}
4041
}
41-
this.expect(shortForm ? ']' : ')');
42+
this.expect(expect);
4243
this.next();
4344
return result(shortForm, items);
4445
},

src/parser/loops.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ module.exports = {
149149
var assignList = this.read_assignment_list();
150150
if (this.expect(')')) this.next();
151151
return result(assignList);
152-
} else if (this.token === '[') {
152+
} else if (this.token === '[' || this.token === this.tok.T_ARRAY) {
153153
return this.read_array();
154154
} else {
155155
return this.read_variable(false, false, false);

test/arrayTests.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ describe('Array without keys', function() {
164164
});
165165

166166
describe('mixed tests / coverage', function() {
167+
it('test empty array', function() {
168+
var ast = parser.parseEval('$a = []; $b = array();');
169+
ast.children[0].right.items.length.should.be.exactly(0);
170+
ast.children[1].right.items.length.should.be.exactly(0);
171+
});
167172
it('test short form / keys', function() {
168173
var ast = parser.parseEval('[0 => &$foo, $bar => "foobar"];');
169174
ast.children[0].items.length.should.be.exactly(2);

0 commit comments

Comments
 (0)