Skip to content

Commit

Permalink
fix: clone startObj to prevent unintended mutation
Browse files Browse the repository at this point in the history
FIXES #36
  • Loading branch information
joshswan committed Feb 27, 2024
1 parent 52c39b0 commit b67f25e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* https://github.com/joshswan/gulp-merge/blob/master/LICENSE
*/

const cloneDeep = require('lodash.clonedeep');

This comment has been minimized.

Copy link
@steverep

steverep Feb 27, 2024

Contributor

Consider using structuredClone instead which is probably faster (supported at node 17+).

This comment has been minimized.

Copy link
@joshswan

joshswan Feb 27, 2024

Author Owner

@steverep Good call, open to a PR if you feel like implementing. Would probably need a polyfill or major version bump since I know some people are still on <=16, as much as they shouldn't be.

This comment has been minimized.

Copy link
@steverep

steverep Feb 27, 2024

Contributor

You could use the polyfill from CoreJS, but I wouldn't bother adding a dependency. If you're willing to make 18 the minimum, it's an easy swap. If you plan to keep 16 support much longer, than a quick feature test and use lodash as fallback would be my recommendation.

Thanks for the quick turn around BTW 👍🏻

const mergeWith = require('lodash.mergewith');
const JSON5 = require('json5');
const path = require('path');
Expand Down Expand Up @@ -61,7 +62,7 @@ module.exports = function mergeJson(opts) {
throw new PluginError(PLUGIN_NAME, `${PLUGIN_NAME}: Invalid start and/or end object!`);
}

let merged = options.startObj;
let merged = cloneDeep(options.startObj);
let firstFile = null;

function parseAndMerge(file) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"dependencies": {
"json5": "^2.2.3",
"lodash.clonedeep": "^4.5.0",
"lodash.mergewith": "^4.6.1",
"plugin-error": "^2.0.1",
"through": "^2.3.8",
Expand Down
18 changes: 12 additions & 6 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,17 @@ describe('gulp-merge-json', () => {
});

test('uses supplied start object as base', (done) => {
const stream = gulp.src(['test/json/test1.json', 'test/json/test2.json']).pipe(merge({
startObj: { initial: 'value' },
}));
const startObj = { initial: 'value' };
const stream = gulp.src(['test/json/test1.json', 'test/json/test2.json']).pipe(merge({ startObj }));

stream.on('data', (file) => {
const expected = ['{', '\t"initial": "value",', '\t"name": "Josh",', '\t"pet": {', '\t\t"name": "Indy"', '\t},', '\t"tags": [', '\t\t"awesome",', '\t\t"fun"', '\t],', '\t"place": "San Francisco",', '\t"settings": {', '\t\t"likesSleep": true', '\t}', '}'].join('\n');

expect(file.contents.toString()).toBe(expected);

// Ensure startObj is not modified
expect(startObj).toStrictEqual({ initial: 'value' });

done();
});
});
Expand Down Expand Up @@ -319,14 +322,17 @@ describe('gulp-merge-json', () => {
});

test('merges JSON files containing arrays when passed an array starting object', (done) => {
const stream = gulp.src(['test/json/array1.json', 'test/json/array2.json']).pipe(merge({
startObj: [],
}));
const startObj = [];
const stream = gulp.src(['test/json/array1.json', 'test/json/array2.json']).pipe(merge({ startObj }));

stream.on('data', (file) => {
const expected = ['[', '\t{', '\t\t"a": 1,', '\t\t"b": 2,', '\t\t"c": 3,', '\t\t"d": 4', '\t}', ']'].join('\n');

expect(file.contents.toString()).toBe(expected);

// Ensure startObj is not modified
expect(startObj).toStrictEqual([]);

done();
});
});
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5166,6 +5166,11 @@ lodash.capitalize@^4.2.1:
resolved "https://registry.yarnpkg.com/lodash.capitalize/-/lodash.capitalize-4.2.1.tgz#f826c9b4e2a8511d84e3aca29db05e1a4f3b72a9"
integrity sha512-kZzYOKspf8XVX5AvmQF94gQW0lejFVgb80G85bU4ZWzoJ6C03PQg3coYAUpSTpQWelrZELd3XWgHzw4Ck5kaIw==

lodash.clonedeep@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef"
integrity sha512-H5ZhCF25riFd9uB5UCkVKo61m3S/xZk1x4wA6yp/L3RFP6Z/eHH1ymQcGLo7J3GMPfm0V/7m1tryHuGVxpqEBQ==

lodash.escaperegexp@^4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/lodash.escaperegexp/-/lodash.escaperegexp-4.1.2.tgz#64762c48618082518ac3df4ccf5d5886dae20347"
Expand Down

0 comments on commit b67f25e

Please sign in to comment.