-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1296 from sveltejs/fix-errors
fix CompileError constructor
- Loading branch information
Showing
4 changed files
with
52 additions
and
63 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 was deleted.
Oops, something went wrong.
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,37 @@ | ||
import { locate } from 'locate-character'; | ||
import getCodeFrame from '../utils/getCodeFrame'; | ||
|
||
class CompileError extends Error { | ||
loc: { line: number, column: number }; | ||
end: { line: number, column: number }; | ||
pos: number; | ||
filename: string; | ||
frame: string; | ||
|
||
toString() { | ||
return `${this.message} (${this.loc.line}:${this.loc.column})\n${this.frame}`; | ||
} | ||
} | ||
|
||
export default function error(message: string, props: { | ||
name: string, | ||
source: string, | ||
filename: string, | ||
start: number, | ||
end?: number | ||
}) { | ||
const error = new CompileError(message); | ||
error.name = props.name; | ||
|
||
const start = locate(props.source, props.start); | ||
const end = locate(props.source, props.end || props.start); | ||
|
||
error.loc = { line: start.line + 1, column: start.column }; | ||
error.end = { line: end.line + 1, column: end.column }; | ||
error.pos = props.start; | ||
error.filename = props.filename; | ||
|
||
error.frame = getCodeFrame(props.source, start.line, start.column); | ||
|
||
throw error; | ||
} |
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