Skip to content

Commit

Permalink
fix(babel-parser): all index locations from nodes need to be offset b…
Browse files Browse the repository at this point in the history
…y startIndex when reading source code
  • Loading branch information
DylanPiercey committed Oct 25, 2024
1 parent 61de718 commit 63c4dd3
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 25 deletions.
6 changes: 5 additions & 1 deletion packages/babel-parser/src/parser/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ export default class CommentsParser extends BaseParser {
} else {
/*:: invariant(commentWS.containingNode !== null) */
const { containingNode: node, start: commentStart } = commentWS;
if (this.input.charCodeAt(commentStart - 1) === charCodes.comma) {
if (
this.input.charCodeAt(
this.state.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
// of the last non-null list element
Expand Down
41 changes: 34 additions & 7 deletions packages/babel-parser/src/parser/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ export default abstract class ExpressionParser extends LValParser {
const { type } = this.state;

if (type === tt.parenL || tokenIsIdentifier(type)) {
this.state.potentialArrowAt = this.state.start;
this.state.potentialArrowAt = this.state.sourceToOffsetPos(
this.state.start,
);
}

let left = this.parseMaybeConditional(refExpressionErrors);
Expand Down Expand Up @@ -1121,7 +1123,9 @@ export default abstract class ExpressionParser extends LValParser {
return this.parseBooleanLiteral(false);

case tt.parenL: {
const canBeArrow = this.state.potentialArrowAt === this.state.start;
const canBeArrow =
this.state.potentialArrowAt ===
this.state.sourceToOffsetPos(this.state.start);
return this.parseParenAndDistinguishExpression(canBeArrow);
}

Expand Down Expand Up @@ -1259,7 +1263,9 @@ export default abstract class ExpressionParser extends LValParser {
) {
return this.parseModuleExpression();
}
const canBeArrow = this.state.potentialArrowAt === this.state.start;
const canBeArrow =
this.state.potentialArrowAt ===
this.state.sourceToOffsetPos(this.state.start);
const containsEsc = this.state.containsEsc;
const id = this.parseIdentifier();

Expand Down Expand Up @@ -1662,7 +1668,14 @@ export default abstract class ExpressionParser extends LValParser {
node: any,
): T {
this.addExtra(node, "rawValue", value);
this.addExtra(node, "raw", this.input.slice(node.start, this.state.end));
this.addExtra(
node,
"raw",
this.input.slice(
this.state.offsetToSourcePos(node.start),
this.state.end,
),
);
node.value = value;
this.next();
return this.finishNode<T>(node, type);
Expand Down Expand Up @@ -1696,7 +1709,14 @@ export default abstract class ExpressionParser extends LValParser {
flags: N.RegExpLiteral["flags"];
}) {
const node = this.startNode<N.RegExpLiteral>();
this.addExtra(node, "raw", this.input.slice(node.start, this.state.end));
this.addExtra(
node,
"raw",
this.input.slice(
this.state.offsetToSourcePos(node.start),
this.state.end,
),
);
node.pattern = value.pattern;
node.flags = value.flags;
this.next();
Expand Down Expand Up @@ -1950,7 +1970,12 @@ export default abstract class ExpressionParser extends LValParser {
const endOffset = isTail ? -1 : -2;
const elemEnd = end + endOffset;
elem.value = {
raw: this.input.slice(elemStart, elemEnd).replace(/\r\n?/g, "\n"),
raw: this.input
.slice(
this.state.offsetToSourcePos(elemStart),
this.state.offsetToSourcePos(elemEnd),
)
.replace(/\r\n?/g, "\n"),
cooked: value === null ? null : value.slice(1, endOffset),
};
elem.tail = isTail;
Expand Down Expand Up @@ -3141,7 +3166,9 @@ export default abstract class ExpressionParser extends LValParser {
parseFSharpPipelineBody(this: Parser, prec: number): N.Expression {
const startLoc = this.state.startLoc;

this.state.potentialArrowAt = this.state.start;
this.state.potentialArrowAt = this.state.sourceToOffsetPos(
this.state.start,
);
const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
this.state.inFSharpPipelineDirectBody = true;

Expand Down
9 changes: 6 additions & 3 deletions packages/babel-parser/src/parser/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,10 @@ export default abstract class StatementParser extends ExpressionParser {

const directiveLiteral = directive.value;
const expressionValue = directiveLiteral.value;
const raw = this.input.slice(directiveLiteral.start, directiveLiteral.end);
const raw = this.input.slice(
this.state.offsetToSourcePos(directiveLiteral.start),
this.state.offsetToSourcePos(directiveLiteral.end),
);
const val = (directiveLiteral.value = raw.slice(1, -1)); // remove quotes

this.addExtra(directiveLiteral, "raw", raw);
Expand Down Expand Up @@ -1282,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.start;
label.statementStart = this.state.sourceToOffsetPos(this.state.start);
label.kind = kind;
} else {
break;
Expand All @@ -1292,7 +1295,7 @@ export default abstract class StatementParser extends ExpressionParser {
this.state.labels.push({
name: maybeName,
kind: kind,
statementStart: this.state.start,
statementStart: this.state.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.lastTokEndLoc.index,
this.state.offsetToSourcePos(this.state.lastTokEndLoc.index),
this.state.start,
);
}
Expand Down
13 changes: 10 additions & 3 deletions packages/babel-parser/src/plugins/flow/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2107,7 +2107,9 @@ export default (superClass: typeof Parser) =>
consequent: N.Expression;
failed: boolean;
} {
this.state.noArrowParamsConversionAt.push(this.state.start);
this.state.noArrowParamsConversionAt.push(
this.state.sourceToOffsetPos(this.state.start),
);

const consequent = this.parseMaybeAssignAllowIn();
const failed = !this.match(tt.colon);
Expand Down Expand Up @@ -2181,7 +2183,9 @@ export default (superClass: typeof Parser) =>
): T {
let result: T;
if (this.state.noArrowParamsConversionAt.includes(node.start)) {
this.state.noArrowParamsConversionAt.push(this.state.start);
this.state.noArrowParamsConversionAt.push(
this.state.sourceToOffsetPos(this.state.start),
);
result = parse();
this.state.noArrowParamsConversionAt.pop();
} else {
Expand Down Expand Up @@ -3134,7 +3138,10 @@ export default (superClass: typeof Parser) =>

parseParenAndDistinguishExpression(canBeArrow: boolean): N.Expression {
return super.parseParenAndDistinguishExpression(
canBeArrow && !this.state.noArrowAt.includes(this.state.start),
canBeArrow &&
!this.state.noArrowAt.includes(
this.state.sourceToOffsetPos(this.state.start),
),
);
}

Expand Down
5 changes: 4 additions & 1 deletion packages/babel-parser/src/plugins/placeholders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,10 @@ export default (superClass: typeof Parser) =>

// Throws if the current token and the prev one are separated by a space.
assertNoSpace(): void {
if (this.state.start > this.state.lastTokEndLoc.index) {
if (
this.state.start >
this.state.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(key.start, key.end)}]`,
: `[${this.input.slice(this.state.offsetToSourcePos(key.start), this.state.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(key.start, key.end)}]`,
: `[${this.input.slice(key.start - this.state.startIndex, key.end - this.state.startIndex)}]`,
});
}
}
Expand Down
10 changes: 9 additions & 1 deletion 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.pos + this.startIndex;
const index = this.sourceToOffsetPos(this.pos);
return new Position(this.curLine, index - this.lineStart, index);
}

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

return state;
}

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

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

export type LookaheadState = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"start":8,"end":9,"loc":{"start":{"line":1,"column":11,"index":8},"end":{"line":1,"column":12,"index":9}},
"extra": {
"rawValue": 1,
"raw": ""
"raw": "1"
},
"value": 1
}
Expand All @@ -48,7 +48,7 @@
"start":16,"end":17,"loc":{"start":{"line":2,"column":7,"index":16},"end":{"line":2,"column":8,"index":17}},
"extra": {
"rawValue": 2,
"raw": ""
"raw": "2"
},
"value": 2
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"start":8,"end":9,"loc":{"start":{"line":1,"column":11,"index":8},"end":{"line":1,"column":12,"index":9}},
"extra": {
"rawValue": 1,
"raw": ""
"raw": "1"
},
"value": 1
}
Expand All @@ -48,7 +48,7 @@
"start":16,"end":17,"loc":{"start":{"line":2,"column":7,"index":16},"end":{"line":2,"column":8,"index":17}},
"extra": {
"rawValue": 2,
"raw": ""
"raw": "2"
},
"value": 2
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"start":8,"end":9,"loc":{"start":{"line":3,"column":11,"index":8},"end":{"line":3,"column":12,"index":9}},
"extra": {
"rawValue": 1,
"raw": ""
"raw": "1"
},
"value": 1
}
Expand All @@ -48,7 +48,7 @@
"start":16,"end":17,"loc":{"start":{"line":4,"column":7,"index":16},"end":{"line":4,"column":8,"index":17}},
"extra": {
"rawValue": 2,
"raw": ""
"raw": "2"
},
"value": 2
}
Expand Down

0 comments on commit 63c4dd3

Please sign in to comment.