Skip to content

Commit

Permalink
fix: Use safeTraverse where appropriate (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
okonet authored Mar 21, 2017
1 parent d060ad8 commit dcde2b6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,15 @@ export default (config) => {
}
"
`;

exports[`removeDeprecatedPlugins transforms correctly using "removeDeprecatedPlugins-4" data 1`] = `
"// This should throw
const inst = new webpack.optimize.OccurrenceOrderPlugin()
export default (config) => {
config.plugins = [
inst
]
return config
}
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This should throw
const inst = new webpack.optimize.OccurrenceOrderPlugin()
export default (config) => {
config.plugins = [
inst
]
return config
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ module.exports = function(j, ast, source) {
return utils.findPluginsByName(j, ast, deprecatedPlugingsList)
.forEach(path => {
// For now we only support the case there plugins are defined in an Array
if (path.parent &&
path.parent.value &&
utils.isType(path.parent.value, 'ArrayExpression')
) {
const arrayPath = utils.safeTraverse(path, ['parent','value']);
if (arrayPath && utils.isType(arrayPath, 'ArrayExpression')) {
// Check how many plugins are defined and
// if there is only last plugin left remove `plugins: []` node
if (path.parent.value.elements.length === 1) {
const arrayElementsPath = utils.safeTraverse(arrayPath, ['elements']);
if (arrayElementsPath && arrayElementsPath.length === 1) {
j(path.parent.parent).remove();
} else {
j(path).remove();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ defineTest(__dirname, 'removeDeprecatedPlugins', 'removeDeprecatedPlugins-0');
defineTest(__dirname, 'removeDeprecatedPlugins', 'removeDeprecatedPlugins-1');
defineTest(__dirname, 'removeDeprecatedPlugins', 'removeDeprecatedPlugins-2');
defineTest(__dirname, 'removeDeprecatedPlugins', 'removeDeprecatedPlugins-3');
defineTest(__dirname, 'removeDeprecatedPlugins', 'removeDeprecatedPlugins-4');

0 comments on commit dcde2b6

Please sign in to comment.