metro: collectDependencies: fix error message

Reviewed By: rafeca

Differential Revision: D6544283

fbshipit-source-id: d2e7144ae6f77f312e02ef7720212e9505bd002e
This commit is contained in:
Jean Lauliac 2017-12-12 06:03:47 -08:00 committed by Facebook Github Bot
parent 6f0d786e57
commit 2bd9a503a1
2 changed files with 8 additions and 8 deletions

View File

@ -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\`)"`;

View File

@ -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);
}
/**