Skip to content

Commit 7b9fad4

Browse files
authored
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
1 parent 2ebb277 commit 7b9fad4

File tree

5 files changed

+46
-75
lines changed

5 files changed

+46
-75
lines changed

.changeset/angry-wasps-help.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+
chore: remove `handle_compile_error`

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
import * as state from './state.js';
2+
13
/** @typedef {{ start?: number, end?: number }} NodeLike */
24

35
// interface is duplicated between here (used internally) and ./interfaces.js
46
// (exposed publicly), and I'm not sure how to avoid that
57
export class CompileError extends Error {
68
name = 'CompileError';
79

8-
/** @type {import('#compiler').CompileError['filename']} */
9-
filename = undefined;
10+
filename = state.filename;
1011

1112
/** @type {import('#compiler').CompileError['position']} */
1213
position = undefined;
@@ -25,8 +26,14 @@ export class CompileError extends Error {
2526
*/
2627
constructor(code, message, position) {
2728
super(message);
29+
2830
this.code = code;
2931
this.position = position;
32+
33+
if (position) {
34+
this.start = state.locator(position[0]);
35+
this.end = state.locator(position[1]);
36+
}
3037
}
3138

3239
toString() {

packages/svelte/src/compiler/errors.js

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

3+
import * as state from './state.js';
4+
35
/** @typedef {{ start?: number, end?: number }} NodeLike */
46
// interface is duplicated between here (used internally) and ./interfaces.js
57
// (exposed publicly), and I'm not sure how to avoid that
68
export class CompileError extends Error {
79
name = 'CompileError';
8-
/** @type {import('#compiler').CompileError['filename']} */
9-
filename = undefined;
10+
filename = state.filename;
1011
/** @type {import('#compiler').CompileError['position']} */
1112
position = undefined;
1213
/** @type {import('#compiler').CompileError['start']} */
@@ -24,6 +25,11 @@ export class CompileError extends Error {
2425
super(message);
2526
this.code = code;
2627
this.position = position;
28+
29+
if (position) {
30+
this.start = state.locator(position[0]);
31+
this.end = state.locator(position[1]);
32+
}
2733
}
2834

2935
toString() {

packages/svelte/src/compiler/index.js

Lines changed: 23 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { walk as zimmerframe_walk } from 'zimmerframe';
2-
import { CompileError } from './errors.js';
32
import { convert } from './legacy.js';
43
import { parse as parse_acorn } from './phases/1-parse/acorn.js';
54
import { parse as _parse } from './phases/1-parse/index.js';
@@ -22,38 +21,30 @@ export function compile(source, options) {
2221
const validated = validate_component_options(options, '');
2322
state.reset(source, validated);
2423

25-
try {
26-
let parsed = _parse(source);
24+
let parsed = _parse(source);
2725

28-
const { customElement: customElementOptions, ...parsed_options } = parsed.options || {};
26+
const { customElement: customElementOptions, ...parsed_options } = parsed.options || {};
2927

30-
/** @type {import('#compiler').ValidatedCompileOptions} */
31-
const combined_options = {
32-
...validated,
33-
...parsed_options,
34-
customElementOptions
35-
};
28+
/** @type {import('#compiler').ValidatedCompileOptions} */
29+
const combined_options = {
30+
...validated,
31+
...parsed_options,
32+
customElementOptions
33+
};
3634

37-
if (parsed.metadata.ts) {
38-
parsed = {
39-
...parsed,
40-
fragment: parsed.fragment && remove_typescript_nodes(parsed.fragment),
41-
instance: parsed.instance && remove_typescript_nodes(parsed.instance),
42-
module: parsed.module && remove_typescript_nodes(parsed.module)
43-
};
44-
}
45-
46-
const analysis = analyze_component(parsed, source, combined_options);
47-
const result = transform_component(analysis, source, combined_options);
48-
result.ast = to_public_ast(source, parsed, options.modernAst);
49-
return result;
50-
} catch (e) {
51-
if (e instanceof CompileError) {
52-
handle_compile_error(e);
53-
}
54-
55-
throw e;
35+
if (parsed.metadata.ts) {
36+
parsed = {
37+
...parsed,
38+
fragment: parsed.fragment && remove_typescript_nodes(parsed.fragment),
39+
instance: parsed.instance && remove_typescript_nodes(parsed.instance),
40+
module: parsed.module && remove_typescript_nodes(parsed.module)
41+
};
5642
}
43+
44+
const analysis = analyze_component(parsed, source, combined_options);
45+
const result = transform_component(analysis, source, combined_options);
46+
result.ast = to_public_ast(source, parsed, options.modernAst);
47+
return result;
5748
}
5849

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

71-
try {
72-
const analysis = analyze_module(parse_acorn(source, false), validated);
73-
const result = transform_module(analysis, source, validated);
74-
return result;
75-
} catch (e) {
76-
if (e instanceof CompileError) {
77-
handle_compile_error(e);
78-
}
79-
80-
throw e;
81-
}
82-
}
83-
84-
/**
85-
* @param {import('#compiler').CompileError} error
86-
*/
87-
function handle_compile_error(error) {
88-
error.filename = state.filename;
89-
90-
if (error.position) {
91-
const start = state.locator(error.position[0]);
92-
const end = state.locator(error.position[1]);
93-
94-
error.start = start;
95-
error.end = end;
96-
}
97-
98-
throw error;
62+
const analysis = analyze_module(parse_acorn(source, false), validated);
63+
return transform_module(analysis, source, validated);
9964
}
10065

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

141-
/** @type {import('#compiler').Root} */
142-
let ast;
143-
try {
144-
ast = _parse(source);
145-
} catch (e) {
146-
if (e instanceof CompileError) {
147-
handle_compile_error(e);
148-
}
149-
150-
throw e;
151-
}
152-
106+
const ast = _parse(source);
153107
return to_public_ast(source, ast, modern);
154108
}
155109

packages/svelte/types/index.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,8 +1059,7 @@ declare module 'svelte/compiler' {
10591059
export class CompileError extends Error {
10601060

10611061
constructor(code: string, message: string, position: [number, number] | undefined);
1062-
1063-
filename: CompileError_1['filename'];
1062+
filename: string | undefined;
10641063

10651064
position: CompileError_1['position'];
10661065

0 commit comments

Comments
 (0)