-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
vdemedes
committed
Jan 5, 2017
1 parent
314f7a0
commit 8a4c3ca
Showing
13 changed files
with
410 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const equalLength = require('equal-length'); | ||
const codeExcerpt = require('code-excerpt'); | ||
const truncate = require('cli-truncate'); | ||
const chalk = require('chalk'); | ||
|
||
function formatLineNumber(line, maxLines) { | ||
return ' '.repeat(String(maxLines).length - String(line).length) + line; | ||
} | ||
|
||
module.exports = (file, line, options) => { | ||
const maxWidth = (options || {}).maxWidth || 80; | ||
const indent = (options || {}).indent || 0; | ||
const source = fs.readFileSync(file, 'utf8'); | ||
const excerpt = codeExcerpt(source, line, {around: 1}); | ||
|
||
const lines = excerpt.map(item => ({ | ||
line: item.line, | ||
value: truncate(item.value, maxWidth - String(line).length - 1) | ||
})); | ||
|
||
const joinedLines = lines.map(line => line.value).join('\n'); | ||
const extendedLines = equalLength(joinedLines).split('\n'); | ||
|
||
return lines | ||
.map((item, index) => ({ | ||
line: item.line, | ||
value: extendedLines[index] | ||
})) | ||
.map(item => { | ||
const isErrorSource = item.line === line; | ||
|
||
const lineNumber = formatLineNumber(item.line, line) + ':'; | ||
const coloredLineNumber = isErrorSource ? lineNumber : chalk.grey(lineNumber); | ||
const result = ` ${coloredLineNumber} ${item.value}`; | ||
|
||
return isErrorSource ? chalk.bgRed(result) : result; | ||
}) | ||
.map(line => ' '.repeat(indent) + line) | ||
.join('\n'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use strict'; | ||
|
||
const indentString = require('indent-string'); | ||
const chalk = require('chalk'); | ||
const diff = require('diff'); | ||
|
||
function cleanUp(line) { | ||
if (line[0] === '+') { | ||
return `${chalk.green('+')} ${line.slice(1)}`; | ||
} | ||
|
||
if (line[0] === '-') { | ||
return `${chalk.red('-')} ${line.slice(1)}`; | ||
} | ||
|
||
if (line.match(/@@/)) { | ||
return null; | ||
} | ||
|
||
if (line.match(/\\ No newline/)) { | ||
return null; | ||
} | ||
|
||
return ` ${line}`; | ||
} | ||
|
||
module.exports = err => { | ||
if (err.statements) { | ||
const statements = JSON.parse(err.statements); | ||
|
||
return statements | ||
.map(statement => `${statement[0]}\n${chalk.grey('=>')} ${statement[1]}`) | ||
.join('\n\n') + '\n'; | ||
} | ||
|
||
if ((err.actualType === 'object' || err.actualType === 'array') && err.actualType === err.expectedType) { | ||
const patch = diff.createPatch('string', err.actual, err.expected); | ||
const msg = patch | ||
.split('\n') | ||
.splice(4) | ||
.map(cleanUp) | ||
.filter(Boolean) | ||
.join('\n'); | ||
|
||
return `Difference:\n\n${msg}`; | ||
} | ||
|
||
if (err.actualType === 'string' && err.expectedType === 'string') { | ||
const patch = diff.diffChars(err.actual, err.expected); | ||
const msg = patch | ||
.map(part => { | ||
if (part.added) { | ||
return chalk.black.bgGreen(part.value); | ||
} | ||
|
||
if (part.removed) { | ||
return chalk.black.bgRed(part.value); | ||
} | ||
|
||
return part.value; | ||
}) | ||
.join(''); | ||
|
||
return `Difference:\n\n${msg}\n`; | ||
} | ||
|
||
return [ | ||
'Actual:\n', | ||
`${indentString(err.actual, 2)}\n`, | ||
'Expected:\n', | ||
`${indentString(err.expected, 2)}\n` | ||
].join('\n'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.