Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
Clean up coffeescript error messages
Browse files Browse the repository at this point in the history
If compilation of a coffeescript expression fails, we will report the
associated error if the coffeescript compiler provides one. Otherwise,
we will just report that the compilation failed.

Fixes #2
  • Loading branch information
Matt Loring committed Jan 11, 2016
1 parent 2d45b8c commit 4de169b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
49 changes: 35 additions & 14 deletions lib/v8debugapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
V8_BREAKPOINT_ERROR: 'Unable to set breakpoint in v8',
SYNTAX_ERROR_IN_CONDITION: 'Syntax error in condition: ',
ERROR_EVALUATING_CONDITION: 'Error evaluating condition: ',
ERROR_COMPILING_CONDITION: 'Error compiling condition.',
DISALLOWED_EXPRESSION: 'Expression not allowed',
SOURCE_MAP_URL_NOT_FOUND: 'The source map url could not be found in the compiled file',
SOURCE_MAP_READ_ERROR: 'The source map could not be read or was incorrectly formatted',
Expand Down Expand Up @@ -132,7 +133,15 @@ module.exports.create = function(logger_, config_, fileStats_) {
};
compile = getBreakpointCompiler(breakpoint);
if (breakpoint.condition && compile) {
breakpoint.condition = compile(breakpoint.condition);
try {
breakpoint.condition = compile(breakpoint.condition);
} catch (e) {
logger.info('Unable to compile condition >> ' +
breakpoint.condition + ' <<');
return setErrorStatusAndCallback(cb, breakpoint,
StatusMessage.BREAKPOINT_CONDITION,
messages.ERROR_COMPILING_CONDITION);
}
}
// TODO: more robust file finding of compiled files
scriptPath = scriptPath.substr(0, scriptPath.lastIndexOf('.')) + '.js';
Expand Down Expand Up @@ -323,16 +332,15 @@ module.exports.create = function(logger_, config_, fileStats_) {
switch(path.normalize(breakpoint.location.path).split('.').pop()) {
case 'coffee':
return function(uncompiled) {
try {
var comp = require('coffee-script');
var compiled = comp.compile('0 || (' + uncompiled + ')');
// Strip out coffeescript scoping wrapper to get translated condition
var re = /\(function\(\) {\s*0 \|\| \((.*)\);\n\n\}\)\.call\(this\);/;
var match = re.exec(compiled)[1];
return match ? match.trim() : match;
} catch (err) {
logger.info('Unable to compile break or watch point >> ' +
uncompiled + ' <<', err);
var comp = require('coffee-script');
var compiled = comp.compile('0 || (' + uncompiled + ')');
// Strip out coffeescript scoping wrapper to get translated condition
var re = /\(function\(\) {\s*0 \|\| \((.*)\);\n\n\}\)\.call\(this\);/;
var match = re.exec(compiled);
if (match && match.length > 1) {
return match[1].trim();
} else {
throw new Error('Compilation Error for: ' + uncompiled);
}
};
case 'es6':
Expand Down Expand Up @@ -467,16 +475,29 @@ module.exports.create = function(logger_, config_, fileStats_) {
}

function captureBreakpointData(breakpoint, execState) {
var expressionErrors = [];
if (breakpoint.expressions && breakpoints[breakpoint.id].compile) {
for (var i = 0; i < breakpoint.expressions.length; i++) {
breakpoint.expressions[i] =
breakpoints[breakpoint.id].compile(breakpoint.expressions[i]);
try {
breakpoint.expressions[i] =
breakpoints[breakpoint.id].compile(breakpoint.expressions[i]);
} catch (e) {
logger.info('Unable to compile watch expression >> ' +
breakpoint.expressions[i] + ' <<');
expressionErrors.push({
name: breakpoint.expressions[i],
status: new StatusMessage(StatusMessage.VARIABLE_VALUE,
'Error Compiling Expression', true)
});
breakpoint.expressions.splice(i, 1);
}
}
}
var captured = state.capture(execState, breakpoint.expressions, config);
breakpoint.stackFrames = captured.stackFrames;
breakpoint.variableTable = captured.variableTable;
breakpoint.evaluatedExpressions = captured.evaluatedExpressions;
breakpoint.evaluatedExpressions =
expressionErrors.concat(captured.evaluatedExpressions);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions test/test-v8debugapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,21 @@ describe('v8debugapi', function() {
});
});

it('should show error for invalid conditions in coffeescript',
function (done) {
var bp = {
id: 'coffee-id-1729',
location: { path: './test/fixtures/coffee/transpile.coffee',
line: 3 },
condition: 'process=false'
};
api.set(bp, function(err) {
assert(err);
assert.equal(err.message, 'Error compiling condition.');
done();
});
});

it('should be possible to set conditional breakpoints with babel',
function (done) {
var bp = {
Expand Down Expand Up @@ -489,6 +504,16 @@ describe('v8debugapi', function() {
for (var i in bp.evaluatedExpressions) {
var expr = bp.evaluatedExpressions[i];
assert(expr.status && expr.status.isError);
if (expr.name === ':)' ||
expr.name === 'process=this' ||
expr.name === 'return') {
assert.equal(expr.status.description.format,
'Error Compiling Expression');
} else {
assert.notEqual(
expr.status.description.format.indexOf('Unexpected token'),
-1);
}
}

api.clear(bp);
Expand Down

0 comments on commit 4de169b

Please sign in to comment.