@@ -10,6 +10,7 @@ import { findSymbolInitializer } from './resolveInit';
1010import type { SourceFile } from './sourceFile' ;
1111
1212import type { KnownCodec } from './knownImports' ;
13+ import { errorLeft } from './error' ;
1314
1415type ResolvedIdentifier = Schema | { type : 'codec' ; schema : KnownCodec } ;
1516
@@ -26,9 +27,9 @@ function codecIdentifier(
2627
2728 const imp = source . symbols . imports . find ( ( s ) => s . localName === id . value ) ;
2829 if ( imp === undefined ) {
29- return E . left ( `Unknown identifier ${ id . value } ` ) ;
30+ return errorLeft ( `Unknown identifier ${ id . value } ` ) ;
3031 } 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 } ` ) ;
3233 }
3334 const knownImport = project . resolveKnownImport ( imp . from , imp . importedName ) ;
3435 if ( knownImport !== undefined ) {
@@ -54,10 +55,12 @@ function codecIdentifier(
5455 const object = id . object ;
5556 if ( object . type !== 'Identifier' ) {
5657 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`,
5962 ) ;
60- return E . left ( `Unimplemented object type ${ object . type } ` ) ;
63+ return errorLeft ( `Unimplemented object type ${ object . type } ` ) ;
6164 }
6265
6366 // Parse member expressions that come from `* as foo` imports
@@ -66,7 +69,7 @@ function codecIdentifier(
6669 ) ;
6770 if ( starImportSym !== undefined ) {
6871 if ( id . property . type !== 'Identifier' ) {
69- return E . left ( `Unimplemented property type ${ id . property . type } ` ) ;
72+ return errorLeft ( `Unimplemented property type ${ id . property . type } ` ) ;
7073 }
7174
7275 const name = id . property . value ;
@@ -96,7 +99,7 @@ function codecIdentifier(
9699 ) ;
97100 if ( objectImportSym !== undefined ) {
98101 if ( id . property . type !== 'Identifier' ) {
99- return E . left ( `Unimplemented property type ${ id . property . type } ` ) ;
102+ return errorLeft ( `Unimplemented property type ${ id . property . type } ` ) ;
100103 }
101104 const name = id . property . value ;
102105
@@ -113,9 +116,9 @@ function codecIdentifier(
113116 if ( E . isLeft ( objectSchemaE ) ) {
114117 return objectSchemaE ;
115118 } 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 } '` ) ;
117120 } else if ( objectSchemaE . right . properties [ name ] === undefined ) {
118- return E . left (
121+ return errorLeft (
119122 `Unknown property '${ name } ' in '${ objectImportSym . localName } ' from '${ objectImportSym . from } '` ,
120123 ) ;
121124 } else {
@@ -124,7 +127,7 @@ function codecIdentifier(
124127 }
125128
126129 if ( id . property . type !== 'Identifier' ) {
127- return E . left ( `Unimplemented property type ${ id . property . type } ` ) ;
130+ return errorLeft ( `Unimplemented property type ${ id . property . type } ` ) ;
128131 }
129132
130133 // Parse locally declared member expressions
@@ -136,11 +139,11 @@ function codecIdentifier(
136139 if ( E . isLeft ( schemaE ) ) {
137140 return schemaE ;
138141 } else if ( schemaE . right . type !== 'object' ) {
139- return E . left (
142+ return errorLeft (
140143 `Expected object, got '${ schemaE . right . type } ' for '${ declarationSym . name } '` ,
141144 ) ;
142145 } else if ( schemaE . right . properties [ id . property . value ] === undefined ) {
143- return E . left (
146+ return errorLeft (
144147 `Unknown property '${ id . property . value } ' in '${ declarationSym . name } '` ,
145148 ) ;
146149 } else {
@@ -158,7 +161,7 @@ function codecIdentifier(
158161 }
159162 }
160163
161- return E . left ( `Unimplemented identifier type ${ id . type } ` ) ;
164+ return errorLeft ( `Unimplemented identifier type ${ id . type } ` ) ;
162165}
163166
164167function parseObjectExpression (
@@ -210,19 +213,19 @@ function parseObjectExpression(
210213 schema = schemaE . right ;
211214 }
212215 if ( schema . type !== 'object' ) {
213- return E . left ( `Spread element must be object` ) ;
216+ return errorLeft ( `Spread element must be object` ) ;
214217 }
215218 Object . assign ( result . properties , schema . properties ) ;
216219 result . required . push ( ...schema . required ) ;
217220 continue ;
218221 } else if ( property . type !== 'KeyValueProperty' ) {
219- return E . left ( `Unimplemented property type ${ property . type } ` ) ;
222+ return errorLeft ( `Unimplemented property type ${ property . type } ` ) ;
220223 } else if (
221224 property . key . type !== 'Identifier' &&
222225 property . key . type !== 'StringLiteral' &&
223226 property . key . type !== 'NumericLiteral'
224227 ) {
225- return E . left ( `Unimplemented property key type ${ property . key . type } ` ) ;
228+ return errorLeft ( `Unimplemented property key type ${ property . key . type } ` ) ;
226229 }
227230 const commentEndIdx = property . key . span . start ;
228231 const comments = leadingComment (
@@ -254,7 +257,7 @@ function parseArrayExpression(
254257 const result : Schema [ ] = [ ] ;
255258 for ( const element of array . elements ) {
256259 if ( element === undefined ) {
257- return E . left ( 'Undefined array element' ) ;
260+ return errorLeft ( 'Undefined array element' ) ;
258261 }
259262 const valueE = parsePlainInitializer ( project , source , element . expression ) ;
260263 if ( E . isLeft ( valueE ) ) {
@@ -279,7 +282,7 @@ function parseArrayExpression(
279282 init = schemaE . right ;
280283 }
281284 if ( init . type !== 'tuple' ) {
282- return E . left ( 'Spread element must be array literal' ) ;
285+ return errorLeft ( 'Spread element must be array literal' ) ;
283286 }
284287 result . push ( ...init . schemas ) ;
285288 } else {
@@ -342,7 +345,7 @@ export function parseCodecInitializer(
342345 } else if ( init . type === 'CallExpression' ) {
343346 const callee = init . callee ;
344347 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 } ` ) ;
346349 }
347350 const identifierE = codecIdentifier ( project , source , callee ) ;
348351 if ( E . isLeft ( identifierE ) ) {
@@ -364,10 +367,10 @@ export function parseCodecInitializer(
364367 // schema.location might be a package name -> need to resolve the path from the project types
365368 const path = project . getTypes ( ) [ schema . name ] ;
366369 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` ) ;
368371 refSource = project . get ( path ) ;
369372 if ( refSource === undefined ) {
370- return E . left ( `Cannot find '${ schema . name } ' from '${ schema . location } '` ) ;
373+ return errorLeft ( `Cannot find '${ schema . name } ' from '${ schema . location } '` ) ;
371374 }
372375 }
373376 const initE = findSymbolInitializer ( project , refSource , schema . name ) ;
@@ -394,6 +397,6 @@ export function parseCodecInitializer(
394397 E . chain ( ( args ) => identifier . schema ( deref , ...args ) ) ,
395398 ) ;
396399 } else {
397- return E . left ( `Unimplemented initializer type ${ init . type } ` ) ;
400+ return errorLeft ( `Unimplemented initializer type ${ init . type } ` ) ;
398401 }
399402}
0 commit comments