@ant-yasa/uast-parser-php 0.2.10 → 0.2.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/dist/package.json +0 -1
  2. package/dist/src/parser.js +240 -15
  3. package/dist/src/parser.js.map +1 -1
  4. package/package.json +1 -2
  5. package/src/index.ts +0 -26
  6. package/src/parser.ts +0 -1234
  7. package/tests/benchmark/base/advanced.php +0 -20
  8. package/tests/benchmark/base/advanced.php.json +0 -1269
  9. package/tests/benchmark/base/basic.php +0 -3
  10. package/tests/benchmark/base/basic.php.json +0 -292
  11. package/tests/benchmark/base/closure-class.php +0 -24
  12. package/tests/benchmark/base/closure-class.php.json +0 -1623
  13. package/tests/benchmark/base/control-flow.php +0 -37
  14. package/tests/benchmark/base/control-flow.php.json +0 -2010
  15. package/tests/benchmark/base/enum-trait-adapt.php +0 -31
  16. package/tests/benchmark/base/enum-trait-adapt.php.json +0 -965
  17. package/tests/benchmark/base/exprs.php +0 -4
  18. package/tests/benchmark/base/exprs.php.json +0 -811
  19. package/tests/benchmark/base/flow-extras.php +0 -12
  20. package/tests/benchmark/base/flow-extras.php.json +0 -897
  21. package/tests/benchmark/base/function.php +0 -7
  22. package/tests/benchmark/base/function.php.json +0 -536
  23. package/tests/benchmark/base/modern-php.php +0 -13
  24. package/tests/benchmark/base/modern-php.php.json +0 -508
  25. package/tests/benchmark/base/namespace.php +0 -5
  26. package/tests/benchmark/base/namespace.php.json +0 -307
  27. package/tests/benchmark/base/new-static.php +0 -3
  28. package/tests/benchmark/base/new-static.php.json +0 -399
  29. package/tests/benchmark/base/runtime.php +0 -13
  30. package/tests/benchmark/base/runtime.php.json +0 -1095
  31. package/tests/benchmark/base/strings-casts.php +0 -17
  32. package/tests/benchmark/base/strings-casts.php.json +0 -1357
  33. package/tests/benchmark/base/structures.php +0 -26
  34. package/tests/benchmark/base/structures.php.json +0 -1582
  35. package/tests/index.ts +0 -87
package/dist/package.json CHANGED
@@ -4,7 +4,6 @@
4
4
  "description": "for converting php to UAST",
5
5
  "dependencies": {
6
6
  "@ant-yasa/uast-spec": "^0.2.0",
7
- "php-parser": "^3.4.0",
8
7
  "tree-sitter-php": "^0.24.2",
9
8
  "typescript": "^5.7.2",
10
9
  "web-tree-sitter": "^0.26.8"
@@ -163,12 +163,18 @@ function createParameter(paramNode, opts) {
163
163
  const init = defaultValueNode ? visit(defaultValueNode, opts) : null;
164
164
  const varDecl = UAST.variableDeclaration(id, init, false, UAST.dynamicType());
165
165
  varDecl._meta.variadic = paramNode.type === 'variadic_parameter';
166
- // property_promotion_parameter 的 visibility
166
+ // 引用参数:&$param → byref 标记
167
+ if (paramNode.childForFieldName('reference_modifier')) {
168
+ varDecl._meta.byref = true;
169
+ }
170
+ // property_promotion_parameter 的 visibility 和 readonly
167
171
  if (paramNode.type === 'property_promotion_parameter') {
168
172
  for (const child of paramNode.namedChildren) {
169
173
  if (child.type === 'visibility_modifier') {
170
174
  varDecl._meta.visibility = child.text;
171
- break;
175
+ }
176
+ if (child.type === 'readonly_modifier') {
177
+ varDecl._meta.readonly = true;
172
178
  }
173
179
  }
174
180
  }
@@ -196,6 +202,38 @@ function createFunctionLike(node, opts) {
196
202
  }
197
203
  }
198
204
  const fdef = UAST.functionDefinition(id, parameters, UAST.dynamicType(), body, modifiers);
205
+ // __construct → 标记为构造函数,引擎 scope.ts / initializer.ts 依赖此标志定位 _CTOR_
206
+ if (id?.name === '__construct') {
207
+ fdef._meta.isConstructor = true;
208
+ // Constructor Promotion: visibility 参数自动生成 $this->param = $param 赋值
209
+ const promotionStmts = [];
210
+ for (const param of parameters) {
211
+ if (param._meta?.visibility) {
212
+ const paramName = UAST.isVariableDeclaration(param)
213
+ ? param.id.name
214
+ : '';
215
+ if (paramName) {
216
+ // $this->paramName = $paramName
217
+ const thisExpr = UAST.thisExpression();
218
+ const fieldId = UAST.identifier(paramName);
219
+ const memberExpr = UAST.memberAccess(thisExpr, fieldId, false);
220
+ const valueId = UAST.identifier(paramName);
221
+ const assign = UAST.assignmentExpression(memberExpr, valueId, '=', false);
222
+ const exprStmt = UAST.expressionStatement(assign);
223
+ appendNodeMeta(exprStmt, node, sourcefile);
224
+ promotionStmts.push(exprStmt);
225
+ // readonly 标记
226
+ if (param._meta.readonly) {
227
+ param._meta.readonly = true;
228
+ }
229
+ }
230
+ }
231
+ }
232
+ // prepend 到构造函数 body 开头
233
+ if (promotionStmts.length > 0 && UAST.isScopedStatement(body)) {
234
+ body.body.unshift(...promotionStmts);
235
+ }
236
+ }
199
237
  return appendNodeMeta(fdef, node, sourcefile);
200
238
  }
201
239
  /** 构建类/接口/trait 定义 */
@@ -207,9 +245,9 @@ function createClassLike(node, opts, kind) {
207
245
  const body = bodyNode
208
246
  ? flattenInstructions(visitList(bodyNode.namedChildren, opts))
209
247
  : [];
210
- // extends
248
+ // extends — childForFieldName('base_clause') 在 tree-sitter PHP 返回 NULL,改用 namedChildren 查找
211
249
  const supers = [];
212
- const baseClause = node.childForFieldName('base_clause');
250
+ const baseClause = node.namedChildren.find((c) => c.type === 'base_clause');
213
251
  if (baseClause) {
214
252
  for (const child of baseClause.namedChildren) {
215
253
  const superExpr = visit(child, opts);
@@ -217,9 +255,24 @@ function createClassLike(node, opts, kind) {
217
255
  supers.push(superExpr);
218
256
  }
219
257
  }
258
+ // trait use → 将 trait 名加入 supers,引擎 resolveClassInheritance 会自动合并 trait 方法
259
+ if (body) {
260
+ for (const stmt of body) {
261
+ if (UAST.isExpressionStatement(stmt)) {
262
+ const expr = stmt.expression;
263
+ if (UAST.isCallExpression(expr)
264
+ && UAST.isIdentifier(expr.callee)
265
+ && expr.callee.name === 'trait_use') {
266
+ for (const arg of expr.arguments) {
267
+ supers.push(arg);
268
+ }
269
+ }
270
+ }
271
+ }
272
+ }
220
273
  const cdef = UAST.classDefinition(id, body, supers);
221
- // implements
222
- const implementsClause = node.childForFieldName('class_interface_clause');
274
+ // implements — childForFieldName 同样返回 NULL,改用 namedChildren 查找
275
+ const implementsClause = node.namedChildren.find((c) => c.type === 'class_interface_clause');
223
276
  if (implementsClause) {
224
277
  cdef._meta.implements = visitList(implementsClause.namedChildren, opts);
225
278
  }
@@ -245,7 +298,14 @@ function visit(node, opts) {
245
298
  switch (node.type) {
246
299
  case 'program': {
247
300
  const children = node.namedChildren.filter((child) => child.type !== 'php_tag');
301
+ if (!opts._anonClassDefs)
302
+ opts._anonClassDefs = [];
248
303
  const body = flattenInstructions(visitList(children, opts));
304
+ // 匿名类定义提升到 body 头部
305
+ if (opts._anonClassDefs.length > 0) {
306
+ body.unshift(...opts._anonClassDefs);
307
+ opts._anonClassDefs = [];
308
+ }
249
309
  const compileUnit = UAST.compileUnit(body, 'php', null, sourcefile || '', package_json_1.version);
250
310
  return appendNodeMeta(compileUnit, node, sourcefile);
251
311
  }
@@ -266,6 +326,10 @@ function visit(node, opts) {
266
326
  // $variable -> 'variable'(variable_name 的 namedChild(0) 是 name 节点)
267
327
  const nameNode = node.namedChildren[0];
268
328
  const name = nameNode ? nameNode.text : node.text.replace(/^\$/, '');
329
+ // $this → ThisExpression,引擎 processThisExpression 返回类实例引用
330
+ if (name === 'this') {
331
+ return appendNodeMeta(UAST.thisExpression(), node, sourcefile);
332
+ }
269
333
  return appendNodeMeta(UAST.identifier(name), node, sourcefile);
270
334
  }
271
335
  case 'dynamic_variable_name': {
@@ -324,6 +388,33 @@ function visit(node, opts) {
324
388
  return appendNodeMeta(expr, node, sourcefile);
325
389
  }
326
390
  case 'heredoc': {
391
+ // heredoc 带变量插值时,heredoc_body 含 string_content 和 variable_name 等节点
392
+ const bodyNode = node.namedChildren.find((c) => c.type === 'heredoc_body');
393
+ const hasInterpolation = bodyNode && bodyNode.namedChildren.some((c) => c.type !== 'string_content');
394
+ if (bodyNode && hasInterpolation) {
395
+ // 与 encapsed_string 相同逻辑:遍历 namedChildren,拆成 binary concat
396
+ const parts = bodyNode.namedChildren.map((child) => {
397
+ if (child.type === 'string_content') {
398
+ return appendNodeMeta(UAST.literal(child.text, 'string'), child, sourcefile);
399
+ }
400
+ return visit(child, opts);
401
+ });
402
+ if (parts.length === 0) {
403
+ const literal = UAST.literal('', 'string');
404
+ literal._meta.heredoc = true;
405
+ return appendNodeMeta(literal, node, sourcefile);
406
+ }
407
+ let expr = parts[0];
408
+ for (let i = 1; i < parts.length; i++) {
409
+ expr = appendNodeMeta(UAST.binaryExpression('+', expr, parts[i]), node, sourcefile);
410
+ }
411
+ if (expr && UAST.isNode(expr)) {
412
+ expr._meta.heredoc = true;
413
+ expr._meta.encapsed = true;
414
+ }
415
+ return appendNodeMeta(expr, node, sourcefile);
416
+ }
417
+ // 纯文本 heredoc,保持原逻辑
327
418
  const content = stripHeredoc(node.text);
328
419
  const literal = UAST.literal(content, 'string');
329
420
  literal._meta.heredoc = true;
@@ -341,8 +432,29 @@ function visit(node, opts) {
341
432
  return appendNodeMeta(UAST.literal(null, 'null'), node, sourcefile);
342
433
  case 'argument': {
343
434
  // argument 节点包装了一个表达式 child
344
- const child = node.namedChildren[0];
345
- return child ? visit(child, opts) : appendNodeMeta(UAST.noop(), node, sourcefile);
435
+ // 命名参数:argument 有 name field(如 cmd: $val)
436
+ const argNameNode = node.childForFieldName('name');
437
+ let child;
438
+ if (argNameNode) {
439
+ // 命名参数:name field 是第一个 namedChild,值表达式是后续的 namedChild
440
+ // 注意:childForFieldName 和 namedChildren 返回的不是同一 JS 对象,
441
+ // 不能用 === 比较,改用 id 判等或取索引 >= 1 的 namedChild
442
+ child = node.namedChildren.find((c) => c.id !== argNameNode.id);
443
+ }
444
+ else {
445
+ child = node.namedChildren[0];
446
+ }
447
+ const result = child ? visit(child, opts) : appendNodeMeta(UAST.noop(), node, sourcefile);
448
+ if (argNameNode && result && UAST.isNode(result)) {
449
+ result._meta.argumentName = argNameNode.text;
450
+ }
451
+ return result;
452
+ }
453
+ case 'variadic_unpacking': {
454
+ // ...$args → SpreadElement
455
+ const inner = node.namedChildren[0];
456
+ const expr = inner ? visit(inner, opts) : UAST.noop();
457
+ return appendNodeMeta(UAST.spreadElement(expr), node, sourcefile);
346
458
  }
347
459
  case 'parenthesized_expression': {
348
460
  const child = node.namedChildren[0];
@@ -422,6 +534,11 @@ function visit(node, opts) {
422
534
  const argsNode = node.childForFieldName('arguments');
423
535
  const args = argsNode ? visitList(argsNode.namedChildren, opts) : [];
424
536
  const call = UAST.callExpression(callee, args);
537
+ // 从参数的 _meta.argumentName 收集命名参数名称
538
+ const names = args.map((a) => a?._meta?.argumentName || null);
539
+ if (names.some((n) => n !== null)) {
540
+ call.names = names;
541
+ }
425
542
  return appendNodeMeta(call, node, sourcefile);
426
543
  }
427
544
  case 'member_call_expression': {
@@ -432,6 +549,26 @@ function visit(node, opts) {
432
549
  const argsNode = node.childForFieldName('arguments');
433
550
  const args = argsNode ? visitList(argsNode.namedChildren, opts) : [];
434
551
  const call = UAST.callExpression(memberCallee, args);
552
+ const names = args.map((a) => a?._meta?.argumentName || null);
553
+ if (names.some((n) => n !== null)) {
554
+ call.names = names;
555
+ }
556
+ return appendNodeMeta(call, node, sourcefile);
557
+ }
558
+ case 'nullsafe_member_call_expression': {
559
+ // $obj?->method() — 静态分析中等价于 $obj->method(),避免 ConditionalExpression 导致 UnionValue 阻断链式调用
560
+ const object = visit(node.childForFieldName('object'), opts);
561
+ const name = visit(node.childForFieldName('name'), opts);
562
+ const memberCallee = UAST.memberAccess(object, name, false);
563
+ memberCallee._meta.nullsafe = true;
564
+ appendNodeMeta(memberCallee, node, sourcefile);
565
+ const argsNode = node.childForFieldName('arguments');
566
+ const args = argsNode ? visitList(argsNode.namedChildren, opts) : [];
567
+ const call = UAST.callExpression(memberCallee, args);
568
+ const names = args.map((a) => a?._meta?.argumentName || null);
569
+ if (names.some((n) => n !== null)) {
570
+ call.names = names;
571
+ }
435
572
  return appendNodeMeta(call, node, sourcefile);
436
573
  }
437
574
  case 'scoped_call_expression': {
@@ -443,12 +580,73 @@ function visit(node, opts) {
443
580
  const argsNode = node.childForFieldName('arguments');
444
581
  const args = argsNode ? visitList(argsNode.namedChildren, opts) : [];
445
582
  const call = UAST.callExpression(memberCallee, args);
583
+ const names = args.map((a) => a?._meta?.argumentName || null);
584
+ if (names.some((n) => n !== null)) {
585
+ call.names = names;
586
+ }
446
587
  return appendNodeMeta(call, node, sourcefile);
447
588
  }
448
589
  // ─── new / cast ───
449
590
  case 'object_creation_expression': {
450
- const className = visit(node.namedChildren[0], opts);
451
- const argsNode = node.childForFieldName('arguments');
591
+ const firstChild = node.namedChildren[0];
592
+ // 匿名类:new class { ... }
593
+ if (firstChild && firstChild.type === 'anonymous_class') {
594
+ const anonNode = firstChild;
595
+ // 生成唯一匿名类名,基于文件+行号
596
+ const line = node.startPosition.row + 1;
597
+ const col = node.startPosition.column;
598
+ const anonName = `__anon_class_${line}_${col}`;
599
+ const anonId = UAST.identifier(anonName);
600
+ appendNodeMeta(anonId, anonNode, sourcefile);
601
+ // 用 anonymous_class 节点构建类定义(declaration_list 是 body)
602
+ const bodyNode = anonNode.namedChildren.find((c) => c.type === 'declaration_list');
603
+ const body = bodyNode
604
+ ? flattenInstructions(visitList(bodyNode.namedChildren, opts))
605
+ : [];
606
+ const supers = [];
607
+ const baseClause = anonNode.namedChildren.find((c) => c.type === 'base_clause');
608
+ if (baseClause) {
609
+ for (const child of baseClause.namedChildren) {
610
+ const superExpr = visit(child, opts);
611
+ if (superExpr)
612
+ supers.push(superExpr);
613
+ }
614
+ }
615
+ // trait use
616
+ if (body) {
617
+ for (const stmt of body) {
618
+ if (UAST.isExpressionStatement(stmt)) {
619
+ const expr = stmt.expression;
620
+ if (UAST.isCallExpression(expr)
621
+ && UAST.isIdentifier(expr.callee)
622
+ && expr.callee.name === 'trait_use') {
623
+ for (const arg of expr.arguments) {
624
+ supers.push(arg);
625
+ }
626
+ }
627
+ }
628
+ }
629
+ }
630
+ const cdef = UAST.classDefinition(anonId, body, supers);
631
+ appendNodeMeta(cdef, anonNode, sourcefile);
632
+ // implements
633
+ const implClause = anonNode.namedChildren.find((c) => c.type === 'class_interface_clause');
634
+ if (implClause) {
635
+ cdef._meta.implements = visitList(implClause.namedChildren, opts);
636
+ }
637
+ // 提升类定义到 CompileUnit body
638
+ if (!opts._anonClassDefs)
639
+ opts._anonClassDefs = [];
640
+ opts._anonClassDefs.push(cdef);
641
+ // 构造函数参数(anonymous_class 节点上的 arguments)
642
+ const argsNode = anonNode.namedChildren.find((c) => c.type === 'arguments');
643
+ const args = argsNode ? visitList(argsNode.namedChildren, opts) : [];
644
+ const expr = UAST.newExpression(UAST.identifier(anonName), args);
645
+ return appendNodeMeta(expr, node, sourcefile);
646
+ }
647
+ const className = visit(firstChild, opts);
648
+ // tree-sitter PHP 的 arguments 不是 field,而是类型为 'arguments' 的 namedChild
649
+ const argsNode = node.namedChildren.find((c) => c.type === 'arguments');
452
650
  const args = argsNode ? visitList(argsNode.namedChildren, opts) : [];
453
651
  const expr = UAST.newExpression(className, args);
454
652
  return appendNodeMeta(expr, node, sourcefile);
@@ -469,13 +667,13 @@ function visit(node, opts) {
469
667
  return appendNodeMeta(member, node, sourcefile);
470
668
  }
471
669
  case 'nullsafe_member_access_expression': {
670
+ // $obj?->prop — 静态分析中等价于 $obj->prop
472
671
  const object = visit(node.childForFieldName('object'), opts);
473
672
  const name = visit(node.childForFieldName('name'), opts);
474
673
  const access = UAST.memberAccess(object, name, false);
475
674
  appendNodeMeta(access, node, sourcefile);
476
675
  access._meta.nullsafe = true;
477
- const expr = UAST.conditionalExpression(object, access, UAST.literal(null, 'null'));
478
- return appendNodeMeta(expr, node, sourcefile);
676
+ return access;
479
677
  }
480
678
  case 'scoped_property_access_expression': {
481
679
  const scope = visit(node.childForFieldName('scope'), opts);
@@ -502,6 +700,7 @@ function visit(node, opts) {
502
700
  }
503
701
  // ─── 数组 / 列表 / match ───
504
702
  case 'array_creation_expression': {
703
+ let autoIndex = 0;
505
704
  const properties = node.namedChildren.map((element) => {
506
705
  const elementChildren = element.namedChildren;
507
706
  if (elementChildren.length >= 2) {
@@ -509,11 +708,13 @@ function visit(node, opts) {
509
708
  const key = visit(elementChildren[0], opts);
510
709
  const value = visit(elementChildren[1], opts);
511
710
  const prop = UAST.objectProperty(key, value);
711
+ autoIndex++;
512
712
  return appendNodeMeta(prop, element, sourcefile);
513
713
  }
514
- // value only
714
+ // value only — 使用自增数字索引,与 PHP 数组行为一致
515
715
  const value = visit(elementChildren[0], opts);
516
- const prop = UAST.objectProperty(UAST.literal(null, 'null'), value);
716
+ const prop = UAST.objectProperty(UAST.literal(autoIndex, 'number'), value);
717
+ autoIndex++;
517
718
  return appendNodeMeta(prop, element, sourcefile);
518
719
  });
519
720
  const expr = UAST.objectExpression(properties);
@@ -587,7 +788,24 @@ function visit(node, opts) {
587
788
  }
588
789
  case 'yield_expression': {
589
790
  const valueNode = node.namedChildren[0] || null;
590
- const expr = UAST.yieldExpression(valueNode ? visit(valueNode, opts) : null);
791
+ let yieldValue = null;
792
+ if (valueNode) {
793
+ // tree-sitter-php 将 yield 值包装在 array_element_initializer 中,需要解包取内部表达式
794
+ if (valueNode.type === 'array_element_initializer') {
795
+ const innerChildren = valueNode.namedChildren;
796
+ if (innerChildren.length >= 2) {
797
+ // yield $key => $value 形式:取第二个子节点作为值
798
+ yieldValue = visit(innerChildren[innerChildren.length - 1], opts);
799
+ }
800
+ else if (innerChildren.length === 1) {
801
+ yieldValue = visit(innerChildren[0], opts);
802
+ }
803
+ }
804
+ else {
805
+ yieldValue = visit(valueNode, opts);
806
+ }
807
+ }
808
+ const expr = UAST.yieldExpression(yieldValue);
591
809
  return appendNodeMeta(expr, node, sourcefile);
592
810
  }
593
811
  case 'error_suppression_expression': {
@@ -748,6 +966,7 @@ function visit(node, opts) {
748
966
  }
749
967
  const param = UAST.variableDeclaration(paramId, null, false, UAST.dynamicType());
750
968
  param._meta.catchTypes = catchTypes;
969
+ param._meta.isCatchParam = true;
751
970
  appendNodeMeta(param, node, sourcefile);
752
971
  const clause = UAST.catchClause([param], catchBody);
753
972
  return appendNodeMeta(clause, node, sourcefile);
@@ -1154,6 +1373,12 @@ function parse(content, opts = {}) {
1154
1373
  if (!tree) {
1155
1374
  throw new Error('Failed to parse PHP code');
1156
1375
  }
1376
+ // 确保 opts 是可扩展的对象
1377
+ if (typeof opts !== 'object' || opts === null) {
1378
+ opts = { sourcefile: String(opts) };
1379
+ }
1380
+ if (!opts._anonClassDefs)
1381
+ opts._anonClassDefs = [];
1157
1382
  try {
1158
1383
  const node = visit(tree.rootNode, opts);
1159
1384
  sanitize(node);