Skip to content

Commit

Permalink
Improve flow coverage in Metro
Browse files Browse the repository at this point in the history
Reviewed By: mjesun

Differential Revision: D14258819

fbshipit-source-id: 72cd19789d1c651238334a016581217ad7278999
  • Loading branch information
Peter van der Zee authored and facebook-github-bot committed Mar 1, 2019
1 parent f8764f4 commit 46a28ea
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 20 deletions.
7 changes: 5 additions & 2 deletions packages/metro/src/Bundler/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ function isAssetTypeAnImage(type: string): boolean {
);
}

function filterObject(object, blacklist) {
function filterObject(object: AssetDataWithoutFiles, blacklist: Set<string>) {
const copied = Object.assign({}, object);
for (const key of blacklist) {
delete copied[key];
Expand Down Expand Up @@ -209,7 +209,10 @@ function createRamBundleGroups<T: ModuleTransportLike>(
return result;
}

function* filter(iterator, predicate) {
function* filter(
iterator: ArrayMap<number, number>,
predicate: ([number, Array<number>]) => boolean,
) {
for (const value of iterator) {
if (predicate(value)) {
yield value;
Expand Down
2 changes: 1 addition & 1 deletion packages/metro/src/DeltaBundler/Transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class Transformer {
}
}

function verifyRootExists(root) {
function verifyRootExists(root: string) {
// Verify that the root exists.
assert(fs.statSync(root).isDirectory(), 'Root has to be a valid directory');
}
Expand Down
4 changes: 2 additions & 2 deletions packages/metro/src/DeltaBundler/WorkerFarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class WorkerFarm {
return null;
}

_formatGenericError(err, filename) {
_formatGenericError(err, filename: string) {
const error = new TransformError(`${filename}: ${err.message}`);

return Object.assign(error, {
Expand All @@ -155,7 +155,7 @@ class WorkerFarm {
});
}

_formatBabelError(err, filename) {
_formatBabelError(err, filename: string) {
const error = new TransformError(
`${err.type || 'Error'}${
err.message.includes(filename) ? '' : ' in ' + filename
Expand Down
4 changes: 2 additions & 2 deletions packages/metro/src/lib/getPreludeCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function getPreludeCode({

const excluded = new Set(['__BUNDLE_START_TIME__', '__DEV__', 'process']);

function formatExtraVars(extraVars) {
function formatExtraVars(extraVars: ?{[string]: mixed}) {
const assignments = [];

for (const key in extraVars) {
Expand All @@ -42,7 +42,7 @@ function formatExtraVars(extraVars) {
return assignments;
}

function processEnv(nodeEnv) {
function processEnv(nodeEnv: string) {
return `process.env=process.env||{};process.env.NODE_ENV=process.env.NODE_ENV||${JSON.stringify(
nodeEnv,
)};`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class ResolutionRequest<TModule: Moduleish, TPackage: Packageish> {
}
}

function getResolutionCacheKey(modulePath, depName) {
function getResolutionCacheKey(modulePath: string, depName: string) {
return `${path.resolve(modulePath)}:${depName}`;
}

Expand Down
35 changes: 27 additions & 8 deletions packages/metro/src/shared/output/RamBundle/as-indexed-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,35 @@ function writeBuffers(stream, buffers: Array<Buffer>) {
});
}

function nullTerminatedBuffer(contents, encoding) {
function nullTerminatedBuffer(
contents: string,
encoding: void | 'ascii' | 'utf16le' | 'utf8',
) {
return Buffer.concat([Buffer.from(contents, encoding), nullByteBuffer]);
}

function moduleToBuffer(id, code, encoding) {
function moduleToBuffer(
id: number,
code: string,
encoding: void | 'ascii' | 'utf16le' | 'utf8',
) {
return {
id,
buffer: nullTerminatedBuffer(code, encoding),
};
}

function entryOffset(n) {
function entryOffset(n: number) {
// 2: num_entries + startup_code_len
// n * 2: each entry consists of two uint32s
return (2 + n * 2) * SIZEOF_UINT32;
}

function buildModuleTable(startupCode, moduleBuffers, moduleGroups) {
function buildModuleTable(
startupCode: Buffer,
moduleBuffers: Array<{buffer: Buffer, id: number}>,
moduleGroups: ModuleGroups,
) {
// table format:
// - num_entries: uint_32 number of entries
// - startup_code_len: uint_32 length of the startup section
Expand All @@ -124,7 +135,7 @@ function buildModuleTable(startupCode, moduleBuffers, moduleGroups) {
// - module_offset: uint_32 offset into the modules blob
// - module_length: uint_32 length of the module code in bytes

const moduleIds = Array.from(moduleGroups.modulesById.keys());
const moduleIds = [...moduleGroups.modulesById.keys()];
const maxId = moduleIds.reduce((max, id) => Math.max(max, id));
const numEntries = maxId + 1;
const table: Buffer = Buffer.alloc(entryOffset(numEntries)).fill(0);
Expand Down Expand Up @@ -154,7 +165,11 @@ function buildModuleTable(startupCode, moduleBuffers, moduleGroups) {
return table;
}

function groupCode(rootCode, moduleGroup, modulesById) {
function groupCode(
rootCode: string,
moduleGroup: void | Set<number>,
modulesById: Map<number, ModuleTransportLike>,
) {
if (!moduleGroup || !moduleGroup.size) {
return rootCode;
}
Expand All @@ -166,7 +181,11 @@ function groupCode(rootCode, moduleGroup, modulesById) {
return code.join('\n');
}

function buildModuleBuffers(modules, moduleGroups, encoding) {
function buildModuleBuffers(
modules: $ReadOnlyArray<ModuleTransportLike>,
moduleGroups: ModuleGroups,
encoding: void | 'ascii' | 'utf16le' | 'utf8',
): Array<{buffer: Buffer, id: number}> {
return modules
.filter(m => !moduleGroups.modulesInGroups.has(m.id))
.map(({id, code}) =>
Expand Down Expand Up @@ -214,7 +233,7 @@ function createModuleGroups(
};
}

function* concat(iterators) {
function* concat(iterators: Iterator<Set<number>>) {
for (const it of iterators) {
yield* it;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/metro/src/shared/output/RamBundle/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ function combineSourceMapsAddingOffsets(
function combineMaps(
modules: $ReadOnlyArray<ModuleTransportLike>,
offsets: ?Array<number>,
moduleGroups,
options,
moduleGroups: ?ModuleGroups,
options: ?CombineOptions,
) {
const sections = [];

Expand Down
10 changes: 8 additions & 2 deletions packages/metro/src/shared/output/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ module.exports = function(
return signature;
};

function tryAsciiPromotion(buffer, encoding) {
function tryAsciiPromotion(
buffer: Buffer,
encoding: 'ascii' | 'utf16le' | 'utf8',
) {
if (!isUTF8(encoding)) {
return encoding;
}
Expand All @@ -53,7 +56,10 @@ function tryAsciiPromotion(buffer, encoding) {
return 'ascii';
}

function asBuffer(x, encoding): Buffer {
function asBuffer(
x: Buffer | string,
encoding: 'ascii' | 'utf16le' | 'utf8',
): Buffer {
if (typeof x !== 'string') {
return x;
}
Expand Down

0 comments on commit 46a28ea

Please sign in to comment.