@scaffdog/error
Advanced tools
Comparing version 1.0.0-canary.0 to 1.0.0-canary.1
@@ -6,2 +6,8 @@ # Change Log | ||
# [1.0.0-canary.1](https://github.com/cats-oss/scaffdog/compare/v1.0.0-canary.0...v1.0.0-canary.1) (2021-01-12) | ||
### Features | ||
- improve error message ([4b66723](https://github.com/cats-oss/scaffdog/commit/4b667232ecd6cef7e33a145e7d0f35e8afb2fb81)) | ||
# [1.0.0-canary.0](https://github.com/cats-oss/scaffdog/compare/v0.3.0...v1.0.0-canary.0) (2021-01-11) | ||
@@ -8,0 +14,0 @@ |
@@ -1,9 +0,5 @@ | ||
export declare type ErrorLoc = { | ||
line: number; | ||
column: number; | ||
}; | ||
import type { SourceLocation } from '@scaffdog/types'; | ||
export declare type ScaffdogErrorOptions = { | ||
source: string; | ||
start: ErrorLoc; | ||
end: ErrorLoc; | ||
loc: SourceLocation; | ||
color: boolean; | ||
@@ -10,0 +6,0 @@ }; |
@@ -16,7 +16,6 @@ "use strict"; | ||
}; | ||
if (opts.source && opts.start && opts.end) { | ||
if (opts.source && opts.loc) { | ||
this.message = this._format(message, { | ||
source: opts.source, | ||
start: opts.start, | ||
end: opts.end, | ||
loc: opts.loc, | ||
color: !!opts.color, | ||
@@ -45,15 +44,16 @@ }); | ||
line.substring(e); | ||
const base = Math.max(options.start.line - 3, 0); | ||
const base = Math.max(options.loc.start.line - 3, 0); | ||
const lines = options.source.split(NEWLINE); | ||
lines.splice(0, base); | ||
lines.splice(options.end.line - options.start.line + 5); | ||
lines.splice(options.loc.end.line - options.loc.start.line + 5); | ||
const start = { | ||
line: options.start.line - 1 - base, | ||
column: options.start.column, | ||
line: options.loc.start.line - 1 - base, | ||
column: options.loc.start.column, | ||
}; | ||
const end = { | ||
line: options.end.line - 1 - base, | ||
column: options.end.column, | ||
line: options.loc.end.line - 1 - base, | ||
column: options.loc.end.column, | ||
}; | ||
const output = []; | ||
const output = [` ${red(`${message}:`)}`, '']; | ||
const gutter = chk.gray('│'); | ||
lines.forEach((line, index) => { | ||
@@ -67,9 +67,11 @@ if (start.line <= index && index <= end.line) { | ||
}; | ||
output.push(emphasis(line, col.start, col.end), space(col.start - 1) + mark(col.end - col.start + 1)); | ||
output.push(` ${red('>')} ${gutter} ` + emphasis(line, col.start, col.end), ` ${gutter} ` + | ||
space(col.start - 1) + | ||
mark(col.end - col.start + 1)); | ||
} | ||
else { | ||
output.push(line); | ||
output.push(` ${gutter} ${line}`); | ||
} | ||
}); | ||
return [`${red(message)}:`, output.join('\n')].join('\n\n'); | ||
return output.join('\n'); | ||
} | ||
@@ -76,0 +78,0 @@ } |
{ | ||
"name": "@scaffdog/error", | ||
"version": "1.0.0-canary.0", | ||
"version": "1.0.0-canary.1", | ||
"description": "A utility module of scaffdog error.", | ||
@@ -34,6 +34,9 @@ "keywords": [ | ||
}, | ||
"devDependencies": { | ||
"@scaffdog/types": "1.0.0-canary.1" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"gitHead": "87ed8ec940b3cff7c1a5eedb49271e96b56c8978" | ||
"gitHead": "b7221e4a15338cd73ba8c963edf32339b915b255" | ||
} |
6233
1
90