Skip to content

Commit

Permalink
Ignore require if it is defined (#975)
Browse files Browse the repository at this point in the history
  • Loading branch information
fathyb authored and devongovett committed Mar 28, 2018
1 parent 74438d4 commit 12b649b
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/visitors/dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ module.exports = {
types.isIdentifier(callee) &&
callee.name === 'require' &&
args.length === 1 &&
types.isStringLiteral(args[0]);
types.isStringLiteral(args[0]) &&
!hasBinding(ancestors, 'require');

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

function hasBinding(node, name) {
if (Array.isArray(node)) {
return node.some(ancestor => hasBinding(ancestor, name));
} else if (
types.isProgram(node) ||
types.isBlockStatement(node) ||
types.isBlock(node)
) {
return node.body.some(statement => hasBinding(statement, name));
} else if (
types.isFunctionDeclaration(node) ||
types.isFunctionExpression(node) ||
types.isArrowFunctionExpression(node)
) {
return (
(node.id !== null && node.id.name === name) ||
node.params.some(
param => types.isIdentifier(param) && param.name === name
)
);
} else if (types.isVariableDeclaration(node)) {
return node.declarations.some(declaration => declaration.id.name === name);
}

return false;
}

function addDependency(asset, node, opts = {}) {
if (asset.options.target !== 'browser') {
const isRelativeImport = /^[/~.]/.test(node.value);
Expand Down
35 changes: 35 additions & 0 deletions test/integration/require-scope/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const test = {
unaryFnExpr: 'test failed',
fnExpr: 'test failed',
fnDecl: 'test failed',
varDecl: 'test failed',
topVarDecl: 'test failed'
};

module.exports.test = test;

function main(require) {
require('test passed');
}

main(x => test.fnDecl = x);

(function(require) {
require('test passed')
})(x => test.fnExpr = x);

void function main(require) {
require('test passed');
}(x => test.unaryFnExpr = x);

void function main() {
const require = x => test.varDecl = x

require('test passed')
}()

function require(x) {
return test.topVarDecl = x;
}

require('test passed')
24 changes: 24 additions & 0 deletions test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -711,4 +711,28 @@ describe('javascript', function() {

assert.deepEqual(output, err);
});

it('should ignore require if it is defined in the scope', async function() {
let b = await bundle(__dirname + '/integration/require-scope/index.js');

assertBundleTree(b, {
name: 'index.js',
assets: ['index.js'],
childBundles: [
{
type: 'map'
}
]
});

let output = run(b);

assert.equal(typeof output.test, 'object');

let failed = Object.keys(output.test).some(
key => output.test[key] !== 'test passed'
);

assert.equal(failed, false);
});
});

0 comments on commit 12b649b

Please sign in to comment.