auto-cr-rules 2.0.78 → 2.0.79

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
@@ -22,7 +22,7 @@
22
22
 
23
23
  ## Feature Highlights (Automated Code Review & Static Analysis)
24
24
 
25
- - **Built-in Rule Library**: Ships with SWC AST static analysis rules out of the box, such as `no-deep-relative-imports`.
25
+ - **Built-in Rule Library**: Ships with SWC AST static analysis rules out of the box, such as `no-deep-relative-imports`, `no-circular-dependencies`, and `no-swallowed-errors`.
26
26
  - **Extensible SDK**: `auto-cr-rules` exposes helpers like `defineRule` and `helpers.imports`, reducing the friction of authoring custom TypeScript / JavaScript rules.
27
27
  - **Workspace Friendly**: Manage both the CLI and rule package via pnpm workspaces and validate the full pipeline with a single build.
28
28
  - **Publishing Toolkit**: Version bump scripts and npm publish commands keep both packages in sync.
@@ -121,6 +121,7 @@ npx auto-cr-cmd --output json -- ./src | jq
121
121
  {
122
122
  "rules": {
123
123
  "no-deep-relative-imports": "error",
124
+ "no-circular-dependencies": "warning",
124
125
  "no-swallowed-errors": "off"
125
126
  }
126
127
  }
package/README.zh-CN.md CHANGED
@@ -22,7 +22,7 @@
22
22
 
23
23
  ## 特性亮点(自动化代码审查 & 静态代码分析)
24
24
 
25
- - **内置规则库**:默认集成 SWC AST 静态分析规则,例如 `no-deep-relative-imports`。
25
+ - **内置规则库**:默认集成 SWC AST 静态分析规则,例如 `no-deep-relative-imports`、`no-circular-dependencies`、`no-swallowed-errors`。
26
26
  - **可扩展 SDK**:`auto-cr-rules` 暴露 `defineRule`、`helpers.imports` 等工具,降低编写 TypeScript / JavaScript 自定义规则的复杂度。
27
27
  - **工作区管理**:使用 pnpm workspace 同时管理 CLI 与规则包,一次构建即可验证完整流程。
28
28
  - **发布友好**:内置版本递增脚本与 npm 发布命令,保持两个包的版本同步。
@@ -121,6 +121,7 @@ npx auto-cr-cmd --output json -- ./src | jq
121
121
  {
122
122
  "rules": {
123
123
  "no-deep-relative-imports": "error",
124
+ "no-circular-dependencies": "warning",
124
125
  "no-swallowed-errors": "off"
125
126
  }
126
127
  }
package/dist/imports.js CHANGED
@@ -10,6 +10,7 @@ var isSwcNode = function (value) {
10
10
  }
11
11
  return typeof value.type === 'string';
12
12
  };
13
+ // 递归遍历 AST,遇到带 type 的节点就回调。
13
14
  var traverse = function (value, visitor) {
14
15
  if (Array.isArray(value)) {
15
16
  for (var _i = 0, value_1 = value; _i < value_1.length; _i++) {
@@ -34,6 +35,7 @@ var traverse = function (value, visitor) {
34
35
  };
35
36
  var isImportDeclaration = function (node) { return node.type === 'ImportDeclaration'; };
36
37
  var isCallExpression = function (node) { return node.type === 'CallExpression'; };
38
+ // 收集模块中的所有导入引用(包含动态 import 与 require)。
37
39
  var collectImportReferences = function (module) {
38
40
  var results = [];
39
41
  traverse(module, function (node) {
@@ -55,6 +57,7 @@ var collectImportReferences = function (module) {
55
57
  return results;
56
58
  };
57
59
  exports.collectImportReferences = collectImportReferences;
60
+ // 从调用表达式中提取 import/require 的字符串字面量参数。
58
61
  var extractFromCallExpression = function (node) {
59
62
  if (!node.arguments.length) {
60
63
  return null;
package/dist/messages.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createRuleMessages = void 0;
4
+ // 规则文案多语言表(由 rules 通过 messages 接口访问)。
4
5
  var ruleTranslations = {
5
6
  zh: {
6
7
  noDeepRelativeImports: function (_a) {
@@ -25,7 +26,9 @@ var ruleTranslations = {
25
26
  },
26
27
  },
27
28
  };
29
+ // 固定文案对象,避免运行期被意外修改。
28
30
  Object.values(ruleTranslations).forEach(function (messages) { return Object.freeze(messages); });
31
+ // 根据语言返回对应的规则文案实现。
29
32
  var createRuleMessages = function (language) {
30
33
  var _a;
31
34
  return (_a = ruleTranslations[language]) !== null && _a !== void 0 ? _a : ruleTranslations.zh;
@@ -7,4 +7,5 @@ var noCircularDependencies_1 = require("./noCircularDependencies");
7
7
  Object.defineProperty(exports, "noCircularDependencies", { enumerable: true, get: function () { return noCircularDependencies_1.noCircularDependencies; } });
8
8
  var noSwallowedErrors_1 = require("./noSwallowedErrors");
9
9
  Object.defineProperty(exports, "noSwallowedErrors", { enumerable: true, get: function () { return noSwallowedErrors_1.noSwallowedErrors; } });
10
+ // 内置规则列表,按默认顺序执行。
10
11
  exports.builtinRules = [noDeepRelativeImports_1.noDeepRelativeImports, noCircularDependencies_1.noCircularDependencies, noSwallowedErrors_1.noSwallowedErrors];
@@ -397,6 +397,11 @@ var selectLineNumber = function (computed, fallback) {
397
397
  if (computed === undefined) {
398
398
  return fallback;
399
399
  }
400
+ // 当两者只差一行时,优先使用文本匹配结果,避免出现 +1 的偏移问题。
401
+ if (Math.abs(computed - fallback) <= 1) {
402
+ return fallback;
403
+ }
404
+ // 若 span 指向了更早的注释块,则回退到更靠后的文本行。
400
405
  if (computed < fallback) {
401
406
  return fallback;
402
407
  }
@@ -3,10 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.noDeepRelativeImports = void 0;
4
4
  var types_1 = require("../types");
5
5
  var MAX_DEPTH = 2;
6
+ // 检测相对路径过深的导入,避免维护困难与重构风险。
6
7
  exports.noDeepRelativeImports = (0, types_1.defineRule)('no-deep-relative-imports', { tag: 'base', severity: types_1.RuleSeverity.Warning }, function (_a) {
7
8
  var _b, _c;
8
9
  var ast = _a.ast, helpers = _a.helpers, messages = _a.messages, language = _a.language, source = _a.source;
9
- // Build a per-file line index so we can convert byte offsets emitted by SWC back to line numbers.
10
+ // 构建行号索引,方便将 SWC byte offset 转为行号。
10
11
  var moduleStart = (_c = (_b = ast.span) === null || _b === void 0 ? void 0 : _b.start) !== null && _c !== void 0 ? _c : 0;
11
12
  var lineIndex = buildLineIndex(source);
12
13
  for (var _i = 0, _d = helpers.imports; _i < _d.length; _i++) {
@@ -26,11 +27,12 @@ exports.noDeepRelativeImports = (0, types_1.defineRule)('no-deep-relative-import
26
27
  { text: 'Use a path alias (for example: @shared/deep/utils).' },
27
28
  { text: 'Create an index file at a higher level to re-export the module and shorten the import.' },
28
29
  ];
30
+ // 优先使用 span 计算行号,若异常则退回到文本匹配。
29
31
  var computedLine = reference.span
30
32
  ? resolveLine(lineIndex, bytePosToCharIndex(source, moduleStart, reference.span.start))
31
33
  : undefined;
32
34
  var fallbackLine = findImportLine(source, reference.value);
33
- // Prefer the larger value so we never point at leading block comments when the byte offset is truncated.
35
+ // 取更大的行号,避免 span 截断时指向注释块。
34
36
  var line = selectLineNumber(computedLine, fallbackLine);
35
37
  helpers.reportViolation({
36
38
  description: description,
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.noSwallowedErrors = void 0;
4
4
  var types_1 = require("../types");
5
+ // 检测 try/catch/finally 中未处理任何可执行语句的情况(吞掉异常)。
5
6
  exports.noSwallowedErrors = (0, types_1.defineRule)('no-swallowed-errors', { tag: 'base', severity: types_1.RuleSeverity.Warning }, function (_a) {
6
7
  var _b, _c;
7
8
  var ast = _a.ast, helpers = _a.helpers, messages = _a.messages, source = _a.source;
@@ -13,9 +14,11 @@ exports.noSwallowedErrors = (0, types_1.defineRule)('no-swallowed-errors', { tag
13
14
  var finallyBlock = (_c = tryStatement.finalizer) !== null && _c !== void 0 ? _c : null;
14
15
  var catchHasExecutable = catchBlock ? hasExecutableStatements(catchBlock.stmts) : false;
15
16
  var finallyHasExecutable = finallyBlock ? hasExecutableStatements(finallyBlock.stmts) : false;
17
+ // 任意一段有真实逻辑,则认为异常被处理或至少被记录。
16
18
  if (catchHasExecutable || finallyHasExecutable) {
17
19
  return;
18
20
  }
21
+ // 尽量指向 catch/finally 块本身,保证定位直观。
19
22
  var reportSpan = (_e = (_d = catchBlock === null || catchBlock === void 0 ? void 0 : catchBlock.span) !== null && _d !== void 0 ? _d : finallyBlock === null || finallyBlock === void 0 ? void 0 : finallyBlock.span) !== null && _e !== void 0 ? _e : tryStatement.span;
20
23
  var charIndex = bytePosToCharIndex(source, moduleStart, reportSpan.start);
21
24
  var computedLine = resolveLine(lineIndex, charIndex);
package/dist/runtime.js CHANGED
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createRuleContext = void 0;
4
4
  var imports_1 = require("./imports");
5
5
  var messages_1 = require("./messages");
6
+ // 构建规则执行所需的上下文:包含 AST、imports、文案与统一的 helper 方法。
6
7
  var createRuleContext = function (_a) {
7
8
  var ast = _a.ast, filePath = _a.filePath, source = _a.source, reporter = _a.reporter, language = _a.language;
8
9
  var imports = (0, imports_1.collectImportReferences)(ast);
@@ -19,11 +20,13 @@ var createRuleContext = function (_a) {
19
20
  });
20
21
  };
21
22
  exports.createRuleContext = createRuleContext;
23
+ // 规则 helper:统一封装路径判断、相对深度与违规上报逻辑。
22
24
  var createRuleHelpers = function (reporter, imports) {
23
25
  var isRelativePath = function (value) { return value.startsWith('.'); };
24
26
  var relativeDepth = function (value) {
25
27
  return (value.match(/\.\.\//g) || []).length;
26
28
  };
29
+ // reportViolation 可兼容 string 或结构化对象,并自动选择合适的 reporter 方法。
27
30
  var reportViolation = function (input, spanArg) {
28
31
  var normalized = normalizeViolationInput(input, spanArg);
29
32
  if (typeof reporter.record === 'function') {
@@ -48,6 +51,7 @@ var createRuleHelpers = function (reporter, imports) {
48
51
  reportViolation: reportViolation,
49
52
  };
50
53
  };
54
+ // 统一规则输出结构,便于 reporter 处理 span/line/suggestions。
51
55
  function normalizeViolationInput(input, fallbackSpan) {
52
56
  var _a, _b;
53
57
  if (typeof input === 'string') {
package/dist/types.js CHANGED
@@ -13,6 +13,7 @@ var __assign = (this && this.__assign) || function () {
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
14
  exports.toRule = exports.isRule = exports.RuleSeverity = void 0;
15
15
  exports.defineRule = defineRule;
16
+ // 规则严重级别,决定 reporter 的输出样式与统计。
16
17
  var RuleSeverity;
17
18
  (function (RuleSeverity) {
18
19
  RuleSeverity["Error"] = "error";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "auto-cr-rules",
3
- "version": "2.0.78",
3
+ "version": "2.0.79",
4
4
  "description": "Extensible static analysis rule set for the auto-cr automated code review toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/types/index.d.ts",