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

fix(json)!: log more robustly when JSON parsing fails #1361

Merged
merged 2 commits into from Dec 17, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/json/src/index.js
Expand Up @@ -24,8 +24,7 @@ export default function json(options = {}) {
};
} catch (err) {
const message = 'Could not parse JSON file';
const position = parseInt(/[\d]/.exec(err.message)[0], 10);
this.warn({ message, id, position });
this.error({ message, id, cause: err });
return null;
}
}
Expand Down
27 changes: 12 additions & 15 deletions packages/json/test/test.js
Expand Up @@ -73,21 +73,18 @@ test('handles JSON objects with no valid keys (#19)', async (t) => {
});

test('handles garbage', async (t) => {
const warns = [];

await rollup({
input: 'fixtures/garbage/main.js',
plugins: [json()],
onwarn: (warning) => warns.push(warning)
}).catch(() => {});

const [{ message, id, position, plugin }] = warns;

t.is(warns.length, 1);
t.is(plugin, 'json');
t.is(position, 1);
t.is(message, 'Could not parse JSON file');
t.regex(id, /(.*)bad.json$/);
const err = await t.throwsAsync(
rollup({
input: 'fixtures/garbage/main.js',
plugins: [json()]
})
);
t.is(err.code, 'PLUGIN_ERROR');
t.is(err.plugin, 'json');
t.is(err.message, 'Could not parse JSON file');
t.is(err.name, 'RollupError');
t.is(err.cause.name, 'SyntaxError');
t.regex(err.id, /(.*)bad.json$/);
});

test('does not generate an AST', async (t) => {
Expand Down