Unverified Commit 81bbfee2 authored by Renée Kooi's avatar Renée Kooi Committed by GitHub
Browse files

Improve syntax error messages (#480)

parent 73b34cc1
......@@ -8,11 +8,11 @@ var cwd = process.cwd()
module.exports = ttyError
function ttyError (src, sub, err) {
if (!err.filename || !err.loc) return err
var longFilename = err.filename || err.fileName
if (!longFilename || !getErrorLocation(err)) return err
var longFilename = err.filename
var filename = path.relative(cwd, longFilename)
var loc = err.loc
var loc = getErrorLocation(err)
var line = loc.line
var col = loc.column + 1
......@@ -20,7 +20,7 @@ function ttyError (src, sub, err) {
var padLen = lineNum.length
var empty = padLeft('|', padLen + 1)
var arrow = padLeft('--> ', padLen + 4 - 1)
var syntaxError = padLeft('', col) + '^ Syntax Error'
var syntaxError = padLeft('', col) + '^ ' + getErrorMessage(err)
try {
var file = fs.readFileSync(longFilename, 'utf8')
......@@ -65,3 +65,27 @@ function pad (len, char) {
while (res.length < len) res += char
return res
}
function getErrorLocation (err) {
if (err.loc) return err.loc
if (typeof err.line === 'number' && typeof err.column === 'number') {
return {
line: err.line,
column: err.column
}
}
return null
}
function getErrorMessage (err) {
var loc = getErrorLocation(err)
var message = err.message
// strip file names
.replace(/^.*?:|while parsing file:.*?$/g, '')
// strip position in file
.replace('(' + loc.line + ':' + loc.column + ')', '')
// same, but for typescript
.replace('(' + loc.line + ',' + loc.column + ')', '')
return message || 'Syntax Error'
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment