Skip to content

Commit 032be56

Browse files
committed
fix: avoid side effect on plugin options
1 parent 76bd32c commit 032be56

2 files changed

Lines changed: 72 additions & 5 deletions

File tree

src/index.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,17 @@ const NAME = 'rollup-plugin-prettier';
3232

3333
module.exports = (options) => {
3434
let sourcemap = null;
35+
let newOptions = options;
3536

3637
if (options && hasSourceMap(options)) {
3738
sourcemap = isSourceMapEnabled(options);
3839

3940
// Delete custom option.
40-
deleteSourceMap(options);
41+
newOptions = deleteSourceMap(options);
4142

4243
// Do not send an empty option object.
43-
if (Object.keys(options).length === 0) {
44-
options = undefined;
44+
if (Object.keys(newOptions).length === 0) {
45+
newOptions = undefined;
4546
}
4647
}
4748

@@ -83,7 +84,7 @@ module.exports = (options) => {
8384
* @return {Object} The result containing a `code` property and, if a enabled, a `map` property.
8485
*/
8586
transformBundle(source, outputOptions) {
86-
const output = prettier.format(source, options);
87+
const output = prettier.format(source, newOptions);
8788
const outputOptionsSourcemap = isNil(outputOptions) ? null : isSourceMapEnabled(outputOptions);
8889

8990
// Should we generate sourcemap?
@@ -165,9 +166,18 @@ function isSourceMapEnabled(opts) {
165166
* Delete sourcemap option on object.
166167
*
167168
* @param {Object} opts The object.
169+
* @return {Object} Option object without `sourcemap` entry.
168170
*/
169171
function deleteSourceMap(opts) {
170-
SOURCE_MAPS_OPTS.forEach((p) => delete opts[p]);
172+
const newOptions = {};
173+
174+
Object.keys(opts).forEach((k) => {
175+
if (SOURCE_MAPS_OPTS.indexOf(k) < 0) {
176+
newOptions[k] = opts[k];
177+
}
178+
});
179+
180+
return newOptions;
171181
}
172182

173183
/**

test/index.spec.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
'use strict';
2626

27+
const prettier = require('prettier');
2728
const plugin = require('../dist/index.js');
2829

2930
describe('rollup-plugin-prettier', () => {
@@ -232,4 +233,60 @@ describe('rollup-plugin-prettier', () => {
232233
'var test = "hello world";\n'
233234
);
234235
});
236+
237+
it('should avoid side effect and do not modify plugin options', () => {
238+
const options = {
239+
sourceMap: false,
240+
};
241+
242+
const instance = plugin(options);
243+
instance.options({});
244+
instance.transformBundle('var foo = 0;');
245+
246+
// It should not have been touched.
247+
expect(options).toEqual({
248+
sourceMap: false,
249+
});
250+
});
251+
252+
it('should run prettier without sourcemap options', () => {
253+
const options = {
254+
sourceMap: false,
255+
};
256+
257+
spyOn(prettier, 'format').and.callThrough();
258+
259+
const code = 'var foo = 0;';
260+
const instance = plugin(options);
261+
instance.options({});
262+
instance.transformBundle(code);
263+
264+
expect(prettier.format).toHaveBeenCalledWith(code, undefined);
265+
expect(options).toEqual({
266+
sourceMap: false,
267+
});
268+
});
269+
270+
it('should run prettier without sourcemap options and custom other options', () => {
271+
const options = {
272+
sourceMap: false,
273+
singleQuote: true,
274+
};
275+
276+
spyOn(prettier, 'format').and.callThrough();
277+
278+
const code = 'var foo = 0;';
279+
const instance = plugin(options);
280+
instance.options({});
281+
instance.transformBundle(code);
282+
283+
expect(prettier.format).toHaveBeenCalledWith(code, {
284+
singleQuote: true,
285+
});
286+
287+
expect(options).toEqual({
288+
sourceMap: false,
289+
singleQuote: true,
290+
});
291+
});
235292
});

0 commit comments

Comments
 (0)