[react-packager] Better transform errors

This commit is contained in:
Amjad Masad 2015-02-28 17:13:18 -08:00
parent a9c8b13e6e
commit c9af9963be
6 changed files with 34 additions and 25 deletions

View File

@ -2,8 +2,8 @@
var transformer = require('../packager/transformer.js');
function transformSource(src) {
return transformer.transform(null, src).code;
function transformSource(src, filename) {
return transformer.transform(src, filename).code;
}
module.exports = {
@ -15,7 +15,7 @@ module.exports = {
}
try {
return transformSource(src);
return transformSource(src, fileName);
} catch(e) {
throw new Error('\nError transforming file:\n js/' +
(fileName.split('/js/')[1] || fileName) + ':' + e.lineNumber + ': \'' +

View File

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

View File

@ -78,11 +78,7 @@ Transformer.prototype.invalidateFile = function(filePath) {
this._cache.invalidate(filePath);
};
Transformer.prototype.loadFileAndTransform = function(
transformSets,
filePath,
options
) {
Transformer.prototype.loadFileAndTransform = function(filePath) {
if (this._failedToStart) {
return this._failedToStart;
}
@ -92,15 +88,14 @@ Transformer.prototype.loadFileAndTransform = function(
return readFile(filePath)
.then(function(buffer) {
var sourceCode = buffer.toString();
var opts = _.extend({}, options, {filename: filePath});
return q.nfbind(workers)({
transformSets: transformSets,
sourceCode: sourceCode,
options: opts,
filename: filePath,
}).then(
function(res) {
if (res.error) {
throw formatEsprimaError(res.error, filePath, sourceCode);
throw formatError(res.error, filePath, sourceCode);
}
return {
@ -117,11 +112,26 @@ Transformer.prototype.loadFileAndTransform = function(
function TransformError() {}
util.inherits(TransformError, SyntaxError);
function formatEsprimaError(err, filename, source) {
if (!(err.lineNumber && err.column)) {
return err;
function formatError(err, filename, source) {
if (err.lineNumber && err.column) {
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');
stack.shift();

View File

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

View File

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

View File

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