From d1d59d81e5926acdc745dac40ba9423f87f1f789 Mon Sep 17 00:00:00 2001 From: Burak Yigit Kaya Date: Tue, 10 Oct 2017 06:24:25 -0700 Subject: [PATCH] Show filename and line number on dynamic require errors Reviewed By: cpojer Differential Revision: D6008549 fbshipit-source-id: 72fde0a3f97841a020a6fc877f86513e9a7b8521 --- .../src/JSTransformer/worker/__tests__/worker-test.js | 4 ++-- .../src/JSTransformer/worker/extract-dependencies.js | 8 ++++++-- packages/metro-bundler/src/JSTransformer/worker/index.js | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/metro-bundler/src/JSTransformer/worker/__tests__/worker-test.js b/packages/metro-bundler/src/JSTransformer/worker/__tests__/worker-test.js index da0aa494..451854a8 100644 --- a/packages/metro-bundler/src/JSTransformer/worker/__tests__/worker-test.js +++ b/packages/metro-bundler/src/JSTransformer/worker/__tests__/worker-test.js @@ -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(); }); }); diff --git a/packages/metro-bundler/src/JSTransformer/worker/extract-dependencies.js b/packages/metro-bundler/src/JSTransformer/worker/extract-dependencies.js index 1fbe706e..2f9a02df 100644 --- a/packages/metro-bundler/src/JSTransformer/worker/extract-dependencies.js +++ b/packages/metro-bundler/src/JSTransformer/worker/extract-dependencies.js @@ -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); diff --git a/packages/metro-bundler/src/JSTransformer/worker/index.js b/packages/metro-bundler/src/JSTransformer/worker/index.js index a25913f7..37e2c3f7 100644 --- a/packages/metro-bundler/src/JSTransformer/worker/index.js +++ b/packages/metro-bundler/src/JSTransformer/worker/index.js @@ -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,