Skip to content
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

fix(parser): offset internal index locations by startIndex #16936

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
move
  • Loading branch information
liuxingbaoyu committed Oct 25, 2024
commit f26314faef0af58b8a891d6478c99a9578aa3c4b
9 changes: 9 additions & 0 deletions packages/babel-parser/src/parser/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default class BaseParser {
declare expressionScope: ExpressionScopeHandler;
declare plugins: PluginsMap;
declare filename: string | undefined | null;
declare startIndex: number;
// Names of exports store. `default` is stored as a name for both
// `export default foo;` and `export { foo as default };`.
declare exportedIdentifiers: Set<string>;
Expand All @@ -37,6 +38,14 @@ export default class BaseParser {
// Comment store for Program.comments
declare comments: Array<N.Comment>;

sourceToOffsetPos(sourcePos: number) {
return sourcePos + this.startIndex;
}

offsetToSourcePos(offsetPos: number) {
return offsetPos - this.startIndex;
}

// This method accepts either a string (plugin name) or an array pair
// (plugin name and options object). If an options object is given,
// then each value is non-recursively checked for identity with that
Expand Down
5 changes: 2 additions & 3 deletions packages/babel-parser/src/parser/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,8 @@ export default class CommentsParser extends BaseParser {
/*:: invariant(commentWS.containingNode !== null) */
const { containingNode: node, start: commentStart } = commentWS;
if (
this.input.charCodeAt(
this.state.offsetToSourcePos(commentStart) - 1,
) === charCodes.comma
this.input.charCodeAt(this.offsetToSourcePos(commentStart) - 1) ===
charCodes.comma
) {
// If a commentWhitespace follows a comma and the containingNode allows
// list structures with trailing comma, merge it to the trailingComment
Expand Down
14 changes: 4 additions & 10 deletions packages/babel-parser/src/parser/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export default abstract class ExpressionParser extends LValParser {
): expr is N.ArrowFunctionExpression {
return (
expr.type === "ArrowFunctionExpression" &&
this.state.offsetToSourcePos(expr.start) === potentialArrowAt
this.offsetToSourcePos(expr.start) === potentialArrowAt
);
}

Expand Down Expand Up @@ -950,7 +950,7 @@ export default abstract class ExpressionParser extends LValParser {
!this.canInsertSemicolon() &&
// check there are no escape sequences, such as \u{61}sync
base.end - base.start === 5 &&
this.state.offsetToSourcePos(base.start) === this.state.potentialArrowAt
this.offsetToSourcePos(base.start) === this.state.potentialArrowAt
);
}

Expand Down Expand Up @@ -1666,10 +1666,7 @@ export default abstract class ExpressionParser extends LValParser {
this.addExtra(
node,
"raw",
this.input.slice(
this.state.offsetToSourcePos(node.start),
this.state.end,
),
this.input.slice(this.offsetToSourcePos(node.start), this.state.end),
);
node.value = value;
this.next();
Expand Down Expand Up @@ -1707,10 +1704,7 @@ export default abstract class ExpressionParser extends LValParser {
this.addExtra(
node,
"raw",
this.input.slice(
this.state.offsetToSourcePos(node.start),
this.state.end,
),
this.input.slice(this.offsetToSourcePos(node.start), this.state.end),
);
node.pattern = value.pattern;
node.flags = value.flags;
Expand Down
1 change: 1 addition & 0 deletions packages/babel-parser/src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default class Parser extends StatementParser {
this.initializeScopes();
this.plugins = pluginsMap;
this.filename = options.sourceFilename;
this.startIndex = options.startIndex;
}

// This can be overwritten, for example, by the TypeScript plugin.
Expand Down
8 changes: 4 additions & 4 deletions packages/babel-parser/src/parser/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ export default abstract class StatementParser extends ExpressionParser {
const directiveLiteral = directive.value;
const expressionValue = directiveLiteral.value;
const raw = this.input.slice(
this.state.offsetToSourcePos(directiveLiteral.start),
this.state.offsetToSourcePos(directiveLiteral.end),
this.offsetToSourcePos(directiveLiteral.start),
this.offsetToSourcePos(directiveLiteral.end),
);
const val = (directiveLiteral.value = raw.slice(1, -1)); // remove quotes

Expand Down Expand Up @@ -1285,7 +1285,7 @@ export default abstract class StatementParser extends ExpressionParser {
for (let i = this.state.labels.length - 1; i >= 0; i--) {
const label = this.state.labels[i];
if (label.statementStart === node.start) {
label.statementStart = this.state.sourceToOffsetPos(this.state.start);
label.statementStart = this.sourceToOffsetPos(this.state.start);
label.kind = kind;
} else {
break;
Expand All @@ -1295,7 +1295,7 @@ export default abstract class StatementParser extends ExpressionParser {
this.state.labels.push({
name: maybeName,
kind: kind,
statementStart: this.state.sourceToOffsetPos(this.state.start),
statementStart: this.sourceToOffsetPos(this.state.start),
});
// https://tc39.es/ecma262/#prod-LabelledItem
node.body =
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/parser/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export default abstract class UtilParser extends Tokenizer {
hasPrecedingLineBreak(): boolean {
return hasNewLine(
this.input,
this.state.offsetToSourcePos(this.state.lastTokEndLoc.index),
this.offsetToSourcePos(this.state.lastTokEndLoc.index),
this.state.start,
);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/babel-parser/src/plugins/flow/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2182,7 +2182,7 @@ export default (superClass: typeof Parser) =>
let result: T;
if (
this.state.noArrowParamsConversionAt.includes(
this.state.offsetToSourcePos(node.start),
this.offsetToSourcePos(node.start),
)
) {
this.state.noArrowParamsConversionAt.push(this.state.start);
Expand Down Expand Up @@ -3103,7 +3103,7 @@ export default (superClass: typeof Parser) =>
): void {
if (
this.state.noArrowParamsConversionAt.includes(
this.state.offsetToSourcePos(node.start),
this.offsetToSourcePos(node.start),
)
) {
node.params = params as N.ArrowFunctionExpression["params"];
Expand All @@ -3121,7 +3121,7 @@ export default (superClass: typeof Parser) =>
if (
isArrowFunction &&
this.state.noArrowParamsConversionAt.includes(
this.state.offsetToSourcePos(node.start),
this.offsetToSourcePos(node.start),
)
) {
return;
Expand All @@ -3146,7 +3146,7 @@ export default (superClass: typeof Parser) =>
return super.parseParenAndDistinguishExpression(
canBeArrow &&
!this.state.noArrowAt.includes(
this.state.sourceToOffsetPos(this.state.start),
this.sourceToOffsetPos(this.state.start),
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/plugins/placeholders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ export default (superClass: typeof Parser) =>
assertNoSpace(): void {
if (
this.state.start >
this.state.offsetToSourcePos(this.state.lastTokEndLoc.index)
this.offsetToSourcePos(this.state.lastTokEndLoc.index)
) {
this.raise(PlaceholderErrors.UnexpectedSpace, this.state.lastTokEndLoc);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-parser/src/plugins/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3237,7 +3237,7 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
propertyName:
key.type === "Identifier" && !node.computed
? key.name
: `[${this.input.slice(this.state.offsetToSourcePos(key.start), this.state.offsetToSourcePos(key.end))}]`,
: `[${this.input.slice(this.offsetToSourcePos(key.start), this.offsetToSourcePos(key.end))}]`,
},
);
}
Expand Down Expand Up @@ -4002,7 +4002,7 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
methodName:
key.type === "Identifier" && !method.computed
? key.name
: `[${this.input.slice(this.state.offsetToSourcePos(key.start), this.state.offsetToSourcePos(key.end))}]`,
: `[${this.input.slice(this.offsetToSourcePos(key.start), this.offsetToSourcePos(key.end))}]`,
});
}
}
Expand Down
6 changes: 2 additions & 4 deletions packages/babel-parser/src/tokenizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ export default abstract class Tokenizer extends CommentsParser {
curLine: state.curLine,
lineStart: state.lineStart,
curPosition: state.curPosition,
sourceToOffsetPos: state.sourceToOffsetPos,
offsetToSourcePos: state.offsetToSourcePos,
};
}

Expand Down Expand Up @@ -448,8 +446,8 @@ export default abstract class Tokenizer extends CommentsParser {
if (comments.length > 0) {
const end = this.state.pos;
const commentWhitespace: CommentWhitespace = {
start: this.state.sourceToOffsetPos(spaceStart),
end: this.state.sourceToOffsetPos(end),
start: this.sourceToOffsetPos(spaceStart),
end: this.sourceToOffsetPos(end),
comments,
leadingNode: null,
trailingNode: null,
Expand Down
12 changes: 1 addition & 11 deletions packages/babel-parser/src/tokenizer/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export default class State {
*/

curPosition(): Position {
const index = this.sourceToOffsetPos(this.pos);
const index = this.startIndex + this.pos;
return new Position(this.curLine, index - this.lineStart, index);
}

Expand Down Expand Up @@ -208,14 +208,6 @@ export default class State {

return state;
}

sourceToOffsetPos(sourcePos: number) {
return sourcePos + this.startIndex;
}

offsetToSourcePos(offsetPos: number) {
return offsetPos - this.startIndex;
}
}

export type LookaheadState = {
Expand All @@ -230,8 +222,6 @@ export type LookaheadState = {
curLine: number;
lineStart: number;
curPosition: State["curPosition"];
sourceToOffsetPos: State["sourceToOffsetPos"];
offsetToSourcePos: State["offsetToSourcePos"];
/* Used only in readToken_mult_modulo */
inType: boolean;
// These boolean properties are not initialized in createLookaheadState()
Expand Down
Loading