Skip to content

Commit dcde2b6

Browse files
authored
fix: Use safeTraverse where appropriate (#94)
1 parent d060ad8 commit dcde2b6

4 files changed

Lines changed: 25 additions & 5 deletions

File tree

lib/transformations/removeDeprecatedPlugins/__snapshots__/removeDeprecatedPlugins.test.js.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,15 @@ export default (config) => {
3030
}
3131
"
3232
`;
33+
34+
exports[`removeDeprecatedPlugins transforms correctly using "removeDeprecatedPlugins-4" data 1`] = `
35+
"// This should throw
36+
const inst = new webpack.optimize.OccurrenceOrderPlugin()
37+
export default (config) => {
38+
config.plugins = [
39+
inst
40+
]
41+
return config
42+
}
43+
"
44+
`;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// This should throw
2+
const inst = new webpack.optimize.OccurrenceOrderPlugin()
3+
export default (config) => {
4+
config.plugins = [
5+
inst
6+
]
7+
return config
8+
}

lib/transformations/removeDeprecatedPlugins/removeDeprecatedPlugins.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ module.exports = function(j, ast, source) {
1919
return utils.findPluginsByName(j, ast, deprecatedPlugingsList)
2020
.forEach(path => {
2121
// For now we only support the case there plugins are defined in an Array
22-
if (path.parent &&
23-
path.parent.value &&
24-
utils.isType(path.parent.value, 'ArrayExpression')
25-
) {
22+
const arrayPath = utils.safeTraverse(path, ['parent','value']);
23+
if (arrayPath && utils.isType(arrayPath, 'ArrayExpression')) {
2624
// Check how many plugins are defined and
2725
// if there is only last plugin left remove `plugins: []` node
28-
if (path.parent.value.elements.length === 1) {
26+
const arrayElementsPath = utils.safeTraverse(arrayPath, ['elements']);
27+
if (arrayElementsPath && arrayElementsPath.length === 1) {
2928
j(path.parent.parent).remove();
3029
} else {
3130
j(path).remove();

lib/transformations/removeDeprecatedPlugins/removeDeprecatedPlugins.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ defineTest(__dirname, 'removeDeprecatedPlugins', 'removeDeprecatedPlugins-0');
44
defineTest(__dirname, 'removeDeprecatedPlugins', 'removeDeprecatedPlugins-1');
55
defineTest(__dirname, 'removeDeprecatedPlugins', 'removeDeprecatedPlugins-2');
66
defineTest(__dirname, 'removeDeprecatedPlugins', 'removeDeprecatedPlugins-3');
7+
defineTest(__dirname, 'removeDeprecatedPlugins', 'removeDeprecatedPlugins-4');

0 commit comments

Comments
 (0)