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

Commit 251e8aa

Browse files
committed
Allow ES6 conditions and watches
We were previously rejecting ES6 expressions. Switch over acorn to ES6 mode and add a few tests.
1 parent 49f5d95 commit 251e8aa

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

lib/v8debugapi.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,15 @@ module.exports.create = function(logger_, config_, fileStats_) {
239239
if (breakpoint.condition) {
240240
var acorn = require('acorn');
241241
try {
242-
ast = acorn.parse(breakpoint.condition, { sourceType: 'script'});
242+
// We parse as ES6; even though the underlying V8 version may only
243+
// support a subset. This should be fine as the objective of the parse
244+
// is to heuristically find side-effects. V8 will raise errors later
245+
// if the syntax is invalid. It would have been nice if V8 had made the
246+
// parser API available us :(.
247+
ast = acorn.parse(breakpoint.condition, {
248+
sourceType: 'script',
249+
ecmaVersion: 6
250+
});
243251
var validator = require('./validator.js');
244252
if (!validator.isValid(ast)) {
245253
return setErrorStatusAndCallback(cb, breakpoint,
@@ -588,6 +596,3 @@ module.exports.create = function(logger_, config_, fileStats_) {
588596

589597
return singleton;
590598
};
591-
592-
593-

lib/validator.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ function isValid(node) {
8787
case 'UnaryExpression':
8888
return isValid(node.argument);
8989

90+
case 'SpreadElement':
91+
return isValid(node.argument);
92+
93+
case 'TemplateLiteral':
94+
return node.quasis.every(isValid) && node.expressions.every(isValid);
95+
case 'TaggedTemplateExpression':
96+
return isValid(node.tag) && isValid(node.quasi);
97+
case 'TemplateElement':
98+
return true;
99+
90100
default:
91101
return false;
92102
}

test/test-v8debugapi.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,9 @@ describe('v8debugapi', function() {
303303
'!fib()',
304304
'1+fib()',
305305
'x++',
306-
'[1, 2, 3, 4, x = 1, x == 1, x === 1]'
306+
'[1, 2, 3, 4, x = 1, x == 1, x === 1]',
307+
'[0].values()',
308+
'new Object()',
307309
]);
308310
conditionTests('valid conditions', function(err) { assert.ifError(err); }, [
309311
null,
@@ -319,9 +321,39 @@ describe('v8debugapi', function() {
319321
'{f: process.env}',
320322
'1,2,3,{f:2},4',
321323
'A[this?this:1]',
322-
'[1, 2, 3, 4, x == 1, x === 1, null, undefined]'
324+
'[1, 2, 3, 4, x == 1, x === 1, null, undefined]',
325+
'[0].values',
326+
'[][0]',
327+
'[0][' + MAX_INT + ']',
328+
'"𠮷".length + (5| "𠮷")',
329+
'/ٹوٹ بٹوٹ کے دو مُرغے تھے/',
323330
]);
324331

332+
if (semver.satisfies(process.version, '>=4.0')) {
333+
conditionTests('invalid conditions Node 4+', assert, [
334+
'[][Symbol.iterator]()',
335+
'`${[][Symbol.iterator]()}`',
336+
'`${let x = 1}`',
337+
'`${JSON.parse("{x:1}")}`',
338+
'`${try {1}}`'
339+
]);
340+
conditionTests('valid conditions Node 4+', function(err) {
341+
assert.ifError(err);
342+
}, [
343+
'[][Symbol.iterator]',
344+
'[..."peanut butter"]',
345+
'[0,...[1,2,"foo"]]',
346+
'`${1}`',
347+
'`${[][1+1]}`',
348+
'0b10101010',
349+
'0o70000',
350+
// Disabled because of suspect acorn issues?
351+
// https://tonicdev.com/575b00351a0e0a1300505d00/575b00351a0e0a1300505d01
352+
//'{["foo"]: 1}',
353+
//'{ foo (a,b) {}}'
354+
]);
355+
}
356+
325357
describe('path normalization', function() {
326358
var breakpoints = [
327359
{ id: 'path0', location: {line: 4, path: path.join(path.sep, 'test',

0 commit comments

Comments
 (0)