Skip to content

Commit

Permalink
feat: highlight error code in terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Nov 13, 2017
1 parent a6f2ce7 commit 0994dba
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ Available log levels are: `ERROR`, `WARN`, `INFO`.

## License

MIT, see [LICENSE.md](./LICENSE).
MIT, see [LICENSE](./LICENSE).
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"dependencies": {
"chalk": "^2.3.0",
"strip-ansi": "4.0.0",
"utf8-bar": "0.1.0"
"utf8-bar": "0.1.0",
"@babel/code-frame": "7.0.0-beta.32"
}
}
35 changes: 35 additions & 0 deletions src/LineWriter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable complexity, no-use-extend-native/no-use-extend-native */
const path = require('path');
const fs = require('fs');
const chalk = require('chalk');
const {codeFrameColumns} = require('@babel/code-frame');
const progressBar = require('./progressBar');

const REG_TRACE_LINE = /\s*(.+)\((.+):([0-9]+):([0-9]+)\)$/;
Expand Down Expand Up @@ -49,6 +51,24 @@ const formatStatsBar = (percent, hasErrors) => {
return chalk`{${textStyles} ${percentFormatted}} ${bar}`;
};

const formatCodeFrame = (filePath, line, column) => {
try {
const source = fs.readFileSync(filePath, 'utf8');
const location = {
start: {
column,
line
}
};

return codeFrameColumns(source, location, {
highlightCode: true
});
} catch (error) {
return '';
}
};

class LineWriter {
constructor (logger, root) {
this.counter = 0;
Expand All @@ -71,6 +91,14 @@ class LineWriter {
this.logger.info(formatComment(line));
}

commentBlock (str) {
const lines = str.split('\n');

for (const line of lines) {
this.comment(line);
}
}

start (numSuites) {
this.blank();
this.blank();
Expand Down Expand Up @@ -192,6 +220,7 @@ class LineWriter {
};
const pushTraceLine = (line) => push(chalk` {grey ${line}}`);
const pushTraceLineDim = (line) => pushTraceLine(chalk`{dim ${line}}`);
const pushCodeFrameLine = (line) => push(' ' + line);

let firstLineFormatted = firstLine;

Expand Down Expand Up @@ -234,6 +263,12 @@ class LineWriter {
pushTraceLineDim(formatFailureMessageTraceLine(description, relativeFilePath, row, column));
} else {
pushTraceLine(formatFailureMessageTraceLine(description, relativeFilePath, row, column));

const codeFrame = formatCodeFrame(file, row, column);

push('');
codeFrame.split('\n').forEach((codeFrameLine) => pushCodeFrameLine(codeFrameLine));
push('');
}
} else {
pushTraceLine(line);
Expand Down
6 changes: 6 additions & 0 deletions test/__snapshots__/LineWriter.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ exports[`LineWriter .errors() format stack trace 1`] = `
# Stack:
#
# at Something (../foo/bar.js:10:10)
#
#
#
# at Foobar (../foo/bar2.js:20:20)
#
#
#
# "
`;

Expand Down

0 comments on commit 0994dba

Please sign in to comment.