From 2bd9a503a1d04d4bb7725cc7df8cedd8c3559e21 Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Tue, 12 Dec 2017 06:03:47 -0800 Subject: [PATCH] metro: collectDependencies: fix error message Reviewed By: rafeca Differential Revision: D6544283 fbshipit-source-id: d2e7144ae6f77f312e02ef7720212e9505bd002e --- .../__snapshots__/collectDependencies-test.js.snap | 4 ++-- .../src/ModuleGraph/worker/collectDependencies.js | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/metro/src/ModuleGraph/worker/__tests__/__snapshots__/collectDependencies-test.js.snap b/packages/metro/src/ModuleGraph/worker/__tests__/__snapshots__/collectDependencies-test.js.snap index 2c909c21..e136ff52 100644 --- a/packages/metro/src/ModuleGraph/worker/__tests__/__snapshots__/collectDependencies-test.js.snap +++ b/packages/metro/src/ModuleGraph/worker/__tests__/__snapshots__/collectDependencies-test.js.snap @@ -1,5 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`throws on tagged template literals 1`] = `"Calls to import() expect exactly 1 string literal argument, but this was found: require(tag\`left-pad\`)"`; +exports[`throws on tagged template literals 1`] = `"Calls to require() expect exactly 1 string literal argument, but this was found: require(tag\`left-pad\`)"`; -exports[`throws on template literals with interpolations 1`] = `"Calls to import() expect exactly 1 string literal argument, but this was found: require(\`left\${\\"-\\"}pad\`)"`; +exports[`throws on template literals with interpolations 1`] = `"Calls to require() expect exactly 1 string literal argument, but this was found: require(\`left\${\\"-\\"}pad\`)"`; diff --git a/packages/metro/src/ModuleGraph/worker/collectDependencies.js b/packages/metro/src/ModuleGraph/worker/collectDependencies.js index 3692cc69..c0c39425 100644 --- a/packages/metro/src/ModuleGraph/worker/collectDependencies.js +++ b/packages/metro/src/ModuleGraph/worker/collectDependencies.js @@ -76,7 +76,7 @@ function isRequireCall(callee) { } function processImportCall(context, path, node, depMapIdent) { - const [, name] = getModuleNameFromCallArgs(node); + const [, name] = getModuleNameFromCallArgs('import', node); const index = assignDependencyIndex(context, name, 'import'); const mapLookup = createDepMapLookup(depMapIdent, index); const newImport = makeAsyncRequire({ @@ -87,7 +87,7 @@ function processImportCall(context, path, node, depMapIdent) { } function processRequireCall(context, node, depMapIdent) { - const [nameLiteral, name] = getModuleNameFromCallArgs(node); + const [nameLiteral, name] = getModuleNameFromCallArgs('require', node); const index = assignDependencyIndex(context, name, 'require'); const mapLookup = createDepMapLookup(depMapIdent, index); node.arguments = [mapLookup, nameLiteral]; @@ -98,10 +98,10 @@ function processRequireCall(context, node, depMapIdent) { * Extract the module name from `require` arguments. We support template * literal, for example one could write `require(`foo`)`. */ -function getModuleNameFromCallArgs(node) { +function getModuleNameFromCallArgs(type, node) { const args = node.arguments; if (args.length !== 1) { - throw invalidRequireOf('import', node); + throw invalidRequireOf(type, node); } const nameLiteral = args[0]; if (nameLiteral.type === 'StringLiteral') { @@ -109,11 +109,11 @@ function getModuleNameFromCallArgs(node) { } if (nameLiteral.type === 'TemplateLiteral') { if (nameLiteral.quasis.length !== 1) { - throw invalidRequireOf('import', node); + throw invalidRequireOf(type, node); } return [nameLiteral, nameLiteral.quasis[0].value.cooked]; } - throw invalidRequireOf('import', node); + throw invalidRequireOf(type, node); } /**