Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a79b7c6

Browse files
committed
feat(typescript-estree): lazily compute loc
1 parent 181e705 commit a79b7c6

File tree

4 files changed

+51
-21
lines changed

4 files changed

+51
-21
lines changed

packages/typescript-estree/src/convert-comments.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export function convertComments(
2626
? AST_TOKEN_TYPES.Line
2727
: AST_TOKEN_TYPES.Block;
2828
const range: TSESTree.Range = [comment.pos, comment.end];
29-
const loc = getLocFor(range[0], range[1], ast);
3029

3130
// both comments start with 2 characters - /* or //
3231
const textStart = range[0] + 2;
@@ -40,7 +39,7 @@ export function convertComments(
4039
type,
4140
value: code.slice(textStart, textStart + textEnd),
4241
range,
43-
loc,
42+
loc: getLocFor(range[0], range[1], ast),
4443
});
4544
},
4645
ast,

packages/typescript-estree/src/convert.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import * as ts from 'typescript';
44

55
import { getDecorators, getModifiers } from './getModifiers';
6-
import type { TSError } from './node-utils';
6+
import type { OptionalRangeAndLoc, TSError } from './node-utils';
77
import {
88
canContainDirective,
99
createError,
@@ -251,7 +251,7 @@ export class Converter {
251251

252252
private createNode<T extends TSESTree.Node = TSESTree.Node>(
253253
node: TSESTreeToTSNode<T>,
254-
data: TSESTree.OptionalRangeAndLoc<T>,
254+
data: OptionalRangeAndLoc<T>,
255255
): T {
256256
const result = data;
257257
if (!result.range) {
@@ -2820,7 +2820,7 @@ export class Converter {
28202820
id,
28212821
body,
28222822
global: true,
2823-
} satisfies TSESTree.OptionalRangeAndLoc<
2823+
} satisfies OptionalRangeAndLoc<
28242824
Omit<TSESTree.TSModuleDeclarationGlobal, 'type'>
28252825
>;
28262826
} else if (node.flags & ts.NodeFlags.Namespace) {
@@ -2834,15 +2834,15 @@ export class Converter {
28342834
kind: 'namespace',
28352835
id,
28362836
body,
2837-
} satisfies TSESTree.OptionalRangeAndLoc<
2837+
} satisfies OptionalRangeAndLoc<
28382838
Omit<TSESTree.TSModuleDeclarationNamespace, 'type'>
28392839
>;
28402840
} else {
28412841
return {
28422842
kind: 'module',
28432843
id,
28442844
...(body != null ? { body } : {}),
2845-
} satisfies TSESTree.OptionalRangeAndLoc<
2845+
} satisfies OptionalRangeAndLoc<
28462846
Omit<TSESTree.TSModuleDeclarationModule, 'type'>
28472847
>;
28482848
}

packages/typescript-estree/src/node-utils.ts

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,27 @@ export function getLocFor(
181181
end: number,
182182
ast: ts.SourceFile,
183183
): TSESTree.SourceLocation {
184+
let locStart: TSESTree.Position | null = null;
185+
let locEnd: TSESTree.Position | null = null;
184186
return {
185-
start: getLineAndCharacterFor(start, ast),
186-
end: getLineAndCharacterFor(end, ast),
187+
get start(): TSESTree.Position {
188+
if (locStart == null) {
189+
locStart = getLineAndCharacterFor(start, ast);
190+
}
191+
return locStart;
192+
},
193+
set start(val: TSESTree.Position) {
194+
locStart = val;
195+
},
196+
get end(): TSESTree.Position {
197+
if (locEnd == null) {
198+
locEnd = getLineAndCharacterFor(end, ast);
199+
}
200+
return locEnd;
201+
},
202+
set end(val: TSESTree.Position) {
203+
locEnd = val;
204+
},
187205
};
188206
}
189207

@@ -522,6 +540,17 @@ export function getTokenType(
522540
return AST_TOKEN_TYPES.Identifier;
523541
}
524542

543+
export type OptionalLoc<T> = Pick<T, Exclude<keyof T, 'loc'>> & {
544+
loc?: TSESTree.SourceLocation;
545+
};
546+
export type OptionalRangeAndLoc<T> = Pick<
547+
T,
548+
Exclude<keyof T, 'loc' | 'range'>
549+
> & {
550+
range?: TSESTree.Range;
551+
loc?: TSESTree.SourceLocation;
552+
};
553+
525554
/**
526555
* Extends and formats a given ts.Token, for a given AST
527556
* @param token the ts.Token
@@ -541,26 +570,27 @@ export function convertToken(
541570
const tokenType = getTokenType(token);
542571

543572
if (tokenType === AST_TOKEN_TYPES.RegularExpression) {
544-
return {
545-
type: tokenType,
573+
const newToken: TSESTree.RegularExpressionToken = {
574+
type: AST_TOKEN_TYPES.RegularExpression,
546575
value,
547576
range: [start, end],
548-
loc: getLocFor(start, end, ast),
549577
regex: {
550578
pattern: value.slice(1, value.lastIndexOf('/')),
551579
flags: value.slice(value.lastIndexOf('/') + 1),
552580
},
553-
};
554-
} else {
555-
// @ts-expect-error TS is complaining about `value` not being the correct
556-
// type but it is
557-
return {
558-
type: tokenType,
559-
value,
560-
range: [start, end],
561581
loc: getLocFor(start, end, ast),
562582
};
583+
return newToken;
563584
}
585+
586+
// @ts-expect-error TS is complaining about `value` not being the correct type but it is
587+
const newToken: Exclude<TSESTree.Token, TSESTree.RegularExpressionToken> = {
588+
type: tokenType,
589+
value,
590+
range: [start, end],
591+
loc: getLocFor(start, end, ast),
592+
};
593+
return newToken;
564594
}
565595

566596
/**

packages/typescript-estree/src/simple-traverse.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ function getVisitorKeysForNode(
1313
node: TSESTree.Node,
1414
): readonly (keyof TSESTree.Node)[] {
1515
const keys = allVisitorKeys[node.type];
16-
return (keys ?? []) as never;
16+
// @ts-expect-error -- keys will provably be of the correct type - it's just not possible to type
17+
return keys ?? [];
1718
}
1819

1920
type SimpleTraverseOptions =

0 commit comments

Comments
 (0)