From 26d86b81317aaa8b7bbd8eaff13550728f4c2387 Mon Sep 17 00:00:00 2001 From: Pavithra K Date: Mon, 17 Apr 2017 17:16:24 +0530 Subject: [PATCH 1/9] Test watch transform --- .../watch/__snapshots__/watch.test.js.snap | 25 +++++++++++++++++++ .../watch/__testfixtures__/watch-0.input.js | 6 +++++ .../transformations/watch/watch.test.js | 4 +++ 3 files changed, 35 insertions(+) create mode 100644 lib/creator/transformations/watch/__snapshots__/watch.test.js.snap create mode 100644 lib/creator/transformations/watch/__testfixtures__/watch-0.input.js create mode 100644 lib/creator/transformations/watch/watch.test.js diff --git a/lib/creator/transformations/watch/__snapshots__/watch.test.js.snap b/lib/creator/transformations/watch/__snapshots__/watch.test.js.snap new file mode 100644 index 00000000000..d40ed863bc8 --- /dev/null +++ b/lib/creator/transformations/watch/__snapshots__/watch.test.js.snap @@ -0,0 +1,25 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`watch transforms correctly using "watch-0" data 1`] = ` +"module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + + watch: true +} +" +`; + +exports[`watch transforms correctly using "watch-0" data 2`] = ` +"module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + + watch: false +} +" +`; diff --git a/lib/creator/transformations/watch/__testfixtures__/watch-0.input.js b/lib/creator/transformations/watch/__testfixtures__/watch-0.input.js new file mode 100644 index 00000000000..ea0822c2484 --- /dev/null +++ b/lib/creator/transformations/watch/__testfixtures__/watch-0.input.js @@ -0,0 +1,6 @@ +module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + } +} diff --git a/lib/creator/transformations/watch/watch.test.js b/lib/creator/transformations/watch/watch.test.js new file mode 100644 index 00000000000..e488e00f39b --- /dev/null +++ b/lib/creator/transformations/watch/watch.test.js @@ -0,0 +1,4 @@ +const defineTest = require('../../../transformations/defineTest'); + +defineTest(__dirname, 'watch', 'watch-0', {watch : true}); +defineTest(__dirname, 'watch', 'watch-0', {watch : false}); From 38869c691e5e901ba37c7bc4be238796b79ad2b8 Mon Sep 17 00:00:00 2001 From: Pavithra K Date: Mon, 17 Apr 2017 17:35:47 +0530 Subject: [PATCH 2/9] Test watch transform (Doesnt look for existing obj) --- .../watch/__snapshots__/watch.test.js.snap | 28 ++++++++ .../__snapshots__/watchOptions.test.js.snap | 64 +++++++++++++++++++ .../watch/__testfixtures__/watch-1.input.js | 8 +++ .../watch/__testfixtures__/watch-2.input.js | 10 +++ .../transformations/watch/watch.test.js | 2 + .../watch/watchOptions.test.js | 19 ++++++ 6 files changed, 131 insertions(+) create mode 100644 lib/creator/transformations/watch/__snapshots__/watchOptions.test.js.snap create mode 100644 lib/creator/transformations/watch/__testfixtures__/watch-1.input.js create mode 100644 lib/creator/transformations/watch/__testfixtures__/watch-2.input.js create mode 100644 lib/creator/transformations/watch/watchOptions.test.js diff --git a/lib/creator/transformations/watch/__snapshots__/watch.test.js.snap b/lib/creator/transformations/watch/__snapshots__/watch.test.js.snap index d40ed863bc8..af8fd85fc08 100644 --- a/lib/creator/transformations/watch/__snapshots__/watch.test.js.snap +++ b/lib/creator/transformations/watch/__snapshots__/watch.test.js.snap @@ -23,3 +23,31 @@ exports[`watch transforms correctly using "watch-0" data 2`] = ` } " `; + +exports[`watch transforms correctly using "watch-1" data 1`] = ` +"module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + + watch: false, + watchOptions: {}, + watch: true +} +" +`; + +exports[`watch transforms correctly using "watch-1" data 2`] = ` +"module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + + watch: false, + watchOptions: {}, + watch: false +} +" +`; diff --git a/lib/creator/transformations/watch/__snapshots__/watchOptions.test.js.snap b/lib/creator/transformations/watch/__snapshots__/watchOptions.test.js.snap new file mode 100644 index 00000000000..9f9b0088461 --- /dev/null +++ b/lib/creator/transformations/watch/__snapshots__/watchOptions.test.js.snap @@ -0,0 +1,64 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`watchOptions transforms correctly using "watch-0" data 1`] = ` +"module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + + watchOptions: { + aggregateTimeout: 300, + poll: 1000, + ignored: /node_modules/ + } +} +" +`; + +exports[`watchOptions transforms correctly using "watch-1" data 1`] = ` +"module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + + watch: false, + watchOptions: { + aggregateTimeout: 300, + poll: 1000, + ignored: /node_modules/ + }, + + watchOptions: { + aggregateTimeout: 300, + poll: 1000, + ignored: /node_modules/ + } +} +" +`; + +exports[`watchOptions transforms correctly using "watch-2" data 1`] = ` +"module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + + watch: false, + watchOptions: { + aggregateTimeout: 320, + aggregateTimeout: 300, + poll: 1000, + ignored: /node_modules/ + }, + + watchOptions: { + aggregateTimeout: 300, + poll: 1000, + ignored: /node_modules/ + } +} +" +`; diff --git a/lib/creator/transformations/watch/__testfixtures__/watch-1.input.js b/lib/creator/transformations/watch/__testfixtures__/watch-1.input.js new file mode 100644 index 00000000000..503bb1d4a71 --- /dev/null +++ b/lib/creator/transformations/watch/__testfixtures__/watch-1.input.js @@ -0,0 +1,8 @@ +module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + watch: false, + watchOptions: {} +} diff --git a/lib/creator/transformations/watch/__testfixtures__/watch-2.input.js b/lib/creator/transformations/watch/__testfixtures__/watch-2.input.js new file mode 100644 index 00000000000..1d94f02f15f --- /dev/null +++ b/lib/creator/transformations/watch/__testfixtures__/watch-2.input.js @@ -0,0 +1,10 @@ +module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + watch: false, + watchOptions: { + aggregateTimeout: 320 + } +} diff --git a/lib/creator/transformations/watch/watch.test.js b/lib/creator/transformations/watch/watch.test.js index e488e00f39b..1b5e1e195c3 100644 --- a/lib/creator/transformations/watch/watch.test.js +++ b/lib/creator/transformations/watch/watch.test.js @@ -2,3 +2,5 @@ const defineTest = require('../../../transformations/defineTest'); defineTest(__dirname, 'watch', 'watch-0', {watch : true}); defineTest(__dirname, 'watch', 'watch-0', {watch : false}); +defineTest(__dirname, 'watch', 'watch-1', {watch : true}); +defineTest(__dirname, 'watch', 'watch-1', {watch : false}); diff --git a/lib/creator/transformations/watch/watchOptions.test.js b/lib/creator/transformations/watch/watchOptions.test.js new file mode 100644 index 00000000000..c457bfbb388 --- /dev/null +++ b/lib/creator/transformations/watch/watchOptions.test.js @@ -0,0 +1,19 @@ +const defineTest = require('../../../transformations/defineTest'); + +defineTest(__dirname, 'watchOptions', 'watch-0', {watchOptions : { + aggregateTimeout: 300, + poll: 1000, + ignored: '/node_modules/' +}}); + +defineTest(__dirname, 'watchOptions', 'watch-1', {watchOptions : { + aggregateTimeout: 300, + poll: 1000, + ignored: '/node_modules/' +}}); + +defineTest(__dirname, 'watchOptions', 'watch-2', {watchOptions : { + aggregateTimeout: 300, + poll: 1000, + ignored: '/node_modules/' +}}); From 90e6877b7ea6aba3bc529e0da4fbaefcfc0ecdf8 Mon Sep 17 00:00:00 2001 From: Pavithra K Date: Mon, 17 Apr 2017 17:36:19 +0530 Subject: [PATCH 3/9] Test target transform (Doesnt look for existing obj) --- .../target/__snapshots__/target.test.js.snap | 26 +++++++++++++++++++ .../target/__testfixtures__/target-0.input.js | 6 +++++ .../target/__testfixtures__/target-1.input.js | 7 +++++ .../transformations/target/target.test.js | 4 +++ 4 files changed, 43 insertions(+) create mode 100644 lib/creator/transformations/target/__snapshots__/target.test.js.snap create mode 100644 lib/creator/transformations/target/__testfixtures__/target-0.input.js create mode 100644 lib/creator/transformations/target/__testfixtures__/target-1.input.js create mode 100644 lib/creator/transformations/target/target.test.js diff --git a/lib/creator/transformations/target/__snapshots__/target.test.js.snap b/lib/creator/transformations/target/__snapshots__/target.test.js.snap new file mode 100644 index 00000000000..663e3b899af --- /dev/null +++ b/lib/creator/transformations/target/__snapshots__/target.test.js.snap @@ -0,0 +1,26 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`target transforms correctly using "target-0" data 1`] = ` +"module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + + target: async-node +} +" +`; + +exports[`target transforms correctly using "target-1" data 1`] = ` +"module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + + target: 'node', + target: async-node +} +" +`; diff --git a/lib/creator/transformations/target/__testfixtures__/target-0.input.js b/lib/creator/transformations/target/__testfixtures__/target-0.input.js new file mode 100644 index 00000000000..ea0822c2484 --- /dev/null +++ b/lib/creator/transformations/target/__testfixtures__/target-0.input.js @@ -0,0 +1,6 @@ +module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + } +} diff --git a/lib/creator/transformations/target/__testfixtures__/target-1.input.js b/lib/creator/transformations/target/__testfixtures__/target-1.input.js new file mode 100644 index 00000000000..c8e83f95936 --- /dev/null +++ b/lib/creator/transformations/target/__testfixtures__/target-1.input.js @@ -0,0 +1,7 @@ +module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + target: 'node' +} diff --git a/lib/creator/transformations/target/target.test.js b/lib/creator/transformations/target/target.test.js new file mode 100644 index 00000000000..f8760bb98dd --- /dev/null +++ b/lib/creator/transformations/target/target.test.js @@ -0,0 +1,4 @@ +const defineTest = require('../../../transformations/defineTest'); + +defineTest(__dirname, 'target', 'target-0', {target : 'async-node'}); +defineTest(__dirname, 'target', 'target-1', {target : 'async-node'}); From e7dff3649c997fe94e363b91fcf93e1b76fc0f6d Mon Sep 17 00:00:00 2001 From: Pavithra K Date: Mon, 17 Apr 2017 23:25:22 +0530 Subject: [PATCH 4/9] Test plugins transform (Doesnt look for existing obj) --- .../plugins/__snapshots__/plugins.test.js.snap | 15 +++++++++++++++ .../plugins/__testfixtures__/plugins-0.input.js | 6 ++++++ .../transformations/plugins/plugins.test.js | 5 +++++ 3 files changed, 26 insertions(+) create mode 100644 lib/creator/transformations/plugins/__snapshots__/plugins.test.js.snap create mode 100644 lib/creator/transformations/plugins/__testfixtures__/plugins-0.input.js create mode 100644 lib/creator/transformations/plugins/plugins.test.js diff --git a/lib/creator/transformations/plugins/__snapshots__/plugins.test.js.snap b/lib/creator/transformations/plugins/__snapshots__/plugins.test.js.snap new file mode 100644 index 00000000000..6fbe0c9fe35 --- /dev/null +++ b/lib/creator/transformations/plugins/__snapshots__/plugins.test.js.snap @@ -0,0 +1,15 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`plugins transforms correctly using "plugins-0" data 1`] = ` +"module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + + plugins: [ + new webpack.optimize.CommonsChunkPlugin({name:'vendor',filename:'vendor-[hash].min.js'}) + ] +} +" +`; diff --git a/lib/creator/transformations/plugins/__testfixtures__/plugins-0.input.js b/lib/creator/transformations/plugins/__testfixtures__/plugins-0.input.js new file mode 100644 index 00000000000..ea0822c2484 --- /dev/null +++ b/lib/creator/transformations/plugins/__testfixtures__/plugins-0.input.js @@ -0,0 +1,6 @@ +module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + } +} diff --git a/lib/creator/transformations/plugins/plugins.test.js b/lib/creator/transformations/plugins/plugins.test.js new file mode 100644 index 00000000000..5825f2a52dd --- /dev/null +++ b/lib/creator/transformations/plugins/plugins.test.js @@ -0,0 +1,5 @@ +const defineTest = require('../../../transformations/defineTest'); + +defineTest(__dirname, 'plugins', 'plugins-0', {plugins : [ + 'new webpack.optimize.CommonsChunkPlugin({name:' + '\'' + 'vendor' + '\'' + ',filename:' + '\'' + 'vendor' + '-[hash].min.js\'})' +]}); From e97dcf6000391372ec0dbf24cd79cfa40f48721a Mon Sep 17 00:00:00 2001 From: Pavithra K Date: Mon, 17 Apr 2017 23:35:51 +0530 Subject: [PATCH 5/9] Add failing test case for node --- .../node/__snapshots__/node.test.js.snap | 3 +++ .../node/__testfixtures__/node-0.input.js | 0 lib/creator/transformations/node/node.test.js | 11 +++++++++++ 3 files changed, 14 insertions(+) create mode 100644 lib/creator/transformations/node/__snapshots__/node.test.js.snap create mode 100644 lib/creator/transformations/node/__testfixtures__/node-0.input.js create mode 100644 lib/creator/transformations/node/node.test.js diff --git a/lib/creator/transformations/node/__snapshots__/node.test.js.snap b/lib/creator/transformations/node/__snapshots__/node.test.js.snap new file mode 100644 index 00000000000..51f6c6e9206 --- /dev/null +++ b/lib/creator/transformations/node/__snapshots__/node.test.js.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`node transforms correctly using "node-0" data 1`] = `""`; diff --git a/lib/creator/transformations/node/__testfixtures__/node-0.input.js b/lib/creator/transformations/node/__testfixtures__/node-0.input.js new file mode 100644 index 00000000000..e69de29bb2d diff --git a/lib/creator/transformations/node/node.test.js b/lib/creator/transformations/node/node.test.js new file mode 100644 index 00000000000..bd1b0e0484b --- /dev/null +++ b/lib/creator/transformations/node/node.test.js @@ -0,0 +1,11 @@ +const defineTest = require('../../../transformations/defineTest'); + +defineTest(__dirname, 'node', 'node-0', {node : { + console: false, + global: true, + process: true, + Buffer: true, + __filename: 'mock', + __dirname: 'mock', + setImmediate: true +}}); From 2a4a792626d282d1e9e890f631d1101636a116b8 Mon Sep 17 00:00:00 2001 From: Pavithra K Date: Mon, 17 Apr 2017 23:36:44 +0530 Subject: [PATCH 6/9] Add test case for node --- .../node/__snapshots__/node.test.js.snap | 20 ++++++++++++++++++- .../node/__testfixtures__/node-0.input.js | 6 ++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/creator/transformations/node/__snapshots__/node.test.js.snap b/lib/creator/transformations/node/__snapshots__/node.test.js.snap index 51f6c6e9206..0046bc33d63 100644 --- a/lib/creator/transformations/node/__snapshots__/node.test.js.snap +++ b/lib/creator/transformations/node/__snapshots__/node.test.js.snap @@ -1,3 +1,21 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`node transforms correctly using "node-0" data 1`] = `""`; +exports[`node transforms correctly using "node-0" data 1`] = ` +"module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + + node: { + console: false, + global: true, + process: true, + Buffer: true, + __filename: mock, + __dirname: mock, + setImmediate: true + } +} +" +`; diff --git a/lib/creator/transformations/node/__testfixtures__/node-0.input.js b/lib/creator/transformations/node/__testfixtures__/node-0.input.js index e69de29bb2d..ea0822c2484 100644 --- a/lib/creator/transformations/node/__testfixtures__/node-0.input.js +++ b/lib/creator/transformations/node/__testfixtures__/node-0.input.js @@ -0,0 +1,6 @@ +module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + } +} From 4c6939987c2e0d8569a72b1cd373396a9b6ee9ad Mon Sep 17 00:00:00 2001 From: Pavithra K Date: Tue, 18 Apr 2017 14:48:30 +0530 Subject: [PATCH 7/9] Add first instance of duplicate check and override --- .../watch/__snapshots__/watch.test.js.snap | 20 ++++++++----------- lib/creator/transformations/watch/watch.js | 12 ++++++++--- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/creator/transformations/watch/__snapshots__/watch.test.js.snap b/lib/creator/transformations/watch/__snapshots__/watch.test.js.snap index af8fd85fc08..70131f1c10c 100644 --- a/lib/creator/transformations/watch/__snapshots__/watch.test.js.snap +++ b/lib/creator/transformations/watch/__snapshots__/watch.test.js.snap @@ -26,28 +26,24 @@ exports[`watch transforms correctly using "watch-0" data 2`] = ` exports[`watch transforms correctly using "watch-1" data 1`] = ` "module.exports = { - entry: 'index.js', - output: { + entry: 'index.js', + output: { filename: 'bundle.js' }, - - watch: false, - watchOptions: {}, - watch: true + watch: true, + watchOptions: {} } " `; exports[`watch transforms correctly using "watch-1" data 2`] = ` "module.exports = { - entry: 'index.js', - output: { + entry: 'index.js', + output: { filename: 'bundle.js' }, - - watch: false, - watchOptions: {}, - watch: false + watch: false, + watchOptions: {} } " `; diff --git a/lib/creator/transformations/watch/watch.js b/lib/creator/transformations/watch/watch.js index 2e23c0c2616..490be124dd1 100644 --- a/lib/creator/transformations/watch/watch.js +++ b/lib/creator/transformations/watch/watch.js @@ -3,9 +3,15 @@ const isAssignment = require('../../utils/is-assignment'); module.exports = function(j, ast, yeomanConfig) { const webpackProperties = yeomanConfig.webpackOptions; function createWatchProperty(p) { - return p.value.properties.push( - j.property('init', j.identifier('watch'), j.literal(webpackProperties['watch'])) - ); + const existingProp = p.value.properties.filter(prop => prop.key.name === 'watch'); + let prop; + if (existingProp.length > 0){ + prop = existingProp[0]; + prop.value = j.literal(webpackProperties['watch']); + } else { + prop = j.property('init', j.identifier('watch'), j.literal(webpackProperties['watch'])); + p.value.properties.push(prop); + } } if(typeof(webpackProperties['watch']) === 'boolean') { return ast.find(j.ObjectExpression).filter(p => isAssignment(p, createWatchProperty)); From 4c5d8409fb38d47edea77ca1c4f1c0c44fa02bc2 Mon Sep 17 00:00:00 2001 From: Pavithra K Date: Tue, 18 Apr 2017 14:52:38 +0530 Subject: [PATCH 8/9] Extract a function --- lib/creator/transformations/watch/watch.js | 24 ++++++++++++++-------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/creator/transformations/watch/watch.js b/lib/creator/transformations/watch/watch.js index 490be124dd1..3b2290ffc98 100644 --- a/lib/creator/transformations/watch/watch.js +++ b/lib/creator/transformations/watch/watch.js @@ -1,17 +1,11 @@ const isAssignment = require('../../utils/is-assignment'); +const transformUtils = require('../../../transformations/utils'); module.exports = function(j, ast, yeomanConfig) { const webpackProperties = yeomanConfig.webpackOptions; function createWatchProperty(p) { - const existingProp = p.value.properties.filter(prop => prop.key.name === 'watch'); - let prop; - if (existingProp.length > 0){ - prop = existingProp[0]; - prop.value = j.literal(webpackProperties['watch']); - } else { - prop = j.property('init', j.identifier('watch'), j.literal(webpackProperties['watch'])); - p.value.properties.push(prop); - } + const propValue = transformUtils.createIdentifierOrLiteral(j, webpackProperties['watch']); + checkIfExistsAndAddValue(j, p, 'watch', propValue); } if(typeof(webpackProperties['watch']) === 'boolean') { return ast.find(j.ObjectExpression).filter(p => isAssignment(p, createWatchProperty)); @@ -19,3 +13,15 @@ module.exports = function(j, ast, yeomanConfig) { return ast; } }; + +function checkIfExistsAndAddValue(j, p, key, value) { + const existingProp = p.value.properties.filter(prop => prop.key.name === key); + let prop; + if (existingProp.length > 0){ + prop = existingProp[0]; + prop.value = value; + } else { + prop = j.property('init', j.identifier('watch'), value); + p.value.properties.push(prop); + } +} From 07a423d99aca82de9d5dfbf753724b1ab78f597c Mon Sep 17 00:00:00 2001 From: Pavithra K Date: Tue, 18 Apr 2017 15:05:34 +0530 Subject: [PATCH 9/9] Make a util to override object params --- lib/creator/transformations/watch/watch.js | 14 ++-------- .../__snapshots__/utils.test.js.snap | 17 ++++++++++++ lib/transformations/utils.js | 26 ++++++++++++++++++- lib/transformations/utils.test.js | 21 +++++++++++++++ 4 files changed, 65 insertions(+), 13 deletions(-) diff --git a/lib/creator/transformations/watch/watch.js b/lib/creator/transformations/watch/watch.js index 3b2290ffc98..188b34f59cf 100644 --- a/lib/creator/transformations/watch/watch.js +++ b/lib/creator/transformations/watch/watch.js @@ -5,7 +5,7 @@ module.exports = function(j, ast, yeomanConfig) { const webpackProperties = yeomanConfig.webpackOptions; function createWatchProperty(p) { const propValue = transformUtils.createIdentifierOrLiteral(j, webpackProperties['watch']); - checkIfExistsAndAddValue(j, p, 'watch', propValue); + transformUtils.checkIfExistsAndAddValue(j, p, 'watch', propValue); } if(typeof(webpackProperties['watch']) === 'boolean') { return ast.find(j.ObjectExpression).filter(p => isAssignment(p, createWatchProperty)); @@ -14,14 +14,4 @@ module.exports = function(j, ast, yeomanConfig) { } }; -function checkIfExistsAndAddValue(j, p, key, value) { - const existingProp = p.value.properties.filter(prop => prop.key.name === key); - let prop; - if (existingProp.length > 0){ - prop = existingProp[0]; - prop.value = value; - } else { - prop = j.property('init', j.identifier('watch'), value); - p.value.properties.push(prop); - } -} + diff --git a/lib/transformations/__snapshots__/utils.test.js.snap b/lib/transformations/__snapshots__/utils.test.js.snap index ac742027a35..400542763b9 100644 --- a/lib/transformations/__snapshots__/utils.test.js.snap +++ b/lib/transformations/__snapshots__/utils.test.js.snap @@ -1,5 +1,22 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`utils checkIfExistsAndAddValue should create new prop if none exist 1`] = ` +" + module.exports = { + entry: 'index.js', + externals: \\"React\\" + } + " +`; + +exports[`utils checkIfExistsAndAddValue should override prop if it exists 1`] = ` +" + module.exports = { + entry: \\"app.js\\" + } + " +`; + exports[`utils createIdentifierOrLiteral should create basic literal 1`] = `"'stringLiteral'"`; exports[`utils createIdentifierOrLiteral should create boolean 1`] = `"true"`; diff --git a/lib/transformations/utils.js b/lib/transformations/utils.js index cccfff5d57f..46b21c788ab 100644 --- a/lib/transformations/utils.js +++ b/lib/transformations/utils.js @@ -271,6 +271,29 @@ function getRequire(j, constName, packagePath) { ]); } +/* +* @function checkIfExistsAndAddValue +* +* If a prop exists, it overrides it, else it creates a new one +* @param j — jscodeshift API +* @param { Node } node - objectexpression to check +* @param { string } key - Key of the property +* @param { string } value - computed value of the property +* @returns - nothing +*/ + +function checkIfExistsAndAddValue(j, node, key, value) { + const existingProp = node.value.properties.filter(prop => prop.key.name === key); + let prop; + if (existingProp.length > 0){ + prop = existingProp[0]; + prop.value = value; + } else { + prop = j.property('init', j.identifier(key), value); + node.value.properties.push(prop); + } +} + module.exports = { safeTraverse, createProperty, @@ -282,5 +305,6 @@ module.exports = { createLiteral, createIdentifierOrLiteral, findObjWithOneOfKeys, - getRequire + getRequire, + checkIfExistsAndAddValue }; diff --git a/lib/transformations/utils.test.js b/lib/transformations/utils.test.js index c98d9444a0a..7b0b3d8bdfd 100644 --- a/lib/transformations/utils.test.js +++ b/lib/transformations/utils.test.js @@ -176,4 +176,25 @@ var a = { plugs: [] } expect(j(require).toSource()).toMatchSnapshot(); }); }); + + describe('checkIfExistsAndAddValue', () => { + it('should create new prop if none exist', () => { + const ast = j(` + module.exports = { + entry: 'index.js' + } + `); + ast.find(j.ObjectExpression).forEach(node => utils.checkIfExistsAndAddValue(j, node, 'externals', j.literal('React'))); + expect(ast.toSource()).toMatchSnapshot(); + }); + it('should override prop if it exists', () => { + const ast = j(` + module.exports = { + entry: 'index.js' + } + `); + ast.find(j.ObjectExpression).forEach(node => utils.checkIfExistsAndAddValue(j, node, 'entry', j.literal('app.js'))); + expect(ast.toSource()).toMatchSnapshot(); + }); + }); });