Skip to content

Commit

Permalink
Corrections to syntax error handling in compress operation. Fixes gh-583
Browse files Browse the repository at this point in the history


The correct algorithm:

1. Let ast be a.parse(source)
2. If ast is a syntax error (e), let m be e.message
    1. Set the value of ast to u.parse(source)
        1. If ast is a syntax error (e), append e.message to m.
            1. throw new SyntaxError(m)
  • Loading branch information
rwaldron committed Feb 19, 2016
1 parent 5a2b69c commit e6639b4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
28 changes: 17 additions & 11 deletions lib/tessel/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,7 @@ actions.compress = function(source) {
};

var ast;
var errors = [];

try {
// Attempt to use acorn, this will provide
Expand All @@ -986,18 +987,23 @@ actions.compress = function(source) {
})
);
// However, if it fails, also try uglify...
} catch (error) {

// If uglify lands _better_ ES6 support sooner,
// this will handle that
ast = uglify.parse(source, {
bare_returns: true,
fromString: true,
warnings: false,
});
// Otherwise the whole thing is a failure.
} catch (acornError) {
errors.push(`Acorn Parser: ${acornError.message}`);

throw new Error(error);
try {
// If uglify lands _better_ ES6 support sooner,
// this will handle that
ast = uglify.parse(source, {
bare_returns: true,
fromString: true,
warnings: false,
});
} catch (uglifyError) {
errors.push(`Uglify Parser: ${uglifyError.message}`);

// Otherwise the whole thing is a failure.
throw new SyntaxError(errors.join('\n'));
}
}

ast.figure_out_scope();
Expand Down
33 changes: 28 additions & 5 deletions test/unit/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ exports['deploy.tarBundle'] = {
},

slimSyntaxErrorRejects: function(test) {
test.expect(1);
test.expect(2);

var entryPoint = 'index.js';
var target = 'test/unit/fixtures/syntax-error';
Expand All @@ -792,15 +792,38 @@ exports['deploy.tarBundle'] = {
target: path.normalize(target),
resolvedEntryPoint: entryPoint,
slim: true,
}).then(function() {
}).then(() => {
test.fail();
test.done();
}).catch(function(error) {
test.ok(error.message.indexOf('Unexpected token') !== -1);
}).catch((error) => {
var messages = error.message.split('\n');
test.equal(messages[0], 'Acorn Parser: Unexpected token (1:5)');
test.equal(messages[1], 'Uglify Parser: Unexpected token: name (is)');
test.done();
}.bind(this));
});
},

slimAcornSyntaxErrorUglifySuccess: function(test) {
test.expect(1);

// It's _possible_, but unlikely, that some ES6 thing
// will be supported in Uglify before it's supported
// in Acorn. To simulate that scenario, acorn.parse
// must fail and uglify.parse must be successful.

var uparse = uglify.parse;

this.uglifyParse = sandbox.stub(uglify, 'parse', (source, options) => {
return uparse('var a = 1;', options);
});

deploy.tarBundle({
target: path.normalize('test/unit/fixtures/syntax-error'),
}).then(() => {
test.ok(true);
test.done();
});
},


slimTesselInit: function(test) {
Expand Down

0 comments on commit e6639b4

Please sign in to comment.