diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 075df742d132..199ddd6c02f1 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -43,7 +43,7 @@ import { isIdentifierStart, canBeReservedWord, } from "../util/identifier"; -import { Position, createPositionFromPosition } from "../util/location"; +import { Position, createPositionWithColumnOffset } from "../util/location"; import * as charCodes from "charcodes"; import { BIND_OUTSIDE, @@ -1859,7 +1859,7 @@ export default class ExpressionParser extends LValParser { const elemStart = start + 1; const elem = this.startNodeAt( elemStart, - createPositionFromPosition(this.state.startLoc, 1), + createPositionWithColumnOffset(this.state.startLoc, 1), ); if (value === null) { if (!isTagged) { @@ -1880,7 +1880,7 @@ export default class ExpressionParser extends LValParser { this.resetEndLocation( elem, elemEnd, - createPositionFromPosition(this.state.lastTokEndLoc, endOffset), + createPositionWithColumnOffset(this.state.lastTokEndLoc, endOffset), ); return elem; } diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 3eeae6f6b0c6..43cf3fbc2c94 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -39,7 +39,7 @@ import { } from "../util/expression-scope"; import type { SourceType } from "../options"; import { Token } from "../tokenizer"; -import { createPositionFromPosition } from "../util/location"; +import { createPositionWithColumnOffset } from "../util/location"; import { cloneStringLiteral, cloneIdentifier } from "./node"; const loopLabel = { kind: "loop" }, @@ -69,7 +69,7 @@ function babel7CompatTokens(tokens) { if (!process.env.BABEL_8_BREAKING) { const { loc, start, value, end } = token; const hashEndPos = start + 1; - const hashEndLoc = createPositionFromPosition(loc.start, 1); + const hashEndLoc = createPositionWithColumnOffset(loc.start, 1); tokens.splice( i, 1, @@ -100,7 +100,7 @@ function babel7CompatTokens(tokens) { if (!process.env.BABEL_8_BREAKING) { const { loc, start, value, end } = token; const backquoteEnd = start + 1; - const backquoteEndLoc = createPositionFromPosition(loc.start, 1); + const backquoteEndLoc = createPositionWithColumnOffset(loc.start, 1); let startToken; if (value.charCodeAt(0) === charCodes.graveAccent) { // $FlowIgnore: hacky way to create token @@ -127,7 +127,7 @@ function babel7CompatTokens(tokens) { if (type === tt.templateTail) { // ends with '`' templateElementEnd = end - 1; - templateElementEndLoc = createPositionFromPosition(loc.end, -1); + templateElementEndLoc = createPositionWithColumnOffset(loc.end, -1); templateValue = value.slice(1, -1); // $FlowIgnore: hacky way to create token endToken = new Token({ @@ -141,7 +141,7 @@ function babel7CompatTokens(tokens) { } else { // ends with `${` templateElementEnd = end - 2; - templateElementEndLoc = createPositionFromPosition(loc.end, -2); + templateElementEndLoc = createPositionWithColumnOffset(loc.end, -2); templateValue = value.slice(1, -2); // $FlowIgnore: hacky way to create token endToken = new Token({ diff --git a/packages/babel-parser/src/util/location.js b/packages/babel-parser/src/util/location.js index 66a094705345..66f29cc79875 100644 --- a/packages/babel-parser/src/util/location.js +++ b/packages/babel-parser/src/util/location.js @@ -51,7 +51,18 @@ export function getLineInfo(input: string, offset: number): Position { return new Position(line, offset - lineStart); } -export function createPositionFromPosition( +/** + * creates a new position with a non-zero column offset from the given position. + * This function should be only be used when we create AST node out of the token + * boundaries, such as TemplateElement ends before tt.templateMiddle. This + * function does not skip whitespaces. + * + * @export + * @param {Position} position + * @param {number} columnOffset + * @returns {Position} + */ +export function createPositionWithColumnOffset( position: Position, columnOffset: number, ) {