Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions crates/oxc_diagnostics/README.md

This file was deleted.

40 changes: 36 additions & 4 deletions crates/oxc_napi/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,54 @@
use std::sync::Arc;

use napi_derive::napi;

use oxc_diagnostics::{LabeledSpan, OxcDiagnostic};
use oxc_diagnostics::{LabeledSpan, NamedSource, OxcDiagnostic};

#[napi(object)]
pub struct OxcError {
pub severity: Severity,
pub message: String,
pub labels: Vec<ErrorLabel>,
pub help_message: Option<String>,
pub codeframe: Option<String>,
}

impl OxcError {
pub fn new(message: String) -> Self {
Self { severity: Severity::Error, message, labels: vec![], help_message: None }
Self {
severity: Severity::Error,
message,
labels: vec![],
help_message: None,
codeframe: None,
}
}

pub fn from_diagnostics(
filename: &str,
source_text: &str,
diagnostics: Vec<OxcDiagnostic>,
) -> Vec<Self> {
if diagnostics.is_empty() {
return vec![];
}
let source = Arc::new(NamedSource::new(filename, source_text.to_string()));
diagnostics.into_iter().map(|e| Self::from_diagnostic(&source, e)).collect()
}

pub fn from_diagnostic(
named_source: &Arc<NamedSource<String>>,
diagnostic: OxcDiagnostic,
) -> Self {
let mut error = Self::from(&diagnostic);
let codeframe = diagnostic.with_source_code(Arc::clone(named_source));
error.codeframe = Some(format!("{codeframe:?}"));
error
}
}

impl From<OxcDiagnostic> for OxcError {
fn from(diagnostic: OxcDiagnostic) -> Self {
impl From<&OxcDiagnostic> for OxcError {
fn from(diagnostic: &OxcDiagnostic) -> Self {
let labels = diagnostic
.labels
.as_ref()
Expand All @@ -28,6 +59,7 @@ impl From<OxcDiagnostic> for OxcError {
message: diagnostic.message.to_string(),
labels,
help_message: diagnostic.help.as_ref().map(ToString::to_string),
codeframe: None,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions napi/parser/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export interface OxcError {
message: string
labels: Array<ErrorLabel>
helpMessage?: string
codeframe?: string
}

/**
Expand Down
6 changes: 4 additions & 2 deletions napi/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,15 @@ fn parse_with_return(filename: &str, source_text: String, options: &ParserOption

let mut program = ret.program;
let mut module_record = ret.module_record;
let mut errors = ret.errors.into_iter().map(OxcError::from).collect::<Vec<_>>();
let mut diagnostics = ret.errors;

if options.show_semantic_errors == Some(true) {
let semantic_ret = SemanticBuilder::new().with_check_syntax_error(true).build(&program);
errors.extend(semantic_ret.errors.into_iter().map(OxcError::from));
diagnostics.extend(semantic_ret.errors);
}

let mut errors = OxcError::from_diagnostics(filename, &source_text, diagnostics);

let comments =
convert_utf8_to_utf16(&source_text, &mut program, &mut module_record, &mut errors);

Expand Down
10 changes: 10 additions & 0 deletions napi/parser/test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,15 @@ describe('UTF-16 span', () => {
expect(ret.errors).toMatchInlineSnapshot(`
[
{
"codeframe": "
x Expected a semicolon or an implicit semicolon after a statement, but found
| none
,-[test.js:1:12]
1 | "🤨";asdf asdf
: ^
\`----
help: Try insert a semicolon here
",
"helpMessage": "Try insert a semicolon here",
"labels": [
{
Expand All @@ -396,6 +405,7 @@ describe('error', () => {
it('returns structured error', () => {
const ret = parseSync('test.js', code);
expect(ret.errors.length).toBe(1);
delete ret.errors[0].codeframe;
expect(ret.errors[0]).toStrictEqual({
'helpMessage': 'Try insert a semicolon here',
'labels': [
Expand Down
1 change: 1 addition & 0 deletions napi/playground/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export interface OxcError {
message: string
labels: Array<ErrorLabel>
helpMessage?: string
codeframe?: string
}

export interface OxcLinterOptions {
Expand Down
2 changes: 1 addition & 1 deletion napi/playground/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Oxc {

#[napi]
pub fn get_diagnostics(&self) -> Vec<OxcError> {
self.diagnostics.iter().cloned().map(OxcError::from).collect()
self.diagnostics.iter().map(OxcError::from).collect()
}

#[napi]
Expand Down
1 change: 1 addition & 0 deletions napi/transform/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ export interface OxcError {
message: string
labels: Array<ErrorLabel>
helpMessage?: string
codeframe?: string
}

export interface ReactRefreshOptions {
Expand Down
3 changes: 2 additions & 1 deletion napi/transform/src/isolated_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ pub fn isolated_declaration(
.with_options(CodegenOptions { source_map_path, ..CodegenOptions::default() })
.build(&transformed_ret.program);

let errors = ret.errors.into_iter().chain(transformed_ret.errors).map(OxcError::from).collect();
let diagnostics = ret.errors.into_iter().chain(transformed_ret.errors).collect::<Vec<_>>();
let errors = OxcError::from_diagnostics(&filename, &source_text, diagnostics);

IsolatedDeclarationsResult {
code: codegen_ret.code,
Expand Down
6 changes: 3 additions & 3 deletions napi/transform/src/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ pub fn transform(
Ok(compiler) => compiler,
Err(errors) => {
return TransformResult {
errors: errors.into_iter().map(OxcError::from).collect(),
errors: OxcError::from_diagnostics(&filename, &source_text, errors),
..Default::default()
};
}
Expand All @@ -715,7 +715,7 @@ pub fn transform(
declaration: compiler.declaration,
declaration_map: compiler.declaration_map,
helpers_used: compiler.helpers_used,
errors: compiler.errors.into_iter().map(OxcError::from).collect(),
errors: OxcError::from_diagnostics(&filename, &source_text, compiler.errors),
}
}

Expand Down Expand Up @@ -818,6 +818,6 @@ pub fn module_runner_transform(
map: map.map(Into::into),
deps: deps.into_iter().collect::<Vec<String>>(),
dynamic_deps: dynamic_deps.into_iter().collect::<Vec<String>>(),
errors: parser_ret.errors.into_iter().map(OxcError::from).collect(),
errors: OxcError::from_diagnostics(&filename, &source_text, parser_ret.errors),
}
}
Loading