Skip to content

Commit

Permalink
fix(typescript): Simplified typescript import
Browse files Browse the repository at this point in the history
  • Loading branch information
grantila committed Mar 19, 2023
1 parent 8d4624c commit 0fc163b
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 67 deletions.
44 changes: 22 additions & 22 deletions lib/core-types-to-ts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ts from 'typescript'
import {
type AndType,
type ArrayType,
Expand All @@ -15,7 +16,6 @@ import {
UnsupportedError,
} from 'core-types'

import { ts, tst } from './ts.js'
import {
createCodeHeader,
generateCode,
Expand Down Expand Up @@ -106,7 +106,7 @@ export function convertSingleCoreTypeToTypeScriptAst(
opts: Pick< ToTsOptions, 'useUnknown' | 'declaration' | 'namespaces' > =
{ }
)
: { declaration: tst.Declaration; namespaceList: string[ ]; }
: { declaration: ts.Declaration; namespaceList: string[ ]; }
{
const {
useUnknown = false,
Expand All @@ -123,7 +123,7 @@ export function convertSingleCoreTypeToTypeScriptAst(

const ret = tsType( ctx, node );

const doExport = ( tsNode: tst.Declaration ) =>
const doExport = ( tsNode: ts.Declaration ) =>
wrapAnnotations( tsNode, node );

const typeDeclaration =
Expand Down Expand Up @@ -166,7 +166,7 @@ function createExportModifier( declaration: boolean )
);
}

function declareType( declaration: boolean, name: string, node: tst.TypeNode )
function declareType( declaration: boolean, name: string, node: ts.TypeNode )
{
return factory.createTypeAliasDeclaration(
createExportModifier( declaration ), // modifiers
Expand All @@ -179,7 +179,7 @@ function declareType( declaration: boolean, name: string, node: tst.TypeNode )
function declareInterface(
declaration: boolean,
name: string,
nodes: Array< tst.TypeElement >
nodes: Array< ts.TypeElement >
)
{
return factory.createInterfaceDeclaration(
Expand All @@ -193,16 +193,16 @@ function declareInterface(

interface TsTypeReturnAsObject {
type: 'object';
node: tst.TypeLiteralNode;
properties: Array< tst.TypeElement >;
node: ts.TypeLiteralNode;
properties: Array< ts.TypeElement >;
}
interface TsTypeReturnAsFlowType {
type: 'flow-type';
node: tst.TypeNode;
node: ts.TypeNode;
}
type TsTypeReturn = TsTypeReturnAsObject | TsTypeReturnAsFlowType;

function tsTypeUnion( ctx: Context, node: OrType ): tst.TypeNode
function tsTypeUnion( ctx: Context, node: OrType ): ts.TypeNode
{
return factory.createUnionTypeNode(
node.or.map( elem =>
Expand All @@ -211,7 +211,7 @@ function tsTypeUnion( ctx: Context, node: OrType ): tst.TypeNode
)
}

function tsTypeIntersection( ctx: Context, node: AndType ): tst.TypeNode
function tsTypeIntersection( ctx: Context, node: AndType ): ts.TypeNode
{
return factory.createIntersectionTypeNode(
node.and.map( elem =>
Expand All @@ -220,23 +220,23 @@ function tsTypeIntersection( ctx: Context, node: AndType ): tst.TypeNode
)
}

function tsTypeAndOrSchema( ctx: Context, node: NodeType ): tst.TypeNode
function tsTypeAndOrSchema( ctx: Context, node: NodeType ): ts.TypeNode
{
if ( node.type === 'and' || node.type === 'or' )
return tsTypeAndOr( ctx, node );
else
return tsType( ctx, node ).node;
}

function tsTypeAndOr( ctx: Context, andOr: AndType | OrType ): tst.TypeNode
function tsTypeAndOr( ctx: Context, andOr: AndType | OrType ): ts.TypeNode
{
if ( andOr.type === 'and' )
return tsTypeIntersection( ctx, andOr );
else
return tsTypeUnion( ctx, andOr );
}

function tsAny( ctx: Context ): tst.TypeNode
function tsAny( ctx: Context ): ts.TypeNode
{
return ctx.useUnknown
? tsUnknownTypeAnnotation( )
Expand Down Expand Up @@ -289,7 +289,7 @@ function tsType( ctx: Context, node: NodeType ): TsTypeReturn
throwUnsupported( `Type ${(node as any).type} not supported`, node );
}

function tsNullType( ): tst.TypeNode
function tsNullType( ): ts.TypeNode
{
return factory.createLiteralTypeNode(
factory.createToken( ts.SyntaxKind.NullKeyword )
Expand All @@ -303,7 +303,7 @@ const primitiveTypeMap = {
boolean: ts.SyntaxKind.BooleanKeyword,
} as const;

function tsPrimitiveType( node: PrimitiveType ): tst.TypeNode
function tsPrimitiveType( node: PrimitiveType ): ts.TypeNode
{
const { type } = node;
if ( type === "null" )
Expand All @@ -316,7 +316,7 @@ function tsPrimitiveType( node: PrimitiveType ): tst.TypeNode
throwUnsupported( `Invalid primitive type: ${type}`, node );
}

function tsConstType( ctx: Context, node: NodeType, value: any ): tst.TypeNode
function tsConstType( ctx: Context, node: NodeType, value: any ): ts.TypeNode
{
return value === "null"
? tsNullType( )
Expand Down Expand Up @@ -345,15 +345,15 @@ function tsArrayConstExpression< T >(
node: NodeType,
value: Array< T >
)
: tst.TypeNode
: ts.TypeNode
{
return factory.createTupleTypeNode(
value.map( elem => tsConstType( ctx, node, elem ) )
)
}

function createAdditionalMembers( ctx: Context, type: true | NodeType )
: tst.IndexSignatureDeclaration
: ts.IndexSignatureDeclaration
{
if ( type === true )
return createAdditionalMembers( ctx, { type: 'any' } );
Expand Down Expand Up @@ -390,7 +390,7 @@ function tsObjectType( ctx: Context, node: ObjectType )
? undefined
: factory.createToken( ts.SyntaxKind.QuestionToken );

const propertyNodes: Array< tst.TypeElement > = [
const propertyNodes: Array< ts.TypeElement > = [
...Object
.keys( properties )
.map( name => ( { name, ...properties[ name ] } ) )
Expand All @@ -413,14 +413,14 @@ function tsObjectType( ctx: Context, node: ObjectType )
return { properties: propertyNodes, node: objectAsNode };
}

function tsSpreadType( ctx: Context, node: NodeType ): tst.TypeNode
function tsSpreadType( ctx: Context, node: NodeType ): ts.TypeNode
{
return factory.createArrayTypeNode(
factory.createRestTypeNode( tsType( ctx, node ).node )
);
}

function tsArrayType( ctx: Context, node: ArrayType | TupleType ): tst.TypeNode
function tsArrayType( ctx: Context, node: ArrayType | TupleType ): ts.TypeNode
{
// TODO: Add support for minItems (making rest arguments optional)
// TODO: Maybe add support for maxItems (turning an array into a tuple of
Expand All @@ -444,7 +444,7 @@ function tsArrayType( ctx: Context, node: ArrayType | TupleType ): tst.TypeNode
);
}

function tsRefType( node: RefType ): tst.TypeNode
function tsRefType( node: RefType ): ts.TypeNode
{
return factory.createTypeReferenceNode( node.ref );
}
21 changes: 10 additions & 11 deletions lib/ts-annotations.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import ts from 'typescript'
import { type CoreTypeAnnotations, mergeAnnotations } from 'core-types'

import { ts, tst } from './ts.js'


interface JSDocContainer {
jsDoc: Array< tst.JSDoc >;
jsDoc: Array< ts.JSDoc >;
}

function extractDescription( text: string | undefined ): CoreTypeAnnotations
Expand All @@ -15,9 +14,9 @@ function extractDescription( text: string | undefined ): CoreTypeAnnotations
return { description: text };
}

function extractTitle( node: tst.Node ): CoreTypeAnnotations
function extractTitle( node: ts.Node ): CoreTypeAnnotations
{
const hasParentWhileUnnamed = ( node: tst.Node ) =>
const hasParentWhileUnnamed = ( node: ts.Node ) =>
node.parent &&
(
ts.isArrayTypeNode( node.parent )
Expand All @@ -31,7 +30,7 @@ function extractTitle( node: tst.Node ): CoreTypeAnnotations
ts.isUnionTypeNode( node.parent )
);

const recurseTypeChain = ( node: tst.Node, child: tst.Node | undefined )
const recurseTypeChain = ( node: ts.Node, child: ts.Node | undefined )
: Array< string > =>
{
if ( !node )
Expand Down Expand Up @@ -96,7 +95,7 @@ function extractTitle( node: tst.Node ): CoreTypeAnnotations
}

function stringifyDoc(
text: undefined | string | tst.NodeArray< tst.JSDocComment >
text: undefined | string | ts.NodeArray< ts.JSDocComment >
)
: string | undefined
{
Expand All @@ -106,14 +105,14 @@ function stringifyDoc(
return text.map( ( { text } ) => text ).join( ' ' );
}

function extractTags( tags: ReadonlyArray< tst.JSDocTag > ): CoreTypeAnnotations
function extractTags( tags: ReadonlyArray< ts.JSDocTag > ): CoreTypeAnnotations
{
const descriptions: Array< string > = [ ];
const examples: Array< string > = [ ];
const _default: Array< string > = [ ];
const see: Array< string > = [ ];

const extractSee = ( tag: tst.JSDocSeeTag ) =>
const extractSee = ( tag: ts.JSDocSeeTag ) =>
( tag.name ? ( tag.name?.getText( ) + ' ' ) : '' ) +
stringifyDoc( tag.comment )?.trim( ) ?? '';

Expand All @@ -127,7 +126,7 @@ function extractTags( tags: ReadonlyArray< tst.JSDocTag > ): CoreTypeAnnotations
else if ( tag.tagName.text === 'default' )
_default.push( stringifyDoc( tag.comment )?.trim( ) ?? '' );
else if ( tag.tagName.text === 'see' )
see.push( extractSee( tag as tst.JSDocSeeTag ) );
see.push( extractSee( tag as ts.JSDocSeeTag ) );
else
{
const text = stringifyDoc( tag.comment )?.trim( ) ?? '';
Expand All @@ -153,7 +152,7 @@ export interface DecorateNodeOptions {
includeJsDoc?: boolean;
}

export function decorateNode( node: tst.Node, options?: DecorateNodeOptions )
export function decorateNode( node: ts.Node, options?: DecorateNodeOptions )
: CoreTypeAnnotations
{
const { includeJsDoc = true } = options ?? { };
Expand Down
14 changes: 7 additions & 7 deletions lib/ts-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import ts from 'typescript'
import {
type CoreTypeAnnotations,
type Location,
stringifyAnnotations,
} from 'core-types'

import { ts, tst } from './ts.js'
import type { ToTsOptions } from './types.js'


Expand All @@ -15,14 +15,14 @@ export function tsUnknownTypeAnnotation( )
return factory.createToken( ts.SyntaxKind.UnknownKeyword );
}

export function safeName( name: string ): tst.StringLiteral | tst.Identifier
export function safeName( name: string ): ts.StringLiteral | ts.Identifier
{
if ( name.match( /^[a-zA-Z$][a-zA-Z0-9_$]*$/ ) )
return factory.createIdentifier( name );
return factory.createStringLiteral( name );
}

export function wrapAnnotations<T extends tst.Node>(
export function wrapAnnotations<T extends ts.Node>(
tsNode: T,
node: CoreTypeAnnotations,
blockComment = true
Expand Down Expand Up @@ -81,7 +81,7 @@ function starBefore( lines: Array< string > ): string
return lines.map( line => ` * ${line}` ).join( "\n" );
}

export function generateCode( node: tst.Node ): string
export function generateCode( node: ts.Node ): string
{
const printer = ts.createPrinter( { newLine: ts.NewLineKind.LineFeed } );
const resultFile = ts.createSourceFile(
Expand All @@ -95,12 +95,12 @@ export function generateCode( node: tst.Node ): string
return s;
}

export function tsStripOptionalType( node: tst.TypeNode ): tst.TypeNode
export function tsStripOptionalType( node: ts.TypeNode ): ts.TypeNode
{
return ts.isOptionalTypeNode( node ) ? node.type : node;
}

export function toLocation( node: tst.Node ): Location
export function toLocation( node: ts.Node ): Location
{
return {
start: node.pos,
Expand All @@ -109,7 +109,7 @@ export function toLocation( node: tst.Node ): Location
}

export function isExportedDeclaration(
node: tst.TypeAliasDeclaration | tst.InterfaceDeclaration
node: ts.TypeAliasDeclaration | ts.InterfaceDeclaration
)
{
return !!node.modifiers?.some( modifier =>
Expand Down
Loading

0 comments on commit 0fc163b

Please sign in to comment.