@ant-yasa/uast-parser-java-js 0.1.20 → 0.1.21-beta.1

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.
package/README.md CHANGED
@@ -1,35 +1,17 @@
1
- ## README的意义
1
+ # @ant-yasa/uast-parser-java-js
2
2
 
3
- README 文件通常是项目的第一个入口点。你应该通过 README 明确地告诉大家,为什么他们应该使用你的项目,以及安装和使用的方法。
3
+ > UAST specification is a Lodash-esque utility library for Unified-AST(UAST) nodes
4
4
 
5
- 如果在仅仅看文档而不看代码的情况下就可以使用你的项目,该文档就完成了。 这个非常重要,因为这将使项目的文档接口与其内部实现分开,只要接口保持不变,就可以自由更改项目的内部结构。
5
+ ## Install
6
6
 
7
- **文档,而不是代码定义了项目的使用方式。**
7
+ Using npm:
8
8
 
9
- 一个规范的README文档能减少用户检索信息的时间。
9
+ ```sh
10
+ npm install --save-dev @ant-yasa/uast-parser-java-js
11
+ ```
10
12
 
11
- ## 标准 README
13
+ or using yarn:
12
14
 
13
- 一个标准的README文件应当至少包含以下的内容:
14
-
15
- - 项目背景:说明创建本项目的背景与动机,创建本项目试图解决的问题
16
- - 安装方法:说明如何快速上手使用该项目
17
- - 使用方法:列出本项目能够提供的功能以及使用这些功能的方法
18
- - 文档:现阶段antcode鼓励用户使用语雀组织项目文档,在README上应当放入项目的语雀文档链接
19
-
20
- ## 附加内容
21
-
22
- 视项目的实际情况,同样也应该包含以下内容:
23
-
24
- - 项目特性:说明本项目相较于其他同类项目所具有的特性
25
- - 兼容环境:说明本项目能够在什么平台上运行
26
- - 使用示例:展示一些使用本项目的小demo
27
- - 主要项目负责人:使用“@”标注出本项目的主要负责人,方便项目的用户沟通
28
- - 参与贡献的方式:规定好其他用户参与本项目并贡献代码的方式
29
- - 项目的参与者:列出项目主要的参与人
30
- - 已知用户:列出已经在生产环境中使用了本项目的全部或部分组件的公司或组织
31
- - 赞助者:列出为本项目提供赞助的用户
32
-
33
- ## 参考示例
34
-
35
- https://code.alipay.com/linkcode/antcode
15
+ ```sh
16
+ yarn add @ant-yasa/uast-parser-java-js --dev
17
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ant-yasa/uast-parser-java-js",
3
- "version": "0.1.20",
3
+ "version": "0.1.21-beta.1",
4
4
  "description": "for converting specific language to UAST",
5
5
  "dependencies": {
6
6
  "@ant-yasa/uast-spec": "beta",
@@ -202,15 +202,7 @@ export class RESOURCEMANAGER extends ServiceSummary {
202
202
  },
203
203
  columns: ['full_host_name']
204
204
  })
205
- // serviceStatusRecord = [{
206
- // full_host_name: 'antyarn1.inc.alipay.net'
207
- // }]
208
- // serviceStatusRecord = [{
209
- // full_host_name: 'rm-dsaas.prefromoffice.alipay.net'
210
- // }, {
211
- // full_host_name: 'rm2-dsaas.prefromoffice.alipay.net'
212
- // }]
213
- //得到resourcemanager的Active和StandBy的RM
205
+
214
206
  this.ret['haState'] = []
215
207
  for (let i = 0; i < serviceStatusRecord.length; i++) {
216
208
  let fullHostName = serviceStatusRecord[i].full_host_name
@@ -409,7 +401,7 @@ export class NODEMANAGER extends ServiceSummary {
409
401
 
410
402
  public async getPartitionsByNmServiceName (nmServiceName: string) {
411
403
  let rmActiveHost = await this.getActiveRmHostByNmServiceName(nmServiceName)
412
- // rmActiveHost = 'rm-dsaas.prefromoffice.alipay.net'
404
+ // rmActiveHost = 'rm-dsaas.prefromoffice.xxxxx.net'
413
405
  return this.getPartitionsByRmHost(rmActiveHost)
414
406
  }
415
407
 
@@ -527,7 +519,7 @@ export class NODEMANAGER extends ServiceSummary {
527
519
  */
528
520
  public async getNodeToLabels (nmServiceName: string) {
529
521
  // console.log('nmServiceName', nmServiceName)
530
- // let rmActiveHost = 'antyarn1.inc.alipay.net'
522
+ // let rmActiveHost = 'antyarn1.inc.xxxxx.net'
531
523
  let rmActiveHost = await this.getActiveRmHostByNmServiceName(nmServiceName)
532
524
  let entry = []
533
525
  const options = {
@@ -591,7 +583,7 @@ export class NODEMANAGER extends ServiceSummary {
591
583
  */
592
584
  public async getNodeListByLabel (nmServiceName: string, labelList: string[]) {
593
585
 
594
- // rmActiveHost = 'rm2-dsaas.prefromoffice.alipay.net'
586
+ // rmActiveHost = 'rm2-dsaas.prefromoffice.xxxxx.net'
595
587
 
596
588
  let nodeToLabels = await this.getNodeToLabels(nmServiceName)
597
589
  let nodeHostList = []
@@ -637,7 +629,7 @@ export class NODEMANAGER extends ServiceSummary {
637
629
  public async getClusterMetaOfNodeList (nmServiceName: string) {
638
630
  let rmActiveHost = await this.getActiveRmHostByNmServiceName(nmServiceName)
639
631
 
640
- // rmActiveHost = 'rm2-dsaas.prefromoffice.alipay.net'
632
+ // rmActiveHost = 'rm2-dsaas.prefromoffice.xxxxx.net'
641
633
 
642
634
  let nodes = await this.getClusterNodesByRmHost(rmActiveHost)
643
635
  let queues = await this.getClusterQueuesByRmHost(rmActiveHost)
@@ -703,7 +695,7 @@ export class NODEMANAGER extends ServiceSummary {
703
695
  public async getSummary (nmServiceName) {
704
696
  // console.log('nmServiceName', nmServiceName)
705
697
  let rmActiveHost = await this.getActiveRmHostByNmServiceName(nmServiceName)
706
- // let rmActiveHost = 'antyarn1.inc.alipay.net'
698
+ // let rmActiveHost = 'antyarn1.inc.xxxxx.net'
707
699
  let nodes = await this.getClusterNodesByRmHost(rmActiveHost)
708
700
 
709
701
  this.ctx.logger.warn('nodes nodes nodes', JSON.stringify(nodes))
package/tests/index.ts CHANGED
@@ -1,66 +1,129 @@
1
1
  import 'mocha';
2
2
  import * as assert from 'assert';
3
- import { parse } from '../src/frontend/javascript/parser'
4
- import * as fs from 'fs'
5
- import * as path from 'path'
3
+ import { parse } from '../src/frontend/javascript/parser';
4
+ import * as fs from 'fs';
5
+ import * as path from 'path';
6
6
  import * as globby from 'fast-glob';
7
7
 
8
- function refreshUastJson() {
9
- const suffixMatch = ['*.js'];
10
- const jsFiles = globby.sync(suffixMatch, { cwd: path.join(__dirname, '/benchmark/base') });
11
- for (let file of jsFiles) {
12
- const content = fs.readFileSync(path.join(__dirname, '/benchmark/base', file), 'utf8');
13
- const ast = parse(content)
14
- fs.writeFileSync(path.join(__dirname,'/benchmark/base', `${file}.json`), JSON.stringify(ast, null, 2), 'utf8');
15
- }
8
+ // 获取当前版本:优先使用环境变量,否则 fallback 到 package.json
9
+ function getCurrentVersion(): string {
10
+ return process.env.UAST_VERSION || require('../package.json').version;
16
11
  }
17
12
 
18
- // uastparser变更了以后,确认修改都对的情况下,使用tsc编译在yasa替换uastparser验证无误以后
19
- // 需要重新生成baseline
20
- // refreshUastJson()
13
+ // 动态替换 AST 中的 sourcefile 和 version 字段
14
+ function normalizeAst(ast: any, currentSourceFile: string, currentVersion: string): any {
15
+ if (!ast || typeof ast !== 'object') return ast;
21
16
 
17
+ const cloned = Array.isArray(ast) ? [...ast] : { ...ast };
22
18
 
23
- describe('benchmark for javascript', () => {
24
- const suffixMatch = ['*.js'];
25
- const jsFiles = globby.sync(suffixMatch, { cwd: path.join(__dirname, '/benchmark/base') });
26
- for (const jsFile of jsFiles) {
27
- it(jsFile, () => {
28
- // fs.writeFileSync(path.join(__dirname, `test.json`), JSON.stringify(parse(content), null, 2), 'utf8');
29
- const content = fs.readFileSync(path.join(__dirname, '/benchmark/base', jsFile), 'utf8');
30
- const actual = parse(content,{sourcefile :path.join(__dirname, '/benchmark/base', jsFile),language:"javascript"});
31
- // console.log(JSON.stringify(parsed));
32
- // const filePath = path.join(__dirname, '/benchmark/base', jsFile + '.json');
33
- // fs.writeFile(filePath, JSON.stringify(actual,null,2), (err) => {
34
- // if (err) {
35
- // console.error('写入文件时发生错误:', err);
36
- // return;
37
- // }
38
- // console.log('文件已成功写入:', filePath);
39
- // });
40
- const baselineJson = fs.readFileSync(path.join(__dirname, '/benchmark/base', jsFile + '.json'), 'utf8')
41
- // const baseline = JSON.parse(jsonContent);
42
- // assert.ok(deepEqual(parsed, baseline), jsFile);
43
-
44
- assert.strictEqual(JSON.stringify(actual,null,2),baselineJson,jsFile)
45
- });
19
+ for (const key in cloned) {
20
+ if (Object.prototype.hasOwnProperty.call(cloned, key)) {
21
+ // 替换 sourcefile 为当前环境的真实路径
22
+ if (key === 'sourcefile' && typeof cloned[key] === 'string') {
23
+ cloned[key] = currentSourceFile;
24
+ }
25
+ // 替换 version 为当前版本
26
+ else if (key === 'version' && typeof cloned[key] === 'string') {
27
+ cloned[key] = currentVersion;
28
+ }
29
+ // 删除空的 uri 字段(可选)
30
+ else if (key === 'uri' && cloned[key] === '') {
31
+ delete cloned[key];
32
+ }
33
+ // 可选:删除 _meta._extra.start/end(避免字节偏移差异)
34
+ else if (key === '_meta' && cloned[key]?.['_extra']?.hasOwnProperty('start')) {
35
+ const extra = { ...cloned[key]['_extra'] };
36
+ delete extra.start;
37
+ delete extra.end;
38
+ cloned[key] = { ...cloned[key], _extra: extra };
39
+ }
40
+ // 递归处理子节点
41
+ else {
42
+ cloned[key] = normalizeAst(cloned[key], currentSourceFile, currentVersion);
43
+ }
46
44
  }
47
- });
45
+ }
46
+ return cloned;
47
+ }
48
48
 
49
- function deepEqual(obj1: any, obj2: any): boolean {
50
- if (obj1 === obj2) return true
51
-
52
- if ((typeof obj1 === 'object' && obj1 !== null) && (typeof obj2 === 'object' && obj2 !== null)) {
53
- if (Object.keys(obj1).length !== Object.keys(obj2).length)
54
- return false;
55
-
56
- for (const prop in obj1) {
57
- if (Object.prototype.hasOwnProperty.call(obj2, prop)) {
58
- if (!deepEqual(obj1[prop], obj2[prop]))
59
- return false;
60
- }
61
- }
62
- return true;
63
- } else {
64
- return false;
65
- }
49
+ // 用于开发者更新 baseline(会写入当前版本)
50
+ function refreshUastJson() {
51
+ const BASE_DIR = path.join(__dirname, 'benchmark', 'base');
52
+ const jsFiles = globby.sync('*.js', { cwd: BASE_DIR });
53
+ const currentVersion = getCurrentVersion();
54
+
55
+ console.log(`🔄 Refreshing UAST baselines with version: ${currentVersion}`);
56
+
57
+ for (const file of jsFiles) {
58
+ const fullPath = path.join(BASE_DIR, file);
59
+ const content = fs.readFileSync(fullPath, 'utf8');
60
+ const ast = parse(content, { sourcefile: fullPath, language: 'javascript' });
61
+
62
+ // ✅ 写入当前版本
63
+ ast.version = currentVersion;
64
+
65
+ fs.writeFileSync(`${fullPath}.json`, JSON.stringify(ast, null, 2), 'utf8');
66
+ console.log(`✅ Updated baseline: ${file}.json`);
67
+ }
68
+
69
+ console.log('✅ All baselines updated.');
66
70
  }
71
+
72
+ // 检查是否是刷新模式
73
+ function shouldRefresh(): boolean {
74
+ return process.argv.includes('--refresh') || process.argv.includes('-r');
75
+ }
76
+
77
+ // 主测试逻辑
78
+ describe('benchmark for javascript', () => {
79
+ const BASE_DIR = path.join(__dirname, 'benchmark', 'base');
80
+ const jsFiles = globby.sync('*.js', { cwd: BASE_DIR });
81
+ const currentVersion = getCurrentVersion();
82
+
83
+ // 如果是刷新模式,只更新 baseline
84
+ if (shouldRefresh()) {
85
+ before(() => {
86
+ refreshUastJson();
87
+ process.exit(0);
88
+ });
89
+ return;
90
+ }
91
+
92
+ // 正常测试流程
93
+ for (const jsFile of jsFiles) {
94
+ it(jsFile, () => {
95
+ const fullPath = path.join(BASE_DIR, jsFile);
96
+ const content = fs.readFileSync(fullPath, 'utf8');
97
+
98
+ // 实际解析结果
99
+ const actual = parse(content, {
100
+ sourcefile: fullPath,
101
+ language: 'javascript',
102
+ });
103
+
104
+ // 读取 baseline
105
+ const baselinePath = `${fullPath}.json`;
106
+ let expected: any;
107
+ try {
108
+ const baselineRaw = fs.readFileSync(baselinePath, 'utf8');
109
+ expected = JSON.parse(baselineRaw);
110
+ } catch (err) {
111
+ assert.fail(`❌ Failed to read baseline ${baselinePath}: ${err}`);
112
+ }
113
+
114
+ // ✅ 标准化 baseline:替换 sourcefile 和 version
115
+ const normalizedExpected = normalizeAst(expected, fullPath, currentVersion);
116
+
117
+ // 字符串化对比(格式化一致)
118
+ const actualStr = JSON.stringify(actual, null, 2);
119
+ const expectedStr = JSON.stringify(normalizedExpected, null, 2);
120
+
121
+ // 断言
122
+ assert.strictEqual(
123
+ actualStr,
124
+ expectedStr,
125
+ `UAST mismatch in ${jsFile}\n\n👉 Run with --refresh to update baselines if changes are expected.`
126
+ );
127
+ });
128
+ }
129
+ });