Skip to content
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

chore: remove handle_compile_error #11639

Merged
merged 5 commits into from
May 16, 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/angry-wasps-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

chore: remove `handle_compile_error`
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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 {
name = 'CompileError';

/** @type {import('#compiler').CompileError['filename']} */
filename = undefined;
filename = state.filename;

/** @type {import('#compiler').CompileError['position']} */
position = undefined;
Expand All @@ -25,8 +26,14 @@ export class CompileError extends Error {
*/
constructor(code, message, position) {
super(message);

this.code = code;
this.position = position;

if (position) {
this.start = state.locator(position[0]);
this.end = state.locator(position[1]);
}
}

toString() {
Expand Down
10 changes: 8 additions & 2 deletions packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* 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 */
// 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 {
name = 'CompileError';
/** @type {import('#compiler').CompileError['filename']} */
filename = undefined;
filename = state.filename;
/** @type {import('#compiler').CompileError['position']} */
position = undefined;
/** @type {import('#compiler').CompileError['start']} */
Expand All @@ -24,6 +25,11 @@ export class CompileError extends Error {
super(message);
this.code = code;
this.position = position;

if (position) {
this.start = state.locator(position[0]);
this.end = state.locator(position[1]);
}
}

toString() {
Expand Down
92 changes: 23 additions & 69 deletions packages/svelte/src/compiler/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { walk as zimmerframe_walk } from 'zimmerframe';
import { CompileError } from './errors.js';
import { convert } from './legacy.js';
import { parse as parse_acorn } from './phases/1-parse/acorn.js';
import { parse as _parse } from './phases/1-parse/index.js';
Expand All @@ -22,38 +21,30 @@ export function compile(source, options) {
const validated = validate_component_options(options, '');
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 || {};

/** @type {import('#compiler').ValidatedCompileOptions} */
const combined_options = {
...validated,
...parsed_options,
customElementOptions
};
/** @type {import('#compiler').ValidatedCompileOptions} */
const combined_options = {
...validated,
...parsed_options,
customElementOptions
};

if (parsed.metadata.ts) {
parsed = {
...parsed,
fragment: parsed.fragment && remove_typescript_nodes(parsed.fragment),
instance: parsed.instance && remove_typescript_nodes(parsed.instance),
module: parsed.module && remove_typescript_nodes(parsed.module)
};
}

const analysis = analyze_component(parsed, source, combined_options);
const result = transform_component(analysis, source, combined_options);
result.ast = to_public_ast(source, parsed, options.modernAst);
return result;
} catch (e) {
if (e instanceof CompileError) {
handle_compile_error(e);
}

throw e;
if (parsed.metadata.ts) {
parsed = {
...parsed,
fragment: parsed.fragment && remove_typescript_nodes(parsed.fragment),
instance: parsed.instance && remove_typescript_nodes(parsed.instance),
module: parsed.module && remove_typescript_nodes(parsed.module)
};
}

const analysis = analyze_component(parsed, source, combined_options);
const result = transform_component(analysis, source, combined_options);
result.ast = to_public_ast(source, parsed, options.modernAst);
return result;
}

/**
Expand All @@ -68,34 +59,8 @@ export function compileModule(source, options) {
const validated = validate_module_options(options, '');
state.reset(source, validated);

try {
const analysis = analyze_module(parse_acorn(source, false), validated);
const result = 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;
const analysis = analyze_module(parse_acorn(source, false), validated);
return transform_module(analysis, source, validated);
}

/**
Expand Down Expand Up @@ -138,18 +103,7 @@ function handle_compile_error(error) {
export function parse(source, { filename, rootDir, modern } = {}) {
state.reset(source, { filename, rootDir }); // TODO it's weird to require filename/rootDir here. reconsider the API

/** @type {import('#compiler').Root} */
let ast;
try {
ast = _parse(source);
} catch (e) {
if (e instanceof CompileError) {
handle_compile_error(e);
}

throw e;
}

const ast = _parse(source);
return to_public_ast(source, ast, modern);
}

Expand Down
3 changes: 1 addition & 2 deletions packages/svelte/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1059,8 +1059,7 @@ declare module 'svelte/compiler' {
export class CompileError extends Error {

constructor(code: string, message: string, position: [number, number] | undefined);

filename: CompileError_1['filename'];
filename: string | undefined;

position: CompileError_1['position'];

Expand Down
Loading