From 74e78ee43f73d0452dcce0db0887f055cf7abbee Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Mon, 24 Jul 2017 15:29:33 -0700 Subject: [PATCH] metro-bundler: @format node-haste subdir Summary: Format everything, one subdir at a time. Reviewed By: mjesun Differential Revision: D5481590 fbshipit-source-id: 6d9157e6857d61b8116bcf9285bd4be415c5ba01 --- .../DependencyGraph/DependencyGraphHelpers.js | 9 +- .../DependencyGraph/ResolutionResponse.js | 28 ++- .../src/node-haste/__mocks__/fs.js | 44 +++-- .../src/node-haste/__mocks__/graceful-fs.js | 3 + .../node-haste/__tests__/AssetModule-test.js | 9 +- .../__tests__/DependencyGraph-test.js | 1 + .../src/node-haste/__tests__/Module-test.js | 183 +++++++++--------- .../src/node-haste/lib/AsyncTaskGroup.js | 5 +- .../lib/__tests__/AssetPaths-test.js | 9 +- .../__tests__/parsePlatformFilePath-test.js | 19 +- .../src/node-haste/lib/toLocalPath.js | 8 +- 11 files changed, 178 insertions(+), 140 deletions(-) diff --git a/packages/metro-bundler/src/node-haste/DependencyGraph/DependencyGraphHelpers.js b/packages/metro-bundler/src/node-haste/DependencyGraph/DependencyGraphHelpers.js index 83af2327..6f399af3 100644 --- a/packages/metro-bundler/src/node-haste/DependencyGraph/DependencyGraphHelpers.js +++ b/packages/metro-bundler/src/node-haste/DependencyGraph/DependencyGraphHelpers.js @@ -1,4 +1,4 @@ - /** +/** * Copyright (c) 2015-present, Facebook, Inc. * All rights reserved. * @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @flow + * @format */ 'use strict'; @@ -16,11 +17,13 @@ const path = require('path'); const NODE_MODULES = path.sep + 'node_modules' + path.sep; class DependencyGraphHelpers { - _providesModuleNodeModules: Array; _assetExts: Array; - constructor({providesModuleNodeModules, assetExts}: { + constructor({ + providesModuleNodeModules, + assetExts, + }: { +providesModuleNodeModules: Array, +assetExts: Array, }) { diff --git a/packages/metro-bundler/src/node-haste/DependencyGraph/ResolutionResponse.js b/packages/metro-bundler/src/node-haste/DependencyGraph/ResolutionResponse.js index 1b3eca62..61476ad5 100644 --- a/packages/metro-bundler/src/node-haste/DependencyGraph/ResolutionResponse.js +++ b/packages/metro-bundler/src/node-haste/DependencyGraph/ResolutionResponse.js @@ -1,4 +1,4 @@ - /** +/** * Copyright (c) 2015-present, Facebook, Inc. * All rights reserved. * @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @flow + * @format */ 'use strict'; @@ -14,7 +15,6 @@ const NO_OPTIONS = {}; class ResolutionResponse { - dependencies: Array; mainModuleId: ?(number | string); mocks: mixed; @@ -49,20 +49,16 @@ class ResolutionResponse { mocks = this.mocks, } = properties; - const numPrependedDependencies = dependencies === this.dependencies - ? this.numPrependedDependencies : 0; + const numPrependedDependencies = + dependencies === this.dependencies ? this.numPrependedDependencies : 0; /* $FlowFixMe: Flow doesn't like Object.assign on class-made objects. */ - return Object.assign( - new this.constructor(this.options), - this, - { - dependencies, - mainModuleId, - mocks, - numPrependedDependencies, - }, - ); + return Object.assign(new this.constructor(this.options), this, { + dependencies, + mainModuleId, + mocks, + numPrependedDependencies, + }); } _assertNotFinalized() { @@ -119,7 +115,9 @@ class ResolutionResponse { this.mocks = mocks; } - getResolvedDependencyPairs(module: TModule): $ReadOnlyArray<[string, TModule]> { + getResolvedDependencyPairs( + module: TModule, + ): $ReadOnlyArray<[string, TModule]> { this._assertFinalized(); return this._mappings[module.hash()]; } diff --git a/packages/metro-bundler/src/node-haste/__mocks__/fs.js b/packages/metro-bundler/src/node-haste/__mocks__/fs.js index 41ad4f1c..9bcbab03 100644 --- a/packages/metro-bundler/src/node-haste/__mocks__/fs.js +++ b/packages/metro-bundler/src/node-haste/__mocks__/fs.js @@ -5,7 +5,10 @@ * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. + * + * @format */ + 'use strict'; const {EventEmitter} = require('events'); @@ -169,20 +172,27 @@ fs.open.mockImplementation(function(filepath) { callback(error, fd); }); -fs.read.mockImplementation((fd, buffer, writeOffset, length, position, callback = noop) => { - let bytesWritten; - try { - if (position == null || position < 0) { - ({position} = fd); +fs.read.mockImplementation( + (fd, buffer, writeOffset, length, position, callback = noop) => { + let bytesWritten; + try { + if (position == null || position < 0) { + ({position} = fd); + } + bytesWritten = fd.buffer.copy( + buffer, + writeOffset, + position, + position + length, + ); + fd.position = position + bytesWritten; + } catch (e) { + callback(Error('invalid argument')); + return; } - bytesWritten = fd.buffer.copy(buffer, writeOffset, position, position + length); - fd.position = position + bytesWritten; - } catch (e) { - callback(Error('invalid argument')); - return; - } - callback(null, bytesWritten, buffer); -}); + callback(null, bytesWritten, buffer); + }, +); fs.close.mockImplementation((fd, callback = noop) => { try { @@ -263,7 +273,9 @@ fs.watch.mockImplementation((filename, options, listener) => { fsWatcher.on('change', listener); fsWatcher.close = () => { watcherList.splice(watcherList.indexOf(fsWatcher), 1); - fsWatcher.close = () => { throw new Error('FSWatcher is already closed'); }; + fsWatcher.close = () => { + throw new Error('FSWatcher is already closed'); + }; }; watcherList.push(fsWatcher); }); @@ -273,7 +285,9 @@ fs.__triggerWatchEvent = (eventType, filename) => { directWatchers.forEach(wtc => wtc.emit('change', eventType)); const dirPath = path.dirname(filename); const dirWatchers = watcherListByPath.get(dirPath) || []; - dirWatchers.forEach(wtc => wtc.emit('change', eventType, path.relative(dirPath, filename))); + dirWatchers.forEach(wtc => + wtc.emit('change', eventType, path.relative(dirPath, filename)), + ); }; function getToNode(filepath) { diff --git a/packages/metro-bundler/src/node-haste/__mocks__/graceful-fs.js b/packages/metro-bundler/src/node-haste/__mocks__/graceful-fs.js index 71b31aa2..fc222a8b 100644 --- a/packages/metro-bundler/src/node-haste/__mocks__/graceful-fs.js +++ b/packages/metro-bundler/src/node-haste/__mocks__/graceful-fs.js @@ -5,7 +5,10 @@ * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. + * + * @format */ + 'use strict'; module.exports = require('./fs'); diff --git a/packages/metro-bundler/src/node-haste/__tests__/AssetModule-test.js b/packages/metro-bundler/src/node-haste/__tests__/AssetModule-test.js index fe1f002a..d48a337f 100644 --- a/packages/metro-bundler/src/node-haste/__tests__/AssetModule-test.js +++ b/packages/metro-bundler/src/node-haste/__tests__/AssetModule-test.js @@ -5,7 +5,10 @@ * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. + * + * @format */ + 'use strict'; const AssetModule = require('../AssetModule'); @@ -14,13 +17,15 @@ describe('AssetModule:', () => { const defaults = {file: '/arbitrary.png'}; it('has no dependencies by default', () => { - return new AssetModule(defaults).getDependencies() + return new AssetModule(defaults) + .getDependencies() .then(deps => expect(deps).toEqual([])); }); it('can be parametrized with dependencies', () => { const dependencies = ['arbitrary', 'dependencies']; - return new AssetModule({...defaults, dependencies}).getDependencies() + return new AssetModule({...defaults, dependencies}) + .getDependencies() .then(deps => expect(deps).toEqual(dependencies)); }); }); diff --git a/packages/metro-bundler/src/node-haste/__tests__/DependencyGraph-test.js b/packages/metro-bundler/src/node-haste/__tests__/DependencyGraph-test.js index 954e162f..e8e9aad0 100644 --- a/packages/metro-bundler/src/node-haste/__tests__/DependencyGraph-test.js +++ b/packages/metro-bundler/src/node-haste/__tests__/DependencyGraph-test.js @@ -8,6 +8,7 @@ * * @format */ + 'use strict'; jest.useRealTimers(); 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 583beba6..5e4ae081 100644 --- a/packages/metro-bundler/src/node-haste/__tests__/Module-test.js +++ b/packages/metro-bundler/src/node-haste/__tests__/Module-test.js @@ -5,10 +5,14 @@ * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. + * + * @format */ + 'use strict'; -jest.mock('fs') +jest + .mock('fs') .mock('graceful-fs') .mock('../ModuleCache') .mock('../DependencyGraph/DependencyGraphHelpers') @@ -20,12 +24,11 @@ const DependencyGraphHelpers = require('../DependencyGraph/DependencyGraphHelper const TransformCaching = require('../../lib/TransformCaching'); const fs = require('fs'); -const packageJson = - JSON.stringify({ - name: 'arbitrary', - version: '1.0.0', - description: "A require('foo') story", - }); +const packageJson = JSON.stringify({ + name: 'arbitrary', + version: '1.0.0', + description: "A require('foo') story", +}); function mockFS(rootChildren) { fs.__setMockFilesystem({root: rootChildren}); @@ -46,9 +49,9 @@ describe('Module', () => { const transformCache = TransformCaching.mocked(); const createCache = () => ({ - get: jest.genMockFn().mockImplementation( - (filepath, field, cb) => cb(filepath) - ), + get: jest + .genMockFn() + .mockImplementation((filepath, field, cb) => cb(filepath)), invalidate: jest.genMockFn(), end: jest.genMockFn(), }); @@ -62,14 +65,14 @@ describe('Module', () => { }, ...options, cache, - file: options && options.file || fileName, + file: (options && options.file) || fileName, depGraphHelpers: new DependencyGraphHelpers(), moduleCache: new ModuleCache({cache}), getTransformCacheKey: () => transformCacheKey, }); - const createJSONModule = - options => createModule({...options, file: '/root/package.json'}); + const createJSONModule = options => + createModule({...options, file: '/root/package.json'}); beforeEach(function() { process.platform = 'linux'; @@ -80,8 +83,7 @@ describe('Module', () => { describe('Module ID', () => { const moduleId = 'arbitraryModule'; - const source = - `/** + const source = `/** * @providesModule ${moduleId} */ `; @@ -97,23 +99,24 @@ describe('Module', () => { }); it('extracts the module name from the header', () => - module.getName().then(name => expect(name).toEqual(moduleId)) - ); + module.getName().then(name => expect(name).toEqual(moduleId))); it('identifies the module as haste module', () => - expect(module.isHaste()).toBe(true) - ); + expect(module.isHaste()).toBe(true)); it('does not transform the file in order to access the name', () => { - const transformCode = - jest.genMockFn().mockReturnValue(Promise.resolve()); - return createModule({transformCode}).getName() + const transformCode = jest + .genMockFn() + .mockReturnValue(Promise.resolve()); + return createModule({transformCode}) + .getName() .then(() => expect(transformCode).not.toBeCalled()); }); it('does not transform the file in order to access the haste status', () => { - const transformCode = - jest.genMockFn().mockReturnValue(Promise.resolve()); + const transformCode = jest + .genMockFn() + .mockReturnValue(Promise.resolve()); createModule({transformCode}).isHaste(); expect(transformCode).not.toBeCalled(); }); @@ -125,23 +128,24 @@ describe('Module', () => { }); it('uses the file name as module name', () => - module.getName().then(name => expect(name).toEqual(fileName)) - ); + module.getName().then(name => expect(name).toEqual(fileName))); it('does not identify the module as haste module', () => - expect(module.isHaste()).toBe(false) - ); + expect(module.isHaste()).toBe(false)); it('does not transform the file in order to access the name', () => { - const transformCode = - jest.genMockFn().mockReturnValue(Promise.resolve()); - return createModule({transformCode}).getName() + const transformCode = jest + .genMockFn() + .mockReturnValue(Promise.resolve()); + return createModule({transformCode}) + .getName() .then(() => expect(transformCode).not.toBeCalled()); }); it('does not transform the file in order to access the haste status', () => { - const transformCode = - jest.genMockFn().mockReturnValue(Promise.resolve()); + const transformCode = jest + .genMockFn() + .mockReturnValue(Promise.resolve()); createModule({transformCode}).isHaste(); expect(transformCode).not.toBeCalled(); }); @@ -155,14 +159,10 @@ describe('Module', () => { }); it('exposes file contents as `code` property on the data exposed by `read()`', () => - createModule().read().then(({code}) => - expect(code).toBe(fileContents)) - ); + createModule().read().then(({code}) => expect(code).toBe(fileContents))); it('exposes file contents via the `getCode()` method', () => - createModule().getCode().then(code => - expect(code).toBe(fileContents)) - ); + createModule().getCode().then(code => expect(code).toBe(fileContents))); }); describe('Custom Code Transform', () => { @@ -176,7 +176,8 @@ describe('Module', () => { beforeEach(function() { transformResult = {code: ''}; - transformCode = jest.genMockFn() + transformCode = jest + .genMockFn() .mockImplementation((module, sourceCode, options) => { transformCache.writeSync({ filePath: module.path, @@ -192,18 +193,18 @@ describe('Module', () => { it('passes the module and file contents to the transform function when reading', () => { const module = createModule({transformCode}); - return module.read() - .then(() => { - expect(transformCode).toBeCalledWith(module, fileContents, undefined); - }); + return module.read().then(() => { + expect(transformCode).toBeCalledWith(module, fileContents, undefined); + }); }); it('passes any additional options to the transform function when reading', () => { const module = createModule({transformCode}); const transformOptions = {arbitrary: Object()}; - return module.read(transformOptions) + return module + .read(transformOptions) .then(() => - expect(transformCode.mock.calls[0][2]).toBe(transformOptions) + expect(transformCode.mock.calls[0][2]).toBe(transformOptions), ); }); @@ -255,85 +256,79 @@ describe('Module', () => { it('exposes the transformed code rather than the raw file contents', () => { transformResult = {code: exampleCode}; const module = createModule({transformCode}); - return Promise.all([module.read(), module.getCode()]) - .then(([data, code]) => { - expect(data.code).toBe(exampleCode); - expect(code).toBe(exampleCode); - }); + return Promise.all([ + module.read(), + module.getCode(), + ]).then(([data, code]) => { + expect(data.code).toBe(exampleCode); + expect(code).toBe(exampleCode); + }); }); it('exposes the raw file contents as `source` property', () => { const module = createModule({transformCode}); - return module.read() - .then(data => expect(data.source).toBe(fileContents)); + return module.read().then(data => expect(data.source).toBe(fileContents)); }); it('exposes a source map returned by the transform', () => { const map = {version: 3}; transformResult = {map, code: exampleCode}; const module = createModule({transformCode}); - return Promise.all([module.read(), module.getMap()]) - .then(([data, sourceMap]) => { - expect(data.map).toBe(map); - expect(sourceMap).toBe(map); - }); + return Promise.all([ + module.read(), + module.getMap(), + ]).then(([data, sourceMap]) => { + expect(data.map).toBe(map); + expect(sourceMap).toBe(map); + }); }); it('caches the transform result for the same transform options', () => { let module = createModule({transformCode}); - return module.read() - .then(() => { + return module.read().then(() => { + expect(transformCode).toHaveBeenCalledTimes(1); + // We want to check transform caching rather than shallow caching of + // Promises returned by read(). + module = createModule({transformCode}); + return module.read().then(() => { expect(transformCode).toHaveBeenCalledTimes(1); - // We want to check transform caching rather than shallow caching of - // Promises returned by read(). - module = createModule({transformCode}); - return module.read() - .then(() => { - expect(transformCode).toHaveBeenCalledTimes(1); - }); }); + }); }); it('triggers a new transform for different transform options', () => { const module = createModule({transformCode}); - return module.read({foo: 1}) - .then(() => { - expect(transformCode).toHaveBeenCalledTimes(1); - return module.read({foo: 2}) - .then(() => { - expect(transformCode).toHaveBeenCalledTimes(2); - }); + return module.read({foo: 1}).then(() => { + expect(transformCode).toHaveBeenCalledTimes(1); + return module.read({foo: 2}).then(() => { + expect(transformCode).toHaveBeenCalledTimes(2); }); + }); }); it('triggers a new transform for different source code', () => { let module = createModule({transformCode}); - return module.read() - .then(() => { - expect(transformCode).toHaveBeenCalledTimes(1); - cache = createCache(); - mockIndexFile('test'); - module = createModule({transformCode}); - return module.read() - .then(() => { - expect(transformCode).toHaveBeenCalledTimes(2); - }); + return module.read().then(() => { + expect(transformCode).toHaveBeenCalledTimes(1); + cache = createCache(); + mockIndexFile('test'); + module = createModule({transformCode}); + return module.read().then(() => { + expect(transformCode).toHaveBeenCalledTimes(2); }); + }); }); it('triggers a new transform for different transform cache key', () => { let module = createModule({transformCode}); - return module.read() - .then(() => { - expect(transformCode).toHaveBeenCalledTimes(1); - transformCacheKey = 'other'; - module = createModule({transformCode}); - return module.read() - .then(() => { - expect(transformCode).toHaveBeenCalledTimes(2); - }); + return module.read().then(() => { + expect(transformCode).toHaveBeenCalledTimes(1); + transformCacheKey = 'other'; + module = createModule({transformCode}); + return module.read().then(() => { + expect(transformCode).toHaveBeenCalledTimes(2); }); + }); }); - }); }); diff --git a/packages/metro-bundler/src/node-haste/lib/AsyncTaskGroup.js b/packages/metro-bundler/src/node-haste/lib/AsyncTaskGroup.js index 81235595..234ca527 100644 --- a/packages/metro-bundler/src/node-haste/lib/AsyncTaskGroup.js +++ b/packages/metro-bundler/src/node-haste/lib/AsyncTaskGroup.js @@ -1,4 +1,4 @@ - /** +/** * Copyright (c) 2016-present, Facebook, Inc. * All rights reserved. * @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @flow + * @format */ 'use strict'; @@ -19,7 +20,7 @@ module.exports = class AsyncTaskGroup { constructor() { this._runningTasks = new Set(); this._resolve = null; - this.done = new Promise(resolve => this._resolve = resolve); + this.done = new Promise(resolve => (this._resolve = resolve)); } start(taskHandle: TTaskHandle) { diff --git a/packages/metro-bundler/src/node-haste/lib/__tests__/AssetPaths-test.js b/packages/metro-bundler/src/node-haste/lib/__tests__/AssetPaths-test.js index 97f4fea3..6759a31a 100644 --- a/packages/metro-bundler/src/node-haste/lib/__tests__/AssetPaths-test.js +++ b/packages/metro-bundler/src/node-haste/lib/__tests__/AssetPaths-test.js @@ -5,7 +5,10 @@ * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. + * + * @format */ + 'use strict'; var AssetPaths = require('../AssetPaths'); @@ -72,7 +75,7 @@ describe('AssetPaths', () => { }); describe('resolution extraction', () => { - it('should extract resolution simple case', () => { + it('should extract resolution simple case', () => { var data = AssetPaths.parse('test@2x.png', TEST_PLATFORMS); expect(data).toEqual({ assetName: 'test.png', @@ -83,7 +86,7 @@ describe('AssetPaths', () => { }); }); - it('should default resolution to 1', () => { + it('should default resolution to 1', () => { var data = AssetPaths.parse('test.png', TEST_PLATFORMS); expect(data).toEqual({ assetName: 'test.png', @@ -94,7 +97,7 @@ describe('AssetPaths', () => { }); }); - it('should support float', () => { + it('should support float', () => { var data = AssetPaths.parse('test@1.1x.png', TEST_PLATFORMS); expect(data).toEqual({ assetName: 'test.png', diff --git a/packages/metro-bundler/src/node-haste/lib/__tests__/parsePlatformFilePath-test.js b/packages/metro-bundler/src/node-haste/lib/__tests__/parsePlatformFilePath-test.js index 7500f31c..a0687b4f 100644 --- a/packages/metro-bundler/src/node-haste/lib/__tests__/parsePlatformFilePath-test.js +++ b/packages/metro-bundler/src/node-haste/lib/__tests__/parsePlatformFilePath-test.js @@ -5,7 +5,10 @@ * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. + * + * @format */ + 'use strict'; var parsePlatformFilePath = require('../parsePlatformFilePath'); @@ -23,9 +26,17 @@ describe('parsePlatformFilePath', function() { expect(get('/b/c/a@1.5x.ios.png')).toBe('ios'); expect(get('/b/c/a@1.5x.lol.png')).toBe(null); expect(get('/b/c/a.lol.png')).toBe(null); - expect(parsePlatformFilePath('a.ios.js', new Set(['ios'])).platform).toBe('ios'); - expect(parsePlatformFilePath('a.android.js', new Set(['android'])).platform).toBe('android'); - expect(parsePlatformFilePath('a.ios.js', new Set(['ubuntu'])).platform).toBe(null); - expect(parsePlatformFilePath('a.ubuntu.js', new Set(['ubuntu'])).platform).toBe('ubuntu'); + expect(parsePlatformFilePath('a.ios.js', new Set(['ios'])).platform).toBe( + 'ios', + ); + expect( + parsePlatformFilePath('a.android.js', new Set(['android'])).platform, + ).toBe('android'); + expect( + parsePlatformFilePath('a.ios.js', new Set(['ubuntu'])).platform, + ).toBe(null); + expect( + parsePlatformFilePath('a.ubuntu.js', new Set(['ubuntu'])).platform, + ).toBe('ubuntu'); }); }); diff --git a/packages/metro-bundler/src/node-haste/lib/toLocalPath.js b/packages/metro-bundler/src/node-haste/lib/toLocalPath.js index 2b8ed331..a4ea441b 100644 --- a/packages/metro-bundler/src/node-haste/lib/toLocalPath.js +++ b/packages/metro-bundler/src/node-haste/lib/toLocalPath.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @flow + * @format */ 'use strict'; @@ -18,7 +19,10 @@ export type LocalPath = OpaqueLocalPath & string; // FIXME: This function has the shortcoming of potentially returning identical // paths for two files in different roots. -function toLocalPath(roots: $ReadOnlyArray, absolutePath: string): LocalPath { +function toLocalPath( + roots: $ReadOnlyArray, + absolutePath: string, +): LocalPath { for (let i = 0; i < roots.length; i++) { const localPath = relative(roots[i], absolutePath); if (localPath[0] !== '.') { @@ -27,7 +31,7 @@ function toLocalPath(roots: $ReadOnlyArray, absolutePath: string): Local } throw new Error( - 'Expected root module to be relative to one of the project roots' + 'Expected root module to be relative to one of the project roots', ); }