Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Degrade gracefully when there are some parse errors #15

Merged
merged 4 commits into from
Aug 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"babel-preset-react": "^6.1.0",
"babel-preset-stage-2": "^6.1.0",
"babel-plugin-transform-function-bind": "^6.1.0",
"core-js": "^2.0.0",
"empower-core": "^0.6.0",
"lerna": "^1.1.2",
"mocha": "^2.4.5"
Expand Down
33 changes: 33 additions & 0 deletions packages/power-assert-context-formatter/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,39 @@ describe('power-assert-context-formatter : reducers option', function () {
}
});

it('degrade gracefully when there are some parse errors caused by not supported syntax', function () {
var format = createFormatter({
reducers: [
appendAst
],
renderers: [
AssertionRenderer,
DiagramRenderer
]
});
try {
var a = 'a';
var b = 'b';
var obj = { foo: 'FOO', bar: 'BAR' };
eval(transpile('assert.deepEqual({ b, ...obj }, { a, ...obj })', false));
} catch (e) {
var result = format(e.powerAssertContext);
baseAssert.equal(result, [
' ',
' assert.deepEqual({ b, ...obj }, { a, ...obj })',
' ? ',
' ? ',
' SyntaxError: Unexpected token (1:22)',
' ',
' If you are using `babel-plugin-espower` and want to use experimental syntax in your assert(), you should set `embedAst` option to true.',
' see: https://github.com/power-assert-js/babel-plugin-espower#optionsembedast',
' ',
' ',
' '
].join('\n'));
}
});

});


Expand Down
7 changes: 6 additions & 1 deletion packages/power-assert-context-reducer-ast/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ module.exports = function (powerAssertContext) {
if (source.ast && source.tokens && source.visitorKeys) {
return powerAssertContext;
}
var astAndTokens = parse(source);
var astAndTokens;
try {
astAndTokens = parse(source);
} catch (e) {
return assign({}, powerAssertContext, { source: assign({}, source, { error: e }) });
}
var newSource = assign({}, source, {
ast: purifyAst(astAndTokens.expression),
tokens: astAndTokens.tokens,
Expand Down
17 changes: 17 additions & 0 deletions packages/power-assert-context-reducer-ast/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,21 @@ describe('Bug reproduction case', function () {
};
assert.deepEqual(actual, expected);
});


it('degrade gracefully when there are some parse errors caused by not supported syntax', function () {
var input = {
source: {
content: 'assert(shallow(<Foo />).contains(<div className="foo" />));',
filepath: 'test/some_test.js',
line: 1
},
args: [
]
};
var actual = reduce(input);
assert(actual.source.error);
assert(actual.source.error instanceof SyntaxError);
});

});
37 changes: 37 additions & 0 deletions packages/power-assert-renderer-assertion/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var BaseRenderer = require('power-assert-renderer-base');
var stringWidth = require('power-assert-util-string-width');
var inherits = require('util').inherits;

function AssertionRenderer () {
Expand All @@ -9,12 +10,48 @@ function AssertionRenderer () {
inherits(AssertionRenderer, BaseRenderer);

AssertionRenderer.prototype.onStart = function (context) {
this.context = context;
this.assertionLine = context.source.content;
};

AssertionRenderer.prototype.onEnd = function () {
this.write('');
this.write(this.assertionLine);
var e = this.context.source.error;
if (e && e instanceof SyntaxError) {
var re = /Unexpected token \(1\:(\d+)\)/;
var matchResult = re.exec(e.message);
if (matchResult) {
var syntaxErrorIndex = Number(matchResult[1]);
this.renderValueAt(syntaxErrorIndex, '?');
this.renderValueAt(syntaxErrorIndex, '?');
this.renderValueAt(syntaxErrorIndex, e.toString());
this.renderValueAt(0, '');
this.renderValueAt(0, 'If you are using `babel-plugin-espower` and want to use experimental syntax in your assert(), you should set `embedAst` option to true.');
this.renderValueAt(0, 'see: https://github.com/power-assert-js/babel-plugin-espower#optionsembedast');
}
}
};

AssertionRenderer.prototype.renderValueAt = function (idx, str) {
var row = createRow(stringWidth(this.assertionLine), ' ');
replaceColumn(idx, row, str);
this.write(row.join(''));
};

function replaceColumn (columnIndex, row, str) {
var i, width = stringWidth(str);
for (i = 0; i < width; i += 1) {
row.splice(columnIndex + i, 1, str.charAt(i));
}
}

function createRow (numCols, initial) {
var row = [], i;
for(i = 0; i < numCols; i += 1) {
row[i] = initial;
}
return row;
}

module.exports = AssertionRenderer;
6 changes: 4 additions & 2 deletions packages/power-assert-renderer-assertion/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
"url": "https://github.com/twada/power-assert-runtime/issues"
},
"dependencies": {
"power-assert-renderer-base": "^1.0.7"
"power-assert-renderer-base": "^1.0.7",
"power-assert-util-string-width": "^1.0.7"
},
"devDependencies": {
"mocha": "^2.4.5"
"mocha": "^2.4.5",
"power-assert-context-reducer-ast": "^1.0.7"
},
"files": [
"README.md",
Expand Down
28 changes: 27 additions & 1 deletion packages/power-assert-renderer-assertion/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ var AssertionRenderer = require('..');
var assert = require('../../../test_helper/empowered-assert');
var transpile = require('../../../test_helper/transpile');
var testRendering = require('../../../test_helper/test-rendering');
var appendAst = require('power-assert-context-reducer-ast');

describe('AssertionRenderer', function () {

it('assert(foo === bar)', function () {
var foo = 'foo';
var bar = 'bar';
Expand All @@ -15,6 +17,30 @@ describe('AssertionRenderer', function () {
}, [
'',
'assert(foo === bar)'
], [AssertionRenderer]);
], { renderers: [AssertionRenderer] });
});

it('show syntax error when there are some parse errors caused by not supported syntax', function () {
var a = 'a';
var b = 'b';
var obj = { foo: 'FOO', bar: 'BAR' };
testRendering(function () {
eval(transpile('assert.deepEqual({ b, ...obj }, { a, ...obj })', false));
}, [
'',
'assert.deepEqual({ b, ...obj }, { a, ...obj })',
' ? ',
' ? ',
' SyntaxError: Unexpected token (1:22)',
' ',
'If you are using `babel-plugin-espower` and want to use experimental syntax in your assert(), you should set `embedAst` option to true.',
'see: https://github.com/power-assert-js/babel-plugin-espower#optionsembedast'
], {
reducers: [
appendAst
],
renderers: [AssertionRenderer]
});
});

});
4 changes: 2 additions & 2 deletions packages/power-assert-renderer-file/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('FileRenderer', function () {
eval(transpile('assert(foo === bar)'));
}, [
'# test/some_test.js:1'
], [FileRenderer]);
], { renderers: [FileRenderer] });
});

it('line number detection', function () {
Expand All @@ -24,7 +24,7 @@ describe('FileRenderer', function () {
eval(transpile('var i = 0;\n\nassert(falsyStr)'));
}, [
'# test/some_test.js:3'
], [FileRenderer]);
], { renderers: [FileRenderer] });
});

});
4 changes: 3 additions & 1 deletion test_helper/create-renderer-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ module.exports = function createRendererTester (renderer) {
it(title + ': ' + expression, function () {
testRendering(function () {
body(transpile(expression));
}, [''].concat(expectedLines), [AssertionRenderer, renderer]);
}, [''].concat(expectedLines), {
renderers: [AssertionRenderer, renderer]
});
});
};
}
10 changes: 5 additions & 5 deletions test_helper/test-rendering.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

var createFormatter = require('../packages/power-assert-context-formatter');
var baseAssert = require('assert');
var assign = require('core-js/library/fn/object/assign');

module.exports = function testRendering (body, expectedLines, renderers) {
module.exports = function testRendering (body, expectedLines, formatterOptions) {
try {
body();
baseAssert.fail('AssertionError should be thrown');
} catch (e) {
baseAssert.equal(typeof e.powerAssertContext, 'object', 'powerAssertContext should exist');
var format = createFormatter({
outputOffset: 0,
renderers: renderers
});
var format = createFormatter(assign({}, {
outputOffset: 0
}, formatterOptions));
baseAssert.equal(format(e.powerAssertContext), expectedLines.join('\n') + '\n');
}
};