fix: expose `CompileError` interface, not class (#12255)

* chore: refactor CompileError stuff

* changeset
pull/12262/head
Rich Harris 6 months ago committed by GitHub
parent 42db9f8c65
commit bcf23cafce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
breaking: expose `CompileError` interface, not class

@ -1,21 +1,20 @@
/** @import { Location } from 'locate-character' */
import * as state from './state.js';
/** @typedef {{ start?: number, end?: number }} NodeLike */
// interface is duplicated between here (used internally) and ./interfaces.js
// (exposed publicly), and I'm not sure how to avoid that
export class CompileError extends Error {
export class InternalCompileError extends Error {
name = 'CompileError';
filename = state.filename;
/** @type {import('#compiler').CompileError['position']} */
/** @type {[number, number] | undefined} */
position = undefined;
/** @type {import('#compiler').CompileError['start']} */
/** @type {Location | undefined} */
start = undefined;
/** @type {import('#compiler').CompileError['end']} */
/** @type {Location | undefined} */
end = undefined;
/**
@ -63,7 +62,7 @@ function e(node, code, message) {
const start = typeof node === 'number' ? node : node?.start;
const end = typeof node === 'number' ? node : node?.end;
throw new CompileError(
throw new InternalCompileError(
code,
message,
start !== undefined && end !== undefined ? [start, end] : undefined

@ -1,18 +1,17 @@
/* This file is generated by scripts/process-messages/index.js. Do not edit! */
/** @import { Location } from 'locate-character' */
import * as state from './state.js';
/** @typedef {{ start?: number, end?: number }} NodeLike */
// interface is duplicated between here (used internally) and ./interfaces.js
// (exposed publicly), and I'm not sure how to avoid that
export class CompileError extends Error {
export class InternalCompileError extends Error {
name = 'CompileError';
filename = state.filename;
/** @type {import('#compiler').CompileError['position']} */
/** @type {[number, number] | undefined} */
position = undefined;
/** @type {import('#compiler').CompileError['start']} */
/** @type {Location | undefined} */
start = undefined;
/** @type {import('#compiler').CompileError['end']} */
/** @type {Location | undefined} */
end = undefined;
/**
@ -59,7 +58,7 @@ function e(node, code, message) {
const start = typeof node === 'number' ? node : node?.start;
const end = typeof node === 'number' ? node : node?.end;
throw new CompileError(code, message, start !== undefined && end !== undefined ? [start, end] : undefined);
throw new InternalCompileError(code, message, start !== undefined && end !== undefined ? [start, end] : undefined);
}
/**

@ -139,6 +139,5 @@ export function walk() {
);
}
export { CompileError } from './errors.js';
export { VERSION } from '../version.js';
export { migrate } from './migrate/index.js';

@ -5,4 +5,10 @@ export type {
PreprocessorGroup,
Processed
} from './preprocess/public';
export type { CompileOptions, ModuleCompileOptions, CompileResult, Warning } from './types/index';
export type {
CompileError,
CompileOptions,
ModuleCompileOptions,
CompileResult,
Warning
} from './types/index';

@ -12,6 +12,7 @@ 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';
/** The return value of `compile` from `svelte/compiler` */
export interface CompileResult {
@ -59,13 +60,7 @@ export interface Warning {
filename?: string;
}
export interface CompileError extends Error {
code: string;
filename?: string;
position?: [number, number];
start?: Location;
end?: Location;
}
export interface CompileError extends InternalCompileError {}
export type CssHashGetter = (args: {
name: string;

@ -725,13 +725,7 @@ declare module 'svelte/compiler' {
filename?: string;
}
interface CompileError_1 extends Error {
code: string;
filename?: string;
position?: [number, number];
start?: Location;
end?: Location;
}
interface CompileError extends InternalCompileError {}
type CssHashGetter = (args: {
name: string;
@ -1206,18 +1200,6 @@ declare module 'svelte/compiler' {
function preprocess(source: string, preprocessor: PreprocessorGroup | PreprocessorGroup[], options?: {
filename?: string;
} | undefined): Promise<Processed>;
class CompileError extends Error {
constructor(code: string, message: string, position: [number, number] | undefined);
filename: string | undefined;
position: CompileError_1["position"];
start: CompileError_1["start"];
end: CompileError_1["end"];
code: string;
}
/**
* The current version, as set in package.json.
*
@ -1902,8 +1884,20 @@ declare module 'svelte/compiler' {
content: Program;
attributes: Attribute[];
}
class InternalCompileError extends Error {
constructor(code: string, message: string, position: [number, number] | undefined);
filename: string | undefined;
position: [number, number] | undefined;
start: Location | undefined;
end: Location | undefined;
code: string;
}
export { MarkupPreprocessor, Preprocessor, PreprocessorGroup, Processed, CompileOptions, ModuleCompileOptions, CompileResult, Warning, compile, compileModule, parse, walk, preprocess, CompileError, VERSION, migrate };
export { MarkupPreprocessor, Preprocessor, PreprocessorGroup, Processed, CompileError, CompileOptions, ModuleCompileOptions, CompileResult, Warning, compile, compileModule, parse, walk, preprocess, VERSION, migrate };
}
declare module 'svelte/easing' {

Loading…
Cancel
Save