Add @format to a few files

Reviewed By: davidaurelio

Differential Revision: D5111297

fbshipit-source-id: bde11df412dd694edca78d6a61f4c69e5abba60a
This commit is contained in:
Christoph Pojer 2017-05-23 04:52:46 -07:00 committed by Facebook Github Bot
parent 0eccb5ebf4
commit f8a724121b
5 changed files with 3343 additions and 3011 deletions

View File

@ -5,6 +5,8 @@
* This source code is licensed under the BSD-style license found in the * This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant * LICENSE file in the root directory of this source tree. An additional grant
* 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.
*
* @format
*/ */
'use strict'; 'use strict';
@ -26,117 +28,128 @@ describe('Bundle', () => {
describe('source bundle', () => { describe('source bundle', () => {
it('should create a bundle and get the source', () => { it('should create a bundle and get the source', () => {
return Promise.resolve().then(() => { return Promise.resolve()
return addModule({ .then(() => {
bundle, return addModule({
code: 'transformed foo;', bundle,
sourceCode: 'source foo', code: 'transformed foo;',
sourcePath: 'foo path', sourceCode: 'source foo',
sourcePath: 'foo path',
});
})
.then(() => {
return addModule({
bundle,
code: 'transformed bar;',
sourceCode: 'source bar',
sourcePath: 'bar path',
});
})
.then(() => {
bundle.finalize({});
expect(bundle.getSource({dev: true})).toBe(
[
'transformed foo;',
'transformed bar;',
'\/\/# sourceMappingURL=test_url',
].join('\n'),
);
}); });
}).then(() => {
return addModule({
bundle,
code: 'transformed bar;',
sourceCode: 'source bar',
sourcePath: 'bar path',
});
}).then(() => {
bundle.finalize({});
expect(bundle.getSource({dev: true})).toBe([
'transformed foo;',
'transformed bar;',
'\/\/# sourceMappingURL=test_url',
].join('\n'));
});
}); });
it('should be ok to leave out the source map url', () => { it('should be ok to leave out the source map url', () => {
const otherBundle = new Bundle(); const otherBundle = new Bundle();
return Promise.resolve().then(() => { return Promise.resolve()
return addModule({ .then(() => {
bundle: otherBundle, return addModule({
code: 'transformed foo;', bundle: otherBundle,
sourceCode: 'source foo', code: 'transformed foo;',
sourcePath: 'foo path', sourceCode: 'source foo',
sourcePath: 'foo path',
});
})
.then(() => {
return addModule({
bundle: otherBundle,
code: 'transformed bar;',
sourceCode: 'source bar',
sourcePath: 'bar path',
});
})
.then(() => {
otherBundle.finalize({});
expect(otherBundle.getSource({dev: true})).toBe(
['transformed foo;', 'transformed bar;'].join('\n'),
);
}); });
}).then(() => {
return addModule({
bundle: otherBundle,
code: 'transformed bar;',
sourceCode: 'source bar',
sourcePath: 'bar path',
});
}).then(() => {
otherBundle.finalize({});
expect(otherBundle.getSource({dev: true})).toBe([
'transformed foo;',
'transformed bar;',
].join('\n'));
});
}); });
it('should create a bundle and add run module code', () => { it('should create a bundle and add run module code', () => {
return Promise.resolve().then(() => { return Promise.resolve()
return addModule({ .then(() => {
bundle, return addModule({
code: 'transformed foo;', bundle,
sourceCode: 'source foo', code: 'transformed foo;',
sourcePath: 'foo path', sourceCode: 'source foo',
sourcePath: 'foo path',
});
})
.then(() => {
return addModule({
bundle,
code: 'transformed bar;',
sourceCode: 'source bar',
sourcePath: 'bar path',
});
})
.then(() => {
bundle.setMainModuleId('foo');
bundle.finalize({
runBeforeMainModule: ['bar'],
runModule: true,
});
expect(bundle.getSource({dev: true})).toBe(
[
'transformed foo;',
'transformed bar;',
';require("bar");',
';require("foo");',
'\/\/# sourceMappingURL=test_url',
].join('\n'),
);
}); });
}).then(() => {
return addModule({
bundle,
code: 'transformed bar;',
sourceCode: 'source bar',
sourcePath: 'bar path',
});
}).then(() => {
bundle.setMainModuleId('foo');
bundle.finalize({
runBeforeMainModule: ['bar'],
runModule: true,
});
expect(bundle.getSource({dev: true})).toBe([
'transformed foo;',
'transformed bar;',
';require("bar");',
';require("foo");',
'\/\/# sourceMappingURL=test_url',
].join('\n'));
});
}); });
it('inserts modules in a deterministic order, independent of timing of the wrapper process', it('inserts modules in a deterministic order, independent of timing of the wrapper process', () => {
() => { const moduleTransports = [
const moduleTransports = [ createModuleTransport({name: 'module1'}),
createModuleTransport({name: 'module1'}), createModuleTransport({name: 'module2'}),
createModuleTransport({name: 'module2'}), createModuleTransport({name: 'module3'}),
createModuleTransport({name: 'module3'}), ];
];
const resolves = {}; const resolves = {};
const resolver = { const resolver = {
wrapModule({name}) { wrapModule({name}) {
return new Promise(resolve => { return new Promise(resolve => {
resolves[name] = resolve; resolves[name] = resolve;
}); });
}, },
}; };
const promise = Promise.all(moduleTransports.map( const promise = Promise.all(
m => bundle.addModule(resolver, null, {isPolyfill: () => false}, m) moduleTransports.map(m =>
)).then(() => { bundle.addModule(resolver, null, {isPolyfill: () => false}, m),
expect(bundle.getModules()) ),
.toEqual(moduleTransports); ).then(() => {
}); expect(bundle.getModules()).toEqual(moduleTransports);
});
resolves.module2({code: ''}); resolves.module2({code: ''});
resolves.module3({code: ''}); resolves.module3({code: ''});
resolves.module1({code: ''}); resolves.module1({code: ''});
return promise; return promise;
}, });
);
}); });
describe('sourcemap bundle', () => { describe('sourcemap bundle', () => {
@ -147,89 +160,93 @@ describe('Bundle', () => {
it('should combine sourcemaps', () => { it('should combine sourcemaps', () => {
const otherBundle = new Bundle({sourceMapUrl: 'test_url'}); const otherBundle = new Bundle({sourceMapUrl: 'test_url'});
return Promise.resolve().then(() => { return Promise.resolve()
return addModule({ .then(() => {
bundle: otherBundle, return addModule({
code: 'transformed foo;\n', bundle: otherBundle,
sourceCode: 'source foo', code: 'transformed foo;\n',
map: {name: 'sourcemap foo'}, sourceCode: 'source foo',
sourcePath: 'foo path', map: {name: 'sourcemap foo'},
}); sourcePath: 'foo path',
}).then(() => { });
return addModule({ })
bundle: otherBundle, .then(() => {
code: 'transformed bar;\n', return addModule({
sourceCode: 'source bar', bundle: otherBundle,
map: {name: 'sourcemap bar'}, code: 'transformed bar;\n',
sourcePath: 'bar path', sourceCode: 'source bar',
}); map: {name: 'sourcemap bar'},
}).then(() => { sourcePath: 'bar path',
return addModule({ });
bundle: otherBundle, })
code: 'image module;\nimage module;', .then(() => {
virtual: true, return addModule({
sourceCode: 'image module;\nimage module;', bundle: otherBundle,
sourcePath: 'image.png', code: 'image module;\nimage module;',
}); virtual: true,
}).then(() => { sourceCode: 'image module;\nimage module;',
otherBundle.setMainModuleId('foo'); sourcePath: 'image.png',
otherBundle.finalize({ });
runBeforeMainModule: ['InitializeCore'], })
runModule: true, .then(() => {
}); otherBundle.setMainModuleId('foo');
otherBundle.finalize({
runBeforeMainModule: ['InitializeCore'],
runModule: true,
});
const sourceMap = otherBundle.getSourceMap({dev: true}); const sourceMap = otherBundle.getSourceMap({dev: true});
expect(sourceMap).toEqual({ expect(sourceMap).toEqual({
file: 'test_url', file: 'test_url',
version: 3, version: 3,
sections: [ sections: [
{offset: {line: 0, column: 0}, map: {name: 'sourcemap foo'}}, {offset: {line: 0, column: 0}, map: {name: 'sourcemap foo'}},
{offset: {line: 2, column: 0}, map: {name: 'sourcemap bar'}}, {offset: {line: 2, column: 0}, map: {name: 'sourcemap bar'}},
{ {
offset: { offset: {
column: 0, column: 0,
line: 4, line: 4,
},
map: {
file: 'image.png',
mappings: 'AAAA;AACA;',
names: [],
sources: ['image.png'],
sourcesContent: ['image module;\nimage module;'],
version: 3,
},
}, },
map: { {
file: 'image.png', offset: {
mappings: 'AAAA;AACA;', column: 0,
names: [], line: 6,
sources: ['image.png'], },
sourcesContent: ['image module;\nimage module;'], map: {
version: 3, file: 'require-InitializeCore.js',
mappings: 'AAAA;',
names: [],
sources: ['require-InitializeCore.js'],
sourcesContent: [';require("InitializeCore");'],
version: 3,
},
}, },
}, {
{ offset: {
offset: { column: 0,
column: 0, line: 7,
line: 6, },
map: {
file: 'require-foo.js',
mappings: 'AAAA;',
names: [],
sources: ['require-foo.js'],
sourcesContent: [';require("foo");'],
version: 3,
},
}, },
map: { ],
file: 'require-InitializeCore.js', });
mappings: 'AAAA;',
names: [],
sources: ['require-InitializeCore.js'],
sourcesContent: [';require("InitializeCore");'],
version: 3,
},
},
{
offset: {
column: 0,
line: 7,
},
map: {
file: 'require-foo.js',
mappings: 'AAAA;',
names: [],
sources: ['require-foo.js'],
sourcesContent: [';require("foo");'],
version: 3,
},
},
],
}); });
});
}); });
}); });
@ -248,31 +265,37 @@ describe('Bundle', () => {
describe('getJSModulePaths()', () => { describe('getJSModulePaths()', () => {
it('should return module paths', () => { it('should return module paths', () => {
var otherBundle = new Bundle({sourceMapUrl: 'test_url'}); var otherBundle = new Bundle({sourceMapUrl: 'test_url'});
return Promise.resolve().then(() => { return Promise.resolve()
return addModule({ .then(() => {
bundle: otherBundle, return addModule({
code: 'transformed foo;\n', bundle: otherBundle,
sourceCode: 'source foo', code: 'transformed foo;\n',
sourcePath: 'foo path', sourceCode: 'source foo',
sourcePath: 'foo path',
});
})
.then(() => {
return addModule({
bundle: otherBundle,
code: 'image module;\nimage module;',
virtual: true,
sourceCode: 'image module;\nimage module;',
sourcePath: 'image.png',
});
})
.then(() => {
expect(otherBundle.getJSModulePaths()).toEqual(['foo path']);
}); });
}).then(() => {
return addModule({
bundle: otherBundle,
code: 'image module;\nimage module;',
virtual: true,
sourceCode: 'image module;\nimage module;',
sourcePath: 'image.png',
});
}).then(() => {
expect(otherBundle.getJSModulePaths()).toEqual(['foo path']);
});
}); });
}); });
describe('getEtag()', function() { describe('getEtag()', function() {
it('should return an etag', function() { it('should return an etag', function() {
bundle.finalize({}); bundle.finalize({});
var eTag = crypto.createHash('md5').update(bundle.getSource()).digest('hex'); var eTag = crypto
.createHash('md5')
.update(bundle.getSource())
.digest('hex');
expect(bundle.getEtag()).toEqual(eTag); expect(bundle.getEtag()).toEqual(eTag);
}); });
}); });
@ -305,51 +328,86 @@ describe('Bundle', () => {
it('can create a single group', () => { it('can create a single group', () => {
bundle = createBundle([fsLocation('React')]); bundle = createBundle([fsLocation('React')]);
const {groups} = bundle.getUnbundle(); const {groups} = bundle.getUnbundle();
expect(groups).toEqual(new Map([ expect(groups).toEqual(
[idFor('React'), new Set(['ReactFoo', 'invariant', 'ReactBar', 'cx'].map(idFor))], new Map([
])); [
idFor('React'),
new Set(['ReactFoo', 'invariant', 'ReactBar', 'cx'].map(idFor)),
],
]),
);
}); });
it('can create two groups', () => { it('can create two groups', () => {
bundle = createBundle([fsLocation('ReactFoo'), fsLocation('ReactBar')]); bundle = createBundle([fsLocation('ReactFoo'), fsLocation('ReactBar')]);
const {groups} = bundle.getUnbundle(); const {groups} = bundle.getUnbundle();
expect(groups).toEqual(new Map([ expect(groups).toEqual(
[idFor('ReactFoo'), new Set([idFor('invariant')])], new Map([
[idFor('ReactBar'), new Set([idFor('cx')])], [idFor('ReactFoo'), new Set([idFor('invariant')])],
])); [idFor('ReactBar'), new Set([idFor('cx')])],
]),
);
}); });
it('can handle circular dependencies', () => { it('can handle circular dependencies', () => {
bundle = createBundle([fsLocation('OtherFramework')]); bundle = createBundle([fsLocation('OtherFramework')]);
const {groups} = bundle.getUnbundle(); const {groups} = bundle.getUnbundle();
expect(groups).toEqual(new Map([[ expect(groups).toEqual(
idFor('OtherFramework'), new Map([
new Set(['OtherFrameworkFoo', 'invariant', 'OtherFrameworkBar', 'crc32'].map(idFor)), [
]])); idFor('OtherFramework'),
new Set(
[
'OtherFrameworkFoo',
'invariant',
'OtherFrameworkBar',
'crc32',
].map(idFor),
),
],
]),
);
}); });
it('omits modules that are contained by more than one group', () => { it('omits modules that are contained by more than one group', () => {
bundle = createBundle([fsLocation('React'), fsLocation('OtherFramework')]); bundle = createBundle([
fsLocation('React'),
fsLocation('OtherFramework'),
]);
expect(() => { expect(() => {
const {groups} = bundle.getUnbundle(); //eslint-disable-line no-unused-vars const {groups} = bundle.getUnbundle(); //eslint-disable-line no-unused-vars
}).toThrow(new Error(`Module ${fsLocation('invariant')} belongs to groups ${fsLocation('React')}` + }).toThrow(
`, and ${fsLocation('OtherFramework')}. Removing it from all groups.`)); new Error(
`Module ${fsLocation('invariant')} belongs to groups ${fsLocation('React')}` +
`, and ${fsLocation('OtherFramework')}. Removing it from all groups.`,
),
);
}); });
it('ignores missing dependencies', () => { it('ignores missing dependencies', () => {
bundle = createBundle([fsLocation('Product1')]); bundle = createBundle([fsLocation('Product1')]);
const {groups} = bundle.getUnbundle(); const {groups} = bundle.getUnbundle();
expect(groups).toEqual(new Map([[ expect(groups).toEqual(
idFor('Product1'), new Map([
new Set(['React', 'ReactFoo', 'invariant', 'ReactBar', 'cx'].map(idFor)), [
]])); idFor('Product1'),
new Set(
['React', 'ReactFoo', 'invariant', 'ReactBar', 'cx'].map(idFor),
),
],
]),
);
}); });
it('throws for group roots that do not exist', () => { it('throws for group roots that do not exist', () => {
bundle = createBundle([fsLocation('DoesNotExist')]); bundle = createBundle([fsLocation('DoesNotExist')]);
expect(() => { expect(() => {
const {groups} = bundle.getUnbundle(); //eslint-disable-line no-unused-vars const {groups} = bundle.getUnbundle(); //eslint-disable-line no-unused-vars
}).toThrow(new Error(`Group root ${fsLocation('DoesNotExist')} is not part of the bundle`)); }).toThrow(
new Error(
`Group root ${fsLocation('DoesNotExist')} is not part of the bundle`,
),
);
}); });
function idFor(name) { function idFor(name) {
@ -397,7 +455,17 @@ function resolverFor(code, map) {
}; };
} }
function addModule({bundle, code, sourceCode, sourcePath, map, virtual, polyfill, meta, id = ''}) { function addModule({
bundle,
code,
sourceCode,
sourcePath,
map,
virtual,
polyfill,
meta,
id = '',
}) {
return bundle.addModule( return bundle.addModule(
resolverFor(code, map), resolverFor(code, map),
null, null,

View File

@ -5,6 +5,8 @@
* This source code is licensed under the BSD-style license found in the * This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant * LICENSE file in the root directory of this source tree. An additional grant
* 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.
*
* @format
*/ */
'use strict'; 'use strict';
@ -46,10 +48,12 @@ describe('transforming JS modules:', () => {
it('passes through file name and code', done => { it('passes through file name and code', done => {
transformModule(sourceCode, options(), (error, result) => { transformModule(sourceCode, options(), (error, result) => {
expect(result.type).toBe('code'); expect(result.type).toBe('code');
expect(result.details).toEqual(expect.objectContaining({ expect(result.details).toEqual(
code: sourceCode, expect.objectContaining({
file: filename, code: sourceCode,
})); file: filename,
}),
);
done(); done();
}); });
}); });
@ -73,11 +77,17 @@ describe('transforming JS modules:', () => {
}); });
it('sets `type` to `"script"` if the input is a polyfill', done => { it('sets `type` to `"script"` if the input is a polyfill', done => {
transformModule(sourceCode, {...options(), polyfill: true}, (error, result) => { transformModule(
expect(result.type).toBe('code'); sourceCode,
expect(result.details).toEqual(expect.objectContaining({type: 'script'})); {...options(), polyfill: true},
done(); (error, result) => {
}); expect(result.type).toBe('code');
expect(result.details).toEqual(
expect.objectContaining({type: 'script'}),
);
done();
},
);
}); });
const defaults = { const defaults = {
@ -89,24 +99,25 @@ describe('transforming JS modules:', () => {
projectRoot: '', projectRoot: '',
}; };
it('calls the passed-in transform function with code, file name, and options ' + it(
'for all passed in variants', 'calls the passed-in transform function with code, file name, and options ' +
'for all passed in variants',
done => { done => {
const variants = {dev: {dev: true}, prod: {dev: false}}; const variants = {dev: {dev: true}, prod: {dev: false}};
transformModule(sourceCode, options(variants), () => { transformModule(sourceCode, options(variants), () => {
expect(transformer.transform).toBeCalledWith({ expect(transformer.transform).toBeCalledWith({
filename, filename,
localPath: filename, localPath: filename,
options: {...defaults, ...variants.dev}, options: {...defaults, ...variants.dev},
src: sourceCode, src: sourceCode,
}); });
expect(transformer.transform).toBeCalledWith({ expect(transformer.transform).toBeCalledWith({
filename, filename,
localPath: filename, localPath: filename,
options: {...defaults, ...variants.prod}, options: {...defaults, ...variants.prod},
src: sourceCode, src: sourceCode,
}); });
done(); done();
}); });
}, },
@ -127,24 +138,27 @@ describe('transforming JS modules:', () => {
expect(error).toEqual(null); expect(error).toEqual(null);
const {code, dependencyMapName} = result.details.transformed.default; const {code, dependencyMapName} = result.details.transformed.default;
expect(code.replace(/\s+/g, '')) expect(code.replace(/\s+/g, '')).toEqual(
.toEqual( `__d(function(global,require,module,exports,${dependencyMapName}){${transformedCode}});`,
`__d(function(global,require,module,exports,${ );
dependencyMapName}){${transformedCode}});`
);
done(); done();
}); });
}); });
it('wraps the code produced by the transform function into an IIFE for polyfills', done => { it('wraps the code produced by the transform function into an IIFE for polyfills', done => {
transformModule(sourceCode, {...options(), polyfill: true}, (error, result) => { transformModule(
expect(error).toEqual(null); sourceCode,
{...options(), polyfill: true},
(error, result) => {
expect(error).toEqual(null);
const {code} = result.details.transformed.default; const {code} = result.details.transformed.default;
expect(code.replace(/\s+/g, '')) expect(code.replace(/\s+/g, '')).toEqual(
.toEqual(`(function(global){${transformedCode}})(this);`); `(function(global){${transformedCode}})(this);`,
done(); );
}); done();
},
);
}); });
it('creates source maps', done => { it('creates source maps', done => {
@ -152,8 +166,9 @@ describe('transforming JS modules:', () => {
const {code, map} = result.details.transformed.default; const {code, map} = result.details.transformed.default;
const column = code.indexOf('code'); const column = code.indexOf('code');
const consumer = new SourceMapConsumer(map); const consumer = new SourceMapConsumer(map);
expect(consumer.originalPositionFor({line: 1, column})) expect(consumer.originalPositionFor({line: 1, column})).toEqual(
.toEqual(expect.objectContaining({line: 1, column: sourceCode.indexOf('code')})); expect.objectContaining({line: 1, column: sourceCode.indexOf('code')}),
);
done(); done();
}); });
}); });
@ -166,8 +181,9 @@ describe('transforming JS modules:', () => {
transformer.transform.stub.returns(transformResult(body)); transformer.transform.stub.returns(transformResult(body));
transformModule(code, options(), (error, result) => { transformModule(code, options(), (error, result) => {
expect(result.details.transformed.default) expect(result.details.transformed.default).toEqual(
.toEqual(expect.objectContaining({dependencies: [dep1, dep2]})); expect.objectContaining({dependencies: [dep1, dep2]}),
);
done(); done();
}); });
}); });
@ -176,22 +192,18 @@ describe('transforming JS modules:', () => {
const variants = {dev: {dev: true}, prod: {dev: false}}; const variants = {dev: {dev: true}, prod: {dev: false}};
transformer.transform.stub transformer.transform.stub
.withArgs(filename, sourceCode, variants.dev) .withArgs(filename, sourceCode, variants.dev)
.returns(transformResult(bodyAst)) .returns(transformResult(bodyAst))
.withArgs(filename, sourceCode, variants.prod) .withArgs(filename, sourceCode, variants.prod)
.returns(transformResult([])); .returns(transformResult([]));
transformModule(sourceCode, options(variants), (error, result) => { transformModule(sourceCode, options(variants), (error, result) => {
const {dev, prod} = result.details.transformed; const {dev, prod} = result.details.transformed;
expect(dev.code.replace(/\s+/g, '')) expect(dev.code.replace(/\s+/g, '')).toEqual(
.toEqual( `__d(function(global,require,module,exports,${dev.dependencyMapName}){arbitrary(code);});`,
`__d(function(global,require,module,exports,${ );
dev.dependencyMapName}){arbitrary(code);});` expect(prod.code.replace(/\s+/g, '')).toEqual(
); `__d(function(global,require,module,exports,${prod.dependencyMapName}){arbitrary(code);});`,
expect(prod.code.replace(/\s+/g, '')) );
.toEqual(
`__d(function(global,require,module,exports,${
prod.dependencyMapName}){arbitrary(code);});`
);
done(); done();
}); });
}); });
@ -199,23 +211,31 @@ describe('transforming JS modules:', () => {
it('prefixes JSON files with `module.exports = `', done => { it('prefixes JSON files with `module.exports = `', done => {
const json = '{"foo":"bar"}'; const json = '{"foo":"bar"}';
transformModule(json, {...options(), filename: 'some.json'}, (error, result) => { transformModule(
const {code} = result.details.transformed.default; json,
expect(code.replace(/\s+/g, '')) {...options(), filename: 'some.json'},
.toEqual( (error, result) => {
const {code} = result.details.transformed.default;
expect(code.replace(/\s+/g, '')).toEqual(
'__d(function(global,require,module,exports){' + '__d(function(global,require,module,exports){' +
`module.exports=${json}});` `module.exports=${json}});`,
); );
done(); done();
}); },
);
}); });
it('does not create source maps for JSON files', done => { it('does not create source maps for JSON files', done => {
transformModule('{}', {...options(), filename: 'some.json'}, (error, result) => { transformModule(
expect(result.details.transformed.default) '{}',
.toEqual(expect.objectContaining({map: null})); {...options(), filename: 'some.json'},
done(); (error, result) => {
}); expect(result.details.transformed.default).toEqual(
expect.objectContaining({map: null}),
);
done();
},
);
}); });
it('adds package data for `package.json` files', done => { it('adds package data for `package.json` files', done => {

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';
@ -70,12 +71,13 @@ function transformModule(
const {filename, transformer, variants = defaultVariants} = options; const {filename, transformer, variants = defaultVariants} = options;
const tasks = {}; const tasks = {};
Object.keys(variants).forEach(name => { Object.keys(variants).forEach(name => {
tasks[name] = asyncify(() => transformer.transform({ tasks[name] = asyncify(() =>
transformer.transform({
filename, filename,
localPath: filename, localPath: filename,
options: {...defaultTransformOptions, ...variants[name]}, options: {...defaultTransformOptions, ...variants[name]},
src: code, src: code,
}) }),
); );
}); });
@ -89,7 +91,12 @@ function transformModule(
//$FlowIssue #14545724 //$FlowIssue #14545724
Object.entries(results).forEach(([key, value]: [*, TransformFnResult]) => { Object.entries(results).forEach(([key, value]: [*, TransformFnResult]) => {
transformed[key] = makeResult(value.ast, filename, code, options.polyfill); transformed[key] = makeResult(
value.ast,
filename,
code,
options.polyfill,
);
}); });
const annotations = docblock.parseAsObject(docblock.extract(code)); const annotations = docblock.parseAsObject(docblock.extract(code));
@ -112,10 +119,7 @@ function transformModule(
function transformJSON(json, options, callback) { function transformJSON(json, options, callback) {
const value = JSON.parse(json); const value = JSON.parse(json);
const {filename} = options; const {filename} = options;
const code = const code = `__d(function(${JsFileWrapping.MODULE_FACTORY_PARAMETERS.join(', ')}) { module.exports = \n${json}\n});`;
`__d(function(${JsFileWrapping.MODULE_FACTORY_PARAMETERS.join(', ')}) { module.exports = \n${
json
}\n});`;
const moduleData = { const moduleData = {
code, code,
@ -124,9 +128,9 @@ function transformJSON(json, options, callback) {
}; };
const transformed = {}; const transformed = {};
Object Object.keys(options.variants || defaultVariants).forEach(
.keys(options.variants || defaultVariants) key => (transformed[key] = moduleData),
.forEach(key => (transformed[key] = moduleData)); );
const result: TransformedCodeFile = { const result: TransformedCodeFile = {
assetContent: null, assetContent: null,

View File

@ -5,6 +5,8 @@
* This source code is licensed under the BSD-style license found in the * This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant * LICENSE file in the root directory of this source tree. An additional grant
* 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.
*
* @format
*/ */
'use strict'; 'use strict';
@ -35,16 +37,15 @@ it('symbolicates stack frames', () => {
]; ];
const stack = mappings.map(m => m.to); const stack = mappings.map(m => m.to);
const maps = const maps = Object.entries(
Object.entries(groupBy(mappings, m => m.from.file)) groupBy(mappings, m => m.from.file),
.map(([file, ms]) => [file, sourceMap(file, ms)]); ).map(([file, ms]) => [file, sourceMap(file, ms)]);
return symbolicate(connection, makeData(stack, maps)) return symbolicate(connection, makeData(stack, maps)).then(() =>
.then(() => expect(connection.end).toBeCalledWith(
expect(connection.end).toBeCalledWith( JSON.stringify({result: mappings.map(m => m.to)}),
JSON.stringify({result: mappings.map(m => m.to)}) ),
) );
);
}); });
it('ignores stack frames without corresponding map', () => { it('ignores stack frames without corresponding map', () => {
@ -54,12 +55,12 @@ it('ignores stack frames without corresponding map', () => {
column: 456, column: 456,
}; };
return symbolicate(connection, makeData([frame], [['other.js', emptyMap()]])) return symbolicate(
.then(() => connection,
expect(connection.end).toBeCalledWith( makeData([frame], [['other.js', emptyMap()]]),
JSON.stringify({result: [frame]}) ).then(() =>
) expect(connection.end).toBeCalledWith(JSON.stringify({result: [frame]})),
); );
}); });
it('ignores `/debuggerWorker.js` stack frames', () => { it('ignores `/debuggerWorker.js` stack frames', () => {
@ -69,12 +70,9 @@ it('ignores `/debuggerWorker.js` stack frames', () => {
column: 456, column: 456,
}; };
return symbolicate(connection, makeData([frame])) return symbolicate(connection, makeData([frame])).then(() =>
.then(() => expect(connection.end).toBeCalledWith(JSON.stringify({result: [frame]})),
expect(connection.end).toBeCalledWith( );
JSON.stringify({result: [frame]})
)
);
}); });
function makeData(stack, maps = []) { function makeData(stack, maps = []) {
@ -85,7 +83,8 @@ function sourceMap(file, mappings) {
const g = new SourceMapGenerator(); const g = new SourceMapGenerator();
g.startFile(file, null); g.startFile(file, null);
mappings.forEach(({from, to}) => mappings.forEach(({from, to}) =>
g.addSourceMapping(to.lineNumber, to.column, from.lineNumber, from.column)); g.addSourceMapping(to.lineNumber, to.column, from.lineNumber, from.column),
);
return g.toMap(); return g.toMap();
} }