-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathindex.js
65 lines (57 loc) · 2.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const rules = require('./src/rules');
const prettier = require('prettier');
/**
* 转换入口导出一个函数,按照如下函数签名
* @param {*} fileInfo 包含 source 和 path 属性
* @param {*} api 包含 gogocode 作为转换工具
* @param {*} options 其他 option 由此传入
* @returns {string} 返回转换后的代码
*/
const transform = function (fileInfo, api, options) {
const sourceCode = fileInfo.source;
const $ = api.gogocode;
if (
!/\.vue$|\.js$|\.ts$|\.json$/.test(fileInfo.path) ||
/node_modules/.test(fileInfo.path)
) {
return sourceCode;
}
const ast = /\.json$/.test(fileInfo.path)
? sourceCode
: /\.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 = rules.filter(r => includeRules.includes(r.name) && !excludeRules.includes(r.name));
if(!rulesToBeApplied.length) {
throw Error(`No valid rule found.`);
}
const outAst = rulesToBeApplied.reduce((ast, ruleCfg) => {
if (!ruleCfg.test.test(fileInfo.path)) {
return ast;
}
try {
return ruleCfg.rule(ast, api, { ...options, filePath: fileInfo.path });
} catch (error) {
console.log(
`文件转换异常,规则:${ruleCfg.name},文件:${fileInfo.path}`,
error
);
return ast;
}
}, ast);
// 命令行的params参数没有配置format=true 则默认格式化代码,如果命令行的params配置了format参数则使用该配置
const format = options.format === undefined || options.format === true;
return /\.json$/.test(fileInfo.path)
? outAst
: (format ? prettier.format(outAst.generate(), {
trailingComma: 'es5',
tabWidth: 2,
semi: false,
singleQuote: true,
printWidth: 80,
parser: /\.vue$/.test(fileInfo.path) ? 'vue' : 'typescript',
}) : outAst.generate());
};
module.exports = { transform };