chore: remove `handle_compile_error` (#11639)

We don't need the awkward handle_compile_error stuff any more, because the relevant information now lives in state.js
pull/11658/head
Rich Harris 7 months ago committed by GitHub
parent 2ebb277be7
commit 7b9fad4b0e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
chore: remove `handle_compile_error`

@ -1,3 +1,5 @@
import * as state from './state.js';
/** @typedef {{ start?: number, end?: number }} NodeLike */ /** @typedef {{ start?: number, end?: number }} NodeLike */
// interface is duplicated between here (used internally) and ./interfaces.js // interface is duplicated between here (used internally) and ./interfaces.js
@ -5,8 +7,7 @@
export class CompileError extends Error { export class CompileError extends Error {
name = 'CompileError'; name = 'CompileError';
/** @type {import('#compiler').CompileError['filename']} */ filename = state.filename;
filename = undefined;
/** @type {import('#compiler').CompileError['position']} */ /** @type {import('#compiler').CompileError['position']} */
position = undefined; position = undefined;
@ -25,8 +26,14 @@ export class CompileError extends Error {
*/ */
constructor(code, message, position) { constructor(code, message, position) {
super(message); super(message);
this.code = code; this.code = code;
this.position = position; this.position = position;
if (position) {
this.start = state.locator(position[0]);
this.end = state.locator(position[1]);
}
} }
toString() { toString() {

@ -1,12 +1,13 @@
/* This file is generated by scripts/process-messages/index.js. Do not edit! */ /* This file is generated by scripts/process-messages/index.js. Do not edit! */
import * as state from './state.js';
/** @typedef {{ start?: number, end?: number }} NodeLike */ /** @typedef {{ start?: number, end?: number }} NodeLike */
// interface is duplicated between here (used internally) and ./interfaces.js // interface is duplicated between here (used internally) and ./interfaces.js
// (exposed publicly), and I'm not sure how to avoid that // (exposed publicly), and I'm not sure how to avoid that
export class CompileError extends Error { export class CompileError extends Error {
name = 'CompileError'; name = 'CompileError';
/** @type {import('#compiler').CompileError['filename']} */ filename = state.filename;
filename = undefined;
/** @type {import('#compiler').CompileError['position']} */ /** @type {import('#compiler').CompileError['position']} */
position = undefined; position = undefined;
/** @type {import('#compiler').CompileError['start']} */ /** @type {import('#compiler').CompileError['start']} */
@ -24,6 +25,11 @@ export class CompileError extends Error {
super(message); super(message);
this.code = code; this.code = code;
this.position = position; this.position = position;
if (position) {
this.start = state.locator(position[0]);
this.end = state.locator(position[1]);
}
} }
toString() { toString() {

@ -1,5 +1,4 @@
import { walk as zimmerframe_walk } from 'zimmerframe'; import { walk as zimmerframe_walk } from 'zimmerframe';
import { CompileError } from './errors.js';
import { convert } from './legacy.js'; import { convert } from './legacy.js';
import { parse as parse_acorn } from './phases/1-parse/acorn.js'; import { parse as parse_acorn } from './phases/1-parse/acorn.js';
import { parse as _parse } from './phases/1-parse/index.js'; import { parse as _parse } from './phases/1-parse/index.js';
@ -22,7 +21,6 @@ export function compile(source, options) {
const validated = validate_component_options(options, ''); const validated = validate_component_options(options, '');
state.reset(source, validated); state.reset(source, validated);
try {
let parsed = _parse(source); let parsed = _parse(source);
const { customElement: customElementOptions, ...parsed_options } = parsed.options || {}; const { customElement: customElementOptions, ...parsed_options } = parsed.options || {};
@ -47,13 +45,6 @@ export function compile(source, options) {
const result = transform_component(analysis, source, combined_options); const result = transform_component(analysis, source, combined_options);
result.ast = to_public_ast(source, parsed, options.modernAst); result.ast = to_public_ast(source, parsed, options.modernAst);
return result; return result;
} catch (e) {
if (e instanceof CompileError) {
handle_compile_error(e);
}
throw e;
}
} }
/** /**
@ -68,34 +59,8 @@ export function compileModule(source, options) {
const validated = validate_module_options(options, ''); const validated = validate_module_options(options, '');
state.reset(source, validated); state.reset(source, validated);
try {
const analysis = analyze_module(parse_acorn(source, false), validated); const analysis = analyze_module(parse_acorn(source, false), validated);
const result = transform_module(analysis, source, validated); return transform_module(analysis, source, validated);
return result;
} catch (e) {
if (e instanceof CompileError) {
handle_compile_error(e);
}
throw e;
}
}
/**
* @param {import('#compiler').CompileError} error
*/
function handle_compile_error(error) {
error.filename = state.filename;
if (error.position) {
const start = state.locator(error.position[0]);
const end = state.locator(error.position[1]);
error.start = start;
error.end = end;
}
throw error;
} }
/** /**
@ -138,18 +103,7 @@ function handle_compile_error(error) {
export function parse(source, { filename, rootDir, modern } = {}) { export function parse(source, { filename, rootDir, modern } = {}) {
state.reset(source, { filename, rootDir }); // TODO it's weird to require filename/rootDir here. reconsider the API state.reset(source, { filename, rootDir }); // TODO it's weird to require filename/rootDir here. reconsider the API
/** @type {import('#compiler').Root} */ const ast = _parse(source);
let ast;
try {
ast = _parse(source);
} catch (e) {
if (e instanceof CompileError) {
handle_compile_error(e);
}
throw e;
}
return to_public_ast(source, ast, modern); return to_public_ast(source, ast, modern);
} }

@ -1059,8 +1059,7 @@ declare module 'svelte/compiler' {
export class CompileError extends Error { export class CompileError extends Error {
constructor(code: string, message: string, position: [number, number] | undefined); constructor(code: string, message: string, position: [number, number] | undefined);
filename: string | undefined;
filename: CompileError_1['filename'];
position: CompileError_1['position']; position: CompileError_1['position'];

Loading…
Cancel
Save