From d7fee2396c4a3a424aff0752b90055b357b8ebca Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Thu, 11 May 2017 04:46:49 -0700 Subject: [PATCH] packager: Module: tweak isHaste return type Reviewed By: cpojer Differential Revision: D5036181 fbshipit-source-id: 6042beb44bc67e64dd57c8dd6043f8dd690e9382 --- .../src/node-haste/AssetModule.js | 2 +- .../node-haste/DependencyGraph/HasteMap.js | 30 ++++++++++--------- .../metro-bundler/src/node-haste/Module.js | 4 +-- .../metro-bundler/src/node-haste/Polyfill.js | 2 +- .../src/node-haste/__tests__/Module-test.js | 12 ++++---- 5 files changed, 26 insertions(+), 24 deletions(-) diff --git a/packages/metro-bundler/src/node-haste/AssetModule.js b/packages/metro-bundler/src/node-haste/AssetModule.js index 8cb8c26f..f03981b1 100644 --- a/packages/metro-bundler/src/node-haste/AssetModule.js +++ b/packages/metro-bundler/src/node-haste/AssetModule.js @@ -37,7 +37,7 @@ class AssetModule extends Module { } isHaste() { - return Promise.resolve(false); + return false; } readCached(): CachedReadResult { diff --git a/packages/metro-bundler/src/node-haste/DependencyGraph/HasteMap.js b/packages/metro-bundler/src/node-haste/DependencyGraph/HasteMap.js index 52809f22..6201fb94 100644 --- a/packages/metro-bundler/src/node-haste/DependencyGraph/HasteMap.js +++ b/packages/metro-bundler/src/node-haste/DependencyGraph/HasteMap.js @@ -123,36 +123,38 @@ class HasteMap extends EventEmitter { _processHasteModule(file, previousName) { const module = this._moduleCache.getModule(file); - return module.isHaste().then( - isHaste => isHaste && module.getName() + return Promise.resolve().then(() => { + const isHaste = module.isHaste(); + return isHaste && module.getName() .then(name => { const result = this._updateHasteMap(name, module); if (previousName && name !== previousName) { this.emit('change'); } return result; - }) - ); + }); + }); } _processHastePackage(file, previousName) { const p = this._moduleCache.getPackage(file); - return p.isHaste() - .then(isHaste => isHaste && p.getName() + return Promise.resolve().then(() => { + const isHaste = p.isHaste(); + return isHaste && p.getName() .then(name => { const result = this._updateHasteMap(name, p); if (previousName && name !== previousName) { this.emit('change'); } return result; - })) - .catch(e => { - if (e instanceof SyntaxError) { - // Malformed package.json. - return; - } - throw e; - }); + }); + }).catch(e => { + if (e instanceof SyntaxError) { + // Malformed package.json. + return; + } + throw e; + }); } _updateHasteMap(name, mod) { diff --git a/packages/metro-bundler/src/node-haste/Module.js b/packages/metro-bundler/src/node-haste/Module.js index 7152857a..10b8b9aa 100644 --- a/packages/metro-bundler/src/node-haste/Module.js +++ b/packages/metro-bundler/src/node-haste/Module.js @@ -133,8 +133,8 @@ class Module { this._readResultsByOptionsKey = new Map(); } - isHaste(): Promise { - return Promise.resolve().then(() => this._getHasteName() != null); + isHaste(): boolean { + return this._getHasteName() != null; } getCode(transformOptions: WorkerOptions) { diff --git a/packages/metro-bundler/src/node-haste/Polyfill.js b/packages/metro-bundler/src/node-haste/Polyfill.js index 2e31af4a..3024efde 100644 --- a/packages/metro-bundler/src/node-haste/Polyfill.js +++ b/packages/metro-bundler/src/node-haste/Polyfill.js @@ -32,7 +32,7 @@ class Polyfill extends Module { } isHaste() { - return Promise.resolve(false); + return false; } getName() { diff --git a/packages/metro-bundler/src/node-haste/__tests__/Module-test.js b/packages/metro-bundler/src/node-haste/__tests__/Module-test.js index 3d687fa4..2e6debb8 100644 --- a/packages/metro-bundler/src/node-haste/__tests__/Module-test.js +++ b/packages/metro-bundler/src/node-haste/__tests__/Module-test.js @@ -107,7 +107,7 @@ describe('Module', () => { ); it('identifies the module as haste module', () => - module.isHaste().then(isHaste => expect(isHaste).toBe(true)) + expect(module.isHaste()).toBe(true) ); it('does not transform the file in order to access the name', () => { @@ -120,8 +120,8 @@ describe('Module', () => { it('does not transform the file in order to access the haste status', () => { const transformCode = jest.genMockFn().mockReturnValue(Promise.resolve()); - return createModule({transformCode}).isHaste() - .then(() => expect(transformCode).not.toBeCalled()); + createModule({transformCode}).isHaste(); + expect(transformCode).not.toBeCalled(); }); }); @@ -135,7 +135,7 @@ describe('Module', () => { ); it('does not identify the module as haste module', () => - module.isHaste().then(isHaste => expect(isHaste).toBe(false)) + expect(module.isHaste()).toBe(false) ); it('does not transform the file in order to access the name', () => { @@ -148,8 +148,8 @@ describe('Module', () => { it('does not transform the file in order to access the haste status', () => { const transformCode = jest.genMockFn().mockReturnValue(Promise.resolve()); - return createModule({transformCode}).isHaste() - .then(() => expect(transformCode).not.toBeCalled()); + createModule({transformCode}).isHaste(); + expect(transformCode).not.toBeCalled(); }); }); });