mirror of https://github.com/status-im/metro.git
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:
parent
3ba92ba160
commit
66e19258a4
|
@ -1,3 +1,3 @@
|
||||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
// 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)\`."`;
|
||||||
|
|
|
@ -16,11 +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;
|
||||||
|
const {InvalidRequireCallError} = require('..');
|
||||||
|
|
||||||
describe('code transformation worker:', () => {
|
describe('code transformation worker:', () => {
|
||||||
it('transforms a simple script', async () => {
|
it('transforms a simple script', async () => {
|
||||||
|
|
|
@ -127,9 +127,7 @@ function postTransform(
|
||||||
({dependencies, dependencyMapName} = collectDependencies(ast));
|
({dependencies, dependencyMapName} = collectDependencies(ast));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof collectDependencies.InvalidRequireCallError) {
|
if (error instanceof collectDependencies.InvalidRequireCallError) {
|
||||||
throw new collectDependencies.InvalidRequireCallError(
|
throw new InvalidRequireCallError(error, filename);
|
||||||
`\`${filename}\`: ${error.message}`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
@ -242,7 +240,22 @@ function isAsset(filePath: string, assetExts: $ReadOnlyArray<string>): boolean {
|
||||||
return assetExts.indexOf(path.extname(filePath).slice(1)) !== -1;
|
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 = {
|
module.exports = {
|
||||||
transform: transformCode,
|
transform: transformCode,
|
||||||
minify: minifyCode,
|
minify: minifyCode,
|
||||||
|
InvalidRequireCallError,
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
// 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)\`."`;
|
||||||
|
|
|
@ -153,15 +153,24 @@ const makeAsyncRequire = babelTemplate(
|
||||||
);
|
);
|
||||||
|
|
||||||
function invalidRequireOf(type, node) {
|
function invalidRequireOf(type, node) {
|
||||||
return new InvalidRequireCallError(
|
const str = prettyPrint(node).code;
|
||||||
`Calls to ${type}() expect exactly 1 string literal argument, ` +
|
return new InvalidRequireCallError(type, str, node.loc.start);
|
||||||
`but this was found: \`${prettyPrint(node).code}\`.`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class InvalidRequireCallError extends Error {
|
class InvalidRequireCallError extends Error {
|
||||||
constructor(message) {
|
callType: string;
|
||||||
super(message);
|
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;
|
collectDependencies.InvalidRequireCallError = InvalidRequireCallError;
|
||||||
|
|
Loading…
Reference in New Issue