Skip to content

Commit d51f979

Browse files
authored
feat!: Add getLoc/getRange to SourceCode interface (#89)
1 parent c94ab2d commit d51f979

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

packages/core/src/types.ts

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ export interface FileProblem {
3131
//------------------------------------------------------------------------------
3232

3333
/**
34-
* Represents an AST node or token with location information.
34+
* Represents an AST node or token with location information in ESLint format.
3535
*/
3636
export interface SyntaxElement {
3737
loc: SourceLocation;
38-
range: [number, number];
39-
[key: string]: any;
38+
range: SourceRange;
4039
}
4140

4241
/**
@@ -48,13 +47,34 @@ export interface SourceLocation {
4847
}
4948

5049
/**
51-
* Represents a location coordinate inside the source.
50+
* Represents the start and end coordinates of a node inside the source with an offset.
51+
*/
52+
export interface SourceLocationWithOffset {
53+
start: PositionWithOffset;
54+
end: PositionWithOffset;
55+
}
56+
57+
/**
58+
* Represents a location coordinate inside the source. ESLint-style formats
59+
* have just `line` and `column` while others may have `offset` as well.
5260
*/
5361
export interface Position {
5462
line: number;
5563
column: number;
5664
}
5765

66+
/**
67+
* Represents a location coordinate inside the source with an offset.
68+
*/
69+
export interface PositionWithOffset extends Position {
70+
offset: number;
71+
}
72+
73+
/**
74+
* Represents a range of characters in the source.
75+
*/
76+
export type SourceRange = [number, number];
77+
5878
//------------------------------------------------------------------------------
5979
// Config
6080
//------------------------------------------------------------------------------
@@ -157,8 +177,8 @@ export interface Language {
157177
*/
158178
matchesSelectorClass?(
159179
className: string,
160-
node: SyntaxElement,
161-
ancestry: Array<SyntaxElement>,
180+
node: object,
181+
ancestry: Array<object>,
162182
): boolean;
163183

164184
/**
@@ -222,7 +242,7 @@ export interface File {
222242
/**
223243
* Represents the successful result of parsing a file.
224244
*/
225-
export interface OkParseResult {
245+
export interface OkParseResult<T extends object = object> {
226246
/**
227247
* Indicates if the parse was successful. If true, the parse was successful
228248
* and ESLint should continue on to create a SourceCode object and run rules;
@@ -234,7 +254,7 @@ export interface OkParseResult {
234254
/**
235255
* The abstract syntax tree created by the parser. (only when ok: true)
236256
*/
237-
ast: SyntaxElement;
257+
ast: T;
238258

239259
/**
240260
* Any additional data that the parser wants to provide.
@@ -265,7 +285,9 @@ export interface NotOkParseResult {
265285
[key: string]: any;
266286
}
267287

268-
export type ParseResult = OkParseResult | NotOkParseResult;
288+
export type ParseResult<T extends object = object> =
289+
| OkParseResult<T>
290+
| NotOkParseResult;
269291

270292
/**
271293
* Represents inline configuration found in the source code.
@@ -291,7 +313,7 @@ interface SourceCodeBase {
291313
/**
292314
* Root of the AST.
293315
*/
294-
ast: SyntaxElement;
316+
ast: object;
295317

296318
/**
297319
* The traversal path that tools should take when evaluating the AST.
@@ -300,6 +322,16 @@ interface SourceCodeBase {
300322
*/
301323
visitorKeys?: Record<string, Array<string>>;
302324

325+
/**
326+
* Retrieves the equivalent of `loc` for a given node or token.
327+
*/
328+
getLoc(nodeOrToken: object): SourceLocation;
329+
330+
/**
331+
* Retrieves the equivalent of `range` for a given node or token.
332+
*/
333+
getRange(nodeOrToken: object): SourceRange;
334+
303335
/**
304336
* Traversal of AST.
305337
*/
@@ -323,7 +355,7 @@ interface SourceCodeBase {
323355
* Returns an array of all inline configuration nodes found in the
324356
* source code.
325357
*/
326-
getInlineConfigNodes?(): Array<SyntaxElement>;
358+
getInlineConfigNodes?(): Array<object>;
327359

328360
/**
329361
* Applies configuration found inside of the source code. This method is only
@@ -370,7 +402,7 @@ export type SourceCode = TextSourceCode | BinarySourceCode;
370402
*/
371403
export interface VisitTraversalStep {
372404
kind: 1;
373-
target: SyntaxElement;
405+
target: object;
374406
phase: 1 /* enter */ | 2 /* exit */;
375407
args: Array<any>;
376408
}
@@ -399,7 +431,7 @@ export interface Directive {
399431
/**
400432
* The node of the directive. May be in the AST or a comment/token.
401433
*/
402-
node: SyntaxElement;
434+
node: object;
403435

404436
/**
405437
* The value of the directive.

0 commit comments

Comments
 (0)