Show filename and line number on dynamic require errors

Reviewed By: cpojer

Differential Revision: D6008549

fbshipit-source-id: 72fde0a3f97841a020a6fc877f86513e9a7b8521
This commit is contained in:
Burak Yigit Kaya 2017-10-10 06:24:25 -07:00 committed by Facebook Github Bot
parent a60ba098c5
commit d1d59d81e5
3 changed files with 9 additions and 5 deletions

View File

@ -164,7 +164,7 @@ describe('code transformation worker:', () => {
{},
error => {
expect(error).toBeNull();
expect(extractDependencies).toBeCalledWith(code);
expect(extractDependencies).toBeCalledWith(code, 'filename');
done();
},
);
@ -282,7 +282,7 @@ describe('code transformation worker:', () => {
it('Uses the code obtained from `constant-folding` to extract dependencies', done => {
transformCode(transformer, filename, filename, 'code', options, () => {
expect(extractDependencies).toBeCalledWith(foldedCode);
expect(extractDependencies).toBeCalledWith(foldedCode, filename);
done();
});
});

View File

@ -28,7 +28,8 @@ const babylon = require('babylon');
* because it ignores that the scope may have reassigned or shadowed that value,
* but it's a tradeoff for simplicity.
*/
function extractDependencies(code: string) {
function extractDependencies(code: string, filename: string) {
const ast = babylon.parse(code, {sourceType: 'module'});
const dependencies = new Set();
const dependencyOffsets = [];
@ -40,7 +41,10 @@ function extractDependencies(code: string) {
if (parentType === 'TryStatement') {
return;
}
throw new Error('require() must have a single string literal argument');
throw new Error(
`require() must have a single string literal argument: ${filename}:${arg
.loc.start.line - 1}`,
);
}
dependencyOffsets.push(arg.start);
dependencies.add(arg.value);

View File

@ -143,7 +143,7 @@ const transformCode: TransformCode = asyncify(
const depsResult = isJson
? {dependencies: [], dependencyOffsets: []}
: extractDependencies(code);
: extractDependencies(code, filename);
const timeDelta = process.hrtime(
transformFileStartLogEntry.start_timestamp,