auto-cr-rules 1.0.0 → 1.0.2
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/package.json
CHANGED
package/src/types/global.d.ts
CHANGED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = default_1;
|
|
4
|
-
/**
|
|
5
|
-
* 检查相对路径的深度,如果超过最大允许深度则报告错误
|
|
6
|
-
* @param sourceValue - 导入源路径
|
|
7
|
-
* @param path - Babel AST path 对象
|
|
8
|
-
* @param report - 报告函数
|
|
9
|
-
* @param maxDepth - 最大允许的深度
|
|
10
|
-
*/
|
|
11
|
-
function checkRelativePath(sourceValue, path, maxDepth, report) {
|
|
12
|
-
// 只检查相对路径
|
|
13
|
-
if (sourceValue.startsWith('.')) {
|
|
14
|
-
// 计算路径深度:通过计算 "../" 的出现次数
|
|
15
|
-
const depth = (sourceValue.match(/\.\.\//g) || []).length;
|
|
16
|
-
if (depth > maxDepth) {
|
|
17
|
-
report(path, `导入路径 "${sourceValue}" 不能超过最大层级${maxDepth}`);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
// 导出Babel插件函数
|
|
22
|
-
function default_1(api, options) {
|
|
23
|
-
const { types: t } = api;
|
|
24
|
-
const maxDepth = 2;
|
|
25
|
-
// 从插件参数中获取 report 方法,如果没有提供则使用默认的 console.error
|
|
26
|
-
const { report } = options;
|
|
27
|
-
return {
|
|
28
|
-
name: 'no-deep-relative-imports',
|
|
29
|
-
visitor: {
|
|
30
|
-
// 处理静态导入语句
|
|
31
|
-
ImportDeclaration(path) {
|
|
32
|
-
const sourceValue = path.node.source.value;
|
|
33
|
-
if (typeof sourceValue === 'string') {
|
|
34
|
-
checkRelativePath(sourceValue, path, maxDepth, report);
|
|
35
|
-
}
|
|
36
|
-
},
|
|
37
|
-
// 处理动态 import() 语句和 require() 调用
|
|
38
|
-
CallExpression(path) {
|
|
39
|
-
// 检查是否是 import() 调用
|
|
40
|
-
if (t.isImport(path.node.callee)) {
|
|
41
|
-
const firstArg = path.node.arguments[0];
|
|
42
|
-
// 确保第一个参数是字符串字面量
|
|
43
|
-
if (firstArg && t.isStringLiteral(firstArg)) {
|
|
44
|
-
const sourceValue = firstArg.value;
|
|
45
|
-
checkRelativePath(sourceValue, path, maxDepth, report);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
// 检查是否是 require() 调用
|
|
49
|
-
else if (t.isIdentifier(path.node.callee, { name: 'require' }) ||
|
|
50
|
-
(t.isMemberExpression(path.node.callee) &&
|
|
51
|
-
t.isIdentifier(path.node.callee.object, { name: 'require' }))) {
|
|
52
|
-
const firstArg = path.node.arguments[0];
|
|
53
|
-
// 确保第一个参数是字符串字面量
|
|
54
|
-
if (firstArg && t.isStringLiteral(firstArg)) {
|
|
55
|
-
const sourceValue = firstArg.value;
|
|
56
|
-
checkRelativePath(sourceValue, path, maxDepth, report);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
},
|
|
60
|
-
},
|
|
61
|
-
};
|
|
62
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import BabelCore from '@babel/core';
|
|
2
|
-
import type { CallExpression, ImportDeclaration } from '@babel/types';
|
|
3
|
-
import type { NodePath } from '@babel/traverse';
|
|
4
|
-
export default function (api: typeof BabelCore, options: PluginOptions): {
|
|
5
|
-
name: string;
|
|
6
|
-
visitor: {
|
|
7
|
-
ImportDeclaration(path: NodePath<ImportDeclaration>): void;
|
|
8
|
-
CallExpression(path: NodePath<CallExpression>): void;
|
|
9
|
-
};
|
|
10
|
-
};
|