Skip to content

Commit ebc6ad4

Browse files
feat: better ast for encapsedpart (#420)
1 parent 32d28f0 commit ebc6ad4

13 files changed

+814
-78
lines changed

src/ast/encapsedpart.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@ const KIND = "encapsedpart";
1212
* Part of `Encapsed` node
1313
* @constructor EncapsedPart
1414
* @extends {Expression}
15-
* @property {Expression} what
15+
* @property {Expression} expression
16+
* @property {String} syntax
17+
* @property {Boolean} curly
1618
*/
1719
module.exports = Expression.extends(KIND, function EncapsedPart(
1820
expression,
21+
syntax,
1922
curly,
2023
docs,
2124
location
2225
) {
2326
Expression.apply(this, [KIND, docs, location]);
2427
this.expression = expression;
28+
this.syntax = syntax;
2529
this.curly = curly;
2630
});

src/parser/scalar.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ module.exports = {
201201
*/
202202
read_encapsed_string_item: function(isDoubleQuote) {
203203
const encapsedPart = this.node("encapsedpart");
204+
let syntax = null;
204205
let curly = false;
205206
let result = this.node(),
206207
offset,
@@ -220,6 +221,8 @@ module.exports = {
220221
text
221222
);
222223
} else if (this.token === this.tok.T_DOLLAR_OPEN_CURLY_BRACES) {
224+
syntax = 'simple';
225+
curly = true;
223226
// dynamic variable name
224227
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1239
225228
name = null;
@@ -242,15 +245,16 @@ module.exports = {
242245
name = this.read_expr();
243246
}
244247
this.expect("}") && this.next();
245-
result = result("variable", name, true);
248+
result = result("variable", name);
246249
} else if (this.token === this.tok.T_CURLY_OPEN) {
247250
// expression
248251
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1246
249-
curly = true;
252+
syntax = 'complex';
250253
result.destroy();
251254
result = this.next().read_variable(false, false);
252255
this.expect("}") && this.next();
253256
} else if (this.token === this.tok.T_VARIABLE) {
257+
syntax = 'simple';
254258
// plain variable
255259
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1231
256260
result.destroy();
@@ -284,7 +288,7 @@ module.exports = {
284288
result = result("string", false, value, false, value);
285289
}
286290

287-
return encapsedPart(result, curly);
291+
return encapsedPart(result, syntax, curly);
288292
},
289293
/**
290294
* Reads an encapsed string

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

+9
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,7 @@ Program {
10811081
"offset": 829,
10821082
},
10831083
},
1084+
"syntax": null,
10841085
},
10851086
EncapsedPart {
10861087
"curly": false,
@@ -1116,6 +1117,7 @@ Program {
11161117
"offset": 836,
11171118
},
11181119
},
1120+
"syntax": "simple",
11191121
},
11201122
EncapsedPart {
11211123
"curly": false,
@@ -1153,6 +1155,7 @@ Program {
11531155
"offset": 841,
11541156
},
11551157
},
1158+
"syntax": null,
11561159
},
11571160
EncapsedPart {
11581161
"curly": false,
@@ -1188,6 +1191,7 @@ Program {
11881191
"offset": 843,
11891192
},
11901193
},
1194+
"syntax": "simple",
11911195
},
11921196
EncapsedPart {
11931197
"curly": false,
@@ -1225,6 +1229,7 @@ Program {
12251229
"offset": 852,
12261230
},
12271231
},
1232+
"syntax": null,
12281233
},
12291234
],
12301235
},
@@ -6768,6 +6773,7 @@ next:
67686773
"offset": 2937,
67696774
},
67706775
},
6776+
"syntax": null,
67716777
},
67726778
],
67736779
},
@@ -6897,6 +6903,7 @@ next:
68976903
"offset": 2903,
68986904
},
68996905
},
6906+
"syntax": null,
69006907
},
69016908
],
69026909
},
@@ -7467,6 +7474,7 @@ BAZ
74677474
"offset": 3057,
74687475
},
74697476
},
7477+
"syntax": null,
74707478
},
74717479
],
74727480
},
@@ -7691,6 +7699,7 @@ FOO
76917699
"offset": 3134,
76927700
},
76937701
},
7702+
"syntax": null,
76947703
},
76957704
],
76967705
},

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

+1
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ Program {
582582
"value": "ls -larth",
583583
},
584584
"kind": "encapsedpart",
585+
"syntax": null,
585586
},
586587
],
587588
},

0 commit comments

Comments
 (0)