index.d.cts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. export type VisitTraversalStep = $eslintcore.VisitTraversalStep;
  2. export type CallTraversalStep = $eslintcore.CallTraversalStep;
  3. export type TraversalStep = $eslintcore.TraversalStep;
  4. export type SourceLocation = $eslintcore.SourceLocation;
  5. export type SourceLocationWithOffset = $eslintcore.SourceLocationWithOffset;
  6. export type SourceRange = $eslintcore.SourceRange;
  7. export type IDirective = $eslintcore.Directive;
  8. export type DirectiveType = $eslintcore.DirectiveType;
  9. export type SourceCodeBaseTypeOptions = $eslintcore.SourceCodeBaseTypeOptions;
  10. /**
  11. * <Options>
  12. */
  13. export type TextSourceCode<Options extends SourceCodeBaseTypeOptions = $eslintcore.SourceCodeBaseTypeOptions> = import("@eslint/core").TextSourceCode<Options>;
  14. export type RuleConfig = $eslintcore.RuleConfig;
  15. export type RulesConfig = $eslintcore.RulesConfig;
  16. export type StringConfig = $typests.StringConfig;
  17. export type BooleanConfig = $typests.BooleanConfig;
  18. /**
  19. * A class to represent a step in the traversal process where a
  20. * method is called.
  21. * @implements {CallTraversalStep}
  22. */
  23. export class CallMethodStep implements CallTraversalStep {
  24. /**
  25. * Creates a new instance.
  26. * @param {Object} options The options for the step.
  27. * @param {string} options.target The target of the step.
  28. * @param {Array<any>} options.args The arguments of the step.
  29. */
  30. constructor({ target, args }: {
  31. target: string;
  32. args: Array<any>;
  33. });
  34. /**
  35. * The type of the step.
  36. * @type {"call"}
  37. * @readonly
  38. */
  39. readonly type: "call";
  40. /**
  41. * The kind of the step. Represents the same data as the `type` property
  42. * but it's a number for performance.
  43. * @type {2}
  44. * @readonly
  45. */
  46. readonly kind: 2;
  47. /**
  48. * The name of the method to call.
  49. * @type {string}
  50. */
  51. target: string;
  52. /**
  53. * The arguments to pass to the method.
  54. * @type {Array<any>}
  55. */
  56. args: Array<any>;
  57. }
  58. /**
  59. * Object to parse ESLint configuration comments.
  60. */
  61. export class ConfigCommentParser {
  62. /**
  63. * Parses a list of "name:string_value" or/and "name" options divided by comma or
  64. * whitespace. Used for "global" comments.
  65. * @param {string} string The string to parse.
  66. * @returns {StringConfig} Result map object of names and string values, or null values if no value was provided.
  67. */
  68. parseStringConfig(string: string): StringConfig;
  69. /**
  70. * Parses a JSON-like config.
  71. * @param {string} string The string to parse.
  72. * @returns {({ok: true, config: RulesConfig}|{ok: false, error: {message: string}})} Result map object
  73. */
  74. parseJSONLikeConfig(string: string): ({
  75. ok: true;
  76. config: RulesConfig;
  77. } | {
  78. ok: false;
  79. error: {
  80. message: string;
  81. };
  82. });
  83. /**
  84. * Parses a config of values separated by comma.
  85. * @param {string} string The string to parse.
  86. * @returns {BooleanConfig} Result map of values and true values
  87. */
  88. parseListConfig(string: string): BooleanConfig;
  89. /**
  90. * Parses a directive comment into directive text and value.
  91. * @param {string} string The string with the directive to be parsed.
  92. * @returns {DirectiveComment|undefined} The parsed directive or `undefined` if the directive is invalid.
  93. */
  94. parseDirective(string: string): DirectiveComment | undefined;
  95. #private;
  96. }
  97. /**
  98. * A class to represent a directive comment.
  99. * @implements {IDirective}
  100. */
  101. export class Directive implements IDirective {
  102. /**
  103. * Creates a new instance.
  104. * @param {Object} options The options for the directive.
  105. * @param {"disable"|"enable"|"disable-next-line"|"disable-line"} options.type The type of directive.
  106. * @param {unknown} options.node The node representing the directive.
  107. * @param {string} options.value The value of the directive.
  108. * @param {string} options.justification The justification for the directive.
  109. */
  110. constructor({ type, node, value, justification }: {
  111. type: "disable" | "enable" | "disable-next-line" | "disable-line";
  112. node: unknown;
  113. value: string;
  114. justification: string;
  115. });
  116. /**
  117. * The type of directive.
  118. * @type {DirectiveType}
  119. * @readonly
  120. */
  121. readonly type: DirectiveType;
  122. /**
  123. * The node representing the directive.
  124. * @type {unknown}
  125. * @readonly
  126. */
  127. readonly node: unknown;
  128. /**
  129. * Everything after the "eslint-disable" portion of the directive,
  130. * but before the "--" that indicates the justification.
  131. * @type {string}
  132. * @readonly
  133. */
  134. readonly value: string;
  135. /**
  136. * The justification for the directive.
  137. * @type {string}
  138. * @readonly
  139. */
  140. readonly justification: string;
  141. }
  142. /**
  143. * Source Code Base Object
  144. * @template {SourceCodeBaseTypeOptions & {RootNode: object, SyntaxElementWithLoc: object}} [Options=SourceCodeBaseTypeOptions & {RootNode: object, SyntaxElementWithLoc: object}]
  145. * @implements {TextSourceCode<Options>}
  146. */
  147. export class TextSourceCodeBase<Options extends SourceCodeBaseTypeOptions & {
  148. RootNode: object;
  149. SyntaxElementWithLoc: object;
  150. } = $eslintcore.SourceCodeBaseTypeOptions & {
  151. RootNode: object;
  152. SyntaxElementWithLoc: object;
  153. }> implements TextSourceCode<Options> {
  154. /**
  155. * Creates a new instance.
  156. * @param {Object} options The options for the instance.
  157. * @param {string} options.text The source code text.
  158. * @param {Options['RootNode']} options.ast The root AST node.
  159. * @param {RegExp} [options.lineEndingPattern] The pattern to match lineEndings in the source code. Defaults to `/\r?\n/u`.
  160. */
  161. constructor({ text, ast, lineEndingPattern }: {
  162. text: string;
  163. ast: Options["RootNode"];
  164. lineEndingPattern?: RegExp;
  165. });
  166. /**
  167. * The AST of the source code.
  168. * @type {Options['RootNode']}
  169. */
  170. ast: Options["RootNode"];
  171. /**
  172. * The text of the source code.
  173. * @type {string}
  174. */
  175. text: string;
  176. /**
  177. * Returns the loc information for the given node or token.
  178. * @param {Options['SyntaxElementWithLoc']} nodeOrToken The node or token to get the loc information for.
  179. * @returns {SourceLocation} The loc information for the node or token.
  180. * @throws {Error} If the node or token does not have loc information.
  181. */
  182. getLoc(nodeOrToken: Options["SyntaxElementWithLoc"]): SourceLocation;
  183. /**
  184. * Converts a source text index into a `{ line: number, column: number }` pair.
  185. * @param {number} index The index of a character in a file.
  186. * @throws {TypeError|RangeError} If non-numeric index or index out of range.
  187. * @returns {{line: number, column: number}} A `{ line: number, column: number }` location object with 0 or 1-indexed line and 0 or 1-indexed column based on language.
  188. * @public
  189. */
  190. public getLocFromIndex(index: number): {
  191. line: number;
  192. column: number;
  193. };
  194. /**
  195. * Converts a `{ line: number, column: number }` pair into a source text index.
  196. * @param {Object} loc A line/column location.
  197. * @param {number} loc.line The line number of the location. (0 or 1-indexed based on language.)
  198. * @param {number} loc.column The column number of the location. (0 or 1-indexed based on language.)
  199. * @throws {TypeError|RangeError} If `loc` is not an object with a numeric
  200. * `line` and `column`, if the `line` is less than or equal to zero or
  201. * the `line` or `column` is out of the expected range.
  202. * @returns {number} The index of the line/column location in a file.
  203. * @public
  204. */
  205. public getIndexFromLoc(loc: {
  206. line: number;
  207. column: number;
  208. }): number;
  209. /**
  210. * Returns the range information for the given node or token.
  211. * @param {Options['SyntaxElementWithLoc']} nodeOrToken The node or token to get the range information for.
  212. * @returns {SourceRange} The range information for the node or token.
  213. * @throws {Error} If the node or token does not have range information.
  214. */
  215. getRange(nodeOrToken: Options["SyntaxElementWithLoc"]): SourceRange;
  216. /**
  217. * Returns the parent of the given node.
  218. * @param {Options['SyntaxElementWithLoc']} node The node to get the parent of.
  219. * @returns {Options['SyntaxElementWithLoc']|undefined} The parent of the node.
  220. * @throws {Error} If the method is not implemented in the subclass.
  221. */
  222. getParent(node: Options["SyntaxElementWithLoc"]): Options["SyntaxElementWithLoc"] | undefined;
  223. /**
  224. * Gets all the ancestors of a given node
  225. * @param {Options['SyntaxElementWithLoc']} node The node
  226. * @returns {Array<Options['SyntaxElementWithLoc']>} All the ancestor nodes in the AST, not including the provided node, starting
  227. * from the root node at index 0 and going inwards to the parent node.
  228. * @throws {TypeError} When `node` is missing.
  229. */
  230. getAncestors(node: Options["SyntaxElementWithLoc"]): Array<Options["SyntaxElementWithLoc"]>;
  231. /**
  232. * Gets the source code for the given node.
  233. * @param {Options['SyntaxElementWithLoc']} [node] The AST node to get the text for.
  234. * @param {number} [beforeCount] The number of characters before the node to retrieve.
  235. * @param {number} [afterCount] The number of characters after the node to retrieve.
  236. * @returns {string} The text representing the AST node.
  237. * @public
  238. */
  239. public getText(node?: Options["SyntaxElementWithLoc"], beforeCount?: number, afterCount?: number): string;
  240. /**
  241. * Gets the entire source text split into an array of lines.
  242. * @returns {Array<string>} The source text as an array of lines.
  243. * @public
  244. */
  245. public get lines(): Array<string>;
  246. /**
  247. * Traverse the source code and return the steps that were taken.
  248. * @returns {Iterable<TraversalStep>} The steps that were taken while traversing the source code.
  249. */
  250. traverse(): Iterable<TraversalStep>;
  251. #private;
  252. }
  253. /**
  254. * A class to represent a step in the traversal process where a node is visited.
  255. * @implements {VisitTraversalStep}
  256. */
  257. export class VisitNodeStep implements VisitTraversalStep {
  258. /**
  259. * Creates a new instance.
  260. * @param {Object} options The options for the step.
  261. * @param {object} options.target The target of the step.
  262. * @param {1|2} options.phase The phase of the step.
  263. * @param {Array<any>} options.args The arguments of the step.
  264. */
  265. constructor({ target, phase, args }: {
  266. target: object;
  267. phase: 1 | 2;
  268. args: Array<any>;
  269. });
  270. /**
  271. * The type of the step.
  272. * @type {"visit"}
  273. * @readonly
  274. */
  275. readonly type: "visit";
  276. /**
  277. * The kind of the step. Represents the same data as the `type` property
  278. * but it's a number for performance.
  279. * @type {1}
  280. * @readonly
  281. */
  282. readonly kind: 1;
  283. /**
  284. * The target of the step.
  285. * @type {object}
  286. */
  287. target: object;
  288. /**
  289. * The phase of the step.
  290. * @type {1|2}
  291. */
  292. phase: 1 | 2;
  293. /**
  294. * The arguments of the step.
  295. * @type {Array<any>}
  296. */
  297. args: Array<any>;
  298. }
  299. import type * as $eslintcore from "@eslint/core";
  300. import type * as $typests from "./types.cts";
  301. /**
  302. * Represents a directive comment.
  303. */
  304. declare class DirectiveComment {
  305. /**
  306. * Creates a new directive comment.
  307. * @param {string} label The label of the directive.
  308. * @param {string} value The value of the directive.
  309. * @param {string} justification The justification of the directive.
  310. */
  311. constructor(label: string, value: string, justification: string);
  312. /**
  313. * The label of the directive, such as "eslint", "eslint-disable", etc.
  314. * @type {string}
  315. */
  316. label: string;
  317. /**
  318. * The value of the directive (the string after the label).
  319. * @type {string}
  320. */
  321. value: string;
  322. /**
  323. * The justification of the directive (the string after the --).
  324. * @type {string}
  325. */
  326. justification: string;
  327. }
  328. export {};