mirror of https://github.com/status-im/metro.git
metro-bundler: extract-dependencies: nit fix
Reviewed By: davidaurelio Differential Revision: D5639285 fbshipit-source-id: 1f163160e72cb8a63c0c23846ad1b6cc3da13602
This commit is contained in:
parent
35979c7b00
commit
66182916de
|
@ -7,7 +7,9 @@
|
|||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*
|
||||
* @format
|
||||
* @emails oncall+javascript_tools
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const extractDependencies = require('../extract-dependencies');
|
||||
|
@ -84,12 +86,12 @@ describe('Dependency extraction:', () => {
|
|||
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 {dependencies, dependencyOffsets} = extractDependencies(code);
|
||||
expect(dependencies).toEqual([]);
|
||||
expect(dependencyOffsets).toEqual([]);
|
||||
expect(() => extractDependencies(code)).toThrowError(
|
||||
'require() must have a single string literal argument',
|
||||
);
|
||||
});
|
||||
|
||||
it('does not get confused by previous states', () => {
|
||||
|
|
|
@ -23,6 +23,10 @@ const babylon = require('babylon');
|
|||
* 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.
|
||||
* 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) {
|
||||
const ast = babylon.parse(code);
|
||||
|
@ -33,17 +37,16 @@ function extractDependencies(code: string) {
|
|||
CallExpression(path) {
|
||||
const node = path.node;
|
||||
const callee = node.callee;
|
||||
const arg = node.arguments[0];
|
||||
if (
|
||||
callee.type !== 'Identifier' ||
|
||||
callee.name !== 'require' ||
|
||||
!arg ||
|
||||
arg.type !== 'StringLiteral'
|
||||
) {
|
||||
return;
|
||||
if (callee.type === 'Identifier' && callee.name === 'require') {
|
||||
const arg = node.arguments[0];
|
||||
if (arg == null || arg.type !== 'StringLiteral') {
|
||||
throw new Error(
|
||||
'require() must have a single string literal argument',
|
||||
);
|
||||
}
|
||||
dependencyOffsets.push(arg.start);
|
||||
dependencies.add(arg.value);
|
||||
}
|
||||
dependencyOffsets.push(arg.start);
|
||||
dependencies.add(arg.value);
|
||||
},
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue