UNPKG

18.7 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);
130interface RuleContext<TMessageIds extends string, TOptions extends readonly unknown[]> {
131 /**
132 * The rule ID.
133 */
134 id: string;
135 /**
136 * An array of the configured options for this rule.
137 * This array does not include the rule severity.
138 */
139 options: TOptions;
140 /**
141 * The name of the parser from configuration.
142 */
143 parserPath: string;
144 /**
145 * The parser options configured for this run
146 */
147 parserOptions: Linter.ParserOptions;
148 /**
149 * An object containing parser-provided services for rules
150 */
151 parserServices?: ParserServices;
152 /**
153 * The shared settings from configuration.
154 * We do not have any shared settings in this plugin.
155 */
156 settings: Record<string, unknown>;
157 /**
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.
161 */
162 getAncestors(): TSESTree.Node[];
163 /**
164 * Returns a list of variables declared by the given node.
165 * This information can be used to track references to variables.
166 */
167 getDeclaredVariables(node: TSESTree.Node): Scope.Variable[];
168 /**
169 * Returns the current working directory passed to Linter.
170 * It is a path to a directory that should be considered as the current working directory.
171 * This was added in v6.6.0
172 * @since 6.6.0
173 */
174 getCwd?(): string;
175 /**
176 * Returns the filename associated with the source.
177 */
178 getFilename(): string;
179 /**
180 * Returns the scope of the currently-traversed node.
181 * This information can be used track references to variables.
182 */
183 getScope(): Scope.Scope;
184 /**
185 * Returns a SourceCode object that you can use to work with the source that
186 * was passed to ESLint.
187 */
188 getSourceCode(): Readonly<SourceCode>;
189 /**
190 * Marks a variable with the given name in the current scope as used.
191 * This affects the no-unused-vars rule.
192 */
193 markVariableAsUsed(name: string): boolean;
194 /**
195 * Reports a problem in the code.
196 */
197 report(descriptor: ReportDescriptor<TMessageIds>): void;
198}
199declare type RuleFunction<T extends TSESTree.BaseNode = never> = (node: T) => void;
200interface RuleListener {
201 [nodeSelector: string]: RuleFunction | undefined;
202 ArrayExpression?: RuleFunction<TSESTree.ArrayExpression>;
203 ArrayPattern?: RuleFunction<TSESTree.ArrayPattern>;
204 ArrowFunctionExpression?: RuleFunction<TSESTree.ArrowFunctionExpression>;
205 AssignmentPattern?: RuleFunction<TSESTree.AssignmentPattern>;
206 AssignmentExpression?: RuleFunction<TSESTree.AssignmentExpression>;
207 AwaitExpression?: RuleFunction<TSESTree.AwaitExpression>;
208 BigIntLiteral?: RuleFunction<TSESTree.BigIntLiteral>;
209 BinaryExpression?: RuleFunction<TSESTree.BinaryExpression>;
210 BlockStatement?: RuleFunction<TSESTree.BlockStatement>;
211 BreakStatement?: RuleFunction<TSESTree.BreakStatement>;
212 CallExpression?: RuleFunction<TSESTree.CallExpression>;
213 CatchClause?: RuleFunction<TSESTree.CatchClause>;
214 ChainExpression?: RuleFunction<TSESTree.ChainExpression>;
215 ClassBody?: RuleFunction<TSESTree.ClassBody>;
216 ClassDeclaration?: RuleFunction<TSESTree.ClassDeclaration>;
217 ClassExpression?: RuleFunction<TSESTree.ClassExpression>;
218 ClassProperty?: RuleFunction<TSESTree.ClassProperty>;
219 Comment?: RuleFunction<TSESTree.Comment>;
220 ConditionalExpression?: RuleFunction<TSESTree.ConditionalExpression>;
221 ContinueStatement?: RuleFunction<TSESTree.ContinueStatement>;
222 DebuggerStatement?: RuleFunction<TSESTree.DebuggerStatement>;
223 Decorator?: RuleFunction<TSESTree.Decorator>;
224 DoWhileStatement?: RuleFunction<TSESTree.DoWhileStatement>;
225 EmptyStatement?: RuleFunction<TSESTree.EmptyStatement>;
226 ExportAllDeclaration?: RuleFunction<TSESTree.ExportAllDeclaration>;
227 ExportDefaultDeclaration?: RuleFunction<TSESTree.ExportDefaultDeclaration>;
228 ExportNamedDeclaration?: RuleFunction<TSESTree.ExportNamedDeclaration>;
229 ExportSpecifier?: RuleFunction<TSESTree.ExportSpecifier>;
230 ExpressionStatement?: RuleFunction<TSESTree.ExpressionStatement>;
231 ForInStatement?: RuleFunction<TSESTree.ForInStatement>;
232 ForOfStatement?: RuleFunction<TSESTree.ForOfStatement>;
233 ForStatement?: RuleFunction<TSESTree.ForStatement>;
234 FunctionDeclaration?: RuleFunction<TSESTree.FunctionDeclaration>;
235 FunctionExpression?: RuleFunction<TSESTree.FunctionExpression>;
236 Identifier?: RuleFunction<TSESTree.Identifier>;
237 IfStatement?: RuleFunction<TSESTree.IfStatement>;
238 ImportDeclaration?: RuleFunction<TSESTree.ImportDeclaration>;
239 ImportDefaultSpecifier?: RuleFunction<TSESTree.ImportDefaultSpecifier>;
240 ImportExpression?: RuleFunction<TSESTree.ImportExpression>;
241 ImportNamespaceSpecifier?: RuleFunction<TSESTree.ImportNamespaceSpecifier>;
242 ImportSpecifier?: RuleFunction<TSESTree.ImportSpecifier>;
243 JSXAttribute?: RuleFunction<TSESTree.JSXAttribute>;
244 JSXClosingElement?: RuleFunction<TSESTree.JSXClosingElement>;
245 JSXClosingFragment?: RuleFunction<TSESTree.JSXClosingFragment>;
246 JSXElement?: RuleFunction<TSESTree.JSXElement>;
247 JSXEmptyExpression?: RuleFunction<TSESTree.JSXEmptyExpression>;
248 JSXExpressionContainer?: RuleFunction<TSESTree.JSXExpressionContainer>;
249 JSXFragment?: RuleFunction<TSESTree.JSXFragment>;
250 JSXIdentifier?: RuleFunction<TSESTree.JSXIdentifier>;
251 JSXMemberExpression?: RuleFunction<TSESTree.JSXMemberExpression>;
252 JSXOpeningElement?: RuleFunction<TSESTree.JSXOpeningElement>;
253 JSXOpeningFragment?: RuleFunction<TSESTree.JSXOpeningFragment>;
254 JSXSpreadAttribute?: RuleFunction<TSESTree.JSXSpreadAttribute>;
255 JSXSpreadChild?: RuleFunction<TSESTree.JSXSpreadChild>;
256 JSXText?: RuleFunction<TSESTree.JSXText>;
257 LabeledStatement?: RuleFunction<TSESTree.LabeledStatement>;
258 Literal?: RuleFunction<TSESTree.Literal>;
259 LogicalExpression?: RuleFunction<TSESTree.LogicalExpression>;
260 MemberExpression?: RuleFunction<TSESTree.MemberExpression>;
261 MetaProperty?: RuleFunction<TSESTree.MetaProperty>;
262 MethodDefinition?: RuleFunction<TSESTree.MethodDefinition>;
263 NewExpression?: RuleFunction<TSESTree.NewExpression>;
264 ObjectExpression?: RuleFunction<TSESTree.ObjectExpression>;
265 ObjectPattern?: RuleFunction<TSESTree.ObjectPattern>;
266 Program?: RuleFunction<TSESTree.Program>;
267 Property?: RuleFunction<TSESTree.Property>;
268 RestElement?: RuleFunction<TSESTree.RestElement>;
269 ReturnStatement?: RuleFunction<TSESTree.ReturnStatement>;
270 SequenceExpression?: RuleFunction<TSESTree.SequenceExpression>;
271 SpreadElement?: RuleFunction<TSESTree.SpreadElement>;
272 Super?: RuleFunction<TSESTree.Super>;
273 SwitchCase?: RuleFunction<TSESTree.SwitchCase>;
274 SwitchStatement?: RuleFunction<TSESTree.SwitchStatement>;
275 TaggedTemplateExpression?: RuleFunction<TSESTree.TaggedTemplateExpression>;
276 TemplateElement?: RuleFunction<TSESTree.TemplateElement>;
277 TemplateLiteral?: RuleFunction<TSESTree.TemplateLiteral>;
278 ThisExpression?: RuleFunction<TSESTree.ThisExpression>;
279 ThrowStatement?: RuleFunction<TSESTree.ThrowStatement>;
280 Token?: RuleFunction<TSESTree.Token>;
281 TryStatement?: RuleFunction<TSESTree.TryStatement>;
282 TSAbstractClassProperty?: RuleFunction<TSESTree.TSAbstractClassProperty>;
283 TSAbstractKeyword?: RuleFunction<TSESTree.TSAbstractKeyword>;
284 TSAbstractMethodDefinition?: RuleFunction<TSESTree.TSAbstractMethodDefinition>;
285 TSAnyKeyword?: RuleFunction<TSESTree.TSAnyKeyword>;
286 TSArrayType?: RuleFunction<TSESTree.TSArrayType>;
287 TSAsExpression?: RuleFunction<TSESTree.TSAsExpression>;
288 TSAsyncKeyword?: RuleFunction<TSESTree.TSAsyncKeyword>;
289 TSBigIntKeyword?: RuleFunction<TSESTree.TSBigIntKeyword>;
290 TSBooleanKeyword?: RuleFunction<TSESTree.TSBooleanKeyword>;
291 TSCallSignatureDeclaration?: RuleFunction<TSESTree.TSCallSignatureDeclaration>;
292 TSClassImplements?: RuleFunction<TSESTree.TSClassImplements>;
293 TSConditionalType?: RuleFunction<TSESTree.TSConditionalType>;
294 TSConstructorType?: RuleFunction<TSESTree.TSConstructorType>;
295 TSConstructSignatureDeclaration?: RuleFunction<TSESTree.TSConstructSignatureDeclaration>;
296 TSDeclareKeyword?: RuleFunction<TSESTree.TSDeclareKeyword>;
297 TSDeclareFunction?: RuleFunction<TSESTree.TSDeclareFunction>;
298 TSEmptyBodyFunctionExpression?: RuleFunction<TSESTree.TSEmptyBodyFunctionExpression>;
299 TSEnumDeclaration?: RuleFunction<TSESTree.TSEnumDeclaration>;
300 TSEnumMember?: RuleFunction<TSESTree.TSEnumMember>;
301 TSExportAssignment?: RuleFunction<TSESTree.TSExportAssignment>;
302 TSExportKeyword?: RuleFunction<TSESTree.TSExportKeyword>;
303 TSExternalModuleReference?: RuleFunction<TSESTree.TSExternalModuleReference>;
304 TSFunctionType?: RuleFunction<TSESTree.TSFunctionType>;
305 TSImportEqualsDeclaration?: RuleFunction<TSESTree.TSImportEqualsDeclaration>;
306 TSImportType?: RuleFunction<TSESTree.TSImportType>;
307 TSIndexedAccessType?: RuleFunction<TSESTree.TSIndexedAccessType>;
308 TSIndexSignature?: RuleFunction<TSESTree.TSIndexSignature>;
309 TSInferType?: RuleFunction<TSESTree.TSInferType>;
310 TSInterfaceBody?: RuleFunction<TSESTree.TSInterfaceBody>;
311 TSInterfaceDeclaration?: RuleFunction<TSESTree.TSInterfaceDeclaration>;
312 TSInterfaceHeritage?: RuleFunction<TSESTree.TSInterfaceHeritage>;
313 TSIntersectionType?: RuleFunction<TSESTree.TSIntersectionType>;
314 TSLiteralType?: RuleFunction<TSESTree.TSLiteralType>;
315 TSMappedType?: RuleFunction<TSESTree.TSMappedType>;
316 TSMethodSignature?: RuleFunction<TSESTree.TSMethodSignature>;
317 TSModuleBlock?: RuleFunction<TSESTree.TSModuleBlock>;
318 TSModuleDeclaration?: RuleFunction<TSESTree.TSModuleDeclaration>;
319 TSNamespaceExportDeclaration?: RuleFunction<TSESTree.TSNamespaceExportDeclaration>;
320 TSNeverKeyword?: RuleFunction<TSESTree.TSNeverKeyword>;
321 TSNonNullExpression?: RuleFunction<TSESTree.TSNonNullExpression>;
322 TSNullKeyword?: RuleFunction<TSESTree.TSNullKeyword>;
323 TSNumberKeyword?: RuleFunction<TSESTree.TSNumberKeyword>;
324 TSObjectKeyword?: RuleFunction<TSESTree.TSObjectKeyword>;
325 TSOptionalType?: RuleFunction<TSESTree.TSOptionalType>;
326 TSParameterProperty?: RuleFunction<TSESTree.TSParameterProperty>;
327 TSParenthesizedType?: RuleFunction<TSESTree.TSParenthesizedType>;
328 TSPrivateKeyword?: RuleFunction<TSESTree.TSPrivateKeyword>;
329 TSPropertySignature?: RuleFunction<TSESTree.TSPropertySignature>;
330 TSProtectedKeyword?: RuleFunction<TSESTree.TSProtectedKeyword>;
331 TSPublicKeyword?: RuleFunction<TSESTree.TSPublicKeyword>;
332 TSQualifiedName?: RuleFunction<TSESTree.TSQualifiedName>;
333 TSReadonlyKeyword?: RuleFunction<TSESTree.TSReadonlyKeyword>;
334 TSRestType?: RuleFunction<TSESTree.TSRestType>;
335 TSStaticKeyword?: RuleFunction<TSESTree.TSStaticKeyword>;
336 TSStringKeyword?: RuleFunction<TSESTree.TSStringKeyword>;
337 TSSymbolKeyword?: RuleFunction<TSESTree.TSSymbolKeyword>;
338 TSThisType?: RuleFunction<TSESTree.TSThisType>;
339 TSTupleType?: RuleFunction<TSESTree.TSTupleType>;
340 TSTypeAliasDeclaration?: RuleFunction<TSESTree.TSTypeAliasDeclaration>;
341 TSTypeAnnotation?: RuleFunction<TSESTree.TSTypeAnnotation>;
342 TSTypeAssertion?: RuleFunction<TSESTree.TSTypeAssertion>;
343 TSTypeLiteral?: RuleFunction<TSESTree.TSTypeLiteral>;
344 TSTypeOperator?: RuleFunction<TSESTree.TSTypeOperator>;
345 TSTypeParameter?: RuleFunction<TSESTree.TSTypeParameter>;
346 TSTypeParameterDeclaration?: RuleFunction<TSESTree.TSTypeParameterDeclaration>;
347 TSTypeParameterInstantiation?: RuleFunction<TSESTree.TSTypeParameterInstantiation>;
348 TSTypePredicate?: RuleFunction<TSESTree.TSTypePredicate>;
349 TSTypeQuery?: RuleFunction<TSESTree.TSTypeQuery>;
350 TSTypeReference?: RuleFunction<TSESTree.TSTypeReference>;
351 TSUndefinedKeyword?: RuleFunction<TSESTree.TSUndefinedKeyword>;
352 TSUnionType?: RuleFunction<TSESTree.TSUnionType>;
353 TSUnknownKeyword?: RuleFunction<TSESTree.TSUnknownKeyword>;
354 TSVoidKeyword?: RuleFunction<TSESTree.TSVoidKeyword>;
355 UnaryExpression?: RuleFunction<TSESTree.UnaryExpression>;
356 UpdateExpression?: RuleFunction<TSESTree.UpdateExpression>;
357 VariableDeclaration?: RuleFunction<TSESTree.VariableDeclaration>;
358 VariableDeclarator?: RuleFunction<TSESTree.VariableDeclarator>;
359 WhileStatement?: RuleFunction<TSESTree.WhileStatement>;
360 WithStatement?: RuleFunction<TSESTree.WithStatement>;
361 YieldExpression?: RuleFunction<TSESTree.YieldExpression>;
362}
363interface RuleModule<TMessageIds extends string, TOptions extends readonly unknown[], TRuleListener extends RuleListener = RuleListener> {
364 /**
365 * Metadata about the rule
366 */
367 meta: RuleMetaData<TMessageIds>;
368 /**
369 * Function which returns an object with methods that ESLint calls to “visit”
370 * nodes while traversing the abstract syntax tree.
371 */
372 create(context: Readonly<RuleContext<TMessageIds, TOptions>>): TRuleListener;
373}
374declare type RuleCreateFunction<TMessageIds extends string = never, TOptions extends readonly unknown[] = unknown[], TRuleListener extends RuleListener = RuleListener> = (context: Readonly<RuleContext<TMessageIds, TOptions>>) => TRuleListener;
375export { ReportDescriptor, ReportFixFunction, ReportSuggestionArray, RuleContext, RuleCreateFunction, RuleFix, RuleFixer, RuleFunction, RuleListener, RuleMetaData, RuleMetaDataDocs, RuleModule, };
376//# sourceMappingURL=Rule.d.ts.map
\No newline at end of file