[react-packager] Better transform errors

This commit is contained in:
Amjad Masad 2015-02-28 17:13:18 -08:00
parent c435ce74b6
commit 68b02a843c
5 changed files with 31 additions and 22 deletions

View File

@ -35,7 +35,7 @@ describe('Transformer', function() {
callback(null, 'content'); callback(null, 'content');
}); });
return new Transformer(OPTIONS).loadFileAndTransform([], 'file', {}) return new Transformer(OPTIONS).loadFileAndTransform('file')
.then(function(data) { .then(function(data) {
expect(data).toEqual({ expect(data).toEqual({
code: 'transformed', code: 'transformed',
@ -58,7 +58,7 @@ describe('Transformer', function() {
callback(null, {error: esprimaError}); callback(null, {error: esprimaError});
}); });
return new Transformer(OPTIONS).loadFileAndTransform([], 'foo-file.js', {}) return new Transformer(OPTIONS).loadFileAndTransform('foo-file.js')
.catch(function(error) { .catch(function(error) {
expect(error.type).toEqual('TransformError'); expect(error.type).toEqual('TransformError');
expect(error.snippet).toEqual([ expect(error.snippet).toEqual([

View File

@ -78,11 +78,7 @@ Transformer.prototype.invalidateFile = function(filePath) {
this._cache.invalidate(filePath); this._cache.invalidate(filePath);
}; };
Transformer.prototype.loadFileAndTransform = function( Transformer.prototype.loadFileAndTransform = function(filePath) {
transformSets,
filePath,
options
) {
if (this._failedToStart) { if (this._failedToStart) {
return this._failedToStart; return this._failedToStart;
} }
@ -92,15 +88,14 @@ Transformer.prototype.loadFileAndTransform = function(
return readFile(filePath) return readFile(filePath)
.then(function(buffer) { .then(function(buffer) {
var sourceCode = buffer.toString(); var sourceCode = buffer.toString();
var opts = _.extend({}, options, {filename: filePath});
return q.nfbind(workers)({ return q.nfbind(workers)({
transformSets: transformSets,
sourceCode: sourceCode, sourceCode: sourceCode,
options: opts, filename: filePath,
}).then( }).then(
function(res) { function(res) {
if (res.error) { if (res.error) {
throw formatEsprimaError(res.error, filePath, sourceCode); throw formatError(res.error, filePath, sourceCode);
} }
return { return {
@ -117,11 +112,26 @@ Transformer.prototype.loadFileAndTransform = function(
function TransformError() {} function TransformError() {}
util.inherits(TransformError, SyntaxError); util.inherits(TransformError, SyntaxError);
function formatEsprimaError(err, filename, source) { function formatError(err, filename, source) {
if (!(err.lineNumber && err.column)) { if (err.lineNumber && err.column) {
return err; return formatEsprimaError(err, filename, source);
} else {
return formatGenericError(err, filename, source);
} }
}
function formatGenericError(err, filename) {
var msg = 'TransformError: ' + filename + ': ' + err.message;
var error = new TransformError();
var stack = err.stack.split('\n').slice(0, -1);
stack.push(msg);
error.stack = stack.join('\n');
error.message = msg;
error.type = 'TransformError';
return error;
}
function formatEsprimaError(err, filename, source) {
var stack = err.stack.split('\n'); var stack = err.stack.split('\n');
stack.shift(); stack.shift();

View File

@ -49,7 +49,7 @@ describe('Packager', function() {
}); });
require('../../JSTransformer').prototype.loadFileAndTransform require('../../JSTransformer').prototype.loadFileAndTransform
.mockImpl(function(tsets, path) { .mockImpl(function(path) {
return q({ return q({
code: 'transformed ' + path, code: 'transformed ' + path,
sourceCode: 'source ' + path, sourceCode: 'source ' + path,

View File

@ -126,9 +126,7 @@ Packager.prototype.getDependencies = function(main) {
Packager.prototype._transformModule = function(module) { Packager.prototype._transformModule = function(module) {
var resolver = this._resolver; var resolver = this._resolver;
return this._transformer.loadFileAndTransform( return this._transformer.loadFileAndTransform(
['es6'], path.resolve(module.path)
path.resolve(module.path),
this._opts.transformer || {}
).then(function(transformed) { ).then(function(transformed) {
return _.extend( return _.extend(
{}, {},

View File

@ -16,10 +16,11 @@ var staticTypeSyntax =
var visitorList = reactVisitors; var visitorList = reactVisitors;
function transform(transformSets, srcTxt) { function transform(srcTxt, filename) {
var options = { var options = {
es3: true, es3: true,
sourceType: 'nonStrictModule' sourceType: 'nonStrictModule',
filename: filename,
}; };
// These tranforms mostly just erase type annotations and static typing // These tranforms mostly just erase type annotations and static typing
@ -42,8 +43,8 @@ module.exports = function(data, callback) {
var result; var result;
try { try {
result = transform( result = transform(
data.transformSets, data.sourceCode,
data.sourceCode data.filename
); );
} catch (e) { } catch (e) {
return callback(null, { return callback(null, {