Files
SnowLuma/.pnpm-store/v11/files/7f/c26336d908e479fceb4919e3a6a051aedf0c1f85d0b77c84f66f43f2efec49b6ed4c3f48dec67607c103b78df1e848302652de6378f057882cd814ac6d7e87
TanTan 2a69b23b47
Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Canceled after 0s
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

61 lines
2.1 KiB
Plaintext

import { l as locate, n as error, s as logParseError, t as augmentCodeLocation, u as getCodeFrame } from "./shared/logs-ZGEh6uhb.mjs";
import { n as parseSync, t as parse } from "./shared/parse-D4dtlfWy.mjs";
//#region src/parse-ast-index.ts
function wrap(result, filename, sourceText) {
if (result.errors.length > 0) return normalizeParseError(filename, sourceText, result.errors);
return result.program;
}
function normalizeParseError(filename, sourceText, errors) {
let message = `Parse failed with ${errors.length} error${errors.length < 2 ? "" : "s"}:\n`;
const pos = errors[0]?.labels?.[0]?.start;
for (let i = 0; i < errors.length; i++) {
if (i >= 5) {
message += "\n...";
break;
}
const e = errors[i];
message += e.message + "\n" + e.labels.map((label) => {
const location = locate(sourceText, label.start, { offsetLine: 1 });
if (!location) return;
return getCodeFrame(sourceText, location.line, location.column);
}).filter(Boolean).join("\n");
}
const log = logParseError(message, filename, pos);
if (pos !== void 0 && filename) augmentCodeLocation(log, pos, sourceText, filename);
return error(log);
}
const defaultParserOptions = {
lang: "js",
preserveParens: false
};
/**
* Parse code synchronously and return the AST.
*
* This function is similar to Rollup's `parseAst` function.
* Prefer using {@linkcode parseSync} instead of this function as it has more information in the return value.
*
* @category Utilities
*/
function parseAst(sourceText, options, filename) {
return wrap(parseSync(filename ?? "file.js", sourceText, {
...defaultParserOptions,
...options
}), filename, sourceText);
}
/**
* Parse code asynchronously and return the AST.
*
* This function is similar to Rollup's `parseAstAsync` function.
* Prefer using {@linkcode parseAsync} instead of this function as it has more information in the return value.
*
* @category Utilities
*/
async function parseAstAsync(sourceText, options, filename) {
return wrap(await parse(filename ?? "file.js", sourceText, {
...defaultParserOptions,
...options
}), filename, sourceText);
}
//#endregion
export { parseAst, parseAstAsync };