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 new file mode 100644 index 00000000..991006b1 --- /dev/null +++ b/packages/metro/src/JSTransformer/worker/__tests__/__snapshots__/worker-test.js.snap @@ -0,0 +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)\`."`; diff --git a/packages/metro/src/JSTransformer/worker/__tests__/worker-test.js b/packages/metro/src/JSTransformer/worker/__tests__/worker-test.js index c5e72c11..d338ec48 100644 --- a/packages/metro/src/JSTransformer/worker/__tests__/worker-test.js +++ b/packages/metro/src/JSTransformer/worker/__tests__/worker-test.js @@ -16,6 +16,9 @@ jest .mock('../inline') .mock('../minify'); +const { + InvalidRequireCallError, +} = require('../../../ModuleGraph/worker/collectDependencies'); const path = require('path'); const transformCode = require('..').transform; @@ -110,4 +113,30 @@ describe('code transformation worker:', () => { expect(result.map).toHaveLength(13); expect(result.dependencies).toEqual(['./c', './a', 'b']); }); + + it('reports filename when encountering unsupported dynamic dependency', async () => { + try { + await transformCode( + path.join(__dirname, '../../../transformer.js'), + 'arbitrary/file.js', + `local/file.js`, + [ + 'require("./a");', + 'let a = arbitrary(code);', + 'const b = require(a);', + ].join('\n'), + false, + { + dev: true, + transform: {}, + }, + [], + '', + ); + throw new Error('should not reach this'); + } catch (error) { + expect(error).toBeInstanceOf(InvalidRequireCallError); + expect(error.message).toMatchSnapshot(); + } + }); }); diff --git a/packages/metro/src/JSTransformer/worker/index.js b/packages/metro/src/JSTransformer/worker/index.js index 655fc3ac..941e8f32 100644 --- a/packages/metro/src/JSTransformer/worker/index.js +++ b/packages/metro/src/JSTransformer/worker/index.js @@ -123,7 +123,16 @@ function postTransform( wrappedAst = JsFileWrapping.wrapPolyfill(ast); } else { let dependencyMapName; - ({dependencies, dependencyMapName} = collectDependencies(ast)); + try { + ({dependencies, dependencyMapName} = collectDependencies(ast)); + } catch (error) { + if (error instanceof collectDependencies.InvalidRequireCallError) { + throw new collectDependencies.InvalidRequireCallError( + `\`${filename}\`: ${error.message}`, + ); + } + throw error; + } if (!options.dev) { dependencies = optimizeDependencies(ast, dependencies, dependencyMapName); } 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 e136ff52..7800e577 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 require() 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 require() 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 c0c39425..b7633148 100644 --- a/packages/metro/src/ModuleGraph/worker/collectDependencies.js +++ b/packages/metro/src/ModuleGraph/worker/collectDependencies.js @@ -152,8 +152,7 @@ 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, + `but this was found: \`${prettyPrint(node).code}\`.`, ); }