From 8f6d040c234fe7968d44c944458408bcdb9c2359 Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Wed, 10 May 2017 05:58:15 -0700 Subject: [PATCH] packager: trying out @format Summary: Internally when adding `format` we have a lint rule that automatically reformat using `prettier` and the project rule. I'm concerned how we can ensure this stays consistent when merging PRs though. It's not a big issue because the volume of PRs is low, but we'll have to figure it out later on. Reviewed By: davidaurelio Differential Revision: D5035830 fbshipit-source-id: 6f2bc9eb8212938ff785a34d2684efd1a9813e1a --- packager/src/node-haste/AssetModule.js | 22 ++++-- .../src/node-haste/FilesByDirNameIndex.js | 1 + packager/src/node-haste/Module.js | 76 +++++++++++++------ packager/src/node-haste/ModuleCache.js | 61 ++++++++------- packager/src/node-haste/Package.js | 15 ++-- packager/src/node-haste/Polyfill.js | 12 +-- packager/src/node-haste/types.js | 1 + 7 files changed, 115 insertions(+), 73 deletions(-) diff --git a/packager/src/node-haste/AssetModule.js b/packager/src/node-haste/AssetModule.js index 2d8523cdc..8cb8c26f8 100644 --- a/packager/src/node-haste/AssetModule.js +++ b/packager/src/node-haste/AssetModule.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @flow + * @format */ 'use strict'; @@ -18,13 +19,15 @@ const getAssetDataFromName = require('./lib/getAssetDataFromName'); import type {CachedReadResult, ConstructorArgs, ReadResult} from './Module'; class AssetModule extends Module { - resolution: mixed; _name: string; _type: string; _dependencies: Array; - constructor(args: ConstructorArgs & {dependencies: Array}, platforms: Set) { + constructor( + args: ConstructorArgs & {dependencies: Array}, + platforms: Set, + ) { super(args); const {resolution, name, type} = getAssetDataFromName(this.path, platforms); this.resolution = resolution; @@ -38,9 +41,12 @@ class AssetModule extends Module { } readCached(): CachedReadResult { - /** $FlowFixMe: improper OOP design. AssetModule, being different from a - * normal Module, shouldn't inherit it in the first place. */ - return {result: {dependencies: this._dependencies}, outdatedDependencies: []}; + return { + /** $FlowFixMe: improper OOP design. AssetModule, being different from a + * normal Module, shouldn't inherit it in the first place. */ + result: {dependencies: this._dependencies}, + outdatedDependencies: [], + }; } /** $FlowFixMe: improper OOP design. */ @@ -49,9 +55,9 @@ class AssetModule extends Module { } getName() { - return super.getName().then( - id => id.replace(/\/[^\/]+$/, `/${this._name}.${this._type}`) - ); + return super + .getName() + .then(id => id.replace(/\/[^\/]+$/, `/${this._name}.${this._type}`)); } hash() { diff --git a/packager/src/node-haste/FilesByDirNameIndex.js b/packager/src/node-haste/FilesByDirNameIndex.js index 0da11bf90..10af60cda 100644 --- a/packager/src/node-haste/FilesByDirNameIndex.js +++ b/packager/src/node-haste/FilesByDirNameIndex.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @flow + * @format */ 'use strict'; diff --git a/packager/src/node-haste/Module.js b/packager/src/node-haste/Module.js index eb20216eb..7152857ac 100644 --- a/packager/src/node-haste/Module.js +++ b/packager/src/node-haste/Module.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @flow + * @format */ 'use strict'; @@ -22,13 +23,17 @@ const jsonStableStringify = require('json-stable-stringify'); const {join: joinPath, relative: relativePath, extname} = require('path'); -import type {TransformedCode, Options as WorkerOptions} from '../JSTransformer/worker/worker'; +import type { + TransformedCode, + Options as WorkerOptions, +} from '../JSTransformer/worker/worker'; import type {GlobalTransformCache} from '../lib/GlobalTransformCache'; import type {MappingsMap} from '../lib/SourceMap'; import type {GetTransformCacheKey} from '../lib/TransformCache'; import type {ReadTransformProps} from '../lib/TransformCache'; import type {Reporter} from '../lib/reporting'; -import type DependencyGraphHelpers from './DependencyGraph/DependencyGraphHelpers'; +import type DependencyGraphHelpers + from './DependencyGraph/DependencyGraphHelpers'; import type ModuleCache from './ModuleCache'; export type ReadResult = { @@ -51,12 +56,12 @@ export type TransformCode = ( ) => Promise; export type HasteImpl = { - getHasteName(filePath: string): (string | void), + getHasteName(filePath: string): string | void, // This exists temporarily to enforce consistency while we deprecate // @providesModule. enforceHasteNameMatches?: ( filePath: string, - expectedName: (string | void), + expectedName: string | void, ) => void, }; @@ -81,7 +86,6 @@ type DocBlock = {+[key: string]: string}; const TRANSFORM_CACHE = new TransformCache(); class Module { - path: string; type: string; @@ -155,14 +159,16 @@ class Module { return this.path; } - return p.getName() - .then(packageName => { - if (!packageName) { - return this.path; - } + return p.getName().then(packageName => { + if (!packageName) { + return this.path; + } - return joinPath(packageName, relativePath(p.root, this.path)).replace(/\\/g, '/'); - }); + return joinPath(packageName, relativePath(p.root, this.path)).replace( + /\\/g, + '/', + ); + }); }); } @@ -296,13 +302,18 @@ class Module { return; } _globalCache.fetch(cacheProps).then( - globalCachedResult => process.nextTick(() => { - if (globalCachedResult == null) { - this._transformAndStoreCodeGlobally(cacheProps, _globalCache, callback); - return; - } - callback(undefined, globalCachedResult); - }), + globalCachedResult => + process.nextTick(() => { + if (globalCachedResult == null) { + this._transformAndStoreCodeGlobally( + cacheProps, + _globalCache, + callback, + ); + return; + } + callback(undefined, globalCachedResult); + }), globalCacheError => process.nextTick(() => callback(globalCacheError)), ); } @@ -360,13 +371,22 @@ class Module { transformOptions: WorkerOptions, transformOptionsKey: string, ): CachedReadResult { - const cacheProps = this._getCacheProps(transformOptions, transformOptionsKey); + const cacheProps = this._getCacheProps( + transformOptions, + transformOptionsKey, + ); const cachedResult = TRANSFORM_CACHE.readSync(cacheProps); if (cachedResult.result == null) { - return {result: null, outdatedDependencies: cachedResult.outdatedDependencies}; + return { + result: null, + outdatedDependencies: cachedResult.outdatedDependencies, + }; } return { - result: this._finalizeReadResult(cacheProps.sourceCode, cachedResult.result), + result: this._finalizeReadResult( + cacheProps.sourceCode, + cachedResult.result, + ), outdatedDependencies: [], }; } @@ -394,11 +414,16 @@ class Module { return; } invariant(freshResult != null, 'inconsistent state'); - resolve(this._finalizeReadResult(cacheProps.sourceCode, freshResult)); + resolve( + this._finalizeReadResult(cacheProps.sourceCode, freshResult), + ); }, ); }).then(result => { - this._readResultsByOptionsKey.set(key, {result, outdatedDependencies: []}); + this._readResultsByOptionsKey.set(key, { + result, + outdatedDependencies: [], + }); return result; }); }); @@ -444,7 +469,8 @@ const knownHashes = new WeakMap(); function stableObjectHash(object) { let digest = knownHashes.get(object); if (!digest) { - digest = crypto.createHash('md5') + digest = crypto + .createHash('md5') .update(jsonStableStringify(object)) .digest('base64'); knownHashes.set(object, digest); diff --git a/packager/src/node-haste/ModuleCache.js b/packager/src/node-haste/ModuleCache.js index 6f7817d4e..0a183b161 100644 --- a/packager/src/node-haste/ModuleCache.js +++ b/packager/src/node-haste/ModuleCache.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @flow + * @format */ 'use strict'; @@ -19,13 +20,13 @@ const Polyfill = require('./Polyfill'); import type {GlobalTransformCache} from '../lib/GlobalTransformCache'; import type {GetTransformCacheKey} from '../lib/TransformCache'; import type {Reporter} from '../lib/reporting'; -import type DependencyGraphHelpers from './DependencyGraph/DependencyGraphHelpers'; +import type DependencyGraphHelpers + from './DependencyGraph/DependencyGraphHelpers'; import type {TransformCode, Options as ModuleOptions} from './Module'; type GetClosestPackageFn = (filePath: string) => ?string; class ModuleCache { - _assetDependencies: Array; _depGraphHelpers: DependencyGraphHelpers; _getClosestPackage: GetClosestPackageFn; @@ -39,26 +40,29 @@ class ModuleCache { _transformCode: TransformCode; _reporter: Reporter; - constructor({ - assetDependencies, - depGraphHelpers, - extractRequires, - getClosestPackage, - getTransformCacheKey, - globalTransformCache, - moduleOptions, - reporter, - transformCode, - }: { - assetDependencies: Array, - depGraphHelpers: DependencyGraphHelpers, - getClosestPackage: GetClosestPackageFn, - getTransformCacheKey: GetTransformCacheKey, - globalTransformCache: ?GlobalTransformCache, - moduleOptions: ModuleOptions, - reporter: Reporter, - transformCode: TransformCode, - }, platforms: Set) { + constructor( + { + assetDependencies, + depGraphHelpers, + extractRequires, + getClosestPackage, + getTransformCacheKey, + globalTransformCache, + moduleOptions, + reporter, + transformCode, + }: { + assetDependencies: Array, + depGraphHelpers: DependencyGraphHelpers, + getClosestPackage: GetClosestPackageFn, + getTransformCacheKey: GetTransformCacheKey, + globalTransformCache: ?GlobalTransformCache, + moduleOptions: ModuleOptions, + reporter: Reporter, + transformCode: TransformCode, + }, + platforms: Set, + ) { this._assetDependencies = assetDependencies; this._getClosestPackage = getClosestPackage; this._getTransformCacheKey = getTransformCacheKey; @@ -98,11 +102,14 @@ class ModuleCache { /* $FlowFixMe: missing options. This is because this is an incorrect OOP * design in the first place: AssetModule, being simpler than a normal * Module, should not inherit the Module class. */ - this._moduleCache[filePath] = new AssetModule({ - file: filePath, - moduleCache: this, - dependencies: this._assetDependencies, - }, this._platforms); + this._moduleCache[filePath] = new AssetModule( + { + file: filePath, + moduleCache: this, + dependencies: this._assetDependencies, + }, + this._platforms, + ); } return this._moduleCache[filePath]; } diff --git a/packager/src/node-haste/Package.js b/packager/src/node-haste/Package.js index a6fa0cbba..6e3f3efb3 100644 --- a/packager/src/node-haste/Package.js +++ b/packager/src/node-haste/Package.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @flow + * @format */ 'use strict'; @@ -23,16 +24,13 @@ type PackageContent = { }; class Package { - path: string; root: string; type: string; _content: ?PackageContent; - constructor({file}: { - file: string, - }) { + constructor({file}: {file: string}) { this.path = path.resolve(file); this.root = path.dirname(this.path); this.type = 'Package'; @@ -49,7 +47,8 @@ class Package { let main = json.main || 'index'; if (replacements && typeof replacements === 'object') { - main = replacements[main] || + main = + replacements[main] || replacements[main + '.js'] || replacements[main + '.json'] || replacements[main.replace(/(\.js|\.json)$/, '')] || @@ -85,8 +84,8 @@ class Package { // support exclude with "someDependency": false return replacement === false ? false - /* $FlowFixMe: type of replacements is not being validated */ - : replacement || name; + : /* $FlowFixMe: type of replacements is not being validated */ + replacement || name; } let relPath = './' + path.relative(this.root, name); @@ -113,7 +112,7 @@ class Package { return path.join( this.root, /* $FlowFixMe: `getReplacements` doesn't validate the return value. */ - redirect + redirect, ); } diff --git a/packager/src/node-haste/Polyfill.js b/packager/src/node-haste/Polyfill.js index 8a282520a..2e31af4af 100644 --- a/packager/src/node-haste/Polyfill.js +++ b/packager/src/node-haste/Polyfill.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @flow + * @format */ 'use strict'; @@ -16,14 +17,15 @@ const Module = require('./Module'); import type {ConstructorArgs} from './Module'; class Polyfill extends Module { - _id: string; _dependencies: Array; - constructor(options: ConstructorArgs & { - id: string, - dependencies: Array, - }) { + constructor( + options: ConstructorArgs & { + id: string, + dependencies: Array, + }, + ) { super(options); this._id = options.id; this._dependencies = options.dependencies; diff --git a/packager/src/node-haste/types.js b/packager/src/node-haste/types.js index 59ddf0e9f..b7598ccd5 100644 --- a/packager/src/node-haste/types.js +++ b/packager/src/node-haste/types.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @flow + * @format */ 'use strict';