auto-cr-cmd 2.0.105 → 2.0.106

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.
@@ -56,7 +56,8 @@ function createReporter(filePath, source, options) {
56
56
  optimizing: 0,
57
57
  };
58
58
  var pushRecord = function (record) {
59
- records.push(record);
59
+ var _a;
60
+ records.push(__assign(__assign({}, record), { filePath: (_a = record.filePath) !== null && _a !== void 0 ? _a : filePath }));
60
61
  totalViolations += 1;
61
62
  if (record.severity === auto_cr_rules_1.RuleSeverity.Error) {
62
63
  errorViolations += 1;
@@ -201,11 +202,12 @@ function renderCompactViolations(filePath, violations) {
201
202
  var indent = ' ';
202
203
  var bulletIndent = ' ';
203
204
  violations.forEach(function (violation) {
204
- var _a, _b;
205
+ var _a, _b, _c;
205
206
  var severityLabel = (_a = SEVERITY_LABELS[violation.severity]) !== null && _a !== void 0 ? _a : 'error';
206
207
  var color = useColor ? (_b = SEVERITY_COLORS[violation.severity]) !== null && _b !== void 0 ? _b : '' : '';
207
208
  var reset = useColor ? RESET_COLOR : '';
208
- var location = typeof violation.line === 'number' ? "".concat(filePath, ":").concat(violation.line) : "".concat(filePath, ":-");
209
+ var locationPath = (_c = violation.filePath) !== null && _c !== void 0 ? _c : filePath;
210
+ var location = typeof violation.line === 'number' ? "".concat(locationPath, ":").concat(violation.line) : "".concat(locationPath, ":-");
209
211
  var codeText = violation.code ? compactText(violation.code) : undefined;
210
212
  var message = stripRedundantCodeSuffix(compactText(violation.message), codeText);
211
213
  var header = "".concat(prefix).concat(color, "[").concat(severityLabel, "] ").concat(location, " ").concat(message).concat(reset);
@@ -106,22 +106,23 @@ function analyzeFile(file, rules, format, log, createRuleContext, reporterHooks)
106
106
  helpers = __assign(__assign({}, sharedHelpers), { reportViolation: (function (input, span) {
107
107
  // 统一把规则输出收敛成结构化数据,避免各规则实现重复分支。
108
108
  var normalized = normalizeViolationInput(input, span);
109
+ var resolvedLine = resolveLineForViolation(baseContext.source, baseContext.sourceIndex, normalized);
109
110
  if (typeof reporterWithRecord_1.record === 'function') {
110
111
  reporterWithRecord_1.record({
111
112
  description: normalized.message,
112
113
  code: normalized.code,
113
114
  suggestions: normalized.suggestions,
114
115
  span: normalized.span,
115
- line: normalized.line,
116
+ line: resolvedLine,
116
117
  });
117
118
  return;
118
119
  }
119
- if (normalized.span) {
120
- scopedReporter_1.errorAtSpan(normalized.span, normalized.message);
120
+ if (resolvedLine !== undefined) {
121
+ scopedReporter_1.errorAtLine(resolvedLine, normalized.message);
121
122
  return;
122
123
  }
123
- if (typeof normalized.line === 'number') {
124
- scopedReporter_1.errorAtLine(normalized.line, normalized.message);
124
+ if (normalized.span) {
125
+ scopedReporter_1.errorAtSpan(normalized.span, normalized.message);
125
126
  return;
126
127
  }
127
128
  scopedReporter_1.error(normalized.message);
@@ -217,3 +218,78 @@ function normalizeViolationInput(input, spanArg) {
217
218
  span: spanArg,
218
219
  };
219
220
  }
221
+ var resolveLineForViolation = function (source, sourceIndex, violation) {
222
+ if (typeof violation.line === 'number' && Number.isFinite(violation.line)) {
223
+ return violation.line;
224
+ }
225
+ var span = extractSpan(violation.span);
226
+ if (!span) {
227
+ return undefined;
228
+ }
229
+ var line = resolveLineFromByteOffset(source, sourceIndex, span.start);
230
+ return Number.isFinite(line) ? line : undefined;
231
+ };
232
+ var extractSpan = function (spanLike) {
233
+ if (!spanLike) {
234
+ return undefined;
235
+ }
236
+ if (typeof spanLike === 'object' && 'span' in spanLike) {
237
+ return spanLike.span;
238
+ }
239
+ return spanLike;
240
+ };
241
+ var resolveLineFromByteOffset = function (source, index, byteOffset) {
242
+ var charIndex = bytePosToCharIndex(source, index.moduleStart, byteOffset);
243
+ return resolveLine(index.lineOffsets, charIndex);
244
+ };
245
+ var resolveLine = function (lineOffsets, position) {
246
+ var low = 0;
247
+ var high = lineOffsets.length - 1;
248
+ while (low <= high) {
249
+ var mid = Math.floor((low + high) / 2);
250
+ var current = lineOffsets[mid];
251
+ if (current === position) {
252
+ return mid + 1;
253
+ }
254
+ if (current < position) {
255
+ low = mid + 1;
256
+ }
257
+ else {
258
+ high = mid - 1;
259
+ }
260
+ }
261
+ return high + 1;
262
+ };
263
+ var bytePosToCharIndex = function (source, moduleStart, bytePos) {
264
+ var target = Math.max(bytePos - moduleStart, 0);
265
+ if (target === 0) {
266
+ return 0;
267
+ }
268
+ var index = 0;
269
+ var byteOffset = 0;
270
+ while (index < source.length) {
271
+ var code = source.charCodeAt(index);
272
+ var _a = readUtf8Character(source, index, code), bytes = _a.bytes, nextIndex = _a.nextIndex;
273
+ if (byteOffset + bytes > target) {
274
+ return index;
275
+ }
276
+ byteOffset += bytes;
277
+ index = nextIndex;
278
+ }
279
+ return source.length;
280
+ };
281
+ var readUtf8Character = function (source, index, code) {
282
+ if (code <= 0x7f) {
283
+ return { bytes: 1, nextIndex: index + 1 };
284
+ }
285
+ if (code <= 0x7ff) {
286
+ return { bytes: 2, nextIndex: index + 1 };
287
+ }
288
+ if (code >= 0xd800 && code <= 0xdbff && index + 1 < source.length) {
289
+ var next = source.charCodeAt(index + 1);
290
+ if (next >= 0xdc00 && next <= 0xdfff) {
291
+ return { bytes: 4, nextIndex: index + 2 };
292
+ }
293
+ }
294
+ return { bytes: 3, nextIndex: index + 1 };
295
+ };
@@ -34,6 +34,7 @@ export interface ViolationRecord {
34
34
  tag: string;
35
35
  ruleName: string;
36
36
  severity: Severity;
37
+ filePath?: string;
37
38
  message: string;
38
39
  line?: number;
39
40
  code?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "auto-cr-cmd",
3
- "version": "2.0.105",
3
+ "version": "2.0.106",
4
4
  "description": "Fast automated code review CLI powered by SWC-based static analysis",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/types/index.d.ts",
@@ -39,7 +39,7 @@
39
39
  "consola": "^3.4.2",
40
40
  "jsonc-parser": "^3.3.1",
41
41
  "picomatch": "^4.0.3",
42
- "auto-cr-rules": "2.0.105"
42
+ "auto-cr-rules": "2.0.106"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@eslint/js": "^9.39.2",