Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add line numbers to js errors #56

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,31 @@ export class Preprocessor {
*/
parse(src: string, filename?: string): Parsed[];
}

export interface ParseError {
/**
* Formatted output for CLI
*/
source_code: string;
/**
* Color-Formatted output for CLI
*/
source_code_color: string;

/**
* 0-indexed starting line of the error
*/
start_line: number;
/**
* 0-indexed starting byte-based column of the error
*/
start_column: number;
/**
* 0-indexed ending line of the error
*/
end_line: number;
/**
* 0-indexed ending byte-based column of the error
*/
end_column: number;
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,23 @@ fn as_javascript_error(err: swc_ecma_parser::error::Error, source_map: Lrc<Sourc
js_sys::Reflect::set(
&js_err,
&"source_code_color".into(),
&capture_err_detail(err, source_map, GraphicalTheme::unicode()),
&capture_err_detail(err.clone(), source_map.clone(), GraphicalTheme::unicode()),
)
.unwrap();

let lo = source_map.lookup_char_pos_adj(err.span().lo);
let hi = source_map.lookup_char_pos_adj(err.span().hi);

let start_line = lo.line;
let start_column = lo.col.0;
let end_line = hi.line;
let end_column = hi.col.0;

js_sys::Reflect::set(&js_err, &"start_line".into(), &start_line.into()).unwrap();
js_sys::Reflect::set(&js_err, &"start_column".into(), &start_column.into()).unwrap();
js_sys::Reflect::set(&js_err, &"end_line".into(), &end_line.into()).unwrap();
js_sys::Reflect::set(&js_err, &"end_column".into(), &end_column.into()).unwrap();

return js_err;
}

Expand Down
36 changes: 36 additions & 0 deletions test/node-api-esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,40 @@ export default template(\`Hi\`, {
.to.have.property("source_code_color")
.matches(/Expected ident.*[\u001b].*class \{/s);
});

it("Offers line on parse errors", function () {
let parseError;
try {
p.process(`class {`);
} catch (err) {
parseError = err;
}

expect(parseError.start_line).to.equal(1, "start_line");
expect(parseError.start_column).to.equal(6, "start_column");
expect(parseError.end_line).to.equal(1, "end_line");
expect(parseError.end_column).to.equal(7, "end_column");
});

it("Offers line on <template> parse errors", function () {
let parseError;
try {
p.process(`let foo = 2;

const Foo = <template>{{foo}}</template>

<template>
🥳🥳🥳🥳<Foo />
`);
} catch (err) {
parseError = err;
}

// Unexpected eof
// column 14 is the EOF
expect(parseError.start_line).to.equal(6, "start_line");
expect(parseError.start_column).to.equal(18, "start_column");
expect(parseError.end_line).to.equal(6, "end_line");
expect(parseError.end_column).to.equal(18, "end_column");
});
});