Skip to content

Commit 0937a55

Browse files
committed
fix(napi/parser): utf16 span for errors (oxc-project#9112)
1 parent 23f53c0 commit 0937a55

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

napi/parser/src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fn parse_with_return(filename: &str, source_text: String, options: &ParserOption
6969
let allocator = Allocator::default();
7070
let source_type = get_source_type(filename, options);
7171
let mut ret = parse(&allocator, source_type, &source_text, options);
72-
let errors = ret.errors.into_iter().map(OxcError::from).collect::<Vec<_>>();
72+
let mut errors = ret.errors.into_iter().map(OxcError::from).collect::<Vec<_>>();
7373

7474
let mut comments = ret
7575
.program
@@ -87,8 +87,6 @@ fn parse_with_return(filename: &str, source_text: String, options: &ParserOption
8787
.collect::<Vec<Comment>>();
8888

8989
if options.convert_span_utf16.unwrap_or(false) {
90-
// TODO: fix spans in `errors`
91-
9290
// Empty `comments` so comment spans don't get converted twice
9391
ret.program.comments.clear();
9492

@@ -100,6 +98,13 @@ fn parse_with_return(filename: &str, source_text: String, options: &ParserOption
10098
comment.start = converter.convert_offset(comment.start);
10199
comment.end = converter.convert_offset(comment.end);
102100
}
101+
102+
for error in &mut errors {
103+
for label in &mut error.labels {
104+
label.start = converter.convert_offset(label.start);
105+
label.end = converter.convert_offset(label.end);
106+
}
107+
}
103108
}
104109
let program = serde_json::to_string(&ret.program).unwrap();
105110
let module = EcmaScriptModule::from(&ret.module_record);

napi/parser/test/parse.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,26 @@ it('utf16 span', async () => {
205205
}
206206
`);
207207
}
208+
{
209+
const ret = await parseAsync('test.js', `"🤨";asdf asdf`, {
210+
convertSpanUtf16: true,
211+
});
212+
expect(ret.errors).toMatchInlineSnapshot(`
213+
[
214+
{
215+
"helpMessage": "Try insert a semicolon here",
216+
"labels": [
217+
{
218+
"end": 9,
219+
"start": 9,
220+
},
221+
],
222+
"message": "Expected a semicolon or an implicit semicolon after a statement, but found none",
223+
"severity": "Error",
224+
},
225+
]
226+
`);
227+
}
208228
});
209229

210230
describe('error', () => {

0 commit comments

Comments
 (0)