1
- import {
2
- asError ,
3
- JSONSourceCode ,
4
- LiquidSourceCode ,
5
- toSourceCode as tcToSourceCode ,
6
- UriString ,
7
- } from '@shopify/theme-check-common' ;
1
+ import { asError , toSourceCode as tcToSourceCode , UriString } from '@shopify/theme-check-common' ;
8
2
import { parse as acornParse , Program } from 'acorn' ;
9
- import { CssSourceCode , JsSourceCode } from './types' ;
3
+ import {
4
+ CssSourceCode ,
5
+ FileSourceCode ,
6
+ ImageSourceCode ,
7
+ JsSourceCode ,
8
+ SUPPORTED_ASSET_IMAGE_EXTENSIONS ,
9
+ SvgSourceCode ,
10
+ } from './types' ;
11
+ import { extname } from './utils' ;
10
12
11
13
export async function toCssSourceCode ( uri : UriString , source : string ) : Promise < CssSourceCode > {
12
14
return {
13
15
type : 'css' ,
14
16
uri,
15
17
source,
16
- ast : new Error ( 'CSS parsing not implemented yet' ) , // Placeholder for CSS parsing
18
+ ast : new Error ( 'File parsing not implemented yet' ) , // Placeholder for CSS parsing
19
+ } ;
20
+ }
21
+
22
+ export async function toSvgSourceCode ( uri : UriString , source : string ) : Promise < SvgSourceCode > {
23
+ return {
24
+ type : 'svg' ,
25
+ uri,
26
+ source,
27
+ ast : new Error ( 'File parsing not implemented yet' ) , // Placeholder for SVG parsing
28
+ } ;
29
+ }
30
+
31
+ async function toImageSourceCode ( uri : UriString , source : string ) : Promise < ImageSourceCode > {
32
+ return {
33
+ type : 'image' ,
34
+ uri,
35
+ source,
36
+ ast : new Error ( 'Image files are not parsed' ) ,
17
37
} ;
18
38
}
19
39
@@ -37,16 +57,19 @@ export function parseJs(source: string): Program | Error {
37
57
}
38
58
}
39
59
40
- export async function toSourceCode (
41
- uri : UriString ,
42
- source : string ,
43
- ) : Promise < JSONSourceCode | LiquidSourceCode | JsSourceCode | CssSourceCode > {
44
- if ( uri . endsWith ( '.json' ) || uri . endsWith ( '.liquid' ) ) {
60
+ export async function toSourceCode ( uri : UriString , source : string ) : Promise < FileSourceCode > {
61
+ const extension = extname ( uri ) ;
62
+
63
+ if ( extension === 'json' || extension === 'liquid' ) {
45
64
return tcToSourceCode ( uri , source ) ;
46
- } else if ( uri . endsWith ( '. js') ) {
65
+ } else if ( extension === ' js') {
47
66
return toJsSourceCode ( uri , source ) ;
48
- } else if ( uri . endsWith ( '.css' ) ) {
67
+ } else if ( extension === 'css' ) {
68
+ return toCssSourceCode ( uri , source ) ;
69
+ } else if ( extension === 'svg' ) {
49
70
return toCssSourceCode ( uri , source ) ;
71
+ } else if ( SUPPORTED_ASSET_IMAGE_EXTENSIONS . includes ( extension ) ) {
72
+ return toImageSourceCode ( uri , source ) ;
50
73
} else {
51
74
throw new Error ( `Unknown source code type for ${ uri } ` ) ;
52
75
}
0 commit comments