mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-23 18:21:13 +08:00
fix: 解析不可恢复错误透传 oxc 真实诊断并补 concise 箭头体回归钉
This commit is contained in:
@@ -16,7 +16,7 @@ pub use oxc_span::Span;
|
||||
///
|
||||
/// 使用 oxc 默认 `SourceType`(严格模式由语法自身决定)。返回 `Ok(program)`
|
||||
/// 或 `Err`(收集全部解析错误,按源码顺序排列);若 oxc 内部 panic(不可恢复
|
||||
/// 语法错误)则返回单个通用错误。
|
||||
/// 语法错误)则透传其已收集的诊断(消息 + 位置),无诊断时返回单个通用错误。
|
||||
fn parse_with_source_type<'a>(
|
||||
allocator: &'a Allocator, source: &'a str, source_type: oxc_span::SourceType,
|
||||
) -> Result<Program<'a>, Vec<OxideError>> {
|
||||
@@ -25,6 +25,11 @@ fn parse_with_source_type<'a>(
|
||||
let ret = Parser::new(allocator, source, source_type).parse();
|
||||
|
||||
if ret.panicked {
|
||||
// 不可恢复语法错误:oxc 已为出错 token 累积诊断时按既有路径透传
|
||||
// (消息 + 位置),直指真实触发点;无诊断时兜底通用文本。
|
||||
if !ret.errors.is_empty() {
|
||||
return Err(ret.errors.into_iter().map(OxideError::from).collect());
|
||||
}
|
||||
return Err(vec![OxideError {
|
||||
message: "Parser panicked: unrecoverable syntax error".to_string(),
|
||||
span: (0, 0),
|
||||
|
||||
@@ -49,3 +49,32 @@ fn parse_multiple_statements() {
|
||||
let program = result.unwrap();
|
||||
assert_eq!(program.body.len(), 3, "should have 3 statements");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_concise_arrow_array_member_call() {
|
||||
// concise 箭头体 + 数组字面量 + 成员调用形是合法 JS:必须解析成功。
|
||||
let allocator = Allocator::default();
|
||||
let result = oxide_parser::parse(&allocator, "() => [1, 2].join('|')");
|
||||
assert!(result.is_ok(), "concise 箭头体数组成员调用形应解析成功,实际错误:{:?}", result.err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_concise_arrow_iife_with_kind_helper() {
|
||||
// 顶层 kind 助手 + concise IIFE 九元素数组 .join('|') 形(事故测试源形态复刻):
|
||||
// 必须解析成功;若解析失败,错误消息应携带定位到具体出错 token 的诊断。
|
||||
let allocator = Allocator::default();
|
||||
let source = "const kind = (fn) => { try { fn(); return 'no'; } catch (e) { return e.constructor.name; } };
|
||||
(() => [
|
||||
kind(() => new Temporal.PlainDate(2020n, 1, 1)),
|
||||
kind(() => new Temporal.PlainDate(Symbol(), 1, 1)),
|
||||
kind(() => new Temporal.PlainDate(undefined, 1, 1)),
|
||||
kind(() => new Temporal.PlainDate(2020, 'invalid', 1)),
|
||||
kind(() => new Temporal.PlainDate(Infinity, 1, 1)),
|
||||
kind(() => new Temporal.PlainDate(2020, 1, -Infinity)),
|
||||
kind(() => new Temporal.PlainDate()),
|
||||
kind(() => new Temporal.PlainDate(2021)),
|
||||
kind(() => Temporal.PlainDate(2020, 1, 1)),
|
||||
].join('|'))()";
|
||||
let result = oxide_parser::parse(&allocator, source);
|
||||
assert!(result.is_ok(), "concise IIFE 九元素数组 join 形应解析成功,实际错误:{:?}", result.err());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user