File tree Expand file tree Collapse file tree 4 files changed +44
-2
lines changed
packages/svelte/src/compiler Expand file tree Collapse file tree 4 files changed +44
-2
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import { walk } from 'zimmerframe';
7
7
import { validate_component_options , validate_module_options } from './validate-options.js' ;
8
8
import { convert } from './legacy.js' ;
9
9
import { CompileError } from './errors.js' ;
10
+ import { strip_ts_assertions } from './phases/1-parse/strip_ts_assertions.js' ;
10
11
export { default as preprocess } from './preprocess/index.js' ;
11
12
12
13
/**
@@ -20,14 +21,24 @@ export { default as preprocess } from './preprocess/index.js';
20
21
export function compile ( source , options ) {
21
22
try {
22
23
const validated = validate_component_options ( options , '' ) ;
23
- const parsed = _parse ( source ) ;
24
+ let parsed = _parse ( source ) ;
24
25
25
26
const combined_options = /** @type {import('#compiler').ValidatedCompileOptions } */ ( {
26
27
...validated ,
27
28
...parsed . options
28
29
} ) ;
29
30
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
+
30
40
const analysis = analyze_component ( parsed , combined_options ) ;
41
+
31
42
const result = transform_component ( analysis , source , combined_options ) ;
32
43
return result ;
33
44
} catch ( e ) {
Original file line number Diff line number Diff line change @@ -67,7 +67,10 @@ export class Parser {
67
67
end : null ,
68
68
type : 'Root' ,
69
69
fragment : create_fragment ( ) ,
70
- options : null
70
+ options : null ,
71
+ metadata : {
72
+ ts : this . ts
73
+ }
71
74
} ;
72
75
73
76
this . stack . push ( this . root ) ;
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -56,6 +56,10 @@ export interface Root extends BaseNode {
56
56
instance : Script | null ;
57
57
/** The parsed `<script context="module">` element, if exists */
58
58
module : Script | null ;
59
+ metadata : {
60
+ /** Whether the component was parsed with typescript */
61
+ ts : boolean ;
62
+ }
59
63
}
60
64
61
65
export interface SvelteOptions {
You can’t perform that action at this time.
0 commit comments