1 import { JSONSchema4 } from '../json-schema';
2 import { ParserServices, TSESTree } from '../ts-estree';
3 import { AST } from './AST';
4 import { Linter } from './Linter';
5 import { Scope } from './Scope';
6 import { SourceCode } from './SourceCode';
7 interface RuleMetaDataDocs {
9 * The general category the rule falls within
11 category: 'Best Practices' | 'Stylistic Issues' | 'Variables' | 'Possible Errors';
13 * Concise description of the rule
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
21 recommended: 'error' | 'warn' | false;
23 * The URL of the rule's docs
27 * Specifies whether the rule can return suggestions.
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.
34 requiresTypeChecking?: boolean;
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.
40 extendsBaseRule?: boolean | string;
42 interface RuleMetaData<TMessageIds extends string> {
44 * True if the rule is deprecated, false otherwise
48 * Documentation for the rule, unnecessary for custom rules/plugins
50 docs?: RuleMetaDataDocs;
52 * The fixer category. Omit if there is no fixer
54 fixable?: 'code' | 'whitespace';
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
60 messages: Record<TMessageIds, string>;
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.
67 type: 'suggestion' | 'problem' | 'layout';
69 * The name of the rule this rule was replaced by, if it was deprecated.
71 replacedBy?: string[];
73 * The options schema. Supply an empty array if there are no options.
75 schema: JSONSchema4 | JSONSchema4[];
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;
91 declare type ReportFixFunction = (fixer: RuleFixer) => null | RuleFix | RuleFix[] | IterableIterator<RuleFix>;
92 declare type ReportSuggestionArray<TMessageIds extends string> = ReportDescriptorBase<TMessageIds>[];
93 interface ReportDescriptorBase<TMessageIds extends string> {
95 * The parameters for the message string associated with `messageId`.
97 readonly data?: Readonly<Record<string, unknown>>;
101 readonly fix?: ReportFixFunction | null;
103 * The messageId which is being reported.
105 readonly messageId: TMessageIds;
107 interface ReportDescriptorWithSuggestion<TMessageIds extends string> extends ReportDescriptorBase<TMessageIds> {
109 * 6.7's Suggestions API
111 readonly suggest?: Readonly<ReportSuggestionArray<TMessageIds>> | null;
113 interface ReportDescriptorNodeOptionalLoc {
115 * The Node or AST Token which the report is being attached to
117 readonly node: TSESTree.Node | TSESTree.Comment | TSESTree.Token;
119 * An override of the location of the report
121 readonly loc?: Readonly<TSESTree.SourceLocation> | Readonly<TSESTree.LineAndColumnData>;
123 interface ReportDescriptorLocOnly {
125 * An override of the location of the report
127 loc: Readonly<TSESTree.SourceLocation> | Readonly<TSESTree.LineAndColumnData>;
129 declare type ReportDescriptor<TMessageIds extends string> = ReportDescriptorWithSuggestion<TMessageIds> & (ReportDescriptorNodeOptionalLoc | ReportDescriptorLocOnly);
130 interface RuleContext<TMessageIds extends string, TOptions extends readonly unknown[]> {
136 * An array of the configured options for this rule.
137 * This array does not include the rule severity.
141 * The name of the parser from configuration.
145 * The parser options configured for this run
147 parserOptions: Linter.ParserOptions;
149 * An object containing parser-provided services for rules
151 parserServices?: ParserServices;
153 * The shared settings from configuration.
154 * We do not have any shared settings in this plugin.
156 settings: Record<string, unknown>;
158 * Returns an array of the ancestors of the currently-traversed node, starting at
159 * the root of the AST and continuing through the direct parent of the current node.
160 * This array does not include the currently-traversed node itself.
162 getAncestors(): TSESTree.Node[];
164 * Returns a list of variables declared by the given node.
165 * This information can be used to track references to variables.
167 getDeclaredVariables(node: TSESTree.Node): Scope.Variable[];
169 * Returns the filename associated with the source.
171 getFilename(): string;
173 * Returns the scope of the currently-traversed node.
174 * This information can be used track references to variables.
176 getScope(): Scope.Scope;
178 * Returns a SourceCode object that you can use to work with the source that
179 * was passed to ESLint.
181 getSourceCode(): Readonly<SourceCode>;
183 * Marks a variable with the given name in the current scope as used.
184 * This affects the no-unused-vars rule.
186 markVariableAsUsed(name: string): boolean;
188 * Reports a problem in the code.
190 report(descriptor: ReportDescriptor<TMessageIds>): void;
192 declare type RuleFunction<T extends TSESTree.BaseNode = never> = (node: T) => void;
193 interface RuleListener {
194 [nodeSelector: string]: RuleFunction | undefined;
195 ArrayExpression?: RuleFunction<TSESTree.ArrayExpression>;
196 ArrayPattern?: RuleFunction<TSESTree.ArrayPattern>;
197 ArrowFunctionExpression?: RuleFunction<TSESTree.ArrowFunctionExpression>;
198 AssignmentPattern?: RuleFunction<TSESTree.AssignmentPattern>;
199 AssignmentExpression?: RuleFunction<TSESTree.AssignmentExpression>;
200 AwaitExpression?: RuleFunction<TSESTree.AwaitExpression>;
201 BigIntLiteral?: RuleFunction<TSESTree.BigIntLiteral>;
202 BinaryExpression?: RuleFunction<TSESTree.BinaryExpression>;
203 BlockStatement?: RuleFunction<TSESTree.BlockStatement>;
204 BreakStatement?: RuleFunction<TSESTree.BreakStatement>;
205 CallExpression?: RuleFunction<TSESTree.CallExpression>;
206 CatchClause?: RuleFunction<TSESTree.CatchClause>;
207 ClassBody?: RuleFunction<TSESTree.ClassBody>;
208 ClassDeclaration?: RuleFunction<TSESTree.ClassDeclaration>;
209 ClassExpression?: RuleFunction<TSESTree.ClassExpression>;
210 ClassProperty?: RuleFunction<TSESTree.ClassProperty>;
211 Comment?: RuleFunction<TSESTree.Comment>;
212 ConditionalExpression?: RuleFunction<TSESTree.ConditionalExpression>;
213 ContinueStatement?: RuleFunction<TSESTree.ContinueStatement>;
214 DebuggerStatement?: RuleFunction<TSESTree.DebuggerStatement>;
215 Decorator?: RuleFunction<TSESTree.Decorator>;
216 DoWhileStatement?: RuleFunction<TSESTree.DoWhileStatement>;
217 EmptyStatement?: RuleFunction<TSESTree.EmptyStatement>;
218 ExportAllDeclaration?: RuleFunction<TSESTree.ExportAllDeclaration>;
219 ExportDefaultDeclaration?: RuleFunction<TSESTree.ExportDefaultDeclaration>;
220 ExportNamedDeclaration?: RuleFunction<TSESTree.ExportNamedDeclaration>;
221 ExportSpecifier?: RuleFunction<TSESTree.ExportSpecifier>;
222 ExpressionStatement?: RuleFunction<TSESTree.ExpressionStatement>;
223 ForInStatement?: RuleFunction<TSESTree.ForInStatement>;
224 ForOfStatement?: RuleFunction<TSESTree.ForOfStatement>;
225 ForStatement?: RuleFunction<TSESTree.ForStatement>;
226 FunctionDeclaration?: RuleFunction<TSESTree.FunctionDeclaration>;
227 FunctionExpression?: RuleFunction<TSESTree.FunctionExpression>;
228 Identifier?: RuleFunction<TSESTree.Identifier>;
229 IfStatement?: RuleFunction<TSESTree.IfStatement>;
230 ImportDeclaration?: RuleFunction<TSESTree.ImportDeclaration>;
231 ImportDefaultSpecifier?: RuleFunction<TSESTree.ImportDefaultSpecifier>;
232 ImportExpression?: RuleFunction<TSESTree.ImportExpression>;
233 ImportNamespaceSpecifier?: RuleFunction<TSESTree.ImportNamespaceSpecifier>;
234 ImportSpecifier?: RuleFunction<TSESTree.ImportSpecifier>;
235 JSXAttribute?: RuleFunction<TSESTree.JSXAttribute>;
236 JSXClosingElement?: RuleFunction<TSESTree.JSXClosingElement>;
237 JSXClosingFragment?: RuleFunction<TSESTree.JSXClosingFragment>;
238 JSXElement?: RuleFunction<TSESTree.JSXElement>;
239 JSXEmptyExpression?: RuleFunction<TSESTree.JSXEmptyExpression>;
240 JSXExpressionContainer?: RuleFunction<TSESTree.JSXExpressionContainer>;
241 JSXFragment?: RuleFunction<TSESTree.JSXFragment>;
242 JSXIdentifier?: RuleFunction<TSESTree.JSXIdentifier>;
243 JSXMemberExpression?: RuleFunction<TSESTree.JSXMemberExpression>;
244 JSXOpeningElement?: RuleFunction<TSESTree.JSXOpeningElement>;
245 JSXOpeningFragment?: RuleFunction<TSESTree.JSXOpeningFragment>;
246 JSXSpreadAttribute?: RuleFunction<TSESTree.JSXSpreadAttribute>;
247 JSXSpreadChild?: RuleFunction<TSESTree.JSXSpreadChild>;
248 JSXText?: RuleFunction<TSESTree.JSXText>;
249 LabeledStatement?: RuleFunction<TSESTree.LabeledStatement>;
250 Literal?: RuleFunction<TSESTree.Literal>;
251 LogicalExpression?: RuleFunction<TSESTree.LogicalExpression>;
252 MemberExpression?: RuleFunction<TSESTree.MemberExpression>;
253 MetaProperty?: RuleFunction<TSESTree.MetaProperty>;
254 MethodDefinition?: RuleFunction<TSESTree.MethodDefinition>;
255 NewExpression?: RuleFunction<TSESTree.NewExpression>;
256 ObjectExpression?: RuleFunction<TSESTree.ObjectExpression>;
257 ObjectPattern?: RuleFunction<TSESTree.ObjectPattern>;
258 OptionalCallExpression?: RuleFunction<TSESTree.OptionalCallExpression>;
259 OptionalMemberExpression?: RuleFunction<TSESTree.OptionalMemberExpression>;
260 Program?: RuleFunction<TSESTree.Program>;
261 Property?: RuleFunction<TSESTree.Property>;
262 RestElement?: RuleFunction<TSESTree.RestElement>;
263 ReturnStatement?: RuleFunction<TSESTree.ReturnStatement>;
264 SequenceExpression?: RuleFunction<TSESTree.SequenceExpression>;
265 SpreadElement?: RuleFunction<TSESTree.SpreadElement>;
266 Super?: RuleFunction<TSESTree.Super>;
267 SwitchCase?: RuleFunction<TSESTree.SwitchCase>;
268 SwitchStatement?: RuleFunction<TSESTree.SwitchStatement>;
269 TaggedTemplateExpression?: RuleFunction<TSESTree.TaggedTemplateExpression>;
270 TemplateElement?: RuleFunction<TSESTree.TemplateElement>;
271 TemplateLiteral?: RuleFunction<TSESTree.TemplateLiteral>;
272 ThisExpression?: RuleFunction<TSESTree.ThisExpression>;
273 ThrowStatement?: RuleFunction<TSESTree.ThrowStatement>;
274 Token?: RuleFunction<TSESTree.Token>;
275 TryStatement?: RuleFunction<TSESTree.TryStatement>;
276 TSAbstractClassProperty?: RuleFunction<TSESTree.TSAbstractClassProperty>;
277 TSAbstractKeyword?: RuleFunction<TSESTree.TSAbstractKeyword>;
278 TSAbstractMethodDefinition?: RuleFunction<TSESTree.TSAbstractMethodDefinition>;
279 TSAnyKeyword?: RuleFunction<TSESTree.TSAnyKeyword>;
280 TSArrayType?: RuleFunction<TSESTree.TSArrayType>;
281 TSAsExpression?: RuleFunction<TSESTree.TSAsExpression>;
282 TSAsyncKeyword?: RuleFunction<TSESTree.TSAsyncKeyword>;
283 TSBigIntKeyword?: RuleFunction<TSESTree.TSBigIntKeyword>;
284 TSBooleanKeyword?: RuleFunction<TSESTree.TSBooleanKeyword>;
285 TSCallSignatureDeclaration?: RuleFunction<TSESTree.TSCallSignatureDeclaration>;
286 TSClassImplements?: RuleFunction<TSESTree.TSClassImplements>;
287 TSConditionalType?: RuleFunction<TSESTree.TSConditionalType>;
288 TSConstructorType?: RuleFunction<TSESTree.TSConstructorType>;
289 TSConstructSignatureDeclaration?: RuleFunction<TSESTree.TSConstructSignatureDeclaration>;
290 TSDeclareKeyword?: RuleFunction<TSESTree.TSDeclareKeyword>;
291 TSDeclareFunction?: RuleFunction<TSESTree.TSDeclareFunction>;
292 TSEmptyBodyFunctionExpression?: RuleFunction<TSESTree.TSEmptyBodyFunctionExpression>;
293 TSEnumDeclaration?: RuleFunction<TSESTree.TSEnumDeclaration>;
294 TSEnumMember?: RuleFunction<TSESTree.TSEnumMember>;
295 TSExportAssignment?: RuleFunction<TSESTree.TSExportAssignment>;
296 TSExportKeyword?: RuleFunction<TSESTree.TSExportKeyword>;
297 TSExternalModuleReference?: RuleFunction<TSESTree.TSExternalModuleReference>;
298 TSFunctionType?: RuleFunction<TSESTree.TSFunctionType>;
299 TSImportEqualsDeclaration?: RuleFunction<TSESTree.TSImportEqualsDeclaration>;
300 TSImportType?: RuleFunction<TSESTree.TSImportType>;
301 TSIndexedAccessType?: RuleFunction<TSESTree.TSIndexedAccessType>;
302 TSIndexSignature?: RuleFunction<TSESTree.TSIndexSignature>;
303 TSInferType?: RuleFunction<TSESTree.TSInferType>;
304 TSInterfaceBody?: RuleFunction<TSESTree.TSInterfaceBody>;
305 TSInterfaceDeclaration?: RuleFunction<TSESTree.TSInterfaceDeclaration>;
306 TSInterfaceHeritage?: RuleFunction<TSESTree.TSInterfaceHeritage>;
307 TSIntersectionType?: RuleFunction<TSESTree.TSIntersectionType>;
308 TSLiteralType?: RuleFunction<TSESTree.TSLiteralType>;
309 TSMappedType?: RuleFunction<TSESTree.TSMappedType>;
310 TSMethodSignature?: RuleFunction<TSESTree.TSMethodSignature>;
311 TSModuleBlock?: RuleFunction<TSESTree.TSModuleBlock>;
312 TSModuleDeclaration?: RuleFunction<TSESTree.TSModuleDeclaration>;
313 TSNamespaceExportDeclaration?: RuleFunction<TSESTree.TSNamespaceExportDeclaration>;
314 TSNeverKeyword?: RuleFunction<TSESTree.TSNeverKeyword>;
315 TSNonNullExpression?: RuleFunction<TSESTree.TSNonNullExpression>;
316 TSNullKeyword?: RuleFunction<TSESTree.TSNullKeyword>;
317 TSNumberKeyword?: RuleFunction<TSESTree.TSNumberKeyword>;
318 TSObjectKeyword?: RuleFunction<TSESTree.TSObjectKeyword>;
319 TSOptionalType?: RuleFunction<TSESTree.TSOptionalType>;
320 TSParameterProperty?: RuleFunction<TSESTree.TSParameterProperty>;
321 TSParenthesizedType?: RuleFunction<TSESTree.TSParenthesizedType>;
322 TSPrivateKeyword?: RuleFunction<TSESTree.TSPrivateKeyword>;
323 TSPropertySignature?: RuleFunction<TSESTree.TSPropertySignature>;
324 TSProtectedKeyword?: RuleFunction<TSESTree.TSProtectedKeyword>;
325 TSPublicKeyword?: RuleFunction<TSESTree.TSPublicKeyword>;
326 TSQualifiedName?: RuleFunction<TSESTree.TSQualifiedName>;
327 TSReadonlyKeyword?: RuleFunction<TSESTree.TSReadonlyKeyword>;
328 TSRestType?: RuleFunction<TSESTree.TSRestType>;
329 TSStaticKeyword?: RuleFunction<TSESTree.TSStaticKeyword>;
330 TSStringKeyword?: RuleFunction<TSESTree.TSStringKeyword>;
331 TSSymbolKeyword?: RuleFunction<TSESTree.TSSymbolKeyword>;
332 TSThisType?: RuleFunction<TSESTree.TSThisType>;
333 TSTupleType?: RuleFunction<TSESTree.TSTupleType>;
334 TSTypeAliasDeclaration?: RuleFunction<TSESTree.TSTypeAliasDeclaration>;
335 TSTypeAnnotation?: RuleFunction<TSESTree.TSTypeAnnotation>;
336 TSTypeAssertion?: RuleFunction<TSESTree.TSTypeAssertion>;
337 TSTypeLiteral?: RuleFunction<TSESTree.TSTypeLiteral>;
338 TSTypeOperator?: RuleFunction<TSESTree.TSTypeOperator>;
339 TSTypeParameter?: RuleFunction<TSESTree.TSTypeParameter>;
340 TSTypeParameterDeclaration?: RuleFunction<TSESTree.TSTypeParameterDeclaration>;
341 TSTypeParameterInstantiation?: RuleFunction<TSESTree.TSTypeParameterInstantiation>;
342 TSTypePredicate?: RuleFunction<TSESTree.TSTypePredicate>;
343 TSTypeQuery?: RuleFunction<TSESTree.TSTypeQuery>;
344 TSTypeReference?: RuleFunction<TSESTree.TSTypeReference>;
345 TSUndefinedKeyword?: RuleFunction<TSESTree.TSUndefinedKeyword>;
346 TSUnionType?: RuleFunction<TSESTree.TSUnionType>;
347 TSUnknownKeyword?: RuleFunction<TSESTree.TSUnknownKeyword>;
348 TSVoidKeyword?: RuleFunction<TSESTree.TSVoidKeyword>;
349 UnaryExpression?: RuleFunction<TSESTree.UnaryExpression>;
350 UpdateExpression?: RuleFunction<TSESTree.UpdateExpression>;
351 VariableDeclaration?: RuleFunction<TSESTree.VariableDeclaration>;
352 VariableDeclarator?: RuleFunction<TSESTree.VariableDeclarator>;
353 WhileStatement?: RuleFunction<TSESTree.WhileStatement>;
354 WithStatement?: RuleFunction<TSESTree.WithStatement>;
355 YieldExpression?: RuleFunction<TSESTree.YieldExpression>;
357 interface RuleModule<TMessageIds extends string, TOptions extends readonly unknown[], TRuleListener extends RuleListener = RuleListener> {
359 * Metadata about the rule
361 meta: RuleMetaData<TMessageIds>;
363 * Function which returns an object with methods that ESLint calls to “visit”
364 * nodes while traversing the abstract syntax tree.
366 create(context: Readonly<RuleContext<TMessageIds, TOptions>>): TRuleListener;
368 declare type RuleCreateFunction = (context: Readonly<RuleContext<never, unknown[]>>) => RuleListener;
369 export { ReportDescriptor, ReportFixFunction, ReportSuggestionArray, RuleContext, RuleCreateFunction, RuleFix, RuleFixer, RuleFunction, RuleListener, RuleMetaData, RuleMetaDataDocs, RuleModule, };
370 //# sourceMappingURL=Rule.d.ts.map