UNPKG

18.8 kBTypeScriptView Raw
1import { JSONSchema4 } from '../json-schema';
2import { ParserServices, TSESTree } from '../ts-estree';
3import { AST } from './AST';
4import { Linter } from './Linter';
5import { Scope } from './Scope';
6import { SourceCode } from './SourceCode';
7interface RuleMetaDataDocs {
8 /**
9 * The general category the rule falls within
10 */
11 category: 'Best Practices' | 'Stylistic Issues' | 'Variables' | 'Possible Errors';
12 /**
13 * Concise description of the rule
14 */
15 description: string;
16 /**
17 * The recommendation level for the rule.
18 * Used by the build tools to generate the recommended config.
19 * Set to false to not include it as a recommendation
20 */
21 recommended: 'error' | 'warn' | false;
22 /**
23 * The URL of the rule's docs
24 */
25 url: string;
26 /**
27 * Specifies whether the rule can return suggestions.
28 */
29 suggestion?: boolean;
30 /**
31 * Does the rule require us to create a full TypeScript Program in order for it
32 * to type-check code. This is only used for documentation purposes.
33 */
34 requiresTypeChecking?: boolean;
35 /**
36 * Does the rule extend (or is it based off of) an ESLint code rule?
37 * Alternately accepts the name of the base rule, in case the rule has been renamed.
38 * This is only used for documentation purposes.
39 */
40 extendsBaseRule?: boolean | string;
41}
42interface RuleMetaData<TMessageIds extends string> {
43 /**
44 * True if the rule is deprecated, false otherwise
45 */
46 deprecated?: boolean;
47 /**
48 * Documentation for the rule, unnecessary for custom rules/plugins
49 */
50 docs?: RuleMetaDataDocs;
51 /**
52 * The fixer category. Omit if there is no fixer
53 */
54 fixable?: 'code' | 'whitespace';
55 /**
56 * A map of messages which the rule can report.
57 * The key is the messageId, and the string is the parameterised error string.
58 * See: https://eslint.org/docs/developer-guide/working-with-rules#messageids
59 */
60 messages: Record<TMessageIds, string>;
61 /**
62 * The type of rule.
63 * - `"problem"` means the rule is identifying code that either will cause an error or may cause a confusing behavior. Developers should consider this a high priority to resolve.
64 * - `"suggestion"` means the rule is identifying something that could be done in a better way but no errors will occur if the code isn’t changed.
65 * - `"layout"` means the rule cares primarily about whitespace, semicolons, commas, and parentheses, all the parts of the program that determine how the code looks rather than how it executes. These rules work on parts of the code that aren’t specified in the AST.
66 */
67 type: 'suggestion' | 'problem' | 'layout';
68 /**
69 * The name of the rule this rule was replaced by, if it was deprecated.
70 */
71 replacedBy?: string[];
72 /**
73 * The options schema. Supply an empty array if there are no options.
74 */
75 schema: JSONSchema4 | JSONSchema4[];
76}
77interface RuleFix {
78 range: AST.Range;
79 text: string;
80}
81interface RuleFixer {
82 insertTextAfter(nodeOrToken: TSESTree.Node | TSESTree.Token, text: string): RuleFix;
83 insertTextAfterRange(range: AST.Range, text: string): RuleFix;
84 insertTextBefore(nodeOrToken: TSESTree.Node | TSESTree.Token, text: string): RuleFix;
85 insertTextBeforeRange(range: AST.Range, text: string): RuleFix;
86 remove(nodeOrToken: TSESTree.Node | TSESTree.Token): RuleFix;
87 removeRange(range: AST.Range): RuleFix;
88 replaceText(nodeOrToken: TSESTree.Node | TSESTree.Token, text: string): RuleFix;
89 replaceTextRange(range: AST.Range, text: string): RuleFix;
90}
91declare type ReportFixFunction = (fixer: RuleFixer) => null | RuleFix | RuleFix[] | IterableIterator<RuleFix>;
92declare type ReportSuggestionArray<TMessageIds extends string> = ReportDescriptorBase<TMessageIds>[];
93interface ReportDescriptorBase<TMessageIds extends string> {
94 /**
95 * The parameters for the message string associated with `messageId`.
96 */
97 readonly data?: Readonly<Record<string, unknown>>;
98 /**
99 * The fixer function.
100 */
101 readonly fix?: ReportFixFunction | null;
102 /**
103 * The messageId which is being reported.
104 */
105 readonly messageId: TMessageIds;
106}
107interface ReportDescriptorWithSuggestion<TMessageIds extends string> extends ReportDescriptorBase<TMessageIds> {
108 /**
109 * 6.7's Suggestions API
110 */
111 readonly suggest?: Readonly<ReportSuggestionArray<TMessageIds>> | null;
112}
113interface ReportDescriptorNodeOptionalLoc {
114 /**
115 * The Node or AST Token which the report is being attached to
116 */
117 readonly node: TSESTree.Node | TSESTree.Token;
118 /**
119 * An override of the location of the report
120 */
121 readonly loc?: Readonly<TSESTree.SourceLocation> | Readonly<TSESTree.LineAndColumnData>;
122}
123interface ReportDescriptorLocOnly {
124 /**
125 * An override of the location of the report
126 */
127 loc: Readonly<TSESTree.SourceLocation> | Readonly<TSESTree.LineAndColumnData>;
128}
129declare type ReportDescriptor<TMessageIds extends string> = ReportDescriptorWithSuggestion<TMessageIds> & (ReportDescriptorNodeOptionalLoc | ReportDescriptorLocOnly);
130/**
131 * Plugins can add their settings using declaration
132 * merging against this interface.
133 */
134interface SharedConfigurationSettings {
135 [name: string]: unknown;
136}
137interface RuleContext<TMessageIds extends string, TOptions extends readonly unknown[]> {
138 /**
139 * The rule ID.
140 */
141 id: string;
142 /**
143 * An array of the configured options for this rule.
144 * This array does not include the rule severity.
145 */
146 options: TOptions;
147 /**
148 * The name of the parser from configuration.
149 */
150 parserPath: string;
151 /**
152 * The parser options configured for this run
153 */
154 parserOptions: Linter.ParserOptions;
155 /**
156 * An object containing parser-provided services for rules
157 */
158 parserServices?: ParserServices;
159 /**
160 * The shared settings from configuration.
161 * We do not have any shared settings in this plugin.
162 */
163 settings: SharedConfigurationSettings;
164 /**
165 * Returns an array of the ancestors of the currently-traversed node, starting at
166 * the root of the AST and continuing through the direct parent of the current node.
167 * This array does not include the currently-traversed node itself.
168 */
169 getAncestors(): TSESTree.Node[];
170 /**
171 * Returns a list of variables declared by the given node.
172 * This information can be used to track references to variables.
173 */
174 getDeclaredVariables(node: TSESTree.Node): Scope.Variable[];
175 /**
176 * Returns the current working directory passed to Linter.
177 * It is a path to a directory that should be considered as the current working directory.
178 * This was added in v6.6.0
179 * @since 6.6.0
180 */
181 getCwd?(): string;
182 /**
183 * Returns the filename associated with the source.
184 */
185 getFilename(): string;
186 /**
187 * Returns the scope of the currently-traversed node.
188 * This information can be used track references to variables.
189 */
190 getScope(): Scope.Scope;
191 /**
192 * Returns a SourceCode object that you can use to work with the source that
193 * was passed to ESLint.
194 */
195 getSourceCode(): Readonly<SourceCode>;
196 /**
197 * Marks a variable with the given name in the current scope as used.
198 * This affects the no-unused-vars rule.
199 */
200 markVariableAsUsed(name: string): boolean;
201 /**
202 * Reports a problem in the code.
203 */
204 report(descriptor: ReportDescriptor<TMessageIds>): void;
205}
206declare type RuleFunction<T extends TSESTree.BaseNode = never> = (node: T) => void;
207interface RuleListener {
208 [nodeSelector: string]: RuleFunction | undefined;
209 ArrayExpression?: RuleFunction<TSESTree.ArrayExpression>;
210 ArrayPattern?: RuleFunction<TSESTree.ArrayPattern>;
211 ArrowFunctionExpression?: RuleFunction<TSESTree.ArrowFunctionExpression>;
212 AssignmentPattern?: RuleFunction<TSESTree.AssignmentPattern>;
213 AssignmentExpression?: RuleFunction<TSESTree.AssignmentExpression>;
214 AwaitExpression?: RuleFunction<TSESTree.AwaitExpression>;
215 BigIntLiteral?: RuleFunction<TSESTree.BigIntLiteral>;
216 BinaryExpression?: RuleFunction<TSESTree.BinaryExpression>;
217 BlockStatement?: RuleFunction<TSESTree.BlockStatement>;
218 BreakStatement?: RuleFunction<TSESTree.BreakStatement>;
219 CallExpression?: RuleFunction<TSESTree.CallExpression>;
220 CatchClause?: RuleFunction<TSESTree.CatchClause>;
221 ChainExpression?: RuleFunction<TSESTree.ChainExpression>;
222 ClassBody?: RuleFunction<TSESTree.ClassBody>;
223 ClassDeclaration?: RuleFunction<TSESTree.ClassDeclaration>;
224 ClassExpression?: RuleFunction<TSESTree.ClassExpression>;
225 ClassProperty?: RuleFunction<TSESTree.ClassProperty>;
226 ConditionalExpression?: RuleFunction<TSESTree.ConditionalExpression>;
227 ContinueStatement?: RuleFunction<TSESTree.ContinueStatement>;
228 DebuggerStatement?: RuleFunction<TSESTree.DebuggerStatement>;
229 Decorator?: RuleFunction<TSESTree.Decorator>;
230 DoWhileStatement?: RuleFunction<TSESTree.DoWhileStatement>;
231 EmptyStatement?: RuleFunction<TSESTree.EmptyStatement>;
232 ExportAllDeclaration?: RuleFunction<TSESTree.ExportAllDeclaration>;
233 ExportDefaultDeclaration?: RuleFunction<TSESTree.ExportDefaultDeclaration>;
234 ExportNamedDeclaration?: RuleFunction<TSESTree.ExportNamedDeclaration>;
235 ExportSpecifier?: RuleFunction<TSESTree.ExportSpecifier>;
236 ExpressionStatement?: RuleFunction<TSESTree.ExpressionStatement>;
237 ForInStatement?: RuleFunction<TSESTree.ForInStatement>;
238 ForOfStatement?: RuleFunction<TSESTree.ForOfStatement>;
239 ForStatement?: RuleFunction<TSESTree.ForStatement>;
240 FunctionDeclaration?: RuleFunction<TSESTree.FunctionDeclaration>;
241 FunctionExpression?: RuleFunction<TSESTree.FunctionExpression>;
242 Identifier?: RuleFunction<TSESTree.Identifier>;
243 IfStatement?: RuleFunction<TSESTree.IfStatement>;
244 ImportDeclaration?: RuleFunction<TSESTree.ImportDeclaration>;
245 ImportDefaultSpecifier?: RuleFunction<TSESTree.ImportDefaultSpecifier>;
246 ImportExpression?: RuleFunction<TSESTree.ImportExpression>;
247 ImportNamespaceSpecifier?: RuleFunction<TSESTree.ImportNamespaceSpecifier>;
248 ImportSpecifier?: RuleFunction<TSESTree.ImportSpecifier>;
249 JSXAttribute?: RuleFunction<TSESTree.JSXAttribute>;
250 JSXClosingElement?: RuleFunction<TSESTree.JSXClosingElement>;
251 JSXClosingFragment?: RuleFunction<TSESTree.JSXClosingFragment>;
252 JSXElement?: RuleFunction<TSESTree.JSXElement>;
253 JSXEmptyExpression?: RuleFunction<TSESTree.JSXEmptyExpression>;
254 JSXExpressionContainer?: RuleFunction<TSESTree.JSXExpressionContainer>;
255 JSXFragment?: RuleFunction<TSESTree.JSXFragment>;
256 JSXIdentifier?: RuleFunction<TSESTree.JSXIdentifier>;
257 JSXMemberExpression?: RuleFunction<TSESTree.JSXMemberExpression>;
258 JSXOpeningElement?: RuleFunction<TSESTree.JSXOpeningElement>;
259 JSXOpeningFragment?: RuleFunction<TSESTree.JSXOpeningFragment>;
260 JSXSpreadAttribute?: RuleFunction<TSESTree.JSXSpreadAttribute>;
261 JSXSpreadChild?: RuleFunction<TSESTree.JSXSpreadChild>;
262 JSXText?: RuleFunction<TSESTree.JSXText>;
263 LabeledStatement?: RuleFunction<TSESTree.LabeledStatement>;
264 Literal?: RuleFunction<TSESTree.Literal>;
265 LogicalExpression?: RuleFunction<TSESTree.LogicalExpression>;
266 MemberExpression?: RuleFunction<TSESTree.MemberExpression>;
267 MetaProperty?: RuleFunction<TSESTree.MetaProperty>;
268 MethodDefinition?: RuleFunction<TSESTree.MethodDefinition>;
269 NewExpression?: RuleFunction<TSESTree.NewExpression>;
270 ObjectExpression?: RuleFunction<TSESTree.ObjectExpression>;
271 ObjectPattern?: RuleFunction<TSESTree.ObjectPattern>;
272 Program?: RuleFunction<TSESTree.Program>;
273 Property?: RuleFunction<TSESTree.Property>;
274 RestElement?: RuleFunction<TSESTree.RestElement>;
275 ReturnStatement?: RuleFunction<TSESTree.ReturnStatement>;
276 SequenceExpression?: RuleFunction<TSESTree.SequenceExpression>;
277 SpreadElement?: RuleFunction<TSESTree.SpreadElement>;
278 Super?: RuleFunction<TSESTree.Super>;
279 SwitchCase?: RuleFunction<TSESTree.SwitchCase>;
280 SwitchStatement?: RuleFunction<TSESTree.SwitchStatement>;
281 TaggedTemplateExpression?: RuleFunction<TSESTree.TaggedTemplateExpression>;
282 TemplateElement?: RuleFunction<TSESTree.TemplateElement>;
283 TemplateLiteral?: RuleFunction<TSESTree.TemplateLiteral>;
284 ThisExpression?: RuleFunction<TSESTree.ThisExpression>;
285 ThrowStatement?: RuleFunction<TSESTree.ThrowStatement>;
286 TryStatement?: RuleFunction<TSESTree.TryStatement>;
287 TSAbstractClassProperty?: RuleFunction<TSESTree.TSAbstractClassProperty>;
288 TSAbstractKeyword?: RuleFunction<TSESTree.TSAbstractKeyword>;
289 TSAbstractMethodDefinition?: RuleFunction<TSESTree.TSAbstractMethodDefinition>;
290 TSAnyKeyword?: RuleFunction<TSESTree.TSAnyKeyword>;
291 TSArrayType?: RuleFunction<TSESTree.TSArrayType>;
292 TSAsExpression?: RuleFunction<TSESTree.TSAsExpression>;
293 TSAsyncKeyword?: RuleFunction<TSESTree.TSAsyncKeyword>;
294 TSBigIntKeyword?: RuleFunction<TSESTree.TSBigIntKeyword>;
295 TSBooleanKeyword?: RuleFunction<TSESTree.TSBooleanKeyword>;
296 TSCallSignatureDeclaration?: RuleFunction<TSESTree.TSCallSignatureDeclaration>;
297 TSClassImplements?: RuleFunction<TSESTree.TSClassImplements>;
298 TSConditionalType?: RuleFunction<TSESTree.TSConditionalType>;
299 TSConstructorType?: RuleFunction<TSESTree.TSConstructorType>;
300 TSConstructSignatureDeclaration?: RuleFunction<TSESTree.TSConstructSignatureDeclaration>;
301 TSDeclareKeyword?: RuleFunction<TSESTree.TSDeclareKeyword>;
302 TSDeclareFunction?: RuleFunction<TSESTree.TSDeclareFunction>;
303 TSEmptyBodyFunctionExpression?: RuleFunction<TSESTree.TSEmptyBodyFunctionExpression>;
304 TSEnumDeclaration?: RuleFunction<TSESTree.TSEnumDeclaration>;
305 TSEnumMember?: RuleFunction<TSESTree.TSEnumMember>;
306 TSExportAssignment?: RuleFunction<TSESTree.TSExportAssignment>;
307 TSExportKeyword?: RuleFunction<TSESTree.TSExportKeyword>;
308 TSExternalModuleReference?: RuleFunction<TSESTree.TSExternalModuleReference>;
309 TSFunctionType?: RuleFunction<TSESTree.TSFunctionType>;
310 TSImportEqualsDeclaration?: RuleFunction<TSESTree.TSImportEqualsDeclaration>;
311 TSImportType?: RuleFunction<TSESTree.TSImportType>;
312 TSIndexedAccessType?: RuleFunction<TSESTree.TSIndexedAccessType>;
313 TSIndexSignature?: RuleFunction<TSESTree.TSIndexSignature>;
314 TSInferType?: RuleFunction<TSESTree.TSInferType>;
315 TSInterfaceBody?: RuleFunction<TSESTree.TSInterfaceBody>;
316 TSInterfaceDeclaration?: RuleFunction<TSESTree.TSInterfaceDeclaration>;
317 TSInterfaceHeritage?: RuleFunction<TSESTree.TSInterfaceHeritage>;
318 TSIntersectionType?: RuleFunction<TSESTree.TSIntersectionType>;
319 TSLiteralType?: RuleFunction<TSESTree.TSLiteralType>;
320 TSMappedType?: RuleFunction<TSESTree.TSMappedType>;
321 TSMethodSignature?: RuleFunction<TSESTree.TSMethodSignature>;
322 TSModuleBlock?: RuleFunction<TSESTree.TSModuleBlock>;
323 TSModuleDeclaration?: RuleFunction<TSESTree.TSModuleDeclaration>;
324 TSNamespaceExportDeclaration?: RuleFunction<TSESTree.TSNamespaceExportDeclaration>;
325 TSNeverKeyword?: RuleFunction<TSESTree.TSNeverKeyword>;
326 TSNonNullExpression?: RuleFunction<TSESTree.TSNonNullExpression>;
327 TSNullKeyword?: RuleFunction<TSESTree.TSNullKeyword>;
328 TSNumberKeyword?: RuleFunction<TSESTree.TSNumberKeyword>;
329 TSObjectKeyword?: RuleFunction<TSESTree.TSObjectKeyword>;
330 TSOptionalType?: RuleFunction<TSESTree.TSOptionalType>;
331 TSParameterProperty?: RuleFunction<TSESTree.TSParameterProperty>;
332 TSParenthesizedType?: RuleFunction<TSESTree.TSParenthesizedType>;
333 TSPrivateKeyword?: RuleFunction<TSESTree.TSPrivateKeyword>;
334 TSPropertySignature?: RuleFunction<TSESTree.TSPropertySignature>;
335 TSProtectedKeyword?: RuleFunction<TSESTree.TSProtectedKeyword>;
336 TSPublicKeyword?: RuleFunction<TSESTree.TSPublicKeyword>;
337 TSQualifiedName?: RuleFunction<TSESTree.TSQualifiedName>;
338 TSReadonlyKeyword?: RuleFunction<TSESTree.TSReadonlyKeyword>;
339 TSRestType?: RuleFunction<TSESTree.TSRestType>;
340 TSStaticKeyword?: RuleFunction<TSESTree.TSStaticKeyword>;
341 TSStringKeyword?: RuleFunction<TSESTree.TSStringKeyword>;
342 TSSymbolKeyword?: RuleFunction<TSESTree.TSSymbolKeyword>;
343 TSThisType?: RuleFunction<TSESTree.TSThisType>;
344 TSTupleType?: RuleFunction<TSESTree.TSTupleType>;
345 TSTypeAliasDeclaration?: RuleFunction<TSESTree.TSTypeAliasDeclaration>;
346 TSTypeAnnotation?: RuleFunction<TSESTree.TSTypeAnnotation>;
347 TSTypeAssertion?: RuleFunction<TSESTree.TSTypeAssertion>;
348 TSTypeLiteral?: RuleFunction<TSESTree.TSTypeLiteral>;
349 TSTypeOperator?: RuleFunction<TSESTree.TSTypeOperator>;
350 TSTypeParameter?: RuleFunction<TSESTree.TSTypeParameter>;
351 TSTypeParameterDeclaration?: RuleFunction<TSESTree.TSTypeParameterDeclaration>;
352 TSTypeParameterInstantiation?: RuleFunction<TSESTree.TSTypeParameterInstantiation>;
353 TSTypePredicate?: RuleFunction<TSESTree.TSTypePredicate>;
354 TSTypeQuery?: RuleFunction<TSESTree.TSTypeQuery>;
355 TSTypeReference?: RuleFunction<TSESTree.TSTypeReference>;
356 TSUndefinedKeyword?: RuleFunction<TSESTree.TSUndefinedKeyword>;
357 TSUnionType?: RuleFunction<TSESTree.TSUnionType>;
358 TSUnknownKeyword?: RuleFunction<TSESTree.TSUnknownKeyword>;
359 TSVoidKeyword?: RuleFunction<TSESTree.TSVoidKeyword>;
360 UnaryExpression?: RuleFunction<TSESTree.UnaryExpression>;
361 UpdateExpression?: RuleFunction<TSESTree.UpdateExpression>;
362 VariableDeclaration?: RuleFunction<TSESTree.VariableDeclaration>;
363 VariableDeclarator?: RuleFunction<TSESTree.VariableDeclarator>;
364 WhileStatement?: RuleFunction<TSESTree.WhileStatement>;
365 WithStatement?: RuleFunction<TSESTree.WithStatement>;
366 YieldExpression?: RuleFunction<TSESTree.YieldExpression>;
367}
368interface RuleModule<TMessageIds extends string, TOptions extends readonly unknown[], TRuleListener extends RuleListener = RuleListener> {
369 /**
370 * Metadata about the rule
371 */
372 meta: RuleMetaData<TMessageIds>;
373 /**
374 * Function which returns an object with methods that ESLint calls to “visit”
375 * nodes while traversing the abstract syntax tree.
376 */
377 create(context: Readonly<RuleContext<TMessageIds, TOptions>>): TRuleListener;
378}
379declare type RuleCreateFunction<TMessageIds extends string = never, TOptions extends readonly unknown[] = unknown[], TRuleListener extends RuleListener = RuleListener> = (context: Readonly<RuleContext<TMessageIds, TOptions>>) => TRuleListener;
380export { ReportDescriptor, ReportFixFunction, ReportSuggestionArray, RuleContext, RuleCreateFunction, RuleFix, RuleFixer, RuleFunction, RuleListener, RuleMetaData, RuleMetaDataDocs, RuleModule, SharedConfigurationSettings, };
381//# sourceMappingURL=Rule.d.ts.map
\No newline at end of file