Skip to content

Commit ebb5b49

Browse files
committed
strip typescript assertions before analysis
1 parent 77b4c4b commit ebb5b49

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

packages/svelte/src/compiler/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { walk } from 'zimmerframe';
77
import { validate_component_options, validate_module_options } from './validate-options.js';
88
import { convert } from './legacy.js';
99
import { CompileError } from './errors.js';
10+
import { strip_ts_assertions } from './phases/1-parse/strip_ts_assertions.js';
1011
export { default as preprocess } from './preprocess/index.js';
1112

1213
/**
@@ -20,14 +21,24 @@ export { default as preprocess } from './preprocess/index.js';
2021
export function compile(source, options) {
2122
try {
2223
const validated = validate_component_options(options, '');
23-
const parsed = _parse(source);
24+
let parsed = _parse(source);
2425

2526
const combined_options = /** @type {import('#compiler').ValidatedCompileOptions} */ ({
2627
...validated,
2728
...parsed.options
2829
});
2930

31+
if (parsed.metadata.ts) {
32+
parsed = {
33+
...parsed,
34+
fragment: parsed.fragment && strip_ts_assertions(parsed.fragment),
35+
instance: parsed.instance && strip_ts_assertions(parsed.instance),
36+
module: parsed.module && strip_ts_assertions(parsed.module)
37+
};
38+
}
39+
3040
const analysis = analyze_component(parsed, combined_options);
41+
3142
const result = transform_component(analysis, source, combined_options);
3243
return result;
3344
} catch (e) {

packages/svelte/src/compiler/phases/1-parse/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ export class Parser {
6767
end: null,
6868
type: 'Root',
6969
fragment: create_fragment(),
70-
options: null
70+
options: null,
71+
metadata: {
72+
ts: this.ts
73+
}
7174
};
7275

7376
this.stack.push(this.root);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { walk } from 'zimmerframe';
2+
3+
/**
4+
* @type {import('zimmerframe').Visitor<any, null, any>}
5+
*/
6+
function unwrap_expression_visitor(node, { visit }) {
7+
return visit(node.expression);
8+
}
9+
10+
/** @type {import('zimmerframe').Visitors<any, null>} */
11+
const visitors = {
12+
TSAsExpression: unwrap_expression_visitor,
13+
TSNonNullExpression: unwrap_expression_visitor,
14+
TSSatisfiesExpression: unwrap_expression_visitor
15+
};
16+
17+
/**
18+
* @template T
19+
* @param {T} ast
20+
* @returns {T}
21+
*/
22+
export function strip_ts_assertions(ast) {
23+
return walk(ast, null, visitors);
24+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ export interface Root extends BaseNode {
5656
instance: Script | null;
5757
/** The parsed `<script context="module">` element, if exists */
5858
module: Script | null;
59+
metadata: {
60+
/** Whether the component was parsed with typescript */
61+
ts: boolean;
62+
}
5963
}
6064

6165
export interface SvelteOptions {

0 commit comments

Comments
 (0)