UNPKG

auto-cr-rules

Version:

Extensible static analysis rule set for the auto-cr automated code review toolkit

75 lines (74 loc) 2.33 kB
import type { Module, Span } from '@swc/types'; export type Language = 'zh' | 'en'; export interface RuleReporter { error(message: string): void; errorAtSpan(span: Span | undefined, message: string): void; errorAtLine(line: number | undefined, message: string): void; record?(record: RuleReporterRecord): void; } export declare enum RuleSeverity { Error = "error", Warning = "warning", Optimizing = "optimizing" } export interface RuleSuggestion { text: string; link?: string; } export interface RuleReporterRecord { description: string; code?: string; suggestions?: ReadonlyArray<RuleSuggestion>; span?: Span; line?: number; } export interface RuleViolationInit { description?: string; message?: string; code?: string; suggestions?: ReadonlyArray<RuleSuggestion>; span?: Span; line?: number; } export type RuleViolationInput = string | RuleViolationInit; export interface ImportReference { kind: 'static' | 'dynamic' | 'require'; value: string; span?: Span; } export interface RuleMessages { noDeepRelativeImports(params: { value: string; maxDepth: number; }): string; swallowedError(): string; } export interface RuleHelpers { readonly imports: ReadonlyArray<ImportReference>; isRelativePath(value: string): boolean; relativeDepth(value: string): number; reportViolation(input: RuleViolationInput, span?: Span): void; } export interface RuleContext { readonly filePath: string; readonly source: string; readonly language: Language; readonly reporter: RuleReporter; readonly ast: Module; readonly helpers: RuleHelpers; readonly messages: RuleMessages; } export interface RuleMetadata { tag?: string; severity?: RuleSeverity; } export interface Rule { name: string; tag?: string; severity?: RuleSeverity; run(context: RuleContext): void | Promise<void>; } export declare function defineRule(name: string, runner: (context: RuleContext) => void | Promise<void>): Rule; export declare function defineRule(name: string, metadata: RuleMetadata, runner: (context: RuleContext) => void | Promise<void>): Rule; export declare const isRule: (value: unknown) => value is Rule; export declare const toRule: (value: unknown, origin: string) => Rule | null;