-
-
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.
chore: align warning and error objects, add frame property
This aligns warning and error objects to contain the same properties and have their toString methods return the same shape. It's implemented by warnings becoming class objects, too, and sharing the same base class with errors. It also adds back the `frame` property that got lost in the Svelte 4->5 transition. The only difference to Svelte 4 now is a slightly adjusted toString property (which is consistent between warnings and errors now) and a `position` property that contains a tuple of start/end offsets instead of a `pos` property only containing the start offset closes #12151
- Loading branch information
1 parent
ae3bf9e
commit a4b5d50
Showing
9 changed files
with
182 additions
and
186 deletions.
There are no files selected for viewing
32 changes: 3 additions & 29 deletions
32
packages/svelte/scripts/process-messages/templates/compile-errors.js
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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** @import { Location } from 'locate-character' */ | ||
import * as state from '../state.js'; | ||
|
||
const regex_tabs = /^\t+/; | ||
|
||
/** | ||
* @param {string} str | ||
*/ | ||
function tabs_to_spaces(str) { | ||
return str.replace(regex_tabs, (match) => match.split('\t').join(' ')); | ||
} | ||
|
||
/** | ||
* @param {string} source | ||
* @param {number} line | ||
* @param {number} column | ||
*/ | ||
function get_code_frame(source, line, column) { | ||
const lines = source.split('\n'); | ||
const frame_start = Math.max(0, line - 2); | ||
const frame_end = Math.min(line + 3, lines.length); | ||
const digits = String(frame_end + 1).length; | ||
return lines | ||
.slice(frame_start, frame_end) | ||
.map((str, i) => { | ||
const is_error_line = frame_start + i === line; | ||
const line_num = String(i + frame_start + 1).padStart(digits, ' '); | ||
if (is_error_line) { | ||
const indicator = | ||
' '.repeat(digits + 2 + tabs_to_spaces(str.slice(0, column)).length) + '^'; | ||
return `${line_num}: ${tabs_to_spaces(str)}\n${indicator}`; | ||
} | ||
return `${line_num}: ${tabs_to_spaces(str)}`; | ||
}) | ||
.join('\n'); | ||
} | ||
|
||
export class CompileDiagnostic extends Error { | ||
name = 'CompileDiagnostic'; | ||
filename = state.filename; | ||
/** @type {[number, number] | undefined} */ | ||
position = undefined; | ||
/** @type {Location | undefined} */ | ||
start = undefined; | ||
/** @type {Location | undefined} */ | ||
end = undefined; | ||
/** @type {string | undefined} */ | ||
frame = undefined; | ||
|
||
/** | ||
* @param {string} code | ||
* @param {string} message | ||
* @param {[number, number] | undefined} position | ||
*/ | ||
constructor(code, message, position) { | ||
super(message); | ||
this.code = code; | ||
this.position = position; | ||
|
||
if (position) { | ||
this.start = state.locator(position[0]); | ||
this.end = state.locator(position[1]); | ||
if (this.start && this.end) { | ||
this.frame = get_code_frame(state.source, this.start.line - 1, this.end.column); | ||
} | ||
} | ||
} | ||
|
||
toString() { | ||
let out = `${this.code}: ${this.message}`; | ||
|
||
if (this.filename) { | ||
out += `\n${this.filename}`; | ||
|
||
if (this.start) { | ||
out += `:${this.start.line}:${this.start.column}`; | ||
} | ||
} | ||
|
||
if (this.frame) { | ||
out += `\n${this.frame}`; | ||
} | ||
|
||
return out; | ||
} | ||
|
||
toJSON() { | ||
return { | ||
code: this.code, | ||
message: this.message, | ||
filename: this.filename, | ||
start: this.start, | ||
end: this.end, | ||
position: this.position, | ||
frame: this.frame | ||
}; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.