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
* 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.
*
* @format
*/
'use strict';
@ -26,88 +28,99 @@ describe('Bundle', () => {
describe('source bundle', () => {
it('should create a bundle and get the source', () => {
return Promise.resolve().then(() => {
return Promise.resolve()
.then(() => {
return addModule({
bundle,
code: 'transformed foo;',
sourceCode: 'source foo',
sourcePath: 'foo path',
});
}).then(() => {
})
.then(() => {
return addModule({
bundle,
code: 'transformed bar;',
sourceCode: 'source bar',
sourcePath: 'bar path',
});
}).then(() => {
})
.then(() => {
bundle.finalize({});
expect(bundle.getSource({dev: true})).toBe([
expect(bundle.getSource({dev: true})).toBe(
[
'transformed foo;',
'transformed bar;',
'\/\/# sourceMappingURL=test_url',
].join('\n'));
].join('\n'),
);
});
});
it('should be ok to leave out the source map url', () => {
const otherBundle = new Bundle();
return Promise.resolve().then(() => {
return Promise.resolve()
.then(() => {
return addModule({
bundle: otherBundle,
code: 'transformed foo;',
sourceCode: 'source foo',
sourcePath: 'foo path',
});
}).then(() => {
})
.then(() => {
return addModule({
bundle: otherBundle,
code: 'transformed bar;',
sourceCode: 'source bar',
sourcePath: 'bar path',
});
}).then(() => {
})
.then(() => {
otherBundle.finalize({});
expect(otherBundle.getSource({dev: true})).toBe([
'transformed foo;',
'transformed bar;',
].join('\n'));
expect(otherBundle.getSource({dev: true})).toBe(
['transformed foo;', 'transformed bar;'].join('\n'),
);
});
});
it('should create a bundle and add run module code', () => {
return Promise.resolve().then(() => {
return Promise.resolve()
.then(() => {
return addModule({
bundle,
code: 'transformed foo;',
sourceCode: 'source foo',
sourcePath: 'foo path',
});
}).then(() => {
})
.then(() => {
return addModule({
bundle,
code: 'transformed bar;',
sourceCode: 'source bar',
sourcePath: 'bar path',
});
}).then(() => {
})
.then(() => {
bundle.setMainModuleId('foo');
bundle.finalize({
runBeforeMainModule: ['bar'],
runModule: true,
});
expect(bundle.getSource({dev: true})).toBe([
expect(bundle.getSource({dev: true})).toBe(
[
'transformed foo;',
'transformed bar;',
';require("bar");',
';require("foo");',
'\/\/# sourceMappingURL=test_url',
].join('\n'));
].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 = [
createModuleTransport({name: 'module1'}),
createModuleTransport({name: 'module2'}),
@ -123,11 +136,12 @@ describe('Bundle', () => {
},
};
const promise = Promise.all(moduleTransports.map(
m => bundle.addModule(resolver, null, {isPolyfill: () => false}, m)
)).then(() => {
expect(bundle.getModules())
.toEqual(moduleTransports);
const promise = Promise.all(
moduleTransports.map(m =>
bundle.addModule(resolver, null, {isPolyfill: () => false}, m),
),
).then(() => {
expect(bundle.getModules()).toEqual(moduleTransports);
});
resolves.module2({code: ''});
@ -135,8 +149,7 @@ describe('Bundle', () => {
resolves.module1({code: ''});
return promise;
},
);
});
});
describe('sourcemap bundle', () => {
@ -147,7 +160,8 @@ describe('Bundle', () => {
it('should combine sourcemaps', () => {
const otherBundle = new Bundle({sourceMapUrl: 'test_url'});
return Promise.resolve().then(() => {
return Promise.resolve()
.then(() => {
return addModule({
bundle: otherBundle,
code: 'transformed foo;\n',
@ -155,7 +169,8 @@ describe('Bundle', () => {
map: {name: 'sourcemap foo'},
sourcePath: 'foo path',
});
}).then(() => {
})
.then(() => {
return addModule({
bundle: otherBundle,
code: 'transformed bar;\n',
@ -163,7 +178,8 @@ describe('Bundle', () => {
map: {name: 'sourcemap bar'},
sourcePath: 'bar path',
});
}).then(() => {
})
.then(() => {
return addModule({
bundle: otherBundle,
code: 'image module;\nimage module;',
@ -171,7 +187,8 @@ describe('Bundle', () => {
sourceCode: 'image module;\nimage module;',
sourcePath: 'image.png',
});
}).then(() => {
})
.then(() => {
otherBundle.setMainModuleId('foo');
otherBundle.finalize({
runBeforeMainModule: ['InitializeCore'],
@ -248,14 +265,16 @@ describe('Bundle', () => {
describe('getJSModulePaths()', () => {
it('should return module paths', () => {
var otherBundle = new Bundle({sourceMapUrl: 'test_url'});
return Promise.resolve().then(() => {
return Promise.resolve()
.then(() => {
return addModule({
bundle: otherBundle,
code: 'transformed foo;\n',
sourceCode: 'source foo',
sourcePath: 'foo path',
});
}).then(() => {
})
.then(() => {
return addModule({
bundle: otherBundle,
code: 'image module;\nimage module;',
@ -263,7 +282,8 @@ describe('Bundle', () => {
sourceCode: 'image module;\nimage module;',
sourcePath: 'image.png',
});
}).then(() => {
})
.then(() => {
expect(otherBundle.getJSModulePaths()).toEqual(['foo path']);
});
});
@ -272,7 +292,10 @@ describe('Bundle', () => {
describe('getEtag()', function() {
it('should return an etag', function() {
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);
});
});
@ -305,51 +328,86 @@ describe('Bundle', () => {
it('can create a single group', () => {
bundle = createBundle([fsLocation('React')]);
const {groups} = bundle.getUnbundle();
expect(groups).toEqual(new Map([
[idFor('React'), new Set(['ReactFoo', 'invariant', 'ReactBar', 'cx'].map(idFor))],
]));
expect(groups).toEqual(
new Map([
[
idFor('React'),
new Set(['ReactFoo', 'invariant', 'ReactBar', 'cx'].map(idFor)),
],
]),
);
});
it('can create two groups', () => {
bundle = createBundle([fsLocation('ReactFoo'), fsLocation('ReactBar')]);
const {groups} = bundle.getUnbundle();
expect(groups).toEqual(new Map([
expect(groups).toEqual(
new Map([
[idFor('ReactFoo'), new Set([idFor('invariant')])],
[idFor('ReactBar'), new Set([idFor('cx')])],
]));
]),
);
});
it('can handle circular dependencies', () => {
bundle = createBundle([fsLocation('OtherFramework')]);
const {groups} = bundle.getUnbundle();
expect(groups).toEqual(new Map([[
expect(groups).toEqual(
new Map([
[
idFor('OtherFramework'),
new Set(['OtherFrameworkFoo', 'invariant', 'OtherFrameworkBar', 'crc32'].map(idFor)),
]]));
new Set(
[
'OtherFrameworkFoo',
'invariant',
'OtherFrameworkBar',
'crc32',
].map(idFor),
),
],
]),
);
});
it('omits modules that are contained by more than one group', () => {
bundle = createBundle([fsLocation('React'), fsLocation('OtherFramework')]);
bundle = createBundle([
fsLocation('React'),
fsLocation('OtherFramework'),
]);
expect(() => {
const {groups} = bundle.getUnbundle(); //eslint-disable-line no-unused-vars
}).toThrow(new Error(`Module ${fsLocation('invariant')} belongs to groups ${fsLocation('React')}` +
`, and ${fsLocation('OtherFramework')}. Removing it from all groups.`));
}).toThrow(
new Error(
`Module ${fsLocation('invariant')} belongs to groups ${fsLocation('React')}` +
`, and ${fsLocation('OtherFramework')}. Removing it from all groups.`,
),
);
});
it('ignores missing dependencies', () => {
bundle = createBundle([fsLocation('Product1')]);
const {groups} = bundle.getUnbundle();
expect(groups).toEqual(new Map([[
expect(groups).toEqual(
new Map([
[
idFor('Product1'),
new Set(['React', 'ReactFoo', 'invariant', 'ReactBar', 'cx'].map(idFor)),
]]));
new Set(
['React', 'ReactFoo', 'invariant', 'ReactBar', 'cx'].map(idFor),
),
],
]),
);
});
it('throws for group roots that do not exist', () => {
bundle = createBundle([fsLocation('DoesNotExist')]);
expect(() => {
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) {
@ -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(
resolverFor(code, map),
null,

View File

@ -5,6 +5,8 @@
* 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
* of patent rights can be found in the PATENTS file in the same directory.
*
* @format
*/
'use strict';
@ -46,10 +48,12 @@ describe('transforming JS modules:', () => {
it('passes through file name and code', done => {
transformModule(sourceCode, options(), (error, result) => {
expect(result.type).toBe('code');
expect(result.details).toEqual(expect.objectContaining({
expect(result.details).toEqual(
expect.objectContaining({
code: sourceCode,
file: filename,
}));
}),
);
done();
});
});
@ -73,11 +77,17 @@ describe('transforming JS modules:', () => {
});
it('sets `type` to `"script"` if the input is a polyfill', done => {
transformModule(sourceCode, {...options(), polyfill: true}, (error, result) => {
transformModule(
sourceCode,
{...options(), polyfill: true},
(error, result) => {
expect(result.type).toBe('code');
expect(result.details).toEqual(expect.objectContaining({type: 'script'}));
expect(result.details).toEqual(
expect.objectContaining({type: 'script'}),
);
done();
});
},
);
});
const defaults = {
@ -89,7 +99,8 @@ describe('transforming JS modules:', () => {
projectRoot: '',
};
it('calls the passed-in transform function with code, file name, and options ' +
it(
'calls the passed-in transform function with code, file name, and options ' +
'for all passed in variants',
done => {
const variants = {dev: {dev: true}, prod: {dev: false}};
@ -127,24 +138,27 @@ describe('transforming JS modules:', () => {
expect(error).toEqual(null);
const {code, dependencyMapName} = result.details.transformed.default;
expect(code.replace(/\s+/g, ''))
.toEqual(
`__d(function(global,require,module,exports,${
dependencyMapName}){${transformedCode}});`
expect(code.replace(/\s+/g, '')).toEqual(
`__d(function(global,require,module,exports,${dependencyMapName}){${transformedCode}});`,
);
done();
});
});
it('wraps the code produced by the transform function into an IIFE for polyfills', done => {
transformModule(sourceCode, {...options(), polyfill: true}, (error, result) => {
transformModule(
sourceCode,
{...options(), polyfill: true},
(error, result) => {
expect(error).toEqual(null);
const {code} = result.details.transformed.default;
expect(code.replace(/\s+/g, ''))
.toEqual(`(function(global){${transformedCode}})(this);`);
expect(code.replace(/\s+/g, '')).toEqual(
`(function(global){${transformedCode}})(this);`,
);
done();
});
},
);
});
it('creates source maps', done => {
@ -152,8 +166,9 @@ describe('transforming JS modules:', () => {
const {code, map} = result.details.transformed.default;
const column = code.indexOf('code');
const consumer = new SourceMapConsumer(map);
expect(consumer.originalPositionFor({line: 1, column}))
.toEqual(expect.objectContaining({line: 1, column: sourceCode.indexOf('code')}));
expect(consumer.originalPositionFor({line: 1, column})).toEqual(
expect.objectContaining({line: 1, column: sourceCode.indexOf('code')}),
);
done();
});
});
@ -166,8 +181,9 @@ describe('transforming JS modules:', () => {
transformer.transform.stub.returns(transformResult(body));
transformModule(code, options(), (error, result) => {
expect(result.details.transformed.default)
.toEqual(expect.objectContaining({dependencies: [dep1, dep2]}));
expect(result.details.transformed.default).toEqual(
expect.objectContaining({dependencies: [dep1, dep2]}),
);
done();
});
});
@ -182,15 +198,11 @@ describe('transforming JS modules:', () => {
transformModule(sourceCode, options(variants), (error, result) => {
const {dev, prod} = result.details.transformed;
expect(dev.code.replace(/\s+/g, ''))
.toEqual(
`__d(function(global,require,module,exports,${
dev.dependencyMapName}){arbitrary(code);});`
expect(dev.code.replace(/\s+/g, '')).toEqual(
`__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();
});
@ -199,23 +211,31 @@ describe('transforming JS modules:', () => {
it('prefixes JSON files with `module.exports = `', done => {
const json = '{"foo":"bar"}';
transformModule(json, {...options(), filename: 'some.json'}, (error, result) => {
transformModule(
json,
{...options(), filename: 'some.json'},
(error, result) => {
const {code} = result.details.transformed.default;
expect(code.replace(/\s+/g, ''))
.toEqual(
expect(code.replace(/\s+/g, '')).toEqual(
'__d(function(global,require,module,exports){' +
`module.exports=${json}});`
`module.exports=${json}});`,
);
done();
});
},
);
});
it('does not create source maps for JSON files', done => {
transformModule('{}', {...options(), filename: 'some.json'}, (error, result) => {
expect(result.details.transformed.default)
.toEqual(expect.objectContaining({map: null}));
transformModule(
'{}',
{...options(), filename: 'some.json'},
(error, result) => {
expect(result.details.transformed.default).toEqual(
expect.objectContaining({map: null}),
);
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.
*
* @flow
* @format
*/
'use strict';
@ -70,12 +71,13 @@ function transformModule(
const {filename, transformer, variants = defaultVariants} = options;
const tasks = {};
Object.keys(variants).forEach(name => {
tasks[name] = asyncify(() => transformer.transform({
tasks[name] = asyncify(() =>
transformer.transform({
filename,
localPath: filename,
options: {...defaultTransformOptions, ...variants[name]},
src: code,
})
}),
);
});
@ -89,7 +91,12 @@ function transformModule(
//$FlowIssue #14545724
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));
@ -112,10 +119,7 @@ function transformModule(
function transformJSON(json, options, callback) {
const value = JSON.parse(json);
const {filename} = options;
const code =
`__d(function(${JsFileWrapping.MODULE_FACTORY_PARAMETERS.join(', ')}) { module.exports = \n${
json
}\n});`;
const code = `__d(function(${JsFileWrapping.MODULE_FACTORY_PARAMETERS.join(', ')}) { module.exports = \n${json}\n});`;
const moduleData = {
code,
@ -124,9 +128,9 @@ function transformJSON(json, options, callback) {
};
const transformed = {};
Object
.keys(options.variants || defaultVariants)
.forEach(key => (transformed[key] = moduleData));
Object.keys(options.variants || defaultVariants).forEach(
key => (transformed[key] = moduleData),
);
const result: TransformedCodeFile = {
assetContent: null,

View File

@ -5,6 +5,8 @@
* 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
* of patent rights can be found in the PATENTS file in the same directory.
*
* @format
*/
'use strict';
@ -35,15 +37,14 @@ it('symbolicates stack frames', () => {
];
const stack = mappings.map(m => m.to);
const maps =
Object.entries(groupBy(mappings, m => m.from.file))
.map(([file, ms]) => [file, sourceMap(file, ms)]);
const maps = Object.entries(
groupBy(mappings, m => m.from.file),
).map(([file, ms]) => [file, sourceMap(file, ms)]);
return symbolicate(connection, makeData(stack, maps))
.then(() =>
return symbolicate(connection, makeData(stack, maps)).then(() =>
expect(connection.end).toBeCalledWith(
JSON.stringify({result: mappings.map(m => m.to)})
)
JSON.stringify({result: mappings.map(m => m.to)}),
),
);
});
@ -54,11 +55,11 @@ it('ignores stack frames without corresponding map', () => {
column: 456,
};
return symbolicate(connection, makeData([frame], [['other.js', emptyMap()]]))
.then(() =>
expect(connection.end).toBeCalledWith(
JSON.stringify({result: [frame]})
)
return symbolicate(
connection,
makeData([frame], [['other.js', emptyMap()]]),
).then(() =>
expect(connection.end).toBeCalledWith(JSON.stringify({result: [frame]})),
);
});
@ -69,11 +70,8 @@ it('ignores `/debuggerWorker.js` stack frames', () => {
column: 456,
};
return symbolicate(connection, makeData([frame]))
.then(() =>
expect(connection.end).toBeCalledWith(
JSON.stringify({result: [frame]})
)
return symbolicate(connection, makeData([frame])).then(() =>
expect(connection.end).toBeCalledWith(JSON.stringify({result: [frame]})),
);
});
@ -85,7 +83,8 @@ function sourceMap(file, mappings) {
const g = new SourceMapGenerator();
g.startFile(file, null);
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();
}