Skip to content

Commit 37e8b49

Browse files
codeslikejaggarsnovemberborn
authored andcommitted
Use babel-generator to regenerate enhanced assertion statements
Fixes #1513.
1 parent 3b81e2c commit 37e8b49

File tree

5 files changed

+95
-25
lines changed

5 files changed

+95
-25
lines changed

lib/enhance-assert.js

+4-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
const concordance = require('concordance');
33
const dotProp = require('dot-prop');
4+
const generate = require('babel-generator').default;
45
const concordanceOptions = require('./concordance-options').default;
56

67
// When adding patterns, don't forget to add to
@@ -15,31 +16,16 @@ const PATTERNS = [
1516
't.notRegex(contents, regex, [message])'
1617
];
1718

18-
const isRangeMatch = (a, b) => {
19-
return (a[0] === b[0] && a[1] === b[1]) ||
20-
(a[0] > b[0] && a[0] < b[1]) ||
21-
(a[1] > b[0] && a[1] < b[1]);
22-
};
23-
24-
const computeStatement = (tokens, range) => {
25-
return tokens
26-
.filter(token => isRangeMatch(token.range, range))
27-
.map(token => token.value === undefined ? token.type.label : token.value)
28-
.join('');
29-
};
30-
19+
const computeStatement = node => generate(node, {quotes: 'single'}).code;
3120
const getNode = (ast, path) => dotProp.get(ast, path.replace(/\//g, '.'));
3221

3322
const formatter = context => {
3423
const ast = JSON.parse(context.source.ast);
35-
const tokens = JSON.parse(context.source.tokens);
3624
const args = context.args[0].events;
37-
3825
return args
3926
.map(arg => {
40-
const range = getNode(ast, arg.espath).range;
41-
const statement = computeStatement(tokens, range);
42-
27+
const node = getNode(ast, arg.espath);
28+
const statement = computeStatement(node);
4329
const formatted = concordance.format(arg.value, concordanceOptions);
4430
return [statement, formatted];
4531
})

package-lock.json

+32-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
"auto-bind": "^1.1.0",
108108
"ava-init": "^0.2.0",
109109
"babel-core": "^6.17.0",
110+
"babel-generator": "^6.26.0",
110111
"bluebird": "^3.0.0",
111112
"caching-transform": "^1.0.0",
112113
"chalk": "^2.0.1",

test/api.js

+37
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,43 @@ function generateTests(prefix, apiCreator) {
371371
});
372372
});
373373

374+
test(`${prefix} enhanced assertion formatting necessary whitespace and empty strings`, t => {
375+
const expected = [
376+
[
377+
/foo === '' && '' === foo/,
378+
/foo === ''/,
379+
/foo/
380+
],
381+
[
382+
/new Object\(foo\) instanceof Object/,
383+
/Object/,
384+
/new Object\(foo\)/,
385+
/foo/
386+
],
387+
[
388+
/\[foo].filter\(item => {\n\s+return item === 'bar';\n}\).length > 0/,
389+
/\[foo].filter\(item => {\n\s+return item === 'bar';\n}\).length/,
390+
/\[foo].filter\(item => {\n\s+return item === 'bar';\n}\)/,
391+
/\[foo]/,
392+
/foo/
393+
]
394+
];
395+
396+
t.plan(14);
397+
const api = apiCreator();
398+
return api.run([path.join(__dirname, 'fixture/enhanced-assertion-formatting.js')])
399+
.then(result => {
400+
t.is(result.errors.length, 3);
401+
t.is(result.passCount, 0);
402+
403+
result.errors.forEach((error, errorIndex) => {
404+
error.error.statements.forEach((statement, statementIndex) => {
405+
t.match(statement[0], expected[errorIndex][statementIndex]);
406+
});
407+
});
408+
});
409+
});
410+
374411
test(`${prefix} stack traces for exceptions are corrected using a source map file (cache off)`, t => {
375412
t.plan(4);
376413

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import test from '../..';
2+
3+
const foo = 'foo';
4+
5+
test('fails with multiple empty string expressions and mixed quotes', t => {
6+
// eslint-disable-next-line quotes, yoda
7+
t.true(foo === '' && "" === foo);
8+
});
9+
10+
test('fails with "instanceof" expression', t => {
11+
// eslint-disable-next-line no-new-object
12+
t.false(new Object(foo) instanceof Object);
13+
});
14+
15+
test('fails with multiple lines', t => {
16+
t.true(
17+
[foo].filter(item => {
18+
return item === 'bar';
19+
}).length > 0
20+
);
21+
});

0 commit comments

Comments
 (0)