Gathering detailed insights and metrics for go-better-code
Gathering detailed insights and metrics for go-better-code
Gathering detailed insights and metrics for go-better-code
Gathering detailed insights and metrics for go-better-code
npm install go-better-code
Typescript
Module System
Node Version
NPM Version
70.7
Supply Chain
97.3
Quality
76.4
Maintenance
100
Vulnerability
98.6
License
JavaScript (76%)
Vue (23.14%)
HTML (0.57%)
Less (0.29%)
Total Downloads
444
Last Day
4
Last Week
8
Last Month
31
Last Year
444
5,732 Stars
474 Commits
446 Forks
52 Watching
8 Branches
19 Contributors
Minified
Minified + Gzipped
Latest Version
1.1.1
Package Id
go-better-code@1.1.1
Unpacked Size
6.10 MB
Size
1.29 MB
File Count
128
NPM Version
9.8.1
Node Version
18.18.2
Publised On
21 Sept 2024
Cumulative downloads
Total Downloads
Last day
300%
4
Compared to previous day
Last week
-11.1%
8
Compared to previous week
Last month
24%
31
Compared to previous month
Last year
0%
444
Compared to previous year
全网最简单易上手,可读性最强的 AST 处理工具!
npm install gogocode
对于下面的代码
1const code = ` 2 const moment = require('moment'); 3 var a = 1; 4 const b = 2; 5 function log (x, y = 'World') { 6 console.log('a') 7 console.log(a, x, y); 8 } 9`;
创建一个 AST 实例
1const $ = require('gogocode'); 2const AST = $(code);
a
变量名替换为 c
,只需要1$(code).replace('a', 'c')
小明改主意了,只想把 var a = 1
里的变量名改为 c
,需要两步:
1$(code).find('var a = 1');
a
变量名替换为 c
,并输出整体代码1$(code) 2 .find('var a = 1') 3 .attr('declarations.0.id.name', 'c') 4 .root() 5 .generate();
这是直接操作AST的方式,有没有更简单的方法呢?有!
1$(code).replace(`var a = 1`, `var c = 1`)
replace确实用起来爽,但当你在分析转换代码时遇到replace覆盖不到的场景时,请用GoGoCode提供的其他api来精准操作AST吧!
a
都改成 c
,只需要将目标语句改一下写成:1$(code).replace(`var a = $_$`, `var c = $_$`)
看到这里,你应该已经理解
find
和replace
的第一参有点类似‘jquery 选择器’,而这里的选择器是你需要查找的代码片段,无论想要匹配多么复杂的代码都可以匹配到,其中$_$
通配符可以匹配任意确定代码,代码选择器及通配符详细介绍 看这里
var
改为 let
,require
改为 import
,他发现用 GoGoCode 真的可以像字符串的 replace 一样简单!1$(code) 2.replace('var $_$1 = $_$2', 'let $_$1 = $_$2'); 3.replace('const $_$1 = require($_$2)', 'import $_$1 from $_$2')
关于如何书写选择器,以及replace详解,请见GoGoCode详细文档
所有的节点获取操作都会返回一个新的AST实例,实例中可能包含多个AST节点路径,如find()
、siblings()
等,某些api返回的实例只会存在一个AST节点路径,如next()
入参 | 说明 | 类型 | 默认值 | |
---|---|---|---|---|
selector | 代码选择器,可以是代码也可以将代码中的部分内容挖空替换为通配符 | string | 无 | |
options | ignoreSequence | 匹配时是否忽略顺序 忽略顺序的情况: {a:$_$} 匹配{b:1, a:2} 需要严格按照顺序匹配的情况: function($_$, b){} 匹配function(a, b){} | boolean | false |
parseOptions | 同构造函数的parseOptions |
当selector中存在 $_$
通配符时,返回的AST实例中存在 match
属性,也就是被 $_$
匹配到的AST节点,按照$_$紧接着的key做聚合
如:$('const a = { key: 1, value: "gogo" }').find('const $_$1 = $_$2')
下图是是 $_$1
和 $_$2
分别匹配到的节点以及对应的输出
获取某个父节点
入参 | 说明 | 类型 | 默认值 |
---|---|---|---|
level | 自内向外第n层父元素 | number | 0 |
获取所有父节
获取根节点,对于js来说是type
为'File'的节点,对于html来说是nodeType
为'document'的节点
通常对AST进行操作之后需要获取root元素之后再输出
获取所有兄弟节点
获取前一个节点
获取当前节点之前的同级节点
获取后一个节点
获取当前节点之后的同级节点
以每一个匹配的元素作为上下文来执行一个函数。
入参 | 说明 | 类型 | 默认值 |
---|---|---|---|
callback | 对于每个匹配的元素所要执行的函数 执行函数时,会给函数传递当前节点 node 和index | function | 无 |
获取当前链式操作中第N个AST对象
入参 | 说明 | 类型 | 默认值 |
---|---|---|---|
index | 需要获取的AST对象的位置 | number | 0 |
获取或修改AST节点的属性,入参可分三种情况:
入参 | 说明 | 类型 | 默认值 | 举例 |
---|---|---|---|---|
attrName | ast节点的属性名称,支持多层属性,通过.连接 | string | 无 | declarations declarations.0.id.name |
入参 | 说明 | 类型 | 举例 |
---|---|---|---|
attrName | ast节点的属性名称,支持多层属性,通过.连接 | string | declarations declarations.0.id.name |
attrValue | 将第一个入参获取到的节点或属性修改为该入参 注意:字符串不会被解析为ast节点而是直接替换原有属性 | node string |
入参 | 类型 | 默认值 | 举例 | |
---|---|---|---|---|
attrMap | attrName | string | 无 | declarations declarations.0.id.name |
attrValue | node | string | 无 |
1AST.attr('init', initNode) 2 3AST.attr({ 4 init: initNode, 5 'program.body.0.params.0.name': 'a' 6}) 7 8AST.attr('program.body.0.params.0.name')
判断是否有某个子节点,返回值为boolean类型
入参同.find()
返回由当前节点深度复制的新节点
在当前节点内部用replacer
替换selector
匹配到的代码,返回当前节点
入参 | 解释 | 类型 | 例 | |
---|---|---|---|---|
selector | 代码选择器,可以是代码也可以将代码中的部分内容挖空替换为通配符 | string | var $_$1 = $_$2 | |
replacer | 替换代码,同代码选择器通配符顺序与selector的保持一致 也可以是确定的ast节点 | string | node | |
options | ignoreSequence | 匹配时是否忽略顺序 | object | 无 |
parseOptions | 解析入参 | object | 无 |
1AST.replace(`Component`, `module.exports = Magix.View.extend`); 2 3AST.replace( 4 `export default function calculateData($_$1){$_$2}`, 5 `function calculateData($_$1){$_$2}` 6) 7 8AST.replace( 9 `navigateToOutside({url: $_$})`, 10 `jm.attachUrlParams($_$)`, 11 options: { ignoreSequence: true } 12)
用replacerAST替换当前节点,返回新节点
入参 | 类型 |
---|---|
replacerAST | AST node string |
在当前节点后面插入一个同级别的节点,返回当前节点
入参 | 类型 |
---|---|
ast | AST node string |
在当前节点前面插入一个同级别的节点,返回当前节点
入参 | 类型 |
---|---|
ast | AST node string |
在当前节点内部某个数组属性的末尾插入一个子节点,返回当前节点
入参 | 类型 |
---|---|
attr | 当前节点的数组属性名称 |
ast | AST node string |
attr
?因为某些节点中多个属性都为数组,如函数,存在入参params和函数体body两个数组子节点,必须通过attr来判断插入节点的位置
1AST 2.find('function $_$() {}') 3.append('params', 'b') 4.prepend('body', 'b = b || 1;')
在当前节点内部某个数组属性的首位插入一个子节点,返回当前节点
清空当前节点所有子节点,返回当前节点
移除当前节点,返回根节点
将AST对象输出为代码
No vulnerabilities found.
Reason
license file detected
Details
Reason
no binaries found in the repo
Reason
Found 3/27 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
148 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-01-20
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More