-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d43509
commit 9ac4403
Showing
10 changed files
with
371 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { NodePath } from '@babel/traverse' | ||
import * as t from '@babel/types' | ||
import NodeConversionError from '../NodeConversionError' | ||
import { FileConversionContext } from './ConversionContext' | ||
import convertObjectTypeAnnotation from './convertObjectTypeAnnotation' | ||
import template from '@babel/template' | ||
|
||
const templates = { | ||
mergeInexact: template.expression(`%%T%%.mergeInexact(%%OBJECTS%%)`), | ||
} | ||
|
||
export default async function convertInterfaceDeclaration( | ||
context: FileConversionContext, | ||
path: NodePath<t.InterfaceDeclaration> | ||
): Promise<t.Expression> { | ||
if (path.node.mixins?.length) | ||
throw new NodeConversionError( | ||
'interface mixins are not supported', | ||
context.file, | ||
path | ||
) | ||
const extended = [...path.get('implements'), ...path.get('extends')] | ||
const convertedBody = await convertObjectTypeAnnotation( | ||
context, | ||
path.get('body') | ||
) | ||
if (!extended.length) return convertedBody | ||
return templates.mergeInexact({ | ||
T: await context.importT(), | ||
OBJECTS: await Promise.all([ | ||
...extended.map((path) => context.convert(path)), | ||
convertedBody, | ||
]), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { NodePath } from '@babel/traverse' | ||
import * as t from '@babel/types' | ||
import { FileConversionContext } from './ConversionContext' | ||
import convertTSTypeLiteralOrInterfaceBody from './convertTSTypeLiteralOrInterfaceBody' | ||
import template from '@babel/template' | ||
|
||
const templates = { | ||
merge: template.expression(`%%T%%.merge(%%OBJECTS%%)`), | ||
} | ||
|
||
export default async function convertTSInterfaceDeclaration( | ||
context: FileConversionContext, | ||
path: NodePath<t.TSInterfaceDeclaration> | ||
): Promise<t.Expression> { | ||
const extended: NodePath<t.TSExpressionWithTypeArguments>[] | null = path.get( | ||
'extends' | ||
) as any | ||
const convertedBody = await convertTSTypeLiteralOrInterfaceBody( | ||
context, | ||
path.get('body') | ||
) | ||
if (!extended?.length) return convertedBody | ||
return templates.merge({ | ||
T: await context.importT(), | ||
OBJECTS: await Promise.all([ | ||
...extended.map((path) => context.convert(path)), | ||
convertedBody, | ||
]), | ||
}) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,21 @@ | ||
import { NodePath } from '@babel/traverse' | ||
import * as t from '@babel/types' | ||
|
||
function registerIdBinding( | ||
path: NodePath<t.TSTypeAliasDeclaration> | NodePath<t.TSInterfaceDeclaration> | ||
): void { | ||
path.scope.registerBinding( | ||
'type', | ||
path.get('id') as NodePath<any>, | ||
path as NodePath<any> | ||
) | ||
} | ||
|
||
export const TSBindingVisitors: { | ||
TSTypeAliasDeclaration(path: NodePath<t.TSTypeAliasDeclaration>): void | ||
TSInterfaceDeclaration(path: NodePath<t.TSInterfaceDeclaration>): void | ||
} = { | ||
// work around @babel/traverse bug | ||
TSTypeAliasDeclaration(path: NodePath<t.TSTypeAliasDeclaration>) { | ||
path.scope.registerBinding( | ||
'type', | ||
path.get('id') as NodePath<any>, | ||
path as NodePath<any> | ||
) | ||
}, | ||
TSTypeAliasDeclaration: registerIdBinding, | ||
TSInterfaceDeclaration: registerIdBinding, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import * as t from 'typed-validators' | ||
|
||
type Blah = { | ||
bar: number | ||
} | ||
|
||
const BlahType: t.TypeAlias<Blah> = t.alias( | ||
'Blah', | ||
t.object({ | ||
bar: t.number(), | ||
}) | ||
) | ||
|
||
type Test = { | ||
foo: string | ||
blah: Blah | ||
} | ||
|
||
const TestType: t.TypeAlias<Test> = t.alias( | ||
'Test', | ||
t.object({ | ||
foo: t.string(), | ||
blah: t.ref(() => BlahType), | ||
}) | ||
) |
Oops, something went wrong.