Skip to content

Commit

Permalink
rename createPositionFromPosition
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Nov 4, 2021
1 parent d834190 commit ce1440f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
6 changes: 3 additions & 3 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down
10 changes: 5 additions & 5 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -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" },
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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({
Expand All @@ -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({
Expand Down
13 changes: 12 additions & 1 deletion packages/babel-parser/src/util/location.js
Expand Up @@ -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,
) {
Expand Down

0 comments on commit ce1440f

Please sign in to comment.