-
-
Notifications
You must be signed in to change notification settings - Fork 119
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
Showing
8 changed files
with
199 additions
and
189 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
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 |
---|---|---|
@@ -1,60 +1,63 @@ | ||
// import { Type } from './constants.js' | ||
// interface LinePos { line: number; col: number } | ||
import type { LineCounter } from './parse/line-counter' | ||
|
||
export class YAMLError extends Error { | ||
name: 'YAMLParseError' | 'YAMLWarning' | ||
message: string | ||
offset?: number | ||
offset: number | ||
linePos?: { line: number; col: number } | ||
|
||
// nodeType?: Type | ||
// range?: CST.Range | ||
// linePos?: { start: LinePos; end: LinePos } | ||
|
||
constructor(name: YAMLError['name'], offset: number | null, message: string) { | ||
constructor(name: YAMLError['name'], offset: number, message: string) { | ||
if (!message) throw new Error(`Invalid arguments for new ${name}`) | ||
super() | ||
this.name = name | ||
this.message = message | ||
if (typeof offset === 'number') this.offset = offset | ||
} | ||
|
||
/** | ||
* Drops `source` and adds `nodeType`, `range` and `linePos`, as well as | ||
* adding details to `message`. Run automatically for document errors if | ||
* the `prettyErrors` option is set. | ||
*/ | ||
makePretty() { | ||
// this.nodeType = this.source.type | ||
// const cst = this.source.context && this.source.context.root | ||
// if (typeof this.offset === 'number') { | ||
// this.range = new Range(this.offset, this.offset + 1) | ||
// const start = cst && getLinePos(this.offset, cst) | ||
// if (start) { | ||
// const end = { line: start.line, col: start.col + 1 } | ||
// this.linePos = { start, end } | ||
// } | ||
// delete this.offset | ||
// } else { | ||
// this.range = this.source.range | ||
// this.linePos = this.source.rangeAsLinePos | ||
// } | ||
// if (this.linePos) { | ||
// const { line, col } = this.linePos.start | ||
// this.message += ` at line ${line}, column ${col}` | ||
// const ctx = cst && getPrettyContext(this.linePos, cst) | ||
// if (ctx) this.message += `:\n\n${ctx}\n` | ||
// } | ||
this.offset = offset | ||
} | ||
} | ||
|
||
export class YAMLParseError extends YAMLError { | ||
constructor(offset: number | null, message: string) { | ||
constructor(offset: number, message: string) { | ||
super('YAMLParseError', offset, message) | ||
} | ||
} | ||
|
||
export class YAMLWarning extends YAMLError { | ||
constructor(offset: number | null, message: string) { | ||
constructor(offset: number, message: string) { | ||
super('YAMLWarning', offset, message) | ||
} | ||
} | ||
|
||
export const prettifyError = (src: string, lc: LineCounter) => ( | ||
error: YAMLError | ||
) => { | ||
if (error.offset === -1) return | ||
error.linePos = lc.linePos(error.offset) | ||
const { line, col } = error.linePos | ||
error.message += ` at line ${line}, column ${col}` | ||
|
||
let ci = col - 1 | ||
let lineStr = src | ||
.substring(lc.lineStarts[line - 1], lc.lineStarts[line]) | ||
.replace(/[\n\r]+$/, '') | ||
|
||
// Trim to max 80 chars, keeping col position near the middle | ||
if (ci >= 60 && lineStr.length > 80) { | ||
const trimStart = Math.min(ci - 39, lineStr.length - 79) | ||
lineStr = '…' + lineStr.substring(trimStart) | ||
ci -= trimStart - 1 | ||
} | ||
if (lineStr.length > 80) lineStr = lineStr.substring(0, 79) + '…' | ||
|
||
// Include previous line in context if pointing at line start | ||
if (line > 1 && /^ *$/.test(lineStr.substring(0, ci))) { | ||
// Regexp won't match if start is trimmed | ||
let prev = src.substring(lc.lineStarts[line - 2], lc.lineStarts[line - 1]) | ||
if (prev.length > 80) prev = prev.substring(0, 79) + '…\n' | ||
lineStr = prev + lineStr | ||
} | ||
|
||
if (/[^ ]/.test(lineStr)) { | ||
const pointer = ' '.repeat(ci) + '^' | ||
error.message += `:\n\n${lineStr}\n${pointer}\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.