mirror of
https://github.com/marko-js/marko.git
synced 2026-02-01 16:07:13 +00:00
chore: cleanup get loc util, expose single line loc
This commit is contained in:
parent
e662e843be
commit
4eb13efdb4
@ -5,7 +5,7 @@ import { parse, parseExpression } from "@babel/parser";
|
||||
import { codeFrameColumns } from "@babel/code-frame";
|
||||
import { getClientPath } from "lasso-modules-client/transport";
|
||||
import { buildLookup } from "../taglib";
|
||||
import { getLocRange } from "./util/pos-to-loc";
|
||||
import { getLoc, getLocRange } from "./util/pos-to-loc";
|
||||
import checksum from "./util/checksum";
|
||||
const CWD = process.cwd();
|
||||
|
||||
@ -32,11 +32,10 @@ export class MarkoFile extends File {
|
||||
|
||||
this.ast.start = this.ast.program.start = 0;
|
||||
this.ast.end = this.ast.program.end = code.length - 1;
|
||||
this.ast.loc = this.ast.program.loc = getLocRange(
|
||||
this,
|
||||
this.ast.start,
|
||||
this.ast.end
|
||||
);
|
||||
this.ast.loc = this.ast.program.loc = {
|
||||
start: { line: 0, column: 0 },
|
||||
end: getLoc(this, this.ast.end)
|
||||
};
|
||||
this._jsParseOptions = jsParseOptions;
|
||||
this._markoOptions = markoOptions;
|
||||
this._lookup = buildLookup(path.dirname(filename), markoOptions.translator);
|
||||
|
||||
@ -6,7 +6,7 @@ import parseIDShorthand from "./util/parse-id-shorthand";
|
||||
import parseClassnameShorthand from "./util/parse-classname-shorthand";
|
||||
import markoModules from "../../modules";
|
||||
import { types as t } from "@marko/babel-types";
|
||||
import { getLocRange } from "./util/pos-to-loc";
|
||||
import { getLoc, getLocRange } from "./util/pos-to-loc";
|
||||
|
||||
const EMPTY_OBJECT = {};
|
||||
const EMPTY_ARRAY = [];
|
||||
@ -284,7 +284,7 @@ export function parse(file) {
|
||||
}
|
||||
|
||||
node.end = endPos;
|
||||
node.loc = getLocRange(file, node.start, endPos);
|
||||
node.loc.end = getLoc(file, endPos);
|
||||
|
||||
if (
|
||||
!isConcise &&
|
||||
|
||||
@ -1,6 +1,25 @@
|
||||
const LINE_POS_CACHE = new WeakMap();
|
||||
|
||||
export function getLocRange(file, start, end) {
|
||||
const linePositions = getLinePositions(file);
|
||||
const startLoc = findLoc(linePositions, 0, start);
|
||||
|
||||
if (startLoc) {
|
||||
const endLoc =
|
||||
start === end ? startLoc : findLoc(linePositions, startLoc.line - 1, end);
|
||||
|
||||
return {
|
||||
start: startLoc,
|
||||
end: endLoc
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function getLoc(file, pos) {
|
||||
return findLoc(getLinePositions(file), 0, pos);
|
||||
}
|
||||
|
||||
function getLinePositions(file) {
|
||||
let linePositions = LINE_POS_CACHE.get(file);
|
||||
|
||||
if (!linePositions) {
|
||||
@ -14,20 +33,10 @@ export function getLocRange(file, start, end) {
|
||||
LINE_POS_CACHE.set(file, linePositions);
|
||||
}
|
||||
|
||||
const startLoc = getLoc(linePositions, 0, start);
|
||||
|
||||
if (startLoc) {
|
||||
const endLoc =
|
||||
start === end ? startLoc : getLoc(linePositions, startLoc.line - 1, end);
|
||||
|
||||
return {
|
||||
start: startLoc,
|
||||
end: endLoc
|
||||
};
|
||||
}
|
||||
return linePositions;
|
||||
}
|
||||
|
||||
function getLoc(linePositions, startLine, pos) {
|
||||
function findLoc(linePositions, startLine, pos) {
|
||||
const endLine = linePositions.length - 1;
|
||||
let max = endLine;
|
||||
let line = startLine;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user