@kubb/ts-codegen
Advanced tools
Comparing version 1.1.7 to 1.1.8
import ts from 'typescript'; | ||
declare const modifier: { | ||
type ArrayTwoOrMore<T> = { | ||
0: T; | ||
1: T; | ||
} & Array<T>; | ||
declare const modifiers: { | ||
readonly async: ts.ModifierToken<ts.SyntaxKind.AsyncKeyword>; | ||
readonly export: ts.ModifierToken<ts.SyntaxKind.ExportKeyword>; | ||
readonly const: ts.ModifierToken<ts.SyntaxKind.ConstKeyword>; | ||
readonly static: ts.ModifierToken<ts.SyntaxKind.StaticKeyword>; | ||
}; | ||
declare function isValidIdentifier(str: string): boolean; | ||
declare const questionToken: ts.PunctuationToken<ts.SyntaxKind.QuestionToken>; | ||
declare function createQuestionToken(token?: boolean | ts.QuestionToken): ts.PunctuationToken<ts.SyntaxKind.QuestionToken> | undefined; | ||
declare function createIntersectionDeclaration({ nodes }: { | ||
nodes: ArrayTwoOrMore<ts.TypeNode>; | ||
}): ts.IntersectionTypeNode; | ||
/** | ||
* | ||
* Minimum nodes lenght of 2 | ||
* @example `string & number` | ||
*/ | ||
declare function createTupleDeclaration({ nodes }: { | ||
nodes: ArrayTwoOrMore<ts.TypeNode>; | ||
}): ts.TupleTypeNode; | ||
/** | ||
* | ||
* Minimum nodes lenght of 2 | ||
* @example `string | number` | ||
*/ | ||
declare function createUnionDeclaration({ nodes }: { | ||
nodes: ArrayTwoOrMore<ts.TypeNode>; | ||
}): ts.UnionTypeNode; | ||
declare function createPropertySignature({ modifiers, name, questionToken, type, }: { | ||
@@ -16,2 +40,10 @@ modifiers?: Array<ts.Modifier>; | ||
}): ts.PropertySignature; | ||
declare function createParameterSignature(name: string | ts.BindingName, { modifiers, dotDotDotToken, questionToken, type, initializer, }: { | ||
decorators?: Array<ts.Decorator>; | ||
modifiers?: Array<ts.Modifier>; | ||
dotDotDotToken?: ts.DotDotDotToken; | ||
questionToken?: ts.QuestionToken | boolean; | ||
type?: ts.TypeNode; | ||
initializer?: ts.Expression; | ||
}): ts.ParameterDeclaration; | ||
declare function createJSDoc({ comments }: { | ||
@@ -27,10 +59,2 @@ comments: string[]; | ||
}): TNode; | ||
declare function createParameter(name: string | ts.BindingName, { modifiers, dotDotDotToken, questionToken, type, initializer, }: { | ||
decorators?: Array<ts.Decorator>; | ||
modifiers?: Array<ts.Modifier>; | ||
dotDotDotToken?: ts.DotDotDotToken; | ||
questionToken?: ts.QuestionToken | boolean; | ||
type?: ts.TypeNode; | ||
initializer?: ts.Expression; | ||
}): ts.ParameterDeclaration; | ||
declare function createIndexSignature(type: ts.TypeNode, { modifiers, indexName, indexType, }?: { | ||
@@ -61,10 +85,14 @@ indexName?: string; | ||
}): ts.ImportDeclaration; | ||
declare function createExportDeclaration({ path, asAlias, name }: { | ||
declare function createExportDeclaration({ path, asAlias, isTypeOnly, name, }: { | ||
path: string; | ||
asAlias?: boolean; | ||
isTypeOnly?: boolean; | ||
name?: string | Array<ts.Identifier | string>; | ||
}): ts.ExportDeclaration; | ||
declare function createEnumDeclaration({ name, typeName, enums, type, }: { | ||
type: 'enum' | 'asConst' | 'asPascalConst'; | ||
declare function createEnumDeclaration({ type, name, typeName, enums, }: { | ||
/** | ||
* @default `'enum'` | ||
*/ | ||
type?: 'enum' | 'asConst' | 'asPascalConst'; | ||
/** | ||
* Enum name in camelCase. | ||
@@ -77,16 +105,7 @@ */ | ||
typeName: string; | ||
enums: [key: string, value: string | number][]; | ||
enums: [key: string, value: string | number | boolean][]; | ||
}): ts.EnumDeclaration[] | (ts.TypeAliasDeclaration | ts.VariableStatement)[]; | ||
declare function createIntersectionDeclaration({ nodes }: { | ||
nodes: ts.TypeNode[]; | ||
}): ts.IntersectionTypeNode; | ||
declare function createTupleDeclaration({ nodes }: { | ||
nodes: ts.TypeNode[]; | ||
}): ts.TupleTypeNode; | ||
declare function createUnionDeclaration({ nodes }: { | ||
nodes: ts.TypeNode[]; | ||
}): ts.UnionTypeNode; | ||
declare function print(elements: ts.Node | Array<ts.Node | undefined>, fileName?: string): string; | ||
export { appendJSDocToNode, createEnumDeclaration, createExportDeclaration, createImportDeclaration, createIndexSignature, createIntersectionDeclaration, createJSDoc, createParameter, createPropertySignature, createQuestionToken, createTupleDeclaration, createTypeAliasDeclaration, createUnionDeclaration, isValidIdentifier, modifier, print, questionToken }; | ||
export { ArrayTwoOrMore, appendJSDocToNode, createEnumDeclaration, createExportDeclaration, createImportDeclaration, createIndexSignature, createIntersectionDeclaration, createJSDoc, createParameterSignature, createPropertySignature, createQuestionToken, createTupleDeclaration, createTypeAliasDeclaration, createUnionDeclaration, modifiers, print }; |
@@ -6,5 +6,7 @@ import { createRequire } from 'module'; | ||
var { factory } = ts; | ||
var modifier = { | ||
var modifiers = { | ||
async: factory.createModifier(ts.SyntaxKind.AsyncKeyword), | ||
export: factory.createModifier(ts.SyntaxKind.ExportKeyword) | ||
export: factory.createModifier(ts.SyntaxKind.ExportKeyword), | ||
const: factory.createModifier(ts.SyntaxKind.ConstKeyword), | ||
static: factory.createModifier(ts.SyntaxKind.StaticKeyword) | ||
}; | ||
@@ -31,4 +33,13 @@ function isValidIdentifier(str) { | ||
} | ||
function createIntersectionDeclaration({ nodes }) { | ||
return factory.createIntersectionTypeNode(nodes); | ||
} | ||
function createTupleDeclaration({ nodes }) { | ||
return factory.createTupleTypeNode(nodes); | ||
} | ||
function createUnionDeclaration({ nodes }) { | ||
return factory.createUnionTypeNode(nodes); | ||
} | ||
function createPropertySignature({ | ||
modifiers, | ||
modifiers: modifiers2, | ||
name, | ||
@@ -38,9 +49,23 @@ questionToken: questionToken2, | ||
}) { | ||
return factory.createPropertySignature(modifiers, propertyName(name), createQuestionToken(questionToken2), type); | ||
return factory.createPropertySignature(modifiers2, propertyName(name), createQuestionToken(questionToken2), type); | ||
} | ||
function createParameterSignature(name, { | ||
modifiers: modifiers2, | ||
dotDotDotToken, | ||
questionToken: questionToken2, | ||
type, | ||
initializer | ||
}) { | ||
return factory.createParameterDeclaration(modifiers2, dotDotDotToken, name, createQuestionToken(questionToken2), type, initializer); | ||
} | ||
function createJSDoc({ comments }) { | ||
return factory.createJSDocComment( | ||
factory.createNodeArray( | ||
comments?.map((comment, i) => i === comments.length - 1 ? factory.createJSDocText(comment) : factory.createJSDocText(`${comment} | ||
`)) | ||
comments?.map((comment, i) => { | ||
if (i === comments.length - 1) { | ||
return factory.createJSDocText(comment); | ||
} | ||
return factory.createJSDocText(`${comment} | ||
`); | ||
}) | ||
) | ||
@@ -58,23 +83,14 @@ ); | ||
}, "*"); | ||
return ts.addSyntheticLeadingComment(node, ts.SyntaxKind.MultiLineCommentTrivia, `${text || "*"} | ||
return ts.addSyntheticLeadingComment({ ...node }, ts.SyntaxKind.MultiLineCommentTrivia, `${text || "*"} | ||
`, true); | ||
} | ||
function createParameter(name, { | ||
modifiers, | ||
dotDotDotToken, | ||
questionToken: questionToken2, | ||
type, | ||
initializer | ||
}) { | ||
return factory.createParameterDeclaration(modifiers, dotDotDotToken, name, createQuestionToken(questionToken2), type, initializer); | ||
} | ||
function createIndexSignature(type, { | ||
modifiers, | ||
modifiers: modifiers2, | ||
indexName = "key", | ||
indexType = factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword) | ||
} = {}) { | ||
return factory.createIndexSignature(modifiers, [createParameter(indexName, { type: indexType })], type); | ||
return factory.createIndexSignature(modifiers2, [createParameterSignature(indexName, { type: indexType })], type); | ||
} | ||
function createTypeAliasDeclaration({ | ||
modifiers, | ||
modifiers: modifiers2, | ||
name, | ||
@@ -84,3 +100,3 @@ typeParameters, | ||
}) { | ||
return factory.createTypeAliasDeclaration(modifiers, name, typeParameters, type); | ||
return factory.createTypeAliasDeclaration(modifiers2, name, typeParameters, type); | ||
} | ||
@@ -110,13 +126,5 @@ function createImportDeclaration({ | ||
if (obj.name) { | ||
return factory.createImportSpecifier( | ||
false, | ||
typeof obj.propertyName === "string" ? factory.createIdentifier(obj.propertyName) : obj.propertyName, | ||
factory.createIdentifier(obj.name) | ||
); | ||
return factory.createImportSpecifier(false, factory.createIdentifier(obj.propertyName), factory.createIdentifier(obj.name)); | ||
} | ||
return factory.createImportSpecifier( | ||
false, | ||
void 0, | ||
typeof obj.propertyName === "string" ? factory.createIdentifier(obj.propertyName) : obj.propertyName | ||
); | ||
return factory.createImportSpecifier(false, void 0, factory.createIdentifier(obj.propertyName)); | ||
} | ||
@@ -131,7 +139,15 @@ return factory.createImportSpecifier(false, void 0, typeof propertyName2 === "string" ? factory.createIdentifier(propertyName2) : propertyName2); | ||
} | ||
function createExportDeclaration({ path, asAlias, name }) { | ||
function createExportDeclaration({ | ||
path, | ||
asAlias, | ||
isTypeOnly = false, | ||
name | ||
}) { | ||
if (name && !Array.isArray(name) && !asAlias) { | ||
throw new Error("When using `name` as string, `asAlias` should be true"); | ||
} | ||
if (!Array.isArray(name)) { | ||
return factory.createExportDeclaration( | ||
void 0, | ||
false, | ||
isTypeOnly, | ||
asAlias && name ? factory.createNamespaceExport(factory.createIdentifier(name)) : void 0, | ||
@@ -144,3 +160,3 @@ factory.createStringLiteral(path), | ||
void 0, | ||
false, | ||
isTypeOnly, | ||
factory.createNamedExports( | ||
@@ -156,6 +172,6 @@ name.map((propertyName2) => { | ||
function createEnumDeclaration({ | ||
type = "enum", | ||
name, | ||
typeName, | ||
enums, | ||
type | ||
enums | ||
}) { | ||
@@ -168,6 +184,10 @@ if (type === "enum") { | ||
enums.map(([key, value]) => { | ||
return factory.createEnumMember( | ||
factory.createStringLiteral(`${key}`), | ||
typeof value === "number" ? factory.createNumericLiteral(value) : factory.createStringLiteral(`${value}`) | ||
); | ||
let initializer = factory.createStringLiteral(`${value}`); | ||
if (typeof value === "number") { | ||
initializer = factory.createNumericLiteral(value); | ||
} | ||
if (typeof value === "boolean") { | ||
initializer = value ? factory.createTrue() : factory.createFalse(); | ||
} | ||
return factory.createEnumMember(factory.createStringLiteral(`${key}`), initializer); | ||
}) | ||
@@ -190,6 +210,10 @@ ) | ||
enums.map(([key, value]) => { | ||
return factory.createPropertyAssignment( | ||
factory.createStringLiteral(`${key}`), | ||
typeof value === "number" ? factory.createNumericLiteral(value) : factory.createStringLiteral(`${value}`) | ||
); | ||
let initializer = factory.createStringLiteral(`${value}`); | ||
if (typeof value === "number") { | ||
initializer = factory.createNumericLiteral(value); | ||
} | ||
if (typeof value === "boolean") { | ||
initializer = value ? factory.createTrue() : factory.createFalse(); | ||
} | ||
return factory.createPropertyAssignment(factory.createStringLiteral(`${key}`), initializer); | ||
}), | ||
@@ -216,11 +240,2 @@ true | ||
} | ||
function createIntersectionDeclaration({ nodes }) { | ||
return factory.createIntersectionTypeNode(nodes); | ||
} | ||
function createTupleDeclaration({ nodes }) { | ||
return factory.createTupleTypeNode(nodes); | ||
} | ||
function createUnionDeclaration({ nodes }) { | ||
return factory.createUnionTypeNode(nodes); | ||
} | ||
var { factory: factory2 } = ts; | ||
@@ -241,2 +256,2 @@ function print(elements, fileName = "print.ts") { | ||
export { appendJSDocToNode, createEnumDeclaration, createExportDeclaration, createImportDeclaration, createIndexSignature, createIntersectionDeclaration, createJSDoc, createParameter, createPropertySignature, createQuestionToken, createTupleDeclaration, createTypeAliasDeclaration, createUnionDeclaration, isValidIdentifier, modifier, print, questionToken }; | ||
export { appendJSDocToNode, createEnumDeclaration, createExportDeclaration, createImportDeclaration, createIndexSignature, createIntersectionDeclaration, createJSDoc, createParameterSignature, createPropertySignature, createQuestionToken, createTupleDeclaration, createTypeAliasDeclaration, createUnionDeclaration, modifiers, print }; |
{ | ||
"name": "@kubb/ts-codegen", | ||
"version": "1.1.7", | ||
"version": "1.1.8", | ||
"description": "Generator ts-codegen", | ||
"keywords": [ | ||
"typescript", | ||
"plugins", | ||
"kubb", | ||
"codegen" | ||
], | ||
"repository": { | ||
@@ -12,13 +18,4 @@ "type": "git", | ||
"author": "Stijn Van Hulle <stijn@stijnvanhulle.be", | ||
"keywords": [ | ||
"typescript", | ||
"plugins", | ||
"kubb", | ||
"codegen" | ||
], | ||
"sideEffects": false, | ||
"type": "module", | ||
"main": "dist/index.js", | ||
"module": "dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
@@ -33,2 +30,5 @@ ".": { | ||
}, | ||
"main": "dist/index.js", | ||
"module": "dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"typesVersions": { | ||
@@ -50,5 +50,11 @@ "*": {} | ||
"tsup": "^6.7.0", | ||
"@kubb/eslint-config": "0.1.0", | ||
"@kubb/tsup-config": "0.1.0", | ||
"@kubb/eslint-config": "0.1.0" | ||
"@kubb/typescript-config": "0.1.0" | ||
}, | ||
"packageManager": "pnpm@8.3.0", | ||
"engines": { | ||
"node": ">=18", | ||
"pnpm": ">=8.3.0" | ||
}, | ||
"publishConfig": { | ||
@@ -58,16 +64,13 @@ "access": "public", | ||
}, | ||
"engines": { | ||
"node": ">=18", | ||
"pnpm": ">=8" | ||
}, | ||
"scripts": { | ||
"build": "tsup", | ||
"start": "tsup --watch", | ||
"release": "pnpm publish --no-git-check", | ||
"clean": "rimraf ./dist", | ||
"lint": "eslint \"**/*.{ts,tsx}\"", | ||
"lint-fix": "eslint \"**/*.{ts,tsx}\" --quiet --fix", | ||
"release": "pnpm publish --no-git-check", | ||
"start": "tsup --watch", | ||
"test": "vitest --passWithNoTests", | ||
"upgrade": "pnpm update", | ||
"typecheck": "tsc -p ./tsconfig.json --noEmit --emitDeclarationOnly false" | ||
"typecheck": "tsc -p ./tsconfig.json --noEmit --emitDeclarationOnly false", | ||
"upgrade": "pnpm update" | ||
} | ||
} |
@@ -8,36 +8,43 @@ <div align="center"> | ||
TypeScript codegen utils | ||
</p> | ||
</p> | ||
<img src="https://raw.githubusercontent.com/kubb-project/kubb/main/assets/banner.png" alt="logo" height="auto" /> | ||
<!-- Badges --> | ||
<p> | ||
<a href="https://www.npmjs.com/package/@kubb/ts-codegen"> | ||
<!-- Badges --> | ||
<p> | ||
<a href="https://www.npmjs.com/package/@kubb/ts-codegen" target="_blank"> | ||
<img alt="npm version" src="https://img.shields.io/npm/v/@kubb/ts-codegen?style=for-the-badge"/> | ||
</a> | ||
<a href="https://www.npmjs.com/package/@kubb/ts-codegen"> | ||
<img alt="npm downloads" src="https://img.shields.io/bundlephobia/min/@kubb/ts-codegen?style=for-the-badge"/> | ||
</a> | ||
<a href="https://www.npmjs.com/package/@kubb/ts-codegen"> | ||
<a href="https://www.npmjs.com/package/@kubb/ts-codegen" target="_blank"> | ||
<img alt="npm downloads" src="https://img.shields.io/npm/dm/@kubb/ts-codegen?style=for-the-badge"/> | ||
</a> | ||
</p> | ||
</p> | ||
<p> | ||
<a href="https://www.npmjs.com/package/@kubb/ts-codegen" target="_blank"> | ||
<img alt="Minified size" src="https://img.shields.io/bundlephobia/min/@kubb/ts-codegen?style=for-the-badge"/> | ||
</a> | ||
<a href="https://www.npmjs.com/package/@kubb/ts-codegen" target="_blank"> | ||
<img alt="Coverage" src="https://img.shields.io/codecov/c/github/kubb-project/kubb?style=for-the-badge"/> | ||
</a> | ||
<a href="https://www.npmjs.com/package/@kubb/ts-codegen" target="_blank"> | ||
<img alt="Build status" src="https://img.shields.io/github/actions/workflow/status/kubb-project/kubb/ci.yaml?style=for-the-badge"/> | ||
</a> | ||
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --> | ||
<!-- ALL-CONTRIBUTORS-BADGE:END --> | ||
</p> | ||
<h4> | ||
<a href="https://codesandbox.io/s/github/kubb-project/kubb/tree/main/examples/simple">View Demo</a> | ||
<span> · </span> | ||
<a href="https://kubb.dev/" target="_blank">Documentation</a> | ||
<span> · </span> | ||
<a href="https://github.com/kubb-project/kubb/issues/">Report Bug</a> | ||
<span> · </span> | ||
<a href="https://github.com/kubb-project/kubb/issues/">Request Feature</a> | ||
<h4> | ||
<a href="https://codesandbox.io/s/github/kubb-project/kubb/tree/main/examples/simple" target="_blank">View Demo</a> | ||
<span> · </span> | ||
<a href="https://kubb.dev/" target="_blank">Documentation</a> | ||
<span> · </span> | ||
<a href="https://github.com/kubb-project/kubb/issues/" target="_blank">Report Bug</a> | ||
<span> · </span> | ||
<a href="https://github.com/kubb-project/kubb/issues/" target="_blank">Request Feature</a> | ||
</h4> | ||
</div> | ||
<br /> | ||
<!-- About the Project | ||
## :star2: About the Project | ||
<div align="center"> | ||
<img src="assets/screenshot.jpg" alt="screenshot" /> | ||
</div> | ||
--> |
import ts from 'typescript' | ||
import type { ArrayTwoOrMore } from './types' | ||
const { factory } = ts | ||
export const modifier = { | ||
// https://ts-ast-viewer.com/ | ||
export const modifiers = { | ||
async: factory.createModifier(ts.SyntaxKind.AsyncKeyword), | ||
export: factory.createModifier(ts.SyntaxKind.ExportKeyword), | ||
const: factory.createModifier(ts.SyntaxKind.ConstKeyword), | ||
static: factory.createModifier(ts.SyntaxKind.StaticKeyword), | ||
} as const | ||
export function isValidIdentifier(str: string) { | ||
function isValidIdentifier(str: string) { | ||
if (!str.length || str.trim() !== str) return false | ||
@@ -24,3 +30,3 @@ const node = ts.parseIsolatedEntityName(str, ts.ScriptTarget.Latest) | ||
export const questionToken = factory.createToken(ts.SyntaxKind.QuestionToken) | ||
const questionToken = factory.createToken(ts.SyntaxKind.QuestionToken) | ||
@@ -33,2 +39,23 @@ export function createQuestionToken(token?: boolean | ts.QuestionToken) { | ||
export function createIntersectionDeclaration({ nodes }: { nodes: ArrayTwoOrMore<ts.TypeNode> }) { | ||
return factory.createIntersectionTypeNode(nodes) | ||
} | ||
/** | ||
* | ||
* Minimum nodes lenght of 2 | ||
* @example `string & number` | ||
*/ | ||
export function createTupleDeclaration({ nodes }: { nodes: ArrayTwoOrMore<ts.TypeNode> }) { | ||
return factory.createTupleTypeNode(nodes) | ||
} | ||
/** | ||
* | ||
* Minimum nodes lenght of 2 | ||
* @example `string | number` | ||
*/ | ||
export function createUnionDeclaration({ nodes }: { nodes: ArrayTwoOrMore<ts.TypeNode> }) { | ||
return factory.createUnionTypeNode(nodes) | ||
} | ||
export function createPropertySignature({ | ||
@@ -48,6 +75,32 @@ modifiers, | ||
export function createParameterSignature( | ||
name: string | ts.BindingName, | ||
{ | ||
modifiers, | ||
dotDotDotToken, | ||
questionToken, | ||
type, | ||
initializer, | ||
}: { | ||
decorators?: Array<ts.Decorator> | ||
modifiers?: Array<ts.Modifier> | ||
dotDotDotToken?: ts.DotDotDotToken | ||
questionToken?: ts.QuestionToken | boolean | ||
type?: ts.TypeNode | ||
initializer?: ts.Expression | ||
} | ||
): ts.ParameterDeclaration { | ||
return factory.createParameterDeclaration(modifiers, dotDotDotToken, name, createQuestionToken(questionToken), type, initializer) | ||
} | ||
export function createJSDoc({ comments }: { comments: string[] }) { | ||
return factory.createJSDocComment( | ||
factory.createNodeArray( | ||
comments?.map((comment, i) => (i === comments.length - 1 ? factory.createJSDocText(comment) : factory.createJSDocText(`${comment}\n`))) | ||
comments?.map((comment, i) => { | ||
if (i === comments.length - 1) { | ||
return factory.createJSDocText(comment) | ||
} | ||
return factory.createJSDocText(`${comment}\n`) | ||
}) | ||
) | ||
@@ -71,25 +124,6 @@ ) | ||
return ts.addSyntheticLeadingComment(node, ts.SyntaxKind.MultiLineCommentTrivia, `${text || '*'}\n`, true) | ||
// node: {...node}, with that ts.addSyntheticLeadingComment is appending | ||
return ts.addSyntheticLeadingComment({ ...node }, ts.SyntaxKind.MultiLineCommentTrivia, `${text || '*'}\n`, true) | ||
} | ||
export function createParameter( | ||
name: string | ts.BindingName, | ||
{ | ||
modifiers, | ||
dotDotDotToken, | ||
questionToken, | ||
type, | ||
initializer, | ||
}: { | ||
decorators?: Array<ts.Decorator> | ||
modifiers?: Array<ts.Modifier> | ||
dotDotDotToken?: ts.DotDotDotToken | ||
questionToken?: ts.QuestionToken | boolean | ||
type?: ts.TypeNode | ||
initializer?: ts.Expression | ||
} | ||
): ts.ParameterDeclaration { | ||
return factory.createParameterDeclaration(modifiers, dotDotDotToken, name, createQuestionToken(questionToken), type, initializer) | ||
} | ||
export function createIndexSignature( | ||
@@ -108,3 +142,3 @@ type: ts.TypeNode, | ||
) { | ||
return factory.createIndexSignature(modifiers, [createParameter(indexName, { type: indexType })], type) | ||
return factory.createIndexSignature(modifiers, [createParameterSignature(indexName, { type: indexType })], type) | ||
} | ||
@@ -158,13 +192,5 @@ | ||
if (obj.name) { | ||
return factory.createImportSpecifier( | ||
false, | ||
typeof obj.propertyName === 'string' ? factory.createIdentifier(obj.propertyName) : obj.propertyName, | ||
factory.createIdentifier(obj.name) | ||
) | ||
return factory.createImportSpecifier(false, factory.createIdentifier(obj.propertyName), factory.createIdentifier(obj.name)) | ||
} | ||
return factory.createImportSpecifier( | ||
false, | ||
undefined, | ||
typeof obj.propertyName === 'string' ? factory.createIdentifier(obj.propertyName) : obj.propertyName | ||
) | ||
return factory.createImportSpecifier(false, undefined, factory.createIdentifier(obj.propertyName)) | ||
} | ||
@@ -181,7 +207,21 @@ | ||
export function createExportDeclaration({ path, asAlias, name }: { path: string; asAlias?: boolean; name?: string | Array<ts.Identifier | string> }) { | ||
export function createExportDeclaration({ | ||
path, | ||
asAlias, | ||
isTypeOnly = false, | ||
name, | ||
}: { | ||
path: string | ||
asAlias?: boolean | ||
isTypeOnly?: boolean | ||
name?: string | Array<ts.Identifier | string> | ||
}) { | ||
if (name && !Array.isArray(name) && !asAlias) { | ||
throw new Error('When using `name` as string, `asAlias` should be true') | ||
} | ||
if (!Array.isArray(name)) { | ||
return factory.createExportDeclaration( | ||
undefined, | ||
false, | ||
isTypeOnly, | ||
asAlias && name ? factory.createNamespaceExport(factory.createIdentifier(name)) : undefined, | ||
@@ -195,3 +235,3 @@ factory.createStringLiteral(path), | ||
undefined, | ||
false, | ||
isTypeOnly, | ||
factory.createNamedExports( | ||
@@ -208,9 +248,12 @@ name.map((propertyName) => { | ||
export function createEnumDeclaration({ | ||
type = 'enum', | ||
name, | ||
typeName, | ||
enums, | ||
type, | ||
}: { | ||
type: 'enum' | 'asConst' | 'asPascalConst' | ||
/** | ||
* @default `'enum'` | ||
*/ | ||
type?: 'enum' | 'asConst' | 'asPascalConst' | ||
/** | ||
* Enum name in camelCase. | ||
@@ -223,3 +266,3 @@ */ | ||
typeName: string | ||
enums: [key: string, value: string | number][] | ||
enums: [key: string, value: string | number | boolean][] | ||
}) { | ||
@@ -232,6 +275,12 @@ if (type === 'enum') { | ||
enums.map(([key, value]) => { | ||
return factory.createEnumMember( | ||
factory.createStringLiteral(`${key}`), | ||
typeof value === 'number' ? factory.createNumericLiteral(value) : factory.createStringLiteral(`${value}`) | ||
) | ||
let initializer: ts.Expression = factory.createStringLiteral(`${value}`) | ||
if (typeof value === 'number') { | ||
initializer = factory.createNumericLiteral(value) | ||
} | ||
if (typeof value === 'boolean') { | ||
initializer = value ? factory.createTrue() : factory.createFalse() | ||
} | ||
return factory.createEnumMember(factory.createStringLiteral(`${key}`), initializer) | ||
}) | ||
@@ -257,6 +306,12 @@ ), | ||
enums.map(([key, value]) => { | ||
return factory.createPropertyAssignment( | ||
factory.createStringLiteral(`${key}`), | ||
typeof value === 'number' ? factory.createNumericLiteral(value) : factory.createStringLiteral(`${value}`) | ||
) | ||
let initializer: ts.Expression = factory.createStringLiteral(`${value}`) | ||
if (typeof value === 'number') { | ||
initializer = factory.createNumericLiteral(value) | ||
} | ||
if (typeof value === 'boolean') { | ||
initializer = value ? factory.createTrue() : factory.createFalse() | ||
} | ||
return factory.createPropertyAssignment(factory.createStringLiteral(`${key}`), initializer) | ||
}), | ||
@@ -283,13 +338,1 @@ true | ||
} | ||
export function createIntersectionDeclaration({ nodes }: { nodes: ts.TypeNode[] }) { | ||
return factory.createIntersectionTypeNode(nodes) | ||
} | ||
export function createTupleDeclaration({ nodes }: { nodes: ts.TypeNode[] }) { | ||
return factory.createTupleTypeNode(nodes) | ||
} | ||
export function createUnionDeclaration({ nodes }: { nodes: ts.TypeNode[] }) { | ||
return factory.createUnionTypeNode(nodes) | ||
} |
export * from './codegen.ts' | ||
export * from './print.ts' | ||
export * from './types.ts' |
Sorry, the diff of this file is not supported yet
37161
10
920
50
5