@@ -10,6 +10,7 @@ import { findSymbolInitializer } from './resolveInit';
10
10
import type { SourceFile } from './sourceFile' ;
11
11
12
12
import type { KnownCodec } from './knownImports' ;
13
+ import { errorLeft } from './error' ;
13
14
14
15
type ResolvedIdentifier = Schema | { type : 'codec' ; schema : KnownCodec } ;
15
16
@@ -26,9 +27,9 @@ function codecIdentifier(
26
27
27
28
const imp = source . symbols . imports . find ( ( s ) => s . localName === id . value ) ;
28
29
if ( imp === undefined ) {
29
- return E . left ( `Unknown identifier ${ id . value } ` ) ;
30
+ return errorLeft ( `Unknown identifier ${ id . value } ` ) ;
30
31
} else if ( imp . type === 'star' ) {
31
- return E . left ( `Tried to use star import as codec ${ id . value } ` ) ;
32
+ return errorLeft ( `Tried to use star import as codec ${ id . value } ` ) ;
32
33
}
33
34
const knownImport = project . resolveKnownImport ( imp . from , imp . importedName ) ;
34
35
if ( knownImport !== undefined ) {
@@ -54,10 +55,12 @@ function codecIdentifier(
54
55
const object = id . object ;
55
56
if ( object . type !== 'Identifier' ) {
56
57
if ( object . type === 'MemberExpression' )
57
- return E . left (
58
- `Object ${ ( ( object as swc . MemberExpression ) && { value : String } ) . value } is deeply nested, which is unsupported` ,
58
+ return errorLeft (
59
+ `Object ${
60
+ ( ( object as swc . MemberExpression ) && { value : String } ) . value
61
+ } is deeply nested, which is unsupported`,
59
62
) ;
60
- return E . left ( `Unimplemented object type ${ object . type } ` ) ;
63
+ return errorLeft ( `Unimplemented object type ${ object . type } ` ) ;
61
64
}
62
65
63
66
// Parse member expressions that come from `* as foo` imports
@@ -66,7 +69,7 @@ function codecIdentifier(
66
69
) ;
67
70
if ( starImportSym !== undefined ) {
68
71
if ( id . property . type !== 'Identifier' ) {
69
- return E . left ( `Unimplemented property type ${ id . property . type } ` ) ;
72
+ return errorLeft ( `Unimplemented property type ${ id . property . type } ` ) ;
70
73
}
71
74
72
75
const name = id . property . value ;
@@ -96,7 +99,7 @@ function codecIdentifier(
96
99
) ;
97
100
if ( objectImportSym !== undefined ) {
98
101
if ( id . property . type !== 'Identifier' ) {
99
- return E . left ( `Unimplemented property type ${ id . property . type } ` ) ;
102
+ return errorLeft ( `Unimplemented property type ${ id . property . type } ` ) ;
100
103
}
101
104
const name = id . property . value ;
102
105
@@ -113,9 +116,9 @@ function codecIdentifier(
113
116
if ( E . isLeft ( objectSchemaE ) ) {
114
117
return objectSchemaE ;
115
118
} else if ( objectSchemaE . right . type !== 'object' ) {
116
- return E . left ( `Expected object, got '${ objectSchemaE . right . type } '` ) ;
119
+ return errorLeft ( `Expected object, got '${ objectSchemaE . right . type } '` ) ;
117
120
} else if ( objectSchemaE . right . properties [ name ] === undefined ) {
118
- return E . left (
121
+ return errorLeft (
119
122
`Unknown property '${ name } ' in '${ objectImportSym . localName } ' from '${ objectImportSym . from } '` ,
120
123
) ;
121
124
} else {
@@ -124,7 +127,7 @@ function codecIdentifier(
124
127
}
125
128
126
129
if ( id . property . type !== 'Identifier' ) {
127
- return E . left ( `Unimplemented property type ${ id . property . type } ` ) ;
130
+ return errorLeft ( `Unimplemented property type ${ id . property . type } ` ) ;
128
131
}
129
132
130
133
// Parse locally declared member expressions
@@ -136,11 +139,11 @@ function codecIdentifier(
136
139
if ( E . isLeft ( schemaE ) ) {
137
140
return schemaE ;
138
141
} else if ( schemaE . right . type !== 'object' ) {
139
- return E . left (
142
+ return errorLeft (
140
143
`Expected object, got '${ schemaE . right . type } ' for '${ declarationSym . name } '` ,
141
144
) ;
142
145
} else if ( schemaE . right . properties [ id . property . value ] === undefined ) {
143
- return E . left (
146
+ return errorLeft (
144
147
`Unknown property '${ id . property . value } ' in '${ declarationSym . name } '` ,
145
148
) ;
146
149
} else {
@@ -158,7 +161,7 @@ function codecIdentifier(
158
161
}
159
162
}
160
163
161
- return E . left ( `Unimplemented identifier type ${ id . type } ` ) ;
164
+ return errorLeft ( `Unimplemented identifier type ${ id . type } ` ) ;
162
165
}
163
166
164
167
function parseObjectExpression (
@@ -210,19 +213,19 @@ function parseObjectExpression(
210
213
schema = schemaE . right ;
211
214
}
212
215
if ( schema . type !== 'object' ) {
213
- return E . left ( `Spread element must be object` ) ;
216
+ return errorLeft ( `Spread element must be object` ) ;
214
217
}
215
218
Object . assign ( result . properties , schema . properties ) ;
216
219
result . required . push ( ...schema . required ) ;
217
220
continue ;
218
221
} else if ( property . type !== 'KeyValueProperty' ) {
219
- return E . left ( `Unimplemented property type ${ property . type } ` ) ;
222
+ return errorLeft ( `Unimplemented property type ${ property . type } ` ) ;
220
223
} else if (
221
224
property . key . type !== 'Identifier' &&
222
225
property . key . type !== 'StringLiteral' &&
223
226
property . key . type !== 'NumericLiteral'
224
227
) {
225
- return E . left ( `Unimplemented property key type ${ property . key . type } ` ) ;
228
+ return errorLeft ( `Unimplemented property key type ${ property . key . type } ` ) ;
226
229
}
227
230
const commentEndIdx = property . key . span . start ;
228
231
const comments = leadingComment (
@@ -254,7 +257,7 @@ function parseArrayExpression(
254
257
const result : Schema [ ] = [ ] ;
255
258
for ( const element of array . elements ) {
256
259
if ( element === undefined ) {
257
- return E . left ( 'Undefined array element' ) ;
260
+ return errorLeft ( 'Undefined array element' ) ;
258
261
}
259
262
const valueE = parsePlainInitializer ( project , source , element . expression ) ;
260
263
if ( E . isLeft ( valueE ) ) {
@@ -279,7 +282,7 @@ function parseArrayExpression(
279
282
init = schemaE . right ;
280
283
}
281
284
if ( init . type !== 'tuple' ) {
282
- return E . left ( 'Spread element must be array literal' ) ;
285
+ return errorLeft ( 'Spread element must be array literal' ) ;
283
286
}
284
287
result . push ( ...init . schemas ) ;
285
288
} else {
@@ -342,7 +345,7 @@ export function parseCodecInitializer(
342
345
} else if ( init . type === 'CallExpression' ) {
343
346
const callee = init . callee ;
344
347
if ( callee . type !== 'Identifier' && callee . type !== 'MemberExpression' ) {
345
- return E . left ( `Unimplemented callee type ${ init . callee . type } ` ) ;
348
+ return errorLeft ( `Unimplemented callee type ${ init . callee . type } ` ) ;
346
349
}
347
350
const identifierE = codecIdentifier ( project , source , callee ) ;
348
351
if ( E . isLeft ( identifierE ) ) {
@@ -364,10 +367,10 @@ export function parseCodecInitializer(
364
367
// schema.location might be a package name -> need to resolve the path from the project types
365
368
const path = project . getTypes ( ) [ schema . name ] ;
366
369
if ( path === undefined )
367
- return E . left ( `Cannot find module '${ schema . location } ' in the project` ) ;
370
+ return errorLeft ( `Cannot find module '${ schema . location } ' in the project` ) ;
368
371
refSource = project . get ( path ) ;
369
372
if ( refSource === undefined ) {
370
- return E . left ( `Cannot find '${ schema . name } ' from '${ schema . location } '` ) ;
373
+ return errorLeft ( `Cannot find '${ schema . name } ' from '${ schema . location } '` ) ;
371
374
}
372
375
}
373
376
const initE = findSymbolInitializer ( project , refSource , schema . name ) ;
@@ -394,6 +397,6 @@ export function parseCodecInitializer(
394
397
E . chain ( ( args ) => identifier . schema ( deref , ...args ) ) ,
395
398
) ;
396
399
} else {
397
- return E . left ( `Unimplemented initializer type ${ init . type } ` ) ;
400
+ return errorLeft ( `Unimplemented initializer type ${ init . type } ` ) ;
398
401
}
399
402
}
0 commit comments