Skip to content

Commit

Permalink
add code excerpts
Browse files Browse the repository at this point in the history
  • Loading branch information
vdemedes committed Dec 12, 2016
1 parent 415cd2f commit 8daf395
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
27 changes: 27 additions & 0 deletions lib/code-excerpt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

var fs = require('fs');
var codeExcerpt = require('code-excerpt');
var repeating = require('repeating');
var chalk = require('chalk');

function formatLineNumber(line, maxLines) {
return repeating('0', String(maxLines).length - String(line).length) + line;
}

module.exports = function (file, line) {
var source = fs.readFileSync(file, 'utf8');
var excerpt = codeExcerpt(source, line, {around: 1});

return excerpt.map(function (item) {
var lineNumber = formatLineNumber(item.line, line) + ': ';
var coloredLineNumber = item.line === line ? lineNumber : chalk.grey(lineNumber);

var result = ' ' + coloredLineNumber + item.value;
return item.line === line ? chalk.bgRed(result) : result;
})
.map(function (line) {
return ' ' + line;
})
.join('\n');
};
10 changes: 3 additions & 7 deletions lib/reporters/mini.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var cliTruncate = require('cli-truncate');
var cross = require('figures').cross;
var repeating = require('repeating');
var objectAssign = require('object-assign');
var codeExcerpt = require('../code-excerpt');
var colors = require('../colors');

chalk.enabled = true;
Expand Down Expand Up @@ -186,14 +187,9 @@ MiniReporter.prototype.finish = function (runStatus) {
description = JSON.stringify(test);
}

if (isPowerAssert) {
description = stripFirstLine(description).replace(/ {3}/g, ' ');
} else {
description.replace(/ {3}/g, ' ');
}

status += '\n\n ' + colors.title(title) + '\n';
status += colors.stack(errorTitle);
status += colors.stack(errorTitle) + '\n';
status += codeExcerpt(test.error.source.file, test.error.source.line) + '\n\n';
status += colors.errorStack(description);
});
}
Expand Down
4 changes: 3 additions & 1 deletion lib/reporters/verbose.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var figures = require('figures');
var chalk = require('chalk');
var plur = require('plur');
var repeating = require('repeating');
var codeExcerpt = require('../code-excerpt');
var colors = require('../colors');

Object.keys(colors).forEach(function (key) {
Expand Down Expand Up @@ -108,7 +109,8 @@ VerboseReporter.prototype.finish = function (runStatus) {

i++;

output += '\n\n\n ' + colors.error(i + '.', test.title) + '\n';
output += '\n\n\n ' + colors.error(i + '.', test.title) + '\n\n';
output += codeExcerpt(test.error.source.file, test.error.source.line) + '\n\n';
var stack = test.error.stack ? test.error.stack.trimRight() : '';
output += ' ' + colors.stack(stack);
});
Expand Down
13 changes: 12 additions & 1 deletion lib/serialize-error.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
const cleanYamlObject = require('clean-yaml-object');
const ErrorStackParser = require('error-stack-parser');
const beautifyStack = require('./beautify-stack');

function filter(propertyName, isRoot, source, target) {
Expand All @@ -15,4 +16,14 @@ function filter(propertyName, isRoot, source, target) {
return true;
}

module.exports = error => cleanYamlObject(error, filter);
module.exports = error => {
const err = cleanYamlObject(error, filter);

const source = ErrorStackParser.parse(error)[1];
err.source = {
file: source.fileName,
line: source.lineNumber
};

return err;
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,14 @@
"cli-spinners": "^0.3.0",
"cli-truncate": "^0.2.0",
"co-with-promise": "^4.6.0",
"code-excerpt": "^1.0.1",
"common-path-prefix": "^1.0.0",
"convert-source-map": "^1.2.0",
"core-assert": "^0.2.0",
"currently-unhandled": "^0.4.1",
"debug": "^2.2.0",
"empower-core": "^0.6.1",
"error-stack-parser": "^1.3.6",
"figures": "^2.0.0",
"find-cache-dir": "^0.1.1",
"fn-name": "^2.0.0",
Expand Down

0 comments on commit 8daf395

Please sign in to comment.