Skip to content

Commit

Permalink
fix!: Avoid modifying arguments (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
fasttime authored Jul 1, 2022
1 parent 334c165 commit 6c05aba
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
7 changes: 3 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,12 @@ function setDefaults(plugin, message, opts) {
if (typeof plugin === 'object') {
return defaults(plugin);
}
opts = opts || {};
if (message instanceof Error) {
opts.error = message;
opts = Object.assign({}, opts, { error: message });
} else if (typeof message === 'object') {
opts = message;
opts = Object.assign({}, message);
} else {
opts.message = message;
opts = Object.assign({}, opts, { message: message });
}
opts.plugin = plugin;
return defaults(opts);
Expand Down
14 changes: 14 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,18 @@ describe('PluginError()', function () {
expect(err.toString().indexOf('Details:')).toEqual(-1);
done();
});

it('should not modify error argument', function(done) {
var realErr = { message: 'something broke' };
new PluginError('test', realErr);
expect(realErr).toEqual({ message: 'something broke' });
done();
});

it('should not modify options argument', function(done) {
var opts = { showStack: true };
new PluginError('test', 'message', opts);
expect(opts).toEqual({ showStack: true });
done();
});
});

0 comments on commit 6c05aba

Please sign in to comment.