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.
*
* @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<string>;
constructor(args: ConstructorArgs & {dependencies: Array<string>}, platforms: Set<string>) {
constructor(
args: ConstructorArgs & {dependencies: Array<string>},
platforms: Set<string>,
) {
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() {

View File

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

View File

@ -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<TransformedCode>;
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);

View File

@ -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<string>;
_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<string>,
depGraphHelpers: DependencyGraphHelpers,
getClosestPackage: GetClosestPackageFn,
getTransformCacheKey: GetTransformCacheKey,
globalTransformCache: ?GlobalTransformCache,
moduleOptions: ModuleOptions,
reporter: Reporter,
transformCode: TransformCode,
}, platforms: Set<string>) {
constructor(
{
assetDependencies,
depGraphHelpers,
extractRequires,
getClosestPackage,
getTransformCacheKey,
globalTransformCache,
moduleOptions,
reporter,
transformCode,
}: {
assetDependencies: Array<string>,
depGraphHelpers: DependencyGraphHelpers,
getClosestPackage: GetClosestPackageFn,
getTransformCacheKey: GetTransformCacheKey,
globalTransformCache: ?GlobalTransformCache,
moduleOptions: ModuleOptions,
reporter: Reporter,
transformCode: TransformCode,
},
platforms: Set<string>,
) {
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];
}

View File

@ -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,
);
}

View File

@ -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<string>;
constructor(options: ConstructorArgs & {
id: string,
dependencies: Array<string>,
}) {
constructor(
options: ConstructorArgs & {
id: string,
dependencies: Array<string>,
},
) {
super(options);
this._id = options.id;
this._dependencies = options.dependencies;

View File

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