Skip to content

chore: align warning and error objects, add frame property #12326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/small-owls-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

chore: align warning and error objects, add frame property
Original file line number Diff line number Diff line change
@@ -1,54 +1,17 @@
/** @import { Location } from 'locate-character' */
import * as state from './state.js';
import { CompileDiagnostic } from './utils/compile_diagnostic.js';

/** @typedef {{ start?: number, end?: number }} NodeLike */

export class InternalCompileError extends Error {
export class InternalCompileError extends CompileDiagnostic {
name = 'CompileError';

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

side note: I would've called this "CompilerError" (note the additional r). Is "compile error" more correct? I stumble over that mentally each time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error is comming from the compiler, so I would call it a CompilerError. Not meaning there is something is wrong with the compiler, but the an error has been raised from compiler. thoughts?

filename = state.filename;

/** @type {[number, number] | undefined} */
position = undefined;

/** @type {Location | undefined} */
start = undefined;

/** @type {Location | undefined} */
end = 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]);
}
}

toString() {
let out = `${this.name}: ${this.message}`;

out += `\n(${this.code})`;

if (this.filename) {
out += `\n${this.filename}`;

if (this.start) {
out += `${this.start.line}:${this.start.column}`;
}
}

return out;
super(code, message, position);
}
}

Expand All @@ -65,7 +28,7 @@ function e(node, code, message) {
throw new InternalCompileError(
code,
message,
start !== undefined && end !== undefined ? [start, end] : undefined
start !== undefined ? [start, end ?? start] : undefined
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import { filename, locator, warnings, ignore_stack, ignore_map } from './state.js';
import { warnings, ignore_stack, ignore_map } from './state.js';
import { CompileDiagnostic } from './utils/compile_diagnostic.js';

/** @typedef {{ start?: number, end?: number }} NodeLike */

export class InternalCompileWarning extends CompileDiagnostic {
name = 'CompileWarning';

/**
* @param {string} code
* @param {string} message
* @param {[number, number] | undefined} position
*/
constructor(code, message, position) {
super(code, message, position);
}
}

/**
* @param {null | NodeLike} node
* @param {string} code
Expand All @@ -14,13 +28,13 @@ function w(node, code, message) {
}
if (stack && stack.at(-1)?.has(code)) return;

warnings.push({
code,
message,
filename,
start: node?.start !== undefined ? locator(node.start) : undefined,
end: node?.end !== undefined ? locator(node.end) : undefined
});
warnings.push(
new InternalCompileWarning(
code,
message,
node && node.start !== undefined ? [node.start, node.end ?? node.start] : undefined
)
);
}

export const codes = CODES;
Expand Down
40 changes: 4 additions & 36 deletions packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,18 @@
/* This file is generated by scripts/process-messages/index.js. Do not edit! */

/** @import { Location } from 'locate-character' */
import * as state from './state.js';
import { CompileDiagnostic } from './utils/compile_diagnostic.js';

/** @typedef {{ start?: number, end?: number }} NodeLike */
export class InternalCompileError extends Error {
export class InternalCompileError extends CompileDiagnostic {
name = 'CompileError';
filename = state.filename;
/** @type {[number, number] | undefined} */
position = undefined;
/** @type {Location | undefined} */
start = undefined;
/** @type {Location | undefined} */
end = 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]);
}
}

toString() {
let out = `${this.name}: ${this.message}`;

out += `\n(${this.code})`;

if (this.filename) {
out += `\n${this.filename}`;

if (this.start) {
out += `${this.start.line}:${this.start.column}`;
}
}

return out;
super(code, message, position);
}
}

Expand All @@ -58,7 +26,7 @@ function e(node, code, message) {
const start = typeof node === 'number' ? node : node?.start;
const end = typeof node === 'number' ? node : node?.end;

throw new InternalCompileError(code, message, start !== undefined && end !== undefined ? [start, end] : undefined);
throw new InternalCompileError(code, message, start !== undefined ? [start, end ?? start] : undefined);
}

/**
Expand Down
11 changes: 9 additions & 2 deletions packages/svelte/src/compiler/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ export let warnings = [];
*/
export let filename;

/**
* The original source code
* @type {string}
*/
export let source;

export let locator = getLocator('', { offsetLine: 1 });

/**
Expand Down Expand Up @@ -43,10 +49,11 @@ export function pop_ignore() {
}

/**
* @param {string} source
* @param {string} _source
* @param {{ filename?: string, rootDir?: string }} options
*/
export function reset(source, options) {
export function reset(_source, options) {
source = _source;
const root_dir = options.rootDir?.replace(/\\/g, '/');
filename = options.filename?.replace(/\\/g, '/');

Expand Down
14 changes: 3 additions & 11 deletions packages/svelte/src/compiler/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import type {
Identifier,
ImportDeclaration
} from 'estree';
import type { Location } from 'locate-character';
import type { SourceMap } from 'magic-string';
import type { Context } from 'zimmerframe';
import type { Scope } from '../phases/scope.js';
import type { Css } from './css.js';
import type { EachBlock, Namespace, SvelteNode, SvelteOptions } from './template.js';
import type { InternalCompileError } from '../errors.js';
import type { ICompileDiagnostic } from '../utils/compile_diagnostic.js';

/** The return value of `compile` from `svelte/compiler` */
export interface CompileResult {
Expand Down Expand Up @@ -51,16 +50,9 @@ export interface CompileResult {
ast: any;
}

export interface Warning {
start?: Location;
end?: Location;
// TODO there was pos: number in Svelte 4 - do we want to add it back?
code: string;
message: string;
filename?: string;
}
export interface Warning extends ICompileDiagnostic {}

export interface CompileError extends InternalCompileError {}
export interface CompileError extends ICompileDiagnostic {}

export type CssHashGetter = (args: {
name: string;
Expand Down
105 changes: 105 additions & 0 deletions packages/svelte/src/compiler/utils/compile_diagnostic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/** @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');
}

/**
* @typedef {{
* code: string;
* message: string;
* filename?: string;
* start?: Location;
* end?: Location;
* position?: [number, number];
* frame?: string;
* }} ICompileDiagnostic */

/** @implements {ICompileDiagnostic} */
export class CompileDiagnostic extends Error {
name = 'CompileDiagnostic';

/**
* @param {string} code
* @param {string} message
* @param {[number, number] | undefined} position
*/
constructor(code, message, position) {
super(message);
this.code = code;

if (state.filename) {
this.filename = state.filename;
}

if (position) {
this.position = 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
};
}
}
31 changes: 16 additions & 15 deletions packages/svelte/src/compiler/warnings.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
/* This file is generated by scripts/process-messages/index.js. Do not edit! */

import {
filename,
locator,
warnings,
ignore_stack,
ignore_map
} from './state.js';
import { warnings, ignore_stack, ignore_map } from './state.js';
import { CompileDiagnostic } from './utils/compile_diagnostic.js';

/** @typedef {{ start?: number, end?: number }} NodeLike */
export class InternalCompileWarning extends CompileDiagnostic {
name = 'CompileWarning';

/**
* @param {string} code
* @param {string} message
* @param {[number, number] | undefined} position
*/
constructor(code, message, position) {
super(code, message, position);
}
}

/**
* @param {null | NodeLike} node
* @param {string} code
Expand All @@ -22,14 +30,7 @@ function w(node, code, message) {
}

if (stack && stack.at(-1)?.has(code)) return;

warnings.push({
code,
message,
filename,
start: node?.start !== undefined ? locator(node.start) : undefined,
end: node?.end !== undefined ? locator(node.end) : undefined
});
warnings.push(new InternalCompileWarning(code, message, node && node.start !== undefined ? [node.start, node.end ?? node.start] : undefined));
}

export const codes = [
Expand Down
Loading
Loading