Skip to content

Commit bcf23ca

Browse files
authored
fix: expose CompileError interface, not class (#12255)
* chore: refactor CompileError stuff * changeset
1 parent 42db9f8 commit bcf23ca

File tree

7 files changed

+40
-43
lines changed

7 files changed

+40
-43
lines changed

.changeset/good-rice-tap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
breaking: expose `CompileError` interface, not class

packages/svelte/scripts/process-messages/templates/compile-errors.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1+
/** @import { Location } from 'locate-character' */
12
import * as state from './state.js';
23

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

5-
// interface is duplicated between here (used internally) and ./interfaces.js
6-
// (exposed publicly), and I'm not sure how to avoid that
7-
export class CompileError extends Error {
6+
export class InternalCompileError extends Error {
87
name = 'CompileError';
98

109
filename = state.filename;
1110

12-
/** @type {import('#compiler').CompileError['position']} */
11+
/** @type {[number, number] | undefined} */
1312
position = undefined;
1413

15-
/** @type {import('#compiler').CompileError['start']} */
14+
/** @type {Location | undefined} */
1615
start = undefined;
1716

18-
/** @type {import('#compiler').CompileError['end']} */
17+
/** @type {Location | undefined} */
1918
end = undefined;
2019

2120
/**
@@ -63,7 +62,7 @@ function e(node, code, message) {
6362
const start = typeof node === 'number' ? node : node?.start;
6463
const end = typeof node === 'number' ? node : node?.end;
6564

66-
throw new CompileError(
65+
throw new InternalCompileError(
6766
code,
6867
message,
6968
start !== undefined && end !== undefined ? [start, end] : undefined

packages/svelte/src/compiler/errors.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
/* This file is generated by scripts/process-messages/index.js. Do not edit! */
22

3+
/** @import { Location } from 'locate-character' */
34
import * as state from './state.js';
45

56
/** @typedef {{ start?: number, end?: number }} NodeLike */
6-
// interface is duplicated between here (used internally) and ./interfaces.js
7-
// (exposed publicly), and I'm not sure how to avoid that
8-
export class CompileError extends Error {
7+
export class InternalCompileError extends Error {
98
name = 'CompileError';
109
filename = state.filename;
11-
/** @type {import('#compiler').CompileError['position']} */
10+
/** @type {[number, number] | undefined} */
1211
position = undefined;
13-
/** @type {import('#compiler').CompileError['start']} */
12+
/** @type {Location | undefined} */
1413
start = undefined;
15-
/** @type {import('#compiler').CompileError['end']} */
14+
/** @type {Location | undefined} */
1615
end = undefined;
1716

1817
/**
@@ -59,7 +58,7 @@ function e(node, code, message) {
5958
const start = typeof node === 'number' ? node : node?.start;
6059
const end = typeof node === 'number' ? node : node?.end;
6160

62-
throw new CompileError(code, message, start !== undefined && end !== undefined ? [start, end] : undefined);
61+
throw new InternalCompileError(code, message, start !== undefined && end !== undefined ? [start, end] : undefined);
6362
}
6463

6564
/**

packages/svelte/src/compiler/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,5 @@ export function walk() {
139139
);
140140
}
141141

142-
export { CompileError } from './errors.js';
143142
export { VERSION } from '../version.js';
144143
export { migrate } from './migrate/index.js';

packages/svelte/src/compiler/public.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,10 @@ export type {
55
PreprocessorGroup,
66
Processed
77
} from './preprocess/public';
8-
export type { CompileOptions, ModuleCompileOptions, CompileResult, Warning } from './types/index';
8+
export type {
9+
CompileError,
10+
CompileOptions,
11+
ModuleCompileOptions,
12+
CompileResult,
13+
Warning
14+
} from './types/index';

packages/svelte/src/compiler/types/index.d.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type { Context } from 'zimmerframe';
1212
import type { Scope } from '../phases/scope.js';
1313
import type { Css } from './css.js';
1414
import type { EachBlock, Namespace, SvelteNode, SvelteOptions } from './template.js';
15+
import type { InternalCompileError } from '../errors.js';
1516

1617
/** The return value of `compile` from `svelte/compiler` */
1718
export interface CompileResult {
@@ -59,13 +60,7 @@ export interface Warning {
5960
filename?: string;
6061
}
6162

62-
export interface CompileError extends Error {
63-
code: string;
64-
filename?: string;
65-
position?: [number, number];
66-
start?: Location;
67-
end?: Location;
68-
}
63+
export interface CompileError extends InternalCompileError {}
6964

7065
export type CssHashGetter = (args: {
7166
name: string;

packages/svelte/types/index.d.ts

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -725,13 +725,7 @@ declare module 'svelte/compiler' {
725725
filename?: string;
726726
}
727727

728-
interface CompileError_1 extends Error {
729-
code: string;
730-
filename?: string;
731-
position?: [number, number];
732-
start?: Location;
733-
end?: Location;
734-
}
728+
interface CompileError extends InternalCompileError {}
735729

736730
type CssHashGetter = (args: {
737731
name: string;
@@ -1206,18 +1200,6 @@ declare module 'svelte/compiler' {
12061200
function preprocess(source: string, preprocessor: PreprocessorGroup | PreprocessorGroup[], options?: {
12071201
filename?: string;
12081202
} | undefined): Promise<Processed>;
1209-
class CompileError extends Error {
1210-
1211-
constructor(code: string, message: string, position: [number, number] | undefined);
1212-
filename: string | undefined;
1213-
1214-
position: CompileError_1["position"];
1215-
1216-
start: CompileError_1["start"];
1217-
1218-
end: CompileError_1["end"];
1219-
code: string;
1220-
}
12211203
/**
12221204
* The current version, as set in package.json.
12231205
*
@@ -1902,8 +1884,20 @@ declare module 'svelte/compiler' {
19021884
content: Program;
19031885
attributes: Attribute[];
19041886
}
1887+
class InternalCompileError extends Error {
1888+
1889+
constructor(code: string, message: string, position: [number, number] | undefined);
1890+
filename: string | undefined;
1891+
1892+
position: [number, number] | undefined;
1893+
1894+
start: Location | undefined;
1895+
1896+
end: Location | undefined;
1897+
code: string;
1898+
}
19051899

1906-
export { MarkupPreprocessor, Preprocessor, PreprocessorGroup, Processed, CompileOptions, ModuleCompileOptions, CompileResult, Warning, compile, compileModule, parse, walk, preprocess, CompileError, VERSION, migrate };
1900+
export { MarkupPreprocessor, Preprocessor, PreprocessorGroup, Processed, CompileError, CompileOptions, ModuleCompileOptions, CompileResult, Warning, compile, compileModule, parse, walk, preprocess, VERSION, migrate };
19071901
}
19081902

19091903
declare module 'svelte/easing' {

0 commit comments

Comments
 (0)