auto-cr-rules 2.0.19 → 2.0.21

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/dist/messages.js CHANGED
@@ -5,7 +5,7 @@ var ruleTranslations = {
5
5
  zh: {
6
6
  noDeepRelativeImports: function (_a) {
7
7
  var value = _a.value, maxDepth = _a.maxDepth;
8
- return "\u5BFC\u5165\u8DEF\u5F84 \"".concat(value, "\" \u4E0D\u80FD\u8D85\u8FC7\u6700\u5927\u5C42\u7EA7").concat(maxDepth);
8
+ return "\u5BFC\u5165\u8DEF\u5F84 \"".concat(value, "\"\uFF0C\u4E0D\u80FD\u8D85\u8FC7\u6700\u5927\u5C42\u7EA7").concat(maxDepth);
9
9
  },
10
10
  },
11
11
  en: {
@@ -4,7 +4,7 @@ exports.noDeepRelativeImports = void 0;
4
4
  var types_1 = require("../types");
5
5
  var MAX_DEPTH = 2;
6
6
  exports.noDeepRelativeImports = (0, types_1.defineRule)('no-deep-relative-imports', { tag: 'base', severity: types_1.RuleSeverity.Warning }, function (_a) {
7
- var helpers = _a.helpers, messages = _a.messages;
7
+ var helpers = _a.helpers, messages = _a.messages, language = _a.language;
8
8
  for (var _i = 0, _b = helpers.imports; _i < _b.length; _i++) {
9
9
  var reference = _b[_i];
10
10
  if (!helpers.isRelativePath(reference.value)) {
@@ -12,7 +12,22 @@ exports.noDeepRelativeImports = (0, types_1.defineRule)('no-deep-relative-import
12
12
  }
13
13
  var depth = helpers.relativeDepth(reference.value);
14
14
  if (depth > MAX_DEPTH) {
15
- helpers.reportViolation(messages.noDeepRelativeImports({ value: reference.value, maxDepth: MAX_DEPTH }), reference.span);
15
+ var description = messages.noDeepRelativeImports({ value: reference.value, maxDepth: MAX_DEPTH });
16
+ var suggestions = language === 'zh'
17
+ ? [
18
+ { text: '使用别名路径(如 @shared/deep/utils)' },
19
+ { text: '或在上层聚合导出,避免过深相对路径。' },
20
+ ]
21
+ : [
22
+ { text: 'Use a path alias (for example: @shared/deep/utils).' },
23
+ { text: 'Create an index file at a higher level to re-export the module and shorten the import.' },
24
+ ];
25
+ helpers.reportViolation({
26
+ description: description,
27
+ code: reference.value,
28
+ suggestions: suggestions,
29
+ span: reference.span,
30
+ });
16
31
  }
17
32
  }
18
33
  });
package/dist/runtime.js CHANGED
@@ -24,12 +24,22 @@ var createRuleHelpers = function (reporter, imports) {
24
24
  var relativeDepth = function (value) {
25
25
  return (value.match(/\.\.\//g) || []).length;
26
26
  };
27
- var reportViolation = function (message, span) {
28
- if (span) {
29
- reporter.errorAtSpan(span, message);
27
+ var reportViolation = function (input, spanArg) {
28
+ var normalized = normalizeViolationInput(input, spanArg);
29
+ if (typeof reporter.record === 'function') {
30
+ reporter.record(normalized);
30
31
  return;
31
32
  }
32
- reporter.error(message);
33
+ var targetSpan = normalized.span;
34
+ if (targetSpan) {
35
+ reporter.errorAtSpan(targetSpan, normalized.description);
36
+ return;
37
+ }
38
+ if (typeof normalized.line === 'number') {
39
+ reporter.errorAtLine(normalized.line, normalized.description);
40
+ return;
41
+ }
42
+ reporter.error(normalized.description);
33
43
  };
34
44
  return {
35
45
  imports: imports,
@@ -38,3 +48,20 @@ var createRuleHelpers = function (reporter, imports) {
38
48
  reportViolation: reportViolation,
39
49
  };
40
50
  };
51
+ function normalizeViolationInput(input, fallbackSpan) {
52
+ var _a, _b;
53
+ if (typeof input === 'string') {
54
+ return {
55
+ description: input,
56
+ span: fallbackSpan,
57
+ };
58
+ }
59
+ var description = (_a = input.description) !== null && _a !== void 0 ? _a : input.message;
60
+ return {
61
+ description: description !== null && description !== void 0 ? description : 'Rule violation detected.',
62
+ code: input.code,
63
+ suggestions: input.suggestions,
64
+ span: (_b = input.span) !== null && _b !== void 0 ? _b : fallbackSpan,
65
+ line: input.line,
66
+ };
67
+ }
@@ -1,4 +1,4 @@
1
- export type { ImportReference, Language, Rule, RuleMetadata, RuleContext, RuleHelpers, RuleMessages, RuleReporter, } from './types';
1
+ export type { ImportReference, Language, Rule, RuleMetadata, RuleContext, RuleHelpers, RuleMessages, RuleReporter, RuleReporterRecord, RuleSuggestion, RuleViolationInput, } from './types';
2
2
  export { RuleSeverity } from './types';
3
3
  export { defineRule, isRule, toRule } from './types';
4
4
  export { createRuleContext } from './runtime';
@@ -4,12 +4,33 @@ export interface RuleReporter {
4
4
  error(message: string): void;
5
5
  errorAtSpan(span: Span | undefined, message: string): void;
6
6
  errorAtLine(line: number | undefined, message: string): void;
7
+ record?(record: RuleReporterRecord): void;
7
8
  }
8
9
  export declare enum RuleSeverity {
9
10
  Error = "error",
10
11
  Warning = "warning",
11
12
  Optimizing = "optimizing"
12
13
  }
14
+ export interface RuleSuggestion {
15
+ text: string;
16
+ link?: string;
17
+ }
18
+ export interface RuleReporterRecord {
19
+ description: string;
20
+ code?: string;
21
+ suggestions?: ReadonlyArray<RuleSuggestion>;
22
+ span?: Span;
23
+ line?: number;
24
+ }
25
+ export interface RuleViolationInit {
26
+ description?: string;
27
+ message?: string;
28
+ code?: string;
29
+ suggestions?: ReadonlyArray<RuleSuggestion>;
30
+ span?: Span;
31
+ line?: number;
32
+ }
33
+ export type RuleViolationInput = string | RuleViolationInit;
13
34
  export interface ImportReference {
14
35
  kind: 'static' | 'dynamic' | 'require';
15
36
  value: string;
@@ -25,7 +46,7 @@ export interface RuleHelpers {
25
46
  readonly imports: ReadonlyArray<ImportReference>;
26
47
  isRelativePath(value: string): boolean;
27
48
  relativeDepth(value: string): number;
28
- reportViolation(message: string, span?: Span): void;
49
+ reportViolation(input: RuleViolationInput, span?: Span): void;
29
50
  }
30
51
  export interface RuleContext {
31
52
  readonly filePath: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "auto-cr-rules",
3
- "version": "2.0.19",
3
+ "version": "2.0.21",
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",