Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Respect frame on plugin thrown errors when pos is also given (#2309)
  • Loading branch information
Andarist authored and lukastaegert committed Jul 17, 2018
1 parent 76a4844 commit d13aca5
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/utils/transform.ts
Expand Up @@ -40,11 +40,14 @@ function augmentCodeLocation<T extends RollupError | RollupWarning>({
if (pos.line !== undefined && pos.column !== undefined) {
const { line, column } = pos;
object.loc = { file: id, line, column };
object.frame = getCodeFrame(source, line, column);
} else {
object.pos = <any>pos;
const { line, column } = locate(source, pos, { offsetLine: 1 });
object.loc = { file: id, line, column };
}

if (object.frame === undefined) {
const { line, column } = object.loc;
object.frame = getCodeFrame(source, line, column);
}
}
Expand Down
5 changes: 3 additions & 2 deletions test/cli/index.js
Expand Up @@ -3,7 +3,7 @@ const assert = require('assert');
const sander = require('sander');
const buble = require('buble');
const { exec } = require('child_process');
const { deindent, normaliseOutput, runTestSuiteWithSamples } = require('../utils.js');
const { normaliseOutput, runTestSuiteWithSamples } = require('../utils.js');

const cwd = process.cwd();

Expand Down Expand Up @@ -32,7 +32,8 @@ runTestSuiteWithSamples(
}

if ('stderr' in config) {
assert.equal(deindent(config.stderr), stderr.trim());
const shouldContinue = config.stderr(stderr.trim());
if (!shouldContinue) return done();
} else if (stderr) {
console.error(stderr);
}
Expand Down
10 changes: 10 additions & 0 deletions test/cli/samples/custom-frame-with-pos/_config.js
@@ -0,0 +1,10 @@
const assert = require('assert');

module.exports = {
description: 'custom (plugin generated) code frame taking priority over pos generated one',
command: 'rollup -c',
error: () => true,
stderr: stderr => {
assert.ok(/custom code frame/.test(stderr));
}
};
1 change: 1 addition & 0 deletions test/cli/samples/custom-frame-with-pos/main.js
@@ -0,0 +1 @@
console.log("everyday I'm throwing");
18 changes: 18 additions & 0 deletions test/cli/samples/custom-frame-with-pos/rollup.config.js
@@ -0,0 +1,18 @@
module.exports = {
input: 'main.js',
output: {
format: 'cjs'
},
plugins: [
{
transform() {
const err = new Error('My error.');
err.frame = 'custom code frame';
this.error(err, {
line: 1,
column: 5,
});
}
},
]
};
10 changes: 10 additions & 0 deletions test/cli/samples/custom-frame/_config.js
@@ -0,0 +1,10 @@
const assert = require('assert');

module.exports = {
description: 'errors with custom (plugin generated) code frame',
command: 'rollup -c',
error: () => true,
stderr: stderr => {
assert.ok(/custom code frame/.test(stderr));
}
};
1 change: 1 addition & 0 deletions test/cli/samples/custom-frame/main.js
@@ -0,0 +1 @@
console.log("everyday I'm throwing");
15 changes: 15 additions & 0 deletions test/cli/samples/custom-frame/rollup.config.js
@@ -0,0 +1,15 @@
module.exports = {
input: 'main.js',
output: {
format: 'cjs'
},
plugins: [
{
transform() {
const err = new Error('My error.');
err.frame = 'custom code frame';
this.error(err);
}
},
]
};
7 changes: 6 additions & 1 deletion test/cli/samples/silent/_config.js
@@ -1,5 +1,10 @@
const assert = require('assert');

module.exports = {
description: 'does not print warnings with --silent',
command: 'rollup -i main.js -f cjs --silent',
stderr: ``
stderr: stderr => {
assert.equal(stderr, '');
return true;
}
};

0 comments on commit d13aca5

Please sign in to comment.