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
This commit is contained in:
Jean Lauliac 2017-05-10 05:58:15 -07:00 committed by Facebook Github Bot
parent 48c27b9a6c
commit 8f6d040c23
7 changed files with 115 additions and 73 deletions

View File

@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory. * of patent rights can be found in the PATENTS file in the same directory.
* *
* @flow * @flow
* @format
*/ */
'use strict'; 'use strict';
@ -18,13 +19,15 @@ const getAssetDataFromName = require('./lib/getAssetDataFromName');
import type {CachedReadResult, ConstructorArgs, ReadResult} from './Module'; import type {CachedReadResult, ConstructorArgs, ReadResult} from './Module';
class AssetModule extends Module { class AssetModule extends Module {
resolution: mixed; resolution: mixed;
_name: string; _name: string;
_type: string; _type: string;
_dependencies: Array<string>; _dependencies: Array<string>;
constructor(args: ConstructorArgs & {dependencies: Array<string>}, platforms: Set<string>) { constructor(
args: ConstructorArgs & {dependencies: Array<string>},
platforms: Set<string>,
) {
super(args); super(args);
const {resolution, name, type} = getAssetDataFromName(this.path, platforms); const {resolution, name, type} = getAssetDataFromName(this.path, platforms);
this.resolution = resolution; this.resolution = resolution;
@ -38,9 +41,12 @@ class AssetModule extends Module {
} }
readCached(): CachedReadResult { readCached(): CachedReadResult {
/** $FlowFixMe: improper OOP design. AssetModule, being different from a return {
* normal Module, shouldn't inherit it in the first place. */ /** $FlowFixMe: improper OOP design. AssetModule, being different from a
return {result: {dependencies: this._dependencies}, outdatedDependencies: []}; * normal Module, shouldn't inherit it in the first place. */
result: {dependencies: this._dependencies},
outdatedDependencies: [],
};
} }
/** $FlowFixMe: improper OOP design. */ /** $FlowFixMe: improper OOP design. */
@ -49,9 +55,9 @@ class AssetModule extends Module {
} }
getName() { getName() {
return super.getName().then( return super
id => id.replace(/\/[^\/]+$/, `/${this._name}.${this._type}`) .getName()
); .then(id => id.replace(/\/[^\/]+$/, `/${this._name}.${this._type}`));
} }
hash() { hash() {

View File

@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory. * of patent rights can be found in the PATENTS file in the same directory.
* *
* @flow * @flow
* @format
*/ */
'use strict'; 'use strict';

View File

@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory. * of patent rights can be found in the PATENTS file in the same directory.
* *
* @flow * @flow
* @format
*/ */
'use strict'; 'use strict';
@ -22,13 +23,17 @@ const jsonStableStringify = require('json-stable-stringify');
const {join: joinPath, relative: relativePath, extname} = require('path'); 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 {GlobalTransformCache} from '../lib/GlobalTransformCache';
import type {MappingsMap} from '../lib/SourceMap'; import type {MappingsMap} from '../lib/SourceMap';
import type {GetTransformCacheKey} from '../lib/TransformCache'; import type {GetTransformCacheKey} from '../lib/TransformCache';
import type {ReadTransformProps} from '../lib/TransformCache'; import type {ReadTransformProps} from '../lib/TransformCache';
import type {Reporter} from '../lib/reporting'; import type {Reporter} from '../lib/reporting';
import type DependencyGraphHelpers from './DependencyGraph/DependencyGraphHelpers'; import type DependencyGraphHelpers
from './DependencyGraph/DependencyGraphHelpers';
import type ModuleCache from './ModuleCache'; import type ModuleCache from './ModuleCache';
export type ReadResult = { export type ReadResult = {
@ -51,12 +56,12 @@ export type TransformCode = (
) => Promise<TransformedCode>; ) => Promise<TransformedCode>;
export type HasteImpl = { export type HasteImpl = {
getHasteName(filePath: string): (string | void), getHasteName(filePath: string): string | void,
// This exists temporarily to enforce consistency while we deprecate // This exists temporarily to enforce consistency while we deprecate
// @providesModule. // @providesModule.
enforceHasteNameMatches?: ( enforceHasteNameMatches?: (
filePath: string, filePath: string,
expectedName: (string | void), expectedName: string | void,
) => void, ) => void,
}; };
@ -81,7 +86,6 @@ type DocBlock = {+[key: string]: string};
const TRANSFORM_CACHE = new TransformCache(); const TRANSFORM_CACHE = new TransformCache();
class Module { class Module {
path: string; path: string;
type: string; type: string;
@ -155,14 +159,16 @@ class Module {
return this.path; return this.path;
} }
return p.getName() return p.getName().then(packageName => {
.then(packageName => { if (!packageName) {
if (!packageName) { return this.path;
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; return;
} }
_globalCache.fetch(cacheProps).then( _globalCache.fetch(cacheProps).then(
globalCachedResult => process.nextTick(() => { globalCachedResult =>
if (globalCachedResult == null) { process.nextTick(() => {
this._transformAndStoreCodeGlobally(cacheProps, _globalCache, callback); if (globalCachedResult == null) {
return; this._transformAndStoreCodeGlobally(
} cacheProps,
callback(undefined, globalCachedResult); _globalCache,
}), callback,
);
return;
}
callback(undefined, globalCachedResult);
}),
globalCacheError => process.nextTick(() => callback(globalCacheError)), globalCacheError => process.nextTick(() => callback(globalCacheError)),
); );
} }
@ -360,13 +371,22 @@ class Module {
transformOptions: WorkerOptions, transformOptions: WorkerOptions,
transformOptionsKey: string, transformOptionsKey: string,
): CachedReadResult { ): CachedReadResult {
const cacheProps = this._getCacheProps(transformOptions, transformOptionsKey); const cacheProps = this._getCacheProps(
transformOptions,
transformOptionsKey,
);
const cachedResult = TRANSFORM_CACHE.readSync(cacheProps); const cachedResult = TRANSFORM_CACHE.readSync(cacheProps);
if (cachedResult.result == null) { if (cachedResult.result == null) {
return {result: null, outdatedDependencies: cachedResult.outdatedDependencies}; return {
result: null,
outdatedDependencies: cachedResult.outdatedDependencies,
};
} }
return { return {
result: this._finalizeReadResult(cacheProps.sourceCode, cachedResult.result), result: this._finalizeReadResult(
cacheProps.sourceCode,
cachedResult.result,
),
outdatedDependencies: [], outdatedDependencies: [],
}; };
} }
@ -394,11 +414,16 @@ class Module {
return; return;
} }
invariant(freshResult != null, 'inconsistent state'); invariant(freshResult != null, 'inconsistent state');
resolve(this._finalizeReadResult(cacheProps.sourceCode, freshResult)); resolve(
this._finalizeReadResult(cacheProps.sourceCode, freshResult),
);
}, },
); );
}).then(result => { }).then(result => {
this._readResultsByOptionsKey.set(key, {result, outdatedDependencies: []}); this._readResultsByOptionsKey.set(key, {
result,
outdatedDependencies: [],
});
return result; return result;
}); });
}); });
@ -444,7 +469,8 @@ const knownHashes = new WeakMap();
function stableObjectHash(object) { function stableObjectHash(object) {
let digest = knownHashes.get(object); let digest = knownHashes.get(object);
if (!digest) { if (!digest) {
digest = crypto.createHash('md5') digest = crypto
.createHash('md5')
.update(jsonStableStringify(object)) .update(jsonStableStringify(object))
.digest('base64'); .digest('base64');
knownHashes.set(object, digest); knownHashes.set(object, digest);

View File

@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory. * of patent rights can be found in the PATENTS file in the same directory.
* *
* @flow * @flow
* @format
*/ */
'use strict'; 'use strict';
@ -19,13 +20,13 @@ const Polyfill = require('./Polyfill');
import type {GlobalTransformCache} from '../lib/GlobalTransformCache'; import type {GlobalTransformCache} from '../lib/GlobalTransformCache';
import type {GetTransformCacheKey} from '../lib/TransformCache'; import type {GetTransformCacheKey} from '../lib/TransformCache';
import type {Reporter} from '../lib/reporting'; 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'; import type {TransformCode, Options as ModuleOptions} from './Module';
type GetClosestPackageFn = (filePath: string) => ?string; type GetClosestPackageFn = (filePath: string) => ?string;
class ModuleCache { class ModuleCache {
_assetDependencies: Array<string>; _assetDependencies: Array<string>;
_depGraphHelpers: DependencyGraphHelpers; _depGraphHelpers: DependencyGraphHelpers;
_getClosestPackage: GetClosestPackageFn; _getClosestPackage: GetClosestPackageFn;
@ -39,26 +40,29 @@ class ModuleCache {
_transformCode: TransformCode; _transformCode: TransformCode;
_reporter: Reporter; _reporter: Reporter;
constructor({ constructor(
assetDependencies, {
depGraphHelpers, assetDependencies,
extractRequires, depGraphHelpers,
getClosestPackage, extractRequires,
getTransformCacheKey, getClosestPackage,
globalTransformCache, getTransformCacheKey,
moduleOptions, globalTransformCache,
reporter, moduleOptions,
transformCode, reporter,
}: { transformCode,
assetDependencies: Array<string>, }: {
depGraphHelpers: DependencyGraphHelpers, assetDependencies: Array<string>,
getClosestPackage: GetClosestPackageFn, depGraphHelpers: DependencyGraphHelpers,
getTransformCacheKey: GetTransformCacheKey, getClosestPackage: GetClosestPackageFn,
globalTransformCache: ?GlobalTransformCache, getTransformCacheKey: GetTransformCacheKey,
moduleOptions: ModuleOptions, globalTransformCache: ?GlobalTransformCache,
reporter: Reporter, moduleOptions: ModuleOptions,
transformCode: TransformCode, reporter: Reporter,
}, platforms: Set<string>) { transformCode: TransformCode,
},
platforms: Set<string>,
) {
this._assetDependencies = assetDependencies; this._assetDependencies = assetDependencies;
this._getClosestPackage = getClosestPackage; this._getClosestPackage = getClosestPackage;
this._getTransformCacheKey = getTransformCacheKey; this._getTransformCacheKey = getTransformCacheKey;
@ -98,11 +102,14 @@ class ModuleCache {
/* $FlowFixMe: missing options. This is because this is an incorrect OOP /* $FlowFixMe: missing options. This is because this is an incorrect OOP
* design in the first place: AssetModule, being simpler than a normal * design in the first place: AssetModule, being simpler than a normal
* Module, should not inherit the Module class. */ * Module, should not inherit the Module class. */
this._moduleCache[filePath] = new AssetModule({ this._moduleCache[filePath] = new AssetModule(
file: filePath, {
moduleCache: this, file: filePath,
dependencies: this._assetDependencies, moduleCache: this,
}, this._platforms); dependencies: this._assetDependencies,
},
this._platforms,
);
} }
return this._moduleCache[filePath]; return this._moduleCache[filePath];
} }

View File

@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory. * of patent rights can be found in the PATENTS file in the same directory.
* *
* @flow * @flow
* @format
*/ */
'use strict'; 'use strict';
@ -23,16 +24,13 @@ type PackageContent = {
}; };
class Package { class Package {
path: string; path: string;
root: string; root: string;
type: string; type: string;
_content: ?PackageContent; _content: ?PackageContent;
constructor({file}: { constructor({file}: {file: string}) {
file: string,
}) {
this.path = path.resolve(file); this.path = path.resolve(file);
this.root = path.dirname(this.path); this.root = path.dirname(this.path);
this.type = 'Package'; this.type = 'Package';
@ -49,7 +47,8 @@ class Package {
let main = json.main || 'index'; let main = json.main || 'index';
if (replacements && typeof replacements === 'object') { if (replacements && typeof replacements === 'object') {
main = replacements[main] || main =
replacements[main] ||
replacements[main + '.js'] || replacements[main + '.js'] ||
replacements[main + '.json'] || replacements[main + '.json'] ||
replacements[main.replace(/(\.js|\.json)$/, '')] || replacements[main.replace(/(\.js|\.json)$/, '')] ||
@ -85,8 +84,8 @@ class Package {
// support exclude with "someDependency": false // support exclude with "someDependency": false
return replacement === false return replacement === false
? false ? false
/* $FlowFixMe: type of replacements is not being validated */ : /* $FlowFixMe: type of replacements is not being validated */
: replacement || name; replacement || name;
} }
let relPath = './' + path.relative(this.root, name); let relPath = './' + path.relative(this.root, name);
@ -113,7 +112,7 @@ class Package {
return path.join( return path.join(
this.root, this.root,
/* $FlowFixMe: `getReplacements` doesn't validate the return value. */ /* $FlowFixMe: `getReplacements` doesn't validate the return value. */
redirect redirect,
); );
} }

View File

@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory. * of patent rights can be found in the PATENTS file in the same directory.
* *
* @flow * @flow
* @format
*/ */
'use strict'; 'use strict';
@ -16,14 +17,15 @@ const Module = require('./Module');
import type {ConstructorArgs} from './Module'; import type {ConstructorArgs} from './Module';
class Polyfill extends Module { class Polyfill extends Module {
_id: string; _id: string;
_dependencies: Array<string>; _dependencies: Array<string>;
constructor(options: ConstructorArgs & { constructor(
id: string, options: ConstructorArgs & {
dependencies: Array<string>, id: string,
}) { dependencies: Array<string>,
},
) {
super(options); super(options);
this._id = options.id; this._id = options.id;
this._dependencies = options.dependencies; this._dependencies = options.dependencies;

View File

@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory. * of patent rights can be found in the PATENTS file in the same directory.
* *
* @flow * @flow
* @format
*/ */
'use strict'; 'use strict';