Skip to content

Commit

Permalink
Ignore noop dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
fathyb committed Apr 10, 2018
1 parent 3b1a585 commit 862f511
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/visitors/dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ module.exports = {
callee.name === 'require' &&
args.length === 1 &&
types.isStringLiteral(args[0]) &&
!hasBinding(ancestors, 'require');
!hasBinding(ancestors, 'require') &&
!hasNoOpParent(ancestors);

if (isRequire) {
let optional = ancestors.some(a => types.isTryStatement(a)) || undefined;
Expand Down Expand Up @@ -89,6 +90,19 @@ module.exports = {
}
};

function hasNoOpParent(ancestors) {
return ancestors.some(node => {
if (
types.isIfStatement(node) &&
types.isLiteral(node.test) &&
!node.test.value
) {
// only ignore if it's in the consequent branch
return ancestors.indexOf(node.consequent) !== -1;
}
});
}

function hasBinding(node, name) {
if (Array.isArray(node)) {
return node.some(ancestor => hasBinding(ancestor, name));
Expand Down
16 changes: 15 additions & 1 deletion test/integration/optional-dep/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
try {
require('optional-dep');
require('try-optional-dep');
} catch (err) {
module.exports = err;
}

if(false) {
require('if-false-optional-dep');
}

if(false) {
globalStuff(() =>
require('if-false-optional-dep-deep')
)
}

if('') {
require('if-falsy-optional-dep');
}

0 comments on commit 862f511

Please sign in to comment.