Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
bradzacher wants to merge1 commit intomainfromlazy-loc
Closed
Show file tree
Hide file tree
Changes fromall 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 deletionspackages/typescript-estree/src/convert-comments.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionspackages/typescript-estree/src/convert.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand Up@@ -2820,7 +2820,7 @@ export class Converter {
id,
body,
global: true,
} satisfiesTSESTree.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,
} satisfiesTSESTree.OptionalRangeAndLoc<
} satisfies OptionalRangeAndLoc<
Omit<TSESTree.TSModuleDeclarationNamespace, 'type'>
>;
} else {
return {
kind: 'module',
id,
...(body != null ? { body } : {}),
} satisfiesTSESTree.OptionalRangeAndLoc<
} satisfies OptionalRangeAndLoc<
Omit<TSESTree.TSModuleDeclarationModule, 'type'>
>;
}
Expand Down
56 changes: 43 additions & 13 deletionspackages/typescript-estree/src/node-utils.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionpackages/typescript-estree/src/simple-traverse.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp