metro-bundler: extract-dependencies: nit fix

Reviewed By: davidaurelio

Differential Revision: D5639285

fbshipit-source-id: 1f163160e72cb8a63c0c23846ad1b6cc3da13602
This commit is contained in:
Jean Lauliac 2017-08-17 06:33:47 -07:00 committed by Facebook Github Bot
parent 35979c7b00
commit 66182916de
2 changed files with 19 additions and 14 deletions

View File

@ -7,7 +7,9 @@
* of patent rights can be found in the PATENTS file in the same directory. * of patent rights can be found in the PATENTS file in the same directory.
* *
* @format * @format
* @emails oncall+javascript_tools
*/ */
'use strict'; 'use strict';
const extractDependencies = require('../extract-dependencies'); const extractDependencies = require('../extract-dependencies');
@ -84,12 +86,12 @@ describe('Dependency extraction:', () => {
expect(dependencyOffsets).toEqual([]); expect(dependencyOffsets).toEqual([]);
}); });
it('does not extract calls to require with non-static arguments', () => { it('throws on calls to require with non-static arguments', () => {
const code = "require('foo/' + bar)"; const code = "require('foo/' + bar)";
const {dependencies, dependencyOffsets} = extractDependencies(code); expect(() => extractDependencies(code)).toThrowError(
expect(dependencies).toEqual([]); 'require() must have a single string literal argument',
expect(dependencyOffsets).toEqual([]); );
}); });
it('does not get confused by previous states', () => { it('does not get confused by previous states', () => {

View File

@ -23,6 +23,10 @@ const babylon = require('babylon');
* The result of the dependency extraction is an de-duplicated array of * The result of the dependency extraction is an de-duplicated array of
* dependencies, and an array of offsets to the string literals with module IDs. * dependencies, and an array of offsets to the string literals with module IDs.
* The index points to the opening quote. * The index points to the opening quote.
*
* Note the technique of recognizing the identifier "require" is not proper
* because it ignores that the scope may have reassigned or shadowed that value,
* but it's a tradeoff for simplicity.
*/ */
function extractDependencies(code: string) { function extractDependencies(code: string) {
const ast = babylon.parse(code); const ast = babylon.parse(code);
@ -33,17 +37,16 @@ function extractDependencies(code: string) {
CallExpression(path) { CallExpression(path) {
const node = path.node; const node = path.node;
const callee = node.callee; const callee = node.callee;
const arg = node.arguments[0]; if (callee.type === 'Identifier' && callee.name === 'require') {
if ( const arg = node.arguments[0];
callee.type !== 'Identifier' || if (arg == null || arg.type !== 'StringLiteral') {
callee.name !== 'require' || throw new Error(
!arg || 'require() must have a single string literal argument',
arg.type !== 'StringLiteral' );
) { }
return; dependencyOffsets.push(arg.start);
dependencies.add(arg.value);
} }
dependencyOffsets.push(arg.start);
dependencies.add(arg.value);
}, },
}); });