metro: JSTransformer/worker: report the filename when a dynamic dep is encountered

Reviewed By: rafeca

Differential Revision: D6544291

fbshipit-source-id: 618851850b50d49a29c767ba0d21a7bad522c4ca
This commit is contained in:
Jean Lauliac 2017-12-12 06:03:49 -08:00 committed by Facebook Github Bot
parent 2bd9a503a1
commit 26c7a96f44
5 changed files with 45 additions and 5 deletions

View File

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

View File

@ -16,6 +16,9 @@ jest
.mock('../inline') .mock('../inline')
.mock('../minify'); .mock('../minify');
const {
InvalidRequireCallError,
} = require('../../../ModuleGraph/worker/collectDependencies');
const path = require('path'); const path = require('path');
const transformCode = require('..').transform; const transformCode = require('..').transform;
@ -110,4 +113,30 @@ describe('code transformation worker:', () => {
expect(result.map).toHaveLength(13); expect(result.map).toHaveLength(13);
expect(result.dependencies).toEqual(['./c', './a', 'b']); 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();
}
});
}); });

View File

@ -123,7 +123,16 @@ function postTransform(
wrappedAst = JsFileWrapping.wrapPolyfill(ast); wrappedAst = JsFileWrapping.wrapPolyfill(ast);
} else { } else {
let dependencyMapName; let dependencyMapName;
try {
({dependencies, dependencyMapName} = collectDependencies(ast)); ({dependencies, dependencyMapName} = collectDependencies(ast));
} catch (error) {
if (error instanceof collectDependencies.InvalidRequireCallError) {
throw new collectDependencies.InvalidRequireCallError(
`\`${filename}\`: ${error.message}`,
);
}
throw error;
}
if (!options.dev) { if (!options.dev) {
dependencies = optimizeDependencies(ast, dependencies, dependencyMapName); dependencies = optimizeDependencies(ast, dependencies, dependencyMapName);
} }

View File

@ -1,5 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // 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\`)\`."`;

View File

@ -152,8 +152,7 @@ const makeAsyncRequire = babelTemplate(
function invalidRequireOf(type, node) { function invalidRequireOf(type, node) {
return new InvalidRequireCallError( return new InvalidRequireCallError(
`Calls to ${type}() expect exactly 1 string literal argument, ` + `Calls to ${type}() expect exactly 1 string literal argument, ` +
'but this was found: ' + `but this was found: \`${prettyPrint(node).code}\`.`,
prettyPrint(node).code,
); );
} }