diff --git a/packages/metro/src/JSTransformer/worker/__tests__/__snapshots__/worker-test.js.snap b/packages/metro/src/JSTransformer/worker/__tests__/__snapshots__/worker-test.js.snap index 991006b1..b8874390 100644 --- a/packages/metro/src/JSTransformer/worker/__tests__/__snapshots__/worker-test.js.snap +++ b/packages/metro/src/JSTransformer/worker/__tests__/__snapshots__/worker-test.js.snap @@ -1,3 +1,3 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`code transformation worker: reports filename when encountering unsupported dynamic dependency 1`] = `"\`arbitrary/file.js\`: Calls to require() expect exactly 1 string literal argument, but this was found: \`require(a)\`."`; +exports[`code transformation worker: reports filename when encountering unsupported dynamic dependency 1`] = `"arbitrary/file.js:3:10: calls to \`require\` expect exactly 1 string literal argument, but this was found: \`require(a)\`."`; diff --git a/packages/metro/src/JSTransformer/worker/__tests__/worker-test.js b/packages/metro/src/JSTransformer/worker/__tests__/worker-test.js index d338ec48..4255306d 100644 --- a/packages/metro/src/JSTransformer/worker/__tests__/worker-test.js +++ b/packages/metro/src/JSTransformer/worker/__tests__/worker-test.js @@ -16,11 +16,9 @@ jest .mock('../inline') .mock('../minify'); -const { - InvalidRequireCallError, -} = require('../../../ModuleGraph/worker/collectDependencies'); const path = require('path'); const transformCode = require('..').transform; +const {InvalidRequireCallError} = require('..'); describe('code transformation worker:', () => { it('transforms a simple script', async () => { diff --git a/packages/metro/src/JSTransformer/worker/index.js b/packages/metro/src/JSTransformer/worker/index.js index 135da666..b7e02a65 100644 --- a/packages/metro/src/JSTransformer/worker/index.js +++ b/packages/metro/src/JSTransformer/worker/index.js @@ -127,9 +127,7 @@ function postTransform( ({dependencies, dependencyMapName} = collectDependencies(ast)); } catch (error) { if (error instanceof collectDependencies.InvalidRequireCallError) { - throw new collectDependencies.InvalidRequireCallError( - `\`${filename}\`: ${error.message}`, - ); + throw new InvalidRequireCallError(error, filename); } throw error; } @@ -242,7 +240,22 @@ function isAsset(filePath: string, assetExts: $ReadOnlyArray): boolean { return assetExts.indexOf(path.extname(filePath).slice(1)) !== -1; } +class InvalidRequireCallError extends Error { + innerError: collectDependencies.InvalidRequireCallError; + filename: string; + + constructor( + innerError: collectDependencies.InvalidRequireCallError, + filename: string, + ) { + super(`${filename}:${innerError.message}`); + this.innerError = innerError; + this.filename = filename; + } +} + module.exports = { transform: transformCode, minify: minifyCode, + InvalidRequireCallError, }; 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 6e86d66f..6ca3be01 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,7 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Evaluating static arguments throws on tagged template literals 1`] = `"Calls to require() expect exactly 1 string literal argument, but this was found: \`require(tag\`left-pad\`)\`."`; +exports[`Evaluating static arguments throws on tagged template literals 1`] = `"1:0: calls to \`require\` expect exactly 1 string literal argument, but this was found: \`require(tag\`left-pad\`)\`."`; -exports[`Evaluating static arguments throws template literals with dyncamic interpolations 1`] = `"Calls to require() expect exactly 1 string literal argument, but this was found: \`require(\`left\${foo}pad\`)\`."`; +exports[`Evaluating static arguments throws template literals with dyncamic interpolations 1`] = `"1:8: calls to \`require\` expect exactly 1 string literal argument, but this was found: \`require(\`left\${foo}pad\`)\`."`; -exports[`Evaluating static arguments throws when requiring non-strings 1`] = `"Calls to require() expect exactly 1 string literal argument, but this was found: \`require(1)\`."`; +exports[`Evaluating static arguments throws when requiring non-strings 1`] = `"1:0: calls to \`require\` expect exactly 1 string literal argument, but this was found: \`require(1)\`."`; diff --git a/packages/metro/src/ModuleGraph/worker/collectDependencies.js b/packages/metro/src/ModuleGraph/worker/collectDependencies.js index ab256ebf..7c5c3959 100644 --- a/packages/metro/src/ModuleGraph/worker/collectDependencies.js +++ b/packages/metro/src/ModuleGraph/worker/collectDependencies.js @@ -153,15 +153,24 @@ const makeAsyncRequire = babelTemplate( ); function invalidRequireOf(type, node) { - return new InvalidRequireCallError( - `Calls to ${type}() expect exactly 1 string literal argument, ` + - `but this was found: \`${prettyPrint(node).code}\`.`, - ); + const str = prettyPrint(node).code; + return new InvalidRequireCallError(type, str, node.loc.start); } class InvalidRequireCallError extends Error { - constructor(message) { - super(message); + callType: string; + nodeString: string; + location: string; + + constructor(callType, nodeString, loc) { + super( + `${loc.line}:${loc.column}: ` + + `calls to \`${callType}\` expect exactly 1 string literal ` + + `argument, but this was found: \`${nodeString}\`.`, + ); + this.callType = callType; + this.nodeString = nodeString; + this.location = loc; } } collectDependencies.InvalidRequireCallError = InvalidRequireCallError;