Bringing clarity to the Mapping types

Reviewed By: rafeca

Differential Revision: D6702118

fbshipit-source-id: 0fc99454713042117ce41ddfe1d3f84c95265d74
This commit is contained in:
Peter van der Zee 2018-01-11 04:27:23 -08:00 committed by Facebook Github Bot
parent 70858589ff
commit 3ee6e5bfb5
16 changed files with 56 additions and 56 deletions

View File

@ -10,7 +10,7 @@
* @format
*/
type _SourceMap = {
type _BabelSourceMap = {
file?: string,
mappings: string,
names: Array<string>,
@ -108,19 +108,19 @@ type TransformResult = {
ast: Ast,
code: ?string,
ignored: boolean,
map: ?_SourceMap,
map: ?_BabelSourceMap,
};
// https://github.com/babel/babel/blob/master/packages/babel-generator/src/buffer.js#L42
type GeneratorResult = {
code: string,
map: ?_SourceMap,
map: ?_BabelSourceMap,
rawMappings: ?Array<_RawMapping>,
};
type VisitFn = <State>(path: Object, state: State) => any;
declare module 'babel-core' {
declare type Plugins = _Plugins;
declare type SourceMap = _SourceMap;
declare type BabelSourceMap = _BabelSourceMap;
declare type Ast = {};
declare type TransformOptions = _TransformOptions;
declare function transform(

View File

@ -14,7 +14,7 @@
const B64Builder = require('./B64Builder');
import type {SourceMap as MappingsMap} from 'babel-core';
import type {BabelSourceMap} from 'babel-core';
/**
* Generates a source map from raw mappings.
@ -149,7 +149,7 @@ class Generator {
/**
* Return the source map as object.
*/
toMap(file?: string, options: {excludeSource?: boolean}): MappingsMap {
toMap(file?: string, options: {excludeSource?: boolean}): BabelSourceMap {
let content;
if (options && options.excludeSource) {

View File

@ -15,12 +15,11 @@
const Generator = require('./Generator');
const SourceMap = require('source-map');
import type {SourceMap as MappingsMap} from 'babel-core';
import type {BabelSourceMap} from 'babel-core';
import type {RawMapping as BabelRawMapping} from 'babel-generator';
import type {RawMapping as CompactRawMapping} from 'source-map';
import type {RawMapping as UnknownSourceMapMappingType} from 'source-map';
export type {SourceMap as MappingsMap} from 'babel-core';
export type CompactRawMappings = Array<CompactRawMapping>;
export type UnknownSourceMapMappingTypes = Array<UnknownSourceMapMappingType>;
export type RawMappings = Array<BabelRawMapping>;
type GeneratedCodeMapping = [number, number];
@ -50,8 +49,8 @@ export type IndexMap = {
};
export type FBIndexMap = IndexMap & FBExtensions;
export type MetroSourceMap = IndexMap | MappingsMap;
export type FBSourceMap = FBIndexMap | (MappingsMap & FBExtensions);
export type MetroSourceMap = IndexMap | BabelSourceMap;
export type FBSourceMap = FBIndexMap | (BabelSourceMap & FBExtensions);
/**
* Creates a source map from modules with "raw mappings", i.e. an array of
@ -91,7 +90,7 @@ function fromRawMappings(
* Transforms a standard source map object into a Raw Mappings object, to be
* used across the bundler.
*/
function toRawMappings(sourceMap: MappingsMap): RawMappings {
function toRawMappings(sourceMap: BabelSourceMap): RawMappings {
const rawMappings = [];
new SourceMap.SourceMapConsumer(sourceMap).eachMapping(map => {

View File

@ -32,9 +32,9 @@ import type {GlobalTransformCache} from '../lib/GlobalTransformCache';
import type {TransformCache} from '../lib/TransformCaching';
import type {Reporter} from '../lib/reporting';
import type {HasteImpl} from '../node-haste/Module';
import type {BabelSourceMap} from 'babel-core';
import type {
CompactRawMappings,
MappingsMap,
UnknownSourceMapMappingTypes,
MetroSourceMap as SourceMap,
} from 'metro-source-map';
@ -68,8 +68,8 @@ export type GetTransformOptions = (
export type PostMinifyProcess = ({
code: string,
map: ?MappingsMap,
}) => {code: string, map: ?MappingsMap};
map: ?BabelSourceMap,
}) => {code: string, map: ?BabelSourceMap};
export type PostProcessBundleSourcemap = ({
code: Buffer | string,
@ -255,8 +255,8 @@ class Bundler {
async minifyModule(
path: string,
code: string,
map: CompactRawMappings,
): Promise<{code: string, map: CompactRawMappings}> {
map: UnknownSourceMapMappingTypes,
): Promise<{code: string, map: UnknownSourceMapMappingTypes}> {
const sourceMap = fromRawMappings([{code, source: code, map, path}]).toMap(
undefined,
{},

View File

@ -29,7 +29,7 @@ import type DependencyGraph from '../node-haste/DependencyGraph';
import type Module from '../node-haste/Module';
import type {Options as BundleOptions, MainOptions} from './';
import type {DependencyEdges} from './traverseDependencies';
import type {CompactRawMappings} from 'metro-source-map';
import type {UnknownSourceMapMappingTypes} from 'metro-source-map';
export type DeltaEntryType =
| 'asset'
@ -41,7 +41,7 @@ export type DeltaEntryType =
export type DeltaEntry = {|
+code: string,
+id: number,
+map: CompactRawMappings,
+map: UnknownSourceMapMappingTypes,
+name: string,
+path: string,
+source: string,
@ -515,7 +515,7 @@ class DeltaTransformer extends EventEmitter {
): Promise<{
+code: string,
+dependencies: Array<string>,
+map: CompactRawMappings,
+map: UnknownSourceMapMappingTypes,
+source: string,
}> {
return await module.read(

View File

@ -28,7 +28,7 @@ import type DeltaTransformer, {
DeltaEntry,
DeltaTransformResponse,
} from './DeltaTransformer';
import type {MappingsMap} from 'metro-source-map';
import type {BabelSourceMap} from 'babel-core';
export type Options = BundleOptions & {
deltaBundleId: ?string,
@ -86,7 +86,7 @@ async function fullSourceMap(
async function fullSourceMapObject(
deltaBundler: DeltaBundler,
options: Options,
): Promise<MappingsMap> {
): Promise<BabelSourceMap> {
const {modules} = await _getAllModules(deltaBundler, options);
return fromRawMappings(modules).toMap(undefined, {

View File

@ -17,9 +17,9 @@ const {Logger} = require('metro-core');
const debug = require('debug')('Metro:JStransformer');
const Worker = require('jest-worker').default;
import type {BabelSourceMap} from 'babel-core';
import type {Options, TransformedCode} from './worker';
import type {LocalPath} from '../node-haste/lib/toLocalPath';
import type {MappingsMap} from 'metro-source-map';
import type {ResultWithMap} from './worker/minify';
import typeof {minify as Minify, transform as Transform} from './worker';
@ -74,7 +74,7 @@ module.exports = class Transformer {
async minify(
filename: string,
code: string,
sourceMap: MappingsMap,
sourceMap: BabelSourceMap,
): Promise<ResultWithMap> {
return await this._worker.minify(filename, code, sourceMap);
}

View File

@ -14,7 +14,8 @@
const babel = require('babel-core');
import type {Ast, SourceMap as MappingsMap} from 'babel-core';
import type {Ast, BabelSourceMap} from 'babel-core';
const t = babel.types;
const Conditional = {
@ -76,7 +77,7 @@ function constantFolding(
transformResult: {
ast: Ast,
code?: ?string,
map: ?MappingsMap,
map: ?BabelSourceMap,
},
) {
return babel.transformFromAst(transformResult.ast, transformResult.code, {

View File

@ -27,7 +27,8 @@ const path = require('path');
const {compactMapping} = require('metro-source-map');
import type {LogEntry} from 'metro-core/src/Logger';
import type {CompactRawMappings, MappingsMap} from 'metro-source-map';
import type {BabelSourceMap} from 'babel-core';
import type {UnknownSourceMapMappingTypes} from 'metro-source-map';
import type {LocalPath} from '../../node-haste/lib/toLocalPath';
import type {ResultWithMap} from './minify';
import type {Ast, Plugins as BabelPlugins} from 'babel-core';
@ -35,7 +36,7 @@ import type {Ast, Plugins as BabelPlugins} from 'babel-core';
export type TransformedCode = {
code: string,
dependencies: $ReadOnlyArray<string>,
map: CompactRawMappings,
map: UnknownSourceMapMappingTypes,
};
export type TransformArgs<ExtraOptions: {}> = {|
@ -219,7 +220,7 @@ function transformCode(
function minifyCode(
filename: string,
code: string,
sourceMap: MappingsMap,
sourceMap: BabelSourceMap,
): ResultWithMap | Promise<ResultWithMap> {
try {
return minify.withSourceMap(code, sourceMap, filename);

View File

@ -16,7 +16,8 @@ const babel = require('babel-core');
const inlinePlatform = require('./inline-platform');
const invariant = require('fbjs/lib/invariant');
import type {Ast, SourceMap as MappingsMap} from 'babel-core';
import type {Ast} from 'babel-core';
import type {BabelSourceMap} from 'babel-core';
const t = babel.types;
const env = {name: 'env'};
@ -104,12 +105,12 @@ const plugin = () => inlinePlugin;
type AstResult = {
ast: Ast,
code: ?string,
map: ?MappingsMap,
map: ?BabelSourceMap,
};
function inline(
filename: string,
transformResult: {ast?: ?Ast, code: string, map: ?MappingsMap},
transformResult: {ast?: ?Ast, code: string, map: ?BabelSourceMap},
options: {+dev: boolean, +platform: ?string},
): AstResult {
const code = transformResult.code;

View File

@ -14,11 +14,11 @@
const uglify = require('uglify-es');
import type {MappingsMap} from 'metro-source-map';
import type {BabelSourceMap} from 'babel-core';
export type ResultWithMap = {
code: string,
map: MappingsMap,
map: BabelSourceMap,
};
function noSourceMap(code: string): string {
@ -27,17 +27,17 @@ function noSourceMap(code: string): string {
function withSourceMap(
code: string,
sourceMap: ?MappingsMap,
sourceMap: ?BabelSourceMap,
filename: string,
): ResultWithMap {
const result = minify(code, sourceMap);
const map: MappingsMap = JSON.parse(result.map);
const map: BabelSourceMap = JSON.parse(result.map);
map.sources = [filename];
return {code: result.code, map};
}
function minify(inputCode: string, inputMap: ?MappingsMap) {
function minify(inputCode: string, inputMap: ?BabelSourceMap) {
const result = uglify.minify(inputCode, {
mangle: {toplevel: true},
output: {

View File

@ -12,12 +12,9 @@
'use strict';
import type {Ast} from 'babel-core';
import type {BabelSourceMap} from 'babel-core';
import type {Console} from 'console';
import type {
FBSourceMap,
MappingsMap,
MetroSourceMap as SourceMap,
} from 'metro-source-map';
import type {FBSourceMap, MetroSourceMap as SourceMap} from 'metro-source-map';
export type {Transformer} from '../JSTransformer/worker';
@ -37,7 +34,7 @@ type Dependency = {|
export type File = {|
code: string,
map: ?MappingsMap,
map: ?BabelSourceMap,
path: string,
type: CodeFileTypes,
|};
@ -144,7 +141,7 @@ type ResolveOptions = {
export type TransformerResult = {|
ast: ?Ast,
code: string,
map: ?MappingsMap,
map: ?BabelSourceMap,
|};
export type TransformResultDependency = {|
@ -164,7 +161,7 @@ export type TransformResult = {|
code: string,
dependencies: $ReadOnlyArray<TransformResultDependency>,
dependencyMapName?: string,
map: ?MappingsMap,
map: ?BabelSourceMap,
|};
export type TransformResults = {[string]: TransformResult};

View File

@ -23,7 +23,8 @@ const optimizeDependencies = require('./optimizeDependencies');
const sourceMap = require('source-map');
import type {TransformedSourceFile, TransformResult} from '../types.flow';
import type {MappingsMap, MetroSourceMap as SourceMap} from 'metro-source-map';
import type {BabelSourceMap} from 'babel-core';
import type {MetroSourceMap as SourceMap} from 'metro-source-map';
import type {PostMinifyProcess} from '../../Bundler/index.js';
export type OptimizationOptions = {|
@ -112,7 +113,7 @@ function mergeSourceMaps(
file: string,
originalMap: SourceMap,
secondMap: SourceMap,
): MappingsMap {
): BabelSourceMap {
const merged = new sourceMap.SourceMapGenerator();
const inputMap = new sourceMap.SourceMapConsumer(originalMap);
new sourceMap.SourceMapConsumer(secondMap).eachMapping(mapping => {

View File

@ -24,7 +24,7 @@ const writeFileAtomicSync = require('write-file-atomic').sync;
import type {Options as WorkerOptions} from '../JSTransformer/worker';
import type {Reporter} from './reporting';
import type {LocalPath} from '../node-haste/lib/toLocalPath';
import type {CompactRawMappings} from 'metro-source-map';
import type {UnknownSourceMapMappingTypes} from 'metro-source-map';
type CacheFilePaths = {transformedCode: string, metadata: string};
export type GetTransformCacheKey = (options: {}) => string;
@ -34,7 +34,7 @@ const CACHE_SUB_DIR = 'cache';
export type CachedResult = {
code: string,
dependencies: $ReadOnlyArray<string>,
map: CompactRawMappings,
map: UnknownSourceMapMappingTypes,
};
export type TransformCacheResult = ?CachedResult;
@ -324,7 +324,7 @@ function readMetadataFileSync(
cachedResultHash: string,
cachedSourceHash: string,
dependencies: Array<string>,
sourceMap: CompactRawMappings,
sourceMap: UnknownSourceMapMappingTypes,
} {
const metadataStr = fs.readFileSync(metadataFilePath, 'utf8');
const metadata = tryParseJSON(metadataStr);

View File

@ -35,12 +35,12 @@ import type {Reporter} from '../lib/reporting';
import type DependencyGraphHelpers from './DependencyGraph/DependencyGraphHelpers';
import type ModuleCache from './ModuleCache';
import type {LocalPath} from './lib/toLocalPath';
import type {CompactRawMappings} from 'metro-source-map';
import type {UnknownSourceMapMappingTypes} from 'metro-source-map';
export type ReadResult = {
+code: string,
+dependencies: $ReadOnlyArray<string>,
+map: CompactRawMappings,
+map: UnknownSourceMapMappingTypes,
+source: string,
};

View File

@ -15,10 +15,10 @@ const invariant = require('fbjs/lib/invariant');
import type {RamModule} from '../../../DeltaBundler/Serializers';
import type {ModuleGroups, ModuleTransportLike} from '../../types.flow';
import type {BabelSourceMap} from 'babel-core';
import type {
FBIndexMap,
IndexMap,
MappingsMap,
MetroSourceMap as SourceMap,
} from 'metro-source-map';
@ -29,7 +29,7 @@ const countLines = (string: string) => (string.match(newline) || []).length + 1;
function lineToLineSourceMap(
source: string,
filename: string = '',
): MappingsMap {
): BabelSourceMap {
// The first line mapping in our package is the base64vlq code for zeros (A).
const firstLine = 'AAAA;';