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 original parser error to rollup error #3176

Merged
merged 4 commits into from Oct 20, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Module.ts
Expand Up @@ -140,7 +140,8 @@ function tryParse(module: Module, Parser: typeof acorn.Parser, acornOptions: aco
module.error(
{
code: 'PARSE_ERROR',
message
message,
parserError: err
},
err.pos
);
Expand Down
1 change: 1 addition & 0 deletions src/rollup/types.d.ts
Expand Up @@ -4,6 +4,7 @@ import { EventEmitter } from 'events';
export const VERSION: string;

export interface RollupError extends RollupLogProps {
parserError?: Error;
stack?: string;
watchFiles?: string[];
}
Expand Down
9 changes: 9 additions & 0 deletions test/function/samples/double-default-export/_config.js
Expand Up @@ -5,6 +5,15 @@ module.exports = {
error: {
code: 'PARSE_ERROR',
message: `Duplicate export 'default'`,
parserError: {
loc: {
column: 7,
line: 2
},
message: "Duplicate export 'default' (2:7)",
pos: 25,
raisedAt: 34
},
pos: 25,
watchFiles: [path.resolve(__dirname, 'main.js'), path.resolve(__dirname, 'foo.js')],
loc: {
Expand Down
9 changes: 9 additions & 0 deletions test/function/samples/double-named-export/_config.js
Expand Up @@ -5,6 +5,15 @@ module.exports = {
error: {
code: 'PARSE_ERROR',
message: `Duplicate export 'foo'`,
parserError: {
loc: {
column: 9,
line: 3
},
message: "Duplicate export 'foo' (3:9)",
pos: 38,
raisedAt: 43
},
pos: 38,
watchFiles: [path.resolve(__dirname, 'main.js'), path.resolve(__dirname, 'foo.js')],
loc: {
Expand Down
9 changes: 9 additions & 0 deletions test/function/samples/double-named-reexport/_config.js
Expand Up @@ -5,6 +5,15 @@ module.exports = {
error: {
code: 'PARSE_ERROR',
message: `Duplicate export 'foo'`,
parserError: {
loc: {
column: 9,
line: 3
},
message: "Duplicate export 'foo' (3:9)",
pos: 38,
raisedAt: 43
},
pos: 38,
watchFiles: [path.resolve(__dirname, 'main.js'), path.resolve(__dirname, 'foo.js')],
loc: {
Expand Down
9 changes: 9 additions & 0 deletions test/function/samples/duplicate-import-fails/_config.js
Expand Up @@ -5,6 +5,15 @@ module.exports = {
error: {
code: 'PARSE_ERROR',
message: `Identifier 'a' has already been declared`,
parserError: {
loc: {
column: 9,
line: 2
},
message: "Identifier 'a' has already been declared (2:9)",
pos: 36,
raisedAt: 39
},
pos: 36,
watchFiles: [path.resolve(__dirname, 'main.js')],
loc: {
Expand Down
Expand Up @@ -5,6 +5,15 @@ module.exports = {
error: {
code: 'PARSE_ERROR',
message: `Identifier 'a' has already been declared`,
parserError: {
loc: {
column: 12,
line: 1
},
message: "Identifier 'a' has already been declared (1:12)",
pos: 12,
raisedAt: 15
},
pos: 12,
watchFiles: [path.resolve(__dirname, 'main.js')],
loc: {
Expand Down
9 changes: 9 additions & 0 deletions test/function/samples/error-parse-json/_config.js
Expand Up @@ -6,6 +6,15 @@ module.exports = {
error: {
code: 'PARSE_ERROR',
message: 'Unexpected token (Note that you need rollup-plugin-json to import JSON files)',
parserError: {
loc: {
column: 8,
line: 2
},
message: 'Unexpected token (2:8)',
pos: 10,
raisedAt: 11
},
pos: 10,
watchFiles: [path.resolve(__dirname, 'main.js'), path.resolve(__dirname, 'file.json')],
loc: {
Expand Down
Expand Up @@ -7,6 +7,15 @@ module.exports = {
code: 'PARSE_ERROR',
message:
'Unexpected token (Note that you need plugins to import files that are not JavaScript)',
parserError: {
loc: {
column: 0,
line: 1
},
message: 'Unexpected token (1:0)',
pos: 0,
raisedAt: 1
},
pos: 0,
watchFiles: [path.resolve(__dirname, 'main.js'), path.resolve(__dirname, 'file.css')],
loc: {
Expand Down
Expand Up @@ -5,6 +5,15 @@ module.exports = {
error: {
code: 'PARSE_ERROR',
message: `'import' and 'export' may only appear at the top level`,
parserError: {
loc: {
column: 2,
line: 2
},
message: "'import' and 'export' may only appear at the top level (2:2)",
pos: 19,
raisedAt: 25
},
pos: 19,
watchFiles: [path.resolve(__dirname, 'main.js')],
loc: {
Expand Down
Expand Up @@ -5,6 +5,15 @@ module.exports = {
error: {
code: 'PARSE_ERROR',
message: `'import' and 'export' may only appear at the top level`,
parserError: {
loc: {
column: 2,
line: 2
},
message: "'import' and 'export' may only appear at the top level (2:2)",
pos: 19,
raisedAt: 25
},
pos: 19,
watchFiles: [path.resolve(__dirname, 'main.js')],
loc: {
Expand Down
16 changes: 12 additions & 4 deletions test/utils.js
Expand Up @@ -13,11 +13,19 @@ exports.normaliseOutput = normaliseOutput;
exports.runTestSuiteWithSamples = runTestSuiteWithSamples;
exports.assertDirectoriesAreEqual = assertDirectoriesAreEqual;

function compareError(actual, expected) {
delete actual.stack;
actual = Object.assign({}, actual, {
message: actual.message
function normaliseError(error) {
delete error.stack;
return Object.assign({}, error, {
message: error.message
});
}

function compareError(actual, expected) {
actual = normaliseError(actual);

if (actual.parserError) {
actual.parserError = normaliseError(actual.parserError);
}

if (actual.frame) {
actual.frame = actual.frame.replace(/\s+$/gm, '');
Expand Down