From bcf23cafcec9776af28c58bcb2a399e422b57b77 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 1 Jul 2024 19:05:03 -0700 Subject: [PATCH] fix: expose `CompileError` interface, not class (#12255) * chore: refactor CompileError stuff * changeset --- .changeset/good-rice-tap.md | 5 +++ .../templates/compile-errors.js | 13 ++++--- packages/svelte/src/compiler/errors.js | 13 ++++--- packages/svelte/src/compiler/index.js | 1 - packages/svelte/src/compiler/public.d.ts | 8 ++++- packages/svelte/src/compiler/types/index.d.ts | 9 ++--- packages/svelte/types/index.d.ts | 34 ++++++++----------- 7 files changed, 40 insertions(+), 43 deletions(-) create mode 100644 .changeset/good-rice-tap.md diff --git a/.changeset/good-rice-tap.md b/.changeset/good-rice-tap.md new file mode 100644 index 0000000000..73706bb988 --- /dev/null +++ b/.changeset/good-rice-tap.md @@ -0,0 +1,5 @@ +--- +"svelte": patch +--- + +breaking: expose `CompileError` interface, not class diff --git a/packages/svelte/scripts/process-messages/templates/compile-errors.js b/packages/svelte/scripts/process-messages/templates/compile-errors.js index 770d622424..e129eef9f6 100644 --- a/packages/svelte/scripts/process-messages/templates/compile-errors.js +++ b/packages/svelte/scripts/process-messages/templates/compile-errors.js @@ -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 diff --git a/packages/svelte/src/compiler/errors.js b/packages/svelte/src/compiler/errors.js index 1a60f99000..c26586fb1c 100644 --- a/packages/svelte/src/compiler/errors.js +++ b/packages/svelte/src/compiler/errors.js @@ -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); } /** diff --git a/packages/svelte/src/compiler/index.js b/packages/svelte/src/compiler/index.js index c6fde527de..6983fa2424 100644 --- a/packages/svelte/src/compiler/index.js +++ b/packages/svelte/src/compiler/index.js @@ -139,6 +139,5 @@ export function walk() { ); } -export { CompileError } from './errors.js'; export { VERSION } from '../version.js'; export { migrate } from './migrate/index.js'; diff --git a/packages/svelte/src/compiler/public.d.ts b/packages/svelte/src/compiler/public.d.ts index 53742324e9..b7bd392001 100644 --- a/packages/svelte/src/compiler/public.d.ts +++ b/packages/svelte/src/compiler/public.d.ts @@ -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'; diff --git a/packages/svelte/src/compiler/types/index.d.ts b/packages/svelte/src/compiler/types/index.d.ts index 79cc829d6b..129b5da175 100644 --- a/packages/svelte/src/compiler/types/index.d.ts +++ b/packages/svelte/src/compiler/types/index.d.ts @@ -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; diff --git a/packages/svelte/types/index.d.ts b/packages/svelte/types/index.d.ts index 98159374b2..e6ad51eaf1 100644 --- a/packages/svelte/types/index.d.ts +++ b/packages/svelte/types/index.d.ts @@ -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; - 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' {