Skip to content

fix: expose CompileError interface, not class #12255

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 2 commits into from
Jul 2, 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/good-rice-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

breaking: expose `CompileError` interface, not class
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand Down Expand Up @@ -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
Expand Down
13 changes: 6 additions & 7 deletions packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down
1 change: 0 additions & 1 deletion packages/svelte/src/compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,5 @@ export function walk() {
);
}

export { CompileError } from './errors.js';
export { VERSION } from '../version.js';
export { migrate } from './migrate/index.js';
8 changes: 7 additions & 1 deletion packages/svelte/src/compiler/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
9 changes: 2 additions & 7 deletions packages/svelte/src/compiler/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
34 changes: 14 additions & 20 deletions packages/svelte/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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' {
Expand Down
Loading