UNPKG

15.9 kBTypeScriptView Raw
1import 'reflect-metadata';
2import * as ts from 'typescript';
3import { WrappedAst, BooleanCompilerOptions } from 'tsutils';
4export declare class ConfigurationError extends Error {
5}
6export declare abstract class GlobalOptions {
7 readonly [key: string]: {} | null | undefined;
8}
9export declare type LintResult = Iterable<[string, FileSummary]>;
10export declare type FileSummary = LintAndFixFileResult;
11export interface LintAndFixFileResult {
12 content: string;
13 findings: ReadonlyArray<Finding>;
14 fixes: number;
15}
16export interface Replacement {
17 readonly start: number;
18 readonly end: number;
19 readonly text: string;
20}
21export declare const Replacement: {
22 replace(start: number, end: number, text: string): Replacement;
23 append(pos: number, text: string): Replacement;
24 delete(start: number, end: number): Replacement;
25};
26export interface Fix {
27 readonly replacements: ReadonlyArray<Replacement>;
28}
29export interface Finding {
30 readonly start: FindingPosition;
31 readonly end: FindingPosition;
32 readonly message: string;
33 readonly ruleName: string;
34 readonly severity: Severity;
35 readonly fix: Fix | undefined;
36}
37export declare const Finding: {
38 /** Compare two Findings. Intended to be used in `Array.prototype.sort`. */
39 compare(a: Finding, b: Finding): number;
40};
41export interface FindingPosition {
42 readonly line: number;
43 readonly character: number;
44 readonly position: number;
45}
46export declare type Severity = 'error' | 'warning' | 'suggestion';
47export interface RuleConstructor<T extends RuleContext = RuleContext> {
48 readonly requiresTypeInformation: boolean;
49 readonly deprecated?: boolean | string;
50 supports?: RulePredicate;
51 new (context: T): AbstractRule;
52}
53export interface RulePredicateContext {
54 readonly program?: ts.Program;
55 readonly compilerOptions?: ts.CompilerOptions;
56 readonly settings: Settings;
57 readonly options: {} | null | undefined;
58}
59export interface RuleContext extends RulePredicateContext {
60 readonly sourceFile: ts.SourceFile;
61 addFinding(start: number, end: number, message: string, fix?: Replacement | ReadonlyArray<Replacement>): void;
62 getFlatAst(): ReadonlyArray<ts.Node>;
63 getWrappedAst(): WrappedAst;
64}
65export interface TypedRuleContext extends RuleContext {
66 readonly program: ts.Program;
67 readonly compilerOptions: ts.CompilerOptions;
68}
69export declare type Settings = ReadonlyMap<string, {} | null | undefined>;
70export declare function predicate(check: RulePredicate): (target: typeof AbstractRule) => void;
71export declare function typescriptOnly(target: typeof AbstractRule): void;
72export declare function excludeDeclarationFiles(target: typeof AbstractRule): void;
73export declare function requireLibraryFile(fileName: string): (target: typeof TypedRule) => void;
74export declare function requiresCompilerOption(option: BooleanCompilerOptions): (target: typeof TypedRule) => void;
75/** @returns `true`, `false` or a reason */
76export declare type RulePredicate = (sourceFile: ts.SourceFile, context: RulePredicateContext) => boolean | string;
77export declare abstract class AbstractRule {
78 readonly context: RuleContext;
79 static readonly requiresTypeInformation: boolean;
80 static deprecated: boolean | string;
81 static supports?: RulePredicate;
82 static validateConfig?(config: any): string[] | string | undefined;
83 readonly sourceFile: ts.SourceFile;
84 get program(): ts.Program | undefined;
85 constructor(context: RuleContext);
86 abstract apply(): void;
87 addFinding(start: number, end: number, message: string, fix?: Replacement | ReadonlyArray<Replacement>): void;
88 addFindingAtNode(node: ts.Node, message: string, fix?: Replacement | ReadonlyArray<Replacement>): void;
89}
90export declare abstract class ConfigurableRule<T> extends AbstractRule {
91 options: T;
92 constructor(context: RuleContext);
93 protected abstract parseOptions(options: {} | null | undefined): T;
94}
95export declare abstract class TypedRule extends AbstractRule {
96 static readonly requiresTypeInformation = true;
97 readonly context: TypedRuleContext;
98 get program(): ts.Program;
99 /** Lazily evaluated getter for TypeChecker. Use this instead of `this.program.getTypeChecker()` to avoid wasting CPU cycles. */
100 get checker(): ts.TypeChecker;
101 constructor(context: TypedRuleContext);
102}
103export declare abstract class ConfigurableTypedRule<T> extends TypedRule {
104 options: T;
105 constructor(context: TypedRuleContext);
106 protected abstract parseOptions(options: {} | null | undefined): T;
107}
108export declare abstract class AbstractFormatter {
109 prefix?: string;
110 abstract format(filename: string, summary: FileSummary): string | undefined;
111 flush?(): string | undefined;
112}
113export interface FormatterConstructor {
114 new (): AbstractFormatter;
115}
116export interface Configuration {
117 readonly aliases?: ReadonlyMap<string, Configuration.Alias>;
118 readonly rules?: ReadonlyMap<string, Configuration.RuleConfig>;
119 readonly settings?: Settings;
120 readonly filename: string;
121 readonly overrides?: ReadonlyArray<Configuration.Override>;
122 readonly extends: ReadonlyArray<Configuration>;
123 readonly rulesDirectories?: Configuration.RulesDirectoryMap;
124 readonly processor?: string | null | false;
125 readonly exclude?: ReadonlyArray<string>;
126}
127export declare namespace Configuration {
128 type RulesDirectoryMap = ReadonlyMap<string, ReadonlyArray<string>>;
129 type RuleSeverity = 'off' | 'warning' | 'error' | 'suggestion';
130 interface RuleConfig {
131 readonly severity?: RuleSeverity;
132 readonly options?: any;
133 readonly rulesDirectories: ReadonlyArray<string> | undefined;
134 readonly rule: string;
135 }
136 interface Override {
137 readonly rules?: ReadonlyMap<string, RuleConfig>;
138 readonly settings?: ReadonlyMap<string, any>;
139 readonly files: ReadonlyArray<string>;
140 readonly processor?: string | null | false;
141 }
142 interface Alias {
143 readonly rule: string;
144 readonly options?: any;
145 readonly rulesDirectories: ReadonlyArray<string> | undefined;
146 }
147}
148export interface EffectiveConfiguration {
149 rules: Map<string, EffectiveConfiguration.RuleConfig>;
150 settings: Map<string, any>;
151}
152export declare namespace EffectiveConfiguration {
153 interface RuleConfig {
154 severity: Configuration.RuleSeverity;
155 options: any;
156 rulesDirectories: ReadonlyArray<string> | undefined;
157 rule: string;
158 }
159}
160export interface ReducedConfiguration extends EffectiveConfiguration {
161 processor: string | undefined;
162}
163export interface ConfigurationProvider {
164 find(fileToLint: string): string | undefined;
165 resolve(name: string, basedir: string): string;
166 load(fileName: string, context: LoadConfigurationContext): Configuration;
167}
168export declare abstract class ConfigurationProvider {
169}
170export interface LoadConfigurationContext {
171 readonly stack: ReadonlyArray<string>;
172 /**
173 * Resolves the given name relative to the current configuration file and returns the parsed Configuration.
174 * This function detects cycles and caches already loaded configurations.
175 */
176 load(name: string): Configuration;
177}
178export declare enum Format {
179 Yaml = "yaml",
180 Json = "json",
181 Json5 = "json5"
182}
183export interface ProcessorConstructor {
184 getSuffixForFile(context: ProcessorSuffixContext): string;
185 new (context: ProcessorContext): AbstractProcessor;
186}
187export interface ProcessorSuffixContext {
188 fileName: string;
189 getSettings(): Settings;
190 readFile(): string;
191}
192export interface ProcessorContext {
193 source: string;
194 sourceFileName: string;
195 targetFileName: string;
196 settings: Settings;
197}
198export interface ProcessorUpdateResult {
199 transformed: string;
200 changeRange?: ts.TextChangeRange;
201}
202export declare abstract class AbstractProcessor {
203 /**
204 * Returns a new primary extension that is appended to the file name, e.g. '.ts'.
205 * If the file should not get a new extension, just return an empty string.
206 */
207 static getSuffixForFile(_context: ProcessorSuffixContext): string;
208 protected source: string;
209 protected sourceFileName: string;
210 protected targetFileName: string;
211 protected settings: Settings;
212 constructor(context: ProcessorContext);
213 abstract preprocess(): string;
214 abstract postprocess(findings: ReadonlyArray<Finding>): ReadonlyArray<Finding>;
215 abstract updateSource(newSource: string, changeRange: ts.TextChangeRange): ProcessorUpdateResult;
216}
217export interface MessageHandler {
218 log(message: string): void;
219 warn(message: string): void;
220 error(e: Error): void;
221}
222export declare abstract class MessageHandler {
223}
224export interface DeprecationHandler {
225 handle(target: DeprecationTarget, name: string, text?: string): void;
226}
227export declare abstract class DeprecationHandler {
228}
229export declare enum DeprecationTarget {
230 Rule = "rule",
231 Processor = "processor",
232 Formatter = "formatter"
233}
234/**
235 * Low level file system access. All methods are supposed to throw an error on failure.
236 */
237export interface FileSystem {
238 /** Normalizes the path to enable reliable caching in consuming services. */
239 normalizePath(path: string): string;
240 /** Reads the given file. Tries to infer and convert encoding. */
241 readFile(file: string): string;
242 /** Reads directory entries. Returns only the basenames optionally with file type information. */
243 readDirectory(dir: string): Array<string | Dirent>;
244 /** Gets the status of a file or directory. */
245 stat(path: string): Stats;
246 /** Gets the realpath of a given file or directory. */
247 realpath?(path: string): string;
248 /** Writes content to the file, overwriting the existing content. Creates the file if necessary. */
249 writeFile(file: string, content: string): void;
250 /** Deletes a given file. Is not supposed to delete or clear a directory. */
251 deleteFile(path: string): void;
252 /** Creates a single directory and fails on error. Is not supposed to create multiple directories. */
253 createDirectory(dir: string): void;
254}
255export declare abstract class FileSystem {
256}
257export interface Stats {
258 isDirectory(): boolean;
259 isFile(): boolean;
260}
261export interface Dirent extends Stats {
262 name: string;
263 isSymbolicLink(): boolean;
264}
265export interface RuleLoaderHost {
266 loadCoreRule(name: string): RuleConstructor | undefined;
267 loadCustomRule(name: string, directory: string): RuleConstructor | undefined;
268}
269export declare abstract class RuleLoaderHost {
270}
271export interface FormatterLoaderHost {
272 loadCoreFormatter(name: string): FormatterConstructor | undefined;
273 loadCustomFormatter(name: string, basedir: string): FormatterConstructor | undefined;
274}
275export declare abstract class FormatterLoaderHost {
276}
277export interface CacheFactory {
278 /** Creates a new cache instance. */
279 create<K extends object, V = any>(weak: true): Cache<K, V>;
280 create<K = any, V = any>(weak?: false): Cache<K, V>;
281}
282export declare abstract class CacheFactory {
283}
284export interface Cache<K, V> {
285 get(key: K): V | undefined;
286 set(key: K, value: V): void;
287 delete(key: K): void;
288 has(key: K): boolean;
289 clear(): void;
290}
291export interface Resolver {
292 getDefaultExtensions(): ReadonlyArray<string>;
293 resolve(id: string, basedir?: string, extensions?: ReadonlyArray<string>, paths?: ReadonlyArray<string>): string;
294 require(id: string, options?: {
295 cache?: boolean;
296 }): any;
297}
298export declare abstract class Resolver {
299}
300export interface BuiltinResolver {
301 resolveConfig(name: string): string;
302 resolveRule(name: string): string;
303 resolveFormatter(name: string): string;
304}
305export declare abstract class BuiltinResolver {
306}
307export interface DirectoryService {
308 getCurrentDirectory(): string;
309 getHomeDirectory?(): string;
310}
311export declare abstract class DirectoryService {
312}
313export interface FindingFilterFactory {
314 create(context: FindingFilterContext): FindingFilter;
315}
316export declare abstract class FindingFilterFactory {
317}
318export interface FindingFilterContext {
319 sourceFile: ts.SourceFile;
320 ruleNames: ReadonlyArray<string>;
321 getWrappedAst(): WrappedAst;
322}
323export interface FindingFilter {
324 /** @returns `true` if the finding should be used, false if it should be filtered out. Intended for use in `Array.prototype.filter`. */
325 filter(finding: Finding): boolean;
326 /**
327 * @returns Findings to report redundant or unused filter directives.
328 * This is called after calling `filter` for all findings in the file.
329 */
330 reportUseless(severity: Severity): ReadonlyArray<Finding>;
331}
332export interface LineSwitchParser {
333 parse(context: LineSwitchParserContext): ReadonlyArray<RawLineSwitch>;
334}
335export declare abstract class LineSwitchParser {
336}
337export interface LineSwitchParserContext {
338 sourceFile: ts.SourceFile;
339 getCommentAtPosition(pos: number): ts.CommentRange | undefined;
340}
341export interface RawLineSwitch {
342 readonly rules: ReadonlyArray<RawLineSwitchRule>;
343 readonly enable: boolean;
344 readonly pos: number;
345 readonly end?: number;
346 readonly location: Readonly<ts.TextRange>;
347}
348export interface RawLineSwitchRule {
349 readonly predicate: string | RegExp | ((ruleName: string) => boolean);
350 readonly location?: Readonly<ts.TextRange>;
351 readonly fixLocation?: Readonly<ts.TextRange>;
352}
353export interface FileFilterContext {
354 program: ts.Program;
355 host: Required<Pick<ts.CompilerHost, 'directoryExists'>>;
356}
357export interface FileFilterFactory {
358 create(context: FileFilterContext): FileFilter;
359}
360export declare abstract class FileFilterFactory {
361}
362export interface FileFilter {
363 /** @returns `true` if the file should be linted, false if it should be filtered out. Intended for use in `Array.prototype.filter`. */
364 filter(file: ts.SourceFile): boolean;
365}
366export declare type ContentIdHost = Pick<ts.CompilerHost, 'readFile'>;
367export interface ContentId {
368 forFile(fileName: string, host: ContentIdHost): string;
369}
370export declare abstract class ContentId {
371}
372export interface StatePersistence {
373 loadState(project: string): StaticProgramState | undefined;
374 saveState(project: string, state: StaticProgramState): void;
375}
376export declare abstract class StatePersistence {
377}
378export interface StaticProgramState {
379 /** Version of the cache format */
380 readonly v: number;
381 /** TypeScript version */
382 readonly ts: string;
383 /** Whether the state was created using case-sensitive file names */
384 readonly cs: boolean;
385 /** Hash of compilerOptions */
386 readonly options: string;
387 /** Maps filename to index in 'files' array */
388 readonly lookup: Readonly<Record<string, number>>;
389 /** Index of files that affect global scope */
390 readonly global: readonly number[];
391 /** Information about all files in the program */
392 readonly files: readonly StaticProgramState.FileState[];
393}
394export declare namespace StaticProgramState {
395 interface FileState {
396 /** ID of file contents (typically a hash) */
397 readonly id: string;
398 /**
399 * Key: module specifier as referenced in the file, order may be random
400 * Value: - `null` if dependency could not be resolved
401 * - List of files (or rather their index) that the module specifier resolves to.
402 * That is the actual file at that path and/or files containing `declare module "..."` for that module specifier.
403 * May contain the current file.
404 * This list is ordered by the ID of the files ascending,
405 */
406 readonly dependencies?: Readonly<Record<string, null | readonly number[]>>;
407 /** The list of findings if this file has up-to-date results */
408 readonly result?: readonly Finding[];
409 /** Hash of the configuration used to produce `result` for this file */
410 readonly config?: string;
411 }
412}
413
\No newline at end of file