豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit ee242a5

Browse files
authored
fix: attribute location (#1217)
1 parent 180e975 commit ee242a5

File tree

4 files changed

+312
-7
lines changed

4 files changed

+312
-7
lines changed

src/parser/expr.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -730,28 +730,29 @@ module.exports = {
730730
},
731731

732732
read_attribute() {
733+
const node = this.node("attribute");
733734
const name = this.text();
734735
let args = [];
735736
this.next();
736737
if (this.token === "(") {
737738
args = this.read_argument_list();
738739
}
739-
return this.node("attribute")(name, args);
740+
return node(name, args);
740741
},
741742
read_attr_list() {
742743
const list = [];
743744
if (this.token === this.tok.T_ATTRIBUTE) {
744745
do {
745-
const attrGr = this.node("attrgroup")([]);
746+
const node = this.node("attrgroup");
746747
this.next();
747-
attrGr.attrs.push(this.read_attribute());
748+
const attrs = [this.read_attribute()];
748749
while (this.token === ",") {
749750
this.next();
750-
if (this.token !== "]") attrGr.attrs.push(this.read_attribute());
751+
if (this.token !== "]") attrs.push(this.read_attribute());
751752
}
752-
list.push(attrGr);
753753
this.expect("]");
754754
this.next();
755+
list.push(node(attrs));
755756
} while (this.token === this.tok.T_ATTRIBUTE);
756757
}
757758
return list;

test/snapshot/__snapshots__/attributes.test.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,14 +1056,14 @@ Program {
10561056
Attribute {
10571057
"args": [],
10581058
"kind": "attribute",
1059-
"leadingComments": [
1059+
"name": "Pure",
1060+
"trailingComments": [
10601061
CommentBlock {
10611062
"kind": "commentblock",
10621063
"offset": 26,
10631064
"value": "/* Pure */",
10641065
},
10651066
],
1066-
"name": "Pure",
10671067
},
10681068
],
10691069
"kind": "attrgroup",

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

Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2487,6 +2487,308 @@ Program {
24872487
}
24882488
`;
24892489

2490+
exports[`Test locations test attrgroup 1`] = `
2491+
Program {
2492+
"children": [
2493+
_Function {
2494+
"arguments": [],
2495+
"attrGroups": [
2496+
AttrGroup {
2497+
"attrs": [
2498+
Attribute {
2499+
"args": [],
2500+
"kind": "attribute",
2501+
"loc": Location {
2502+
"end": Position {
2503+
"column": 6,
2504+
"line": 1,
2505+
"offset": 6,
2506+
},
2507+
"source": "Pure",
2508+
"start": Position {
2509+
"column": 2,
2510+
"line": 1,
2511+
"offset": 2,
2512+
},
2513+
},
2514+
"name": "Pure",
2515+
},
2516+
],
2517+
"kind": "attrgroup",
2518+
"loc": Location {
2519+
"end": Position {
2520+
"column": 7,
2521+
"line": 1,
2522+
"offset": 7,
2523+
},
2524+
"source": "#[Pure]",
2525+
"start": Position {
2526+
"column": 0,
2527+
"line": 1,
2528+
"offset": 0,
2529+
},
2530+
},
2531+
},
2532+
AttrGroup {
2533+
"attrs": [
2534+
Attribute {
2535+
"args": [],
2536+
"kind": "attribute",
2537+
"loc": Location {
2538+
"end": Position {
2539+
"column": 20,
2540+
"line": 1,
2541+
"offset": 20,
2542+
},
2543+
"source": "Deprecated",
2544+
"start": Position {
2545+
"column": 10,
2546+
"line": 1,
2547+
"offset": 10,
2548+
},
2549+
},
2550+
"name": "Deprecated",
2551+
},
2552+
],
2553+
"kind": "attrgroup",
2554+
"loc": Location {
2555+
"end": Position {
2556+
"column": 21,
2557+
"line": 1,
2558+
"offset": 21,
2559+
},
2560+
"source": "#[Deprecated]",
2561+
"start": Position {
2562+
"column": 8,
2563+
"line": 1,
2564+
"offset": 8,
2565+
},
2566+
},
2567+
},
2568+
],
2569+
"body": Block {
2570+
"children": [],
2571+
"kind": "block",
2572+
"loc": Location {
2573+
"end": Position {
2574+
"column": 39,
2575+
"line": 1,
2576+
"offset": 39,
2577+
},
2578+
"source": "{}",
2579+
"start": Position {
2580+
"column": 37,
2581+
"line": 1,
2582+
"offset": 37,
2583+
},
2584+
},
2585+
},
2586+
"byref": false,
2587+
"kind": "function",
2588+
"loc": Location {
2589+
"end": Position {
2590+
"column": 39,
2591+
"line": 1,
2592+
"offset": 39,
2593+
},
2594+
"source": "function foo()",
2595+
"start": Position {
2596+
"column": 22,
2597+
"line": 1,
2598+
"offset": 22,
2599+
},
2600+
},
2601+
"name": Identifier {
2602+
"kind": "identifier",
2603+
"loc": Location {
2604+
"end": Position {
2605+
"column": 34,
2606+
"line": 1,
2607+
"offset": 34,
2608+
},
2609+
"source": "foo",
2610+
"start": Position {
2611+
"column": 31,
2612+
"line": 1,
2613+
"offset": 31,
2614+
},
2615+
},
2616+
"name": "foo",
2617+
},
2618+
"nullable": false,
2619+
"type": null,
2620+
},
2621+
],
2622+
"errors": [],
2623+
"kind": "program",
2624+
"loc": Location {
2625+
"end": Position {
2626+
"column": 39,
2627+
"line": 1,
2628+
"offset": 39,
2629+
},
2630+
"source": "#[Pure] #[Deprecated] function foo() {}",
2631+
"start": Position {
2632+
"column": 0,
2633+
"line": 1,
2634+
"offset": 0,
2635+
},
2636+
},
2637+
}
2638+
`;
2639+
2640+
exports[`Test locations test attribute 1`] = `
2641+
Program {
2642+
"children": [
2643+
_Function {
2644+
"arguments": [],
2645+
"attrGroups": [
2646+
AttrGroup {
2647+
"attrs": [
2648+
Attribute {
2649+
"args": [
2650+
namedargument {
2651+
"kind": "namedargument",
2652+
"loc": Location {
2653+
"end": Position {
2654+
"column": 32,
2655+
"line": 1,
2656+
"offset": 32,
2657+
},
2658+
"source": "reason: 'since 5.2'",
2659+
"start": Position {
2660+
"column": 13,
2661+
"line": 1,
2662+
"offset": 13,
2663+
},
2664+
},
2665+
"name": "reason",
2666+
"value": String {
2667+
"isDoubleQuote": false,
2668+
"kind": "string",
2669+
"loc": Location {
2670+
"end": Position {
2671+
"column": 32,
2672+
"line": 1,
2673+
"offset": 32,
2674+
},
2675+
"source": "'since 5.2'",
2676+
"start": Position {
2677+
"column": 21,
2678+
"line": 1,
2679+
"offset": 21,
2680+
},
2681+
},
2682+
"raw": "'since 5.2'",
2683+
"unicode": false,
2684+
"value": "since 5.2",
2685+
},
2686+
},
2687+
],
2688+
"kind": "attribute",
2689+
"loc": Location {
2690+
"end": Position {
2691+
"column": 33,
2692+
"line": 1,
2693+
"offset": 33,
2694+
},
2695+
"source": "Deprecated(reason: 'since 5.2')",
2696+
"start": Position {
2697+
"column": 2,
2698+
"line": 1,
2699+
"offset": 2,
2700+
},
2701+
},
2702+
"name": "Deprecated",
2703+
},
2704+
],
2705+
"kind": "attrgroup",
2706+
"loc": Location {
2707+
"end": Position {
2708+
"column": 34,
2709+
"line": 1,
2710+
"offset": 34,
2711+
},
2712+
"source": "#[Deprecated(reason: 'since 5.2')]",
2713+
"start": Position {
2714+
"column": 0,
2715+
"line": 1,
2716+
"offset": 0,
2717+
},
2718+
},
2719+
},
2720+
],
2721+
"body": Block {
2722+
"children": [],
2723+
"kind": "block",
2724+
"loc": Location {
2725+
"end": Position {
2726+
"column": 52,
2727+
"line": 1,
2728+
"offset": 52,
2729+
},
2730+
"source": "{}",
2731+
"start": Position {
2732+
"column": 50,
2733+
"line": 1,
2734+
"offset": 50,
2735+
},
2736+
},
2737+
},
2738+
"byref": false,
2739+
"kind": "function",
2740+
"loc": Location {
2741+
"end": Position {
2742+
"column": 52,
2743+
"line": 1,
2744+
"offset": 52,
2745+
},
2746+
"source": "function foo()",
2747+
"start": Position {
2748+
"column": 35,
2749+
"line": 1,
2750+
"offset": 35,
2751+
},
2752+
},
2753+
"name": Identifier {
2754+
"kind": "identifier",
2755+
"loc": Location {
2756+
"end": Position {
2757+
"column": 47,
2758+
"line": 1,
2759+
"offset": 47,
2760+
},
2761+
"source": "foo",
2762+
"start": Position {
2763+
"column": 44,
2764+
"line": 1,
2765+
"offset": 44,
2766+
},
2767+
},
2768+
"name": "foo",
2769+
},
2770+
"nullable": false,
2771+
"type": null,
2772+
},
2773+
],
2774+
"errors": [],
2775+
"kind": "program",
2776+
"loc": Location {
2777+
"end": Position {
2778+
"column": 52,
2779+
"line": 1,
2780+
"offset": 52,
2781+
},
2782+
"source": "#[Deprecated(reason: 'since 5.2')] function foo() {}",
2783+
"start": Position {
2784+
"column": 0,
2785+
"line": 1,
2786+
"offset": 0,
2787+
},
2788+
},
2789+
}
2790+
`;
2791+
24902792
exports[`Test locations test bin 1`] = `
24912793
Program {
24922794
"children": [

test/snapshot/location.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ string";`,
228228
["intersection type return", "function foo(): A&B { return 1; }"],
229229
["union type parameter", "function foo(string|int $bar) {}"],
230230
["dnf type parameter", "function foo((A&B)|null $bar) {}"],
231+
["attribute", "#[Deprecated(reason: 'since 5.2')] function foo() {}"],
232+
["attrgroup", "#[Pure] #[Deprecated] function foo() {}"],
231233
])("test %s", (_, code) => {
232234
expect(
233235
parser.parseEval(code, {

0 commit comments

Comments
 (0)