index.d.ts 9.3 KB

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