/
githubmirror
/
webpack
Обзор
Документация
Войти
/
githubmirror
/
webpack
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
examples/custom-javascript-parser/internals/oxc-parse.js
42 строки
1 KB
Alexander Akait
refactor: derive ASI positions from source instead of acorn callback (#21453)
19 июл 2026, 23:58
Не верифицирован
19 июл 2026, 23:58
e4a217e
Код
Авторство
О чём код?
"use strict"; const oxc = require("oxc-parser"); /** @typedef {import("estree").Program} Program */ /** @typedef {import("estree").Comment} Comment */ /** @typedef {import("../../../lib/javascript/JavascriptParser").ParseOptions} ParseOptions */ /** @typedef {import("../../../lib/javascript/JavascriptParser").ParseResult} ParseResult */ /** * Oxc has no location API — none is needed: webpack derives line/column * locations from node offsets and the source text itself. ASI positions are * likewise read from the source, so no semicolon collection is required. * @param {string} sourceCode the source code * @param {ParseOptions} options options * @returns {ParseResult} the parsed result */ const oxcParse = (sourceCode, options) => { const result = oxc.parseSync("file.js", sourceCode, { astType: "js", range: true, sourceType: options.sourceType === "module" ? "module" : "script", // @ts-expect-error no types experimentalRawTransfer: true }); const comments = /** @type {(Comment & { start: number, end: number })[]} */ (result.comments); // webpack's magic-comment lookup reads `comment.range` for (const comment of comments) { if (!comment.range) comment.range = [comment.start, comment.end]; } return { ast: /** @type {Program} */ (/** @type {unknown} */ (result.program)), comments }; }; module.exports = oxcParse;