Skip to content

Commit

Permalink
fix: avoid side effect on plugin options
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Dec 30, 2017
1 parent 76bd32c commit 032be56
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ const NAME = 'rollup-plugin-prettier';

module.exports = (options) => {
let sourcemap = null;
let newOptions = options;

if (options && hasSourceMap(options)) {
sourcemap = isSourceMapEnabled(options);

// Delete custom option.
deleteSourceMap(options);
newOptions = deleteSourceMap(options);

// Do not send an empty option object.
if (Object.keys(options).length === 0) {
options = undefined;
if (Object.keys(newOptions).length === 0) {
newOptions = undefined;
}
}

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

// Should we generate sourcemap?
Expand Down Expand Up @@ -165,9 +166,18 @@ function isSourceMapEnabled(opts) {
* Delete sourcemap option on object.
*
* @param {Object} opts The object.
* @return {Object} Option object without `sourcemap` entry.
*/
function deleteSourceMap(opts) {
SOURCE_MAPS_OPTS.forEach((p) => delete opts[p]);
const newOptions = {};

Object.keys(opts).forEach((k) => {
if (SOURCE_MAPS_OPTS.indexOf(k) < 0) {
newOptions[k] = opts[k];
}
});

return newOptions;
}

/**
Expand Down
57 changes: 57 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

'use strict';

const prettier = require('prettier');
const plugin = require('../dist/index.js');

describe('rollup-plugin-prettier', () => {
Expand Down Expand Up @@ -232,4 +233,60 @@ describe('rollup-plugin-prettier', () => {
'var test = "hello world";\n'
);
});

it('should avoid side effect and do not modify plugin options', () => {
const options = {
sourceMap: false,
};

const instance = plugin(options);
instance.options({});
instance.transformBundle('var foo = 0;');

// It should not have been touched.
expect(options).toEqual({
sourceMap: false,
});
});

it('should run prettier without sourcemap options', () => {
const options = {
sourceMap: false,
};

spyOn(prettier, 'format').and.callThrough();

const code = 'var foo = 0;';
const instance = plugin(options);
instance.options({});
instance.transformBundle(code);

expect(prettier.format).toHaveBeenCalledWith(code, undefined);
expect(options).toEqual({
sourceMap: false,
});
});

it('should run prettier without sourcemap options and custom other options', () => {
const options = {
sourceMap: false,
singleQuote: true,
};

spyOn(prettier, 'format').and.callThrough();

const code = 'var foo = 0;';
const instance = plugin(options);
instance.options({});
instance.transformBundle(code);

expect(prettier.format).toHaveBeenCalledWith(code, {
singleQuote: true,
});

expect(options).toEqual({
sourceMap: false,
singleQuote: true,
});
});
});

0 comments on commit 032be56

Please sign in to comment.