collectDependencies: provide more data in invalid-require error

Summary: This improves the error message by providing line/column so we can identify errors encountered in https://github.com/facebook/metro/issues/65 easier. Also provide additional fields so we can better format that later on.

Reviewed By: cpojer

Differential Revision: D6641909

fbshipit-source-id: 0372da6b2fb51010b42ab906fc40b528b1483532
This commit is contained in:
Jean Lauliac 2017-12-28 05:08:33 -08:00 committed by Facebook Github Bot
parent 3ba92ba160
commit 66e19258a4
5 changed files with 36 additions and 16 deletions

View File

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

View File

@ -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 () => {

View File

@ -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<string>): 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,
};

View File

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

View File

@ -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;