Skip to content

feat(typescript-estree): lazily compute loc #6542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/typescript-estree/src/convert-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export function convertComments(
? AST_TOKEN_TYPES.Line
: AST_TOKEN_TYPES.Block;
const range: TSESTree.Range = [comment.pos, comment.end];
const loc = getLocFor(range[0], range[1], ast);

// both comments start with 2 characters - /* or //
const textStart = range[0] + 2;
Expand All @@ -40,7 +39,7 @@ export function convertComments(
type,
value: code.slice(textStart, textStart + textEnd),
range,
loc,
loc: getLocFor(range[0], range[1], ast),
});
},
ast,
Expand Down
10 changes: 5 additions & 5 deletions packages/typescript-estree/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import * as ts from 'typescript';

import { getDecorators, getModifiers } from './getModifiers';
import type { TSError } from './node-utils';
import type { OptionalRangeAndLoc, TSError } from './node-utils';
import {
canContainDirective,
createError,
Expand Down Expand Up @@ -251,7 +251,7 @@ export class Converter {

private createNode<T extends TSESTree.Node = TSESTree.Node>(
node: TSESTreeToTSNode<T>,
data: TSESTree.OptionalRangeAndLoc<T>,
data: OptionalRangeAndLoc<T>,
): T {
const result = data;
if (!result.range) {
Expand Down Expand Up @@ -2820,7 +2820,7 @@ export class Converter {
id,
body,
global: true,
} satisfies TSESTree.OptionalRangeAndLoc<
} satisfies OptionalRangeAndLoc<
Omit<TSESTree.TSModuleDeclarationGlobal, 'type'>
>;
} else if (node.flags & ts.NodeFlags.Namespace) {
Expand All @@ -2834,15 +2834,15 @@ export class Converter {
kind: 'namespace',
id,
body,
} satisfies TSESTree.OptionalRangeAndLoc<
} satisfies OptionalRangeAndLoc<
Omit<TSESTree.TSModuleDeclarationNamespace, 'type'>
>;
} else {
return {
kind: 'module',
id,
...(body != null ? { body } : {}),
} satisfies TSESTree.OptionalRangeAndLoc<
} satisfies OptionalRangeAndLoc<
Omit<TSESTree.TSModuleDeclarationModule, 'type'>
>;
}
Expand Down
56 changes: 43 additions & 13 deletions packages/typescript-estree/src/node-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,27 @@ export function getLocFor(
end: number,
ast: ts.SourceFile,
): TSESTree.SourceLocation {
let locStart: TSESTree.Position | null = null;
let locEnd: TSESTree.Position | null = null;
return {
start: getLineAndCharacterFor(start, ast),
end: getLineAndCharacterFor(end, ast),
get start(): TSESTree.Position {
if (locStart == null) {
locStart = getLineAndCharacterFor(start, ast);
}
return locStart;
},
set start(val: TSESTree.Position) {
locStart = val;
},
get end(): TSESTree.Position {
if (locEnd == null) {
locEnd = getLineAndCharacterFor(end, ast);
}
return locEnd;
},
set end(val: TSESTree.Position) {
locEnd = val;
},
};
}

Expand Down Expand Up @@ -522,6 +540,17 @@ export function getTokenType(
return AST_TOKEN_TYPES.Identifier;
}

export type OptionalLoc<T> = Pick<T, Exclude<keyof T, 'loc'>> & {
loc?: TSESTree.SourceLocation;
};
export type OptionalRangeAndLoc<T> = Pick<
T,
Exclude<keyof T, 'loc' | 'range'>
> & {
range?: TSESTree.Range;
loc?: TSESTree.SourceLocation;
};

/**
* Extends and formats a given ts.Token, for a given AST
* @param token the ts.Token
Expand All @@ -541,26 +570,27 @@ export function convertToken(
const tokenType = getTokenType(token);

if (tokenType === AST_TOKEN_TYPES.RegularExpression) {
return {
type: tokenType,
const newToken: TSESTree.RegularExpressionToken = {
type: AST_TOKEN_TYPES.RegularExpression,
value,
range: [start, end],
loc: getLocFor(start, end, ast),
regex: {
pattern: value.slice(1, value.lastIndexOf('/')),
flags: value.slice(value.lastIndexOf('/') + 1),
},
};
} else {
// @ts-expect-error TS is complaining about `value` not being the correct
// type but it is
return {
type: tokenType,
value,
range: [start, end],
loc: getLocFor(start, end, ast),
};
return newToken;
}

// @ts-expect-error TS is complaining about `value` not being the correct type but it is
const newToken: Exclude<TSESTree.Token, TSESTree.RegularExpressionToken> = {
type: tokenType,
value,
range: [start, end],
loc: getLocFor(start, end, ast),
};
return newToken;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/typescript-estree/src/simple-traverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ function getVisitorKeysForNode(
node: TSESTree.Node,
): readonly (keyof TSESTree.Node)[] {
const keys = allVisitorKeys[node.type];
return (keys ?? []) as never;
// @ts-expect-error -- keys will provably be of the correct type - it's just not possible to type
return keys ?? [];
}

type SimpleTraverseOptions =
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy