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.
|
* 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', () => {
|
||||||
|
|
|
@ -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;
|
||||||
|
if (callee.type === 'Identifier' && callee.name === 'require') {
|
||||||
const arg = node.arguments[0];
|
const arg = node.arguments[0];
|
||||||
if (
|
if (arg == null || arg.type !== 'StringLiteral') {
|
||||||
callee.type !== 'Identifier' ||
|
throw new Error(
|
||||||
callee.name !== 'require' ||
|
'require() must have a single string literal argument',
|
||||||
!arg ||
|
);
|
||||||
arg.type !== 'StringLiteral'
|
|
||||||
) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
dependencyOffsets.push(arg.start);
|
dependencyOffsets.push(arg.start);
|
||||||
dependencies.add(arg.value);
|
dependencies.add(arg.value);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue