Skip to content

Commit

Permalink
feat: include and exclude rules
Browse files Browse the repository at this point in the history
  • Loading branch information
chibing.fy committed Mar 2, 2022
1 parent 4147bdc commit e12fcf9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
19 changes: 15 additions & 4 deletions packages/gogocode-plugin-vue/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,26 @@ gogocode -s ./src -t gogocode-plugin-vue -o ./src-out
```
转换操作执行完毕后新的Vue3代码会被写入到src-out目录中。

## 应用单一规则
## 指定应用规则

添加参数 `-p rule=xxx`,举例使用 `v-model` 规则:
添加参数 `-p include-rules=xxx,yyy`,举例只应用 `vModel``customDirectives` 规则:

```bash
gogocode -s ./src -t gogocode-plugin-vue -o ./src-out -p rule=v-model
gogocode -s ./src -t gogocode-plugin-vue -o ./src-out -p include-rules=vModel,customDirectives
```

全部规则名可在 `src/rules.js` 中查看
规则名称请参看 [rules.js](./src/rules.js) 里面的 `name` 字段。

## 排除应用规则


添加参数 `-p exclude-rules=xxx,yyy`,举例只排除 `vModel``customDirectives` 规则:

```bash
gogocode -s ./src -t gogocode-plugin-vue -o ./src-out -p exclude-rules=vModel,customDirectives
```

规则名称请参看 [rules.js](./src/rules.js) 里面的 `name` 字段。
## 依赖升级
除了升级源码,我们还需要升级 Vue3 相关依赖,这一点也可以自动完成,在终端(terminal)中跳转到需要升级的Vue项目路径,执行如下命令:
```bash
Expand Down
11 changes: 5 additions & 6 deletions packages/gogocode-plugin-vue/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ const transform = function (fileInfo, api, options) {
: /\.vue$/.test(fileInfo.path)
? $(sourceCode, { parseOptions: { language: 'vue' } })
: $(sourceCode);

const includeRules = options['include-rules'] ? options['include-rules'].split(',') : rules.map(r => r.name);
const excludeRules = options['exclude-rules'] ? options['exclude-rules'].split(',') : [];

const rulesToBeApplied = options.rule ? rules.filter(rule => rule.name === options.rule) : rules
const rulesToBeApplied = rules.filter(r => includeRules.includes(r.name) && !excludeRules.includes(r.name));

if(!rulesToBeApplied.length) {
throw Error(`rule:${options.rule} not found`);
}

if(options.rule) {
console.log(`applying rule: ${options.rule}`);
throw Error(`No valid rule found.`);
}

const outAst = rulesToBeApplied.reduce((ast, ruleCfg) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/gogocode-plugin-vue/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gogocode-plugin-vue",
"version": "1.0.27",
"version": "1.0.28",
"description": "",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit e12fcf9

Please sign in to comment.