auto-cr-rules 2.0.105 → 2.0.107

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
@@ -43,7 +43,7 @@ Common flags:
43
43
  - `--language <zh|en>`: Switch CLI output language (defaults to auto-detection).
44
44
  - `--rule-dir <directory>`: Load additional custom rules from a directory or package.
45
45
  - `--output <text|json>`: Choose between human-friendly text logs or structured JSON results (defaults to `text`).
46
- - `--progress [tty-only|yes|no]`: Progress mode (text output only, default `tty-only`); output goes to `stderr`.
46
+ - `--progress [tty-only|yes|no]`: Progress mode (text output only, default `no`); output goes to `stderr`.
47
47
  - `--config <path>`: Point to a `.autocrrc.json` or `.autocrrc.js` file to enable/disable rules.
48
48
  - `--ignore-path <path>`: Point to a `.autocrignore.json` or `.autocrignore.js` file to exclude files/directories from scanning.
49
49
  - `--tsconfig <path>`: Use a custom `tsconfig.json` (defaults to `<cwd>/tsconfig.json`).
@@ -865,6 +865,7 @@ var readFileSafe = function (filePath) {
865
865
  // 用正则匹配常见的导入写法,覆盖 import / dynamic import / require / export-from。
866
866
  var extractImportSpecifiers = function (source) {
867
867
  var results = [];
868
+ var sanitized = stripJsComments(source);
868
869
  var patterns = [
869
870
  /import\s+(?:[^'"]+\s+from\s+)?['"]([^'"]+)['"]/g,
870
871
  /import\s*\(\s*['"]([^'"]+)['"]\s*\)/g,
@@ -874,12 +875,71 @@ var extractImportSpecifiers = function (source) {
874
875
  for (var _i = 0, patterns_2 = patterns; _i < patterns_2.length; _i++) {
875
876
  var pattern = patterns_2[_i];
876
877
  var match = void 0;
877
- while ((match = pattern.exec(source)) !== null) {
878
+ while ((match = pattern.exec(sanitized)) !== null) {
878
879
  results.push(match[1]);
879
880
  }
880
881
  }
881
882
  return results;
882
883
  };
884
+ // 轻量移除 JS/TS 注释,避免注释里的 import/require 被正则误判。
885
+ var stripJsComments = function (content) {
886
+ var result = '';
887
+ var inString = false;
888
+ var stringChar = '';
889
+ var inLineComment = false;
890
+ var inBlockComment = false;
891
+ var escaped = false;
892
+ for (var index = 0; index < content.length; index += 1) {
893
+ var char = content[index];
894
+ var next = content[index + 1];
895
+ if (inLineComment) {
896
+ if (char === '\n') {
897
+ inLineComment = false;
898
+ result += char;
899
+ }
900
+ continue;
901
+ }
902
+ if (inBlockComment) {
903
+ if (char === '*' && next === '/') {
904
+ inBlockComment = false;
905
+ index += 1;
906
+ result += ' ';
907
+ }
908
+ else if (char === '\n') {
909
+ result += char;
910
+ }
911
+ continue;
912
+ }
913
+ if (inString) {
914
+ result += char;
915
+ if (!escaped && char === stringChar) {
916
+ inString = false;
917
+ stringChar = '';
918
+ }
919
+ escaped = !escaped && char === '\\';
920
+ continue;
921
+ }
922
+ if (char === '"' || char === "'" || char === '`') {
923
+ inString = true;
924
+ stringChar = char;
925
+ result += char;
926
+ continue;
927
+ }
928
+ if (char === '/' && next === '/') {
929
+ inLineComment = true;
930
+ index += 1;
931
+ continue;
932
+ }
933
+ if (char === '/' && next === '*') {
934
+ inBlockComment = true;
935
+ index += 1;
936
+ result += ' ';
937
+ continue;
938
+ }
939
+ result += char;
940
+ }
941
+ return result;
942
+ };
883
943
  // 解析相对路径到真实文件:支持自动补全扩展名与目录 index。
884
944
  // 并确保解析结果在项目根目录内。
885
945
  var resolveImportFile = function (fromFile, specifier, root) {
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.noN2ArrayLookup = void 0;
4
+ var sourceIndex_1 = require("../sourceIndex");
4
5
  var types_1 = require("../types");
5
6
  // 线性查找方法集合:filter/some/every 也按线性查找处理。
6
7
  var LINEAR_LOOKUP_METHODS = new Set([
@@ -16,7 +17,7 @@ var LINEAR_LOOKUP_METHODS = new Set([
16
17
  // 检测热路径中对数组进行线性查找的调用,避免潜在 O(n^2) 访问。
17
18
  // 不强制校验 receiver 是否真实数组(静态分析阶段难以确定)。
18
19
  exports.noN2ArrayLookup = (0, types_1.defineRule)('no-n2-array-lookup', { tag: 'performance', severity: types_1.RuleSeverity.Optimizing }, function (_a) {
19
- var analysis = _a.analysis, helpers = _a.helpers, language = _a.language, messages = _a.messages;
20
+ var analysis = _a.analysis, helpers = _a.helpers, language = _a.language, messages = _a.messages, source = _a.source, sourceIndex = _a.sourceIndex;
20
21
  var suggestions = language === 'zh'
21
22
  ? [
22
23
  { text: '预先构建 Map/Set 进行 O(1) 查找。' },
@@ -33,10 +34,14 @@ exports.noN2ArrayLookup = (0, types_1.defineRule)('no-n2-array-lookup', { tag: '
33
34
  if (!method || !LINEAR_LOOKUP_METHODS.has(method)) {
34
35
  continue;
35
36
  }
37
+ var line = callExpression.span
38
+ ? (0, sourceIndex_1.resolveLineFromByteOffset)(source, sourceIndex, callExpression.span.start)
39
+ : undefined;
36
40
  helpers.reportViolation({
37
41
  description: messages.noN2ArrayLookup({ method: method }),
38
42
  code: method,
39
43
  suggestions: suggestions,
44
+ line: line,
40
45
  span: callExpression.span,
41
46
  }, callExpression.span);
42
47
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "auto-cr-rules",
3
- "version": "2.0.105",
3
+ "version": "2.0.107",
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",