Skip to content

Commit

Permalink
feat!: migrate to typescript v5
Browse files Browse the repository at this point in the history
BREAKING CHANGE: removed all decorators, use modifiers instead as specified by API v5 of Typescript compiler
  • Loading branch information
mtrimolet committed Sep 12, 2023
1 parent 9e9a487 commit 84d2bb0
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 48 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ async function generateClient(path: string): Promise<string> {
|--------------------|-----------------|
| v3.x.x | v1 |
| v4.x.x | v2 |
| v5.x.x | v3 |

## License

Expand Down
51 changes: 7 additions & 44 deletions packages/core/lib/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,17 @@ import {
} from "./expression";

export function createTypeAliasDeclaration({
decorators,
modifiers,
name,
typeParameters,
type
}: {
decorators?: ts.Decorator[];
modifiers?: ts.Modifier[];
name: string | ts.Identifier;
typeParameters?: ts.TypeParameterDeclaration[];
type: ts.TypeNode;
}): ts.TypeAliasDeclaration {
return ts.factory.createTypeAliasDeclaration(
decorators,
modifiers,
name,
typeParameters,
Expand All @@ -37,13 +34,11 @@ export function createTypeAliasDeclaration({
export function createFunctionDeclaration(
name: string | ts.Identifier | undefined,
{
decorators,
modifiers,
asteriskToken,
typeParameters,
type
}: {
decorators?: ts.Decorator[];
modifiers?: ts.Modifier[];
asteriskToken?: ts.AsteriskToken;
typeParameters?: ts.TypeParameterDeclaration[];
Expand All @@ -53,7 +48,6 @@ export function createFunctionDeclaration(
body?: ts.Block
): ts.FunctionDeclaration {
return ts.factory.createFunctionDeclaration(
decorators,
modifiers,
asteriskToken,
name,
Expand All @@ -65,22 +59,19 @@ export function createFunctionDeclaration(
}

export function createInterfaceDeclaration({
decorators,
modifiers,
name,
typeParameters,
heritageClauses,
members
}: {
decorators?: ts.Decorator[];
modifiers?: ts.Modifier[];
name: string | ts.Identifier;
typeParameters?: ts.TypeParameterDeclaration[];
heritageClauses?: ts.HeritageClause[];
members: readonly ts.TypeElement[];
}): ts.InterfaceDeclaration {
return ts.factory.createInterfaceDeclaration(
decorators,
modifiers,
name,
typeParameters,
Expand All @@ -90,22 +81,19 @@ export function createInterfaceDeclaration({
}

export function createClassDeclaration({
decorators,
modifiers,
name,
typeParameters,
heritageClauses,
members
}: {
decorators?: ts.Decorator[];
modifiers?: ts.Modifier[];
name?: string | ts.Identifier;
typeParameters?: ts.TypeParameterDeclaration[];
heritageClauses?: ts.HeritageClause[];
members: ts.ClassElement[];
}): ts.ClassDeclaration {
return ts.factory.createClassDeclaration(
decorators,
modifiers,
name,
typeParameters,
Expand All @@ -115,17 +103,15 @@ export function createClassDeclaration({
}

export function createConstructor({
decorators,
modifiers,
parameters,
body
}: {
decorators?: ts.Decorator[];
modifiers?: ts.Modifier[];
parameters: ts.ParameterDeclaration[];
body?: ts.Block;
}): ts.ConstructorDeclaration {
return ts.factory.createConstructorDeclaration(decorators, modifiers, parameters, body);
return ts.factory.createConstructorDeclaration(modifiers, parameters, body);
}

export function createMethod(
Expand All @@ -136,14 +122,12 @@ export function createMethod(
| ts.NumericLiteral
| ts.ComputedPropertyName,
{
decorators,
modifiers,
asteriskToken,
questionToken,
typeParameters,
type
}: {
decorators?: ts.Decorator[];
modifiers?: ts.Modifier[];
asteriskToken?: ts.AsteriskToken;
questionToken?: ts.QuestionToken | boolean;
Expand All @@ -154,7 +138,6 @@ export function createMethod(
body?: ts.Block
): ts.MethodDeclaration {
return ts.factory.createMethodDeclaration(
decorators,
modifiers,
asteriskToken,
name,
Expand All @@ -169,14 +152,12 @@ export function createMethod(
export function createParameter(
name: string | ts.BindingName,
{
decorators,
modifiers,
dotDotDotToken,
questionToken,
type,
initializer
}: {
decorators?: ts.Decorator[];
modifiers?: ts.Modifier[];
dotDotDotToken?: ts.DotDotDotToken;
questionToken?: ts.QuestionToken | boolean;
Expand All @@ -185,7 +166,6 @@ export function createParameter(
}
): ts.ParameterDeclaration {
return ts.factory.createParameterDeclaration(
decorators,
modifiers,
dotDotDotToken,
name,
Expand Down Expand Up @@ -230,40 +210,34 @@ export function createPropertyAssignment(
export function createIndexSignature(
type: ts.TypeNode,
{
decorators,
modifiers,
indexName = "key",
indexType = keywordType.string
}: {
indexName?: string;
indexType?: ts.TypeNode;
decorators?: ts.Decorator[];
modifiers?: ts.Modifier[];
} = {}
): ts.IndexSignatureDeclaration {
return ts.factory.createIndexSignature(
decorators,
modifiers,
[createParameter(indexName, { type: indexType })],
type
);
}

export function createNamedImportDeclaration({
decorators,
modifiers,
bindings,
isTypeOnly,
moduleSpecifier
}: {
decorators?: ts.Decorator[];
modifiers?: ts.Modifier[];
bindings: Array<ts.Identifier | string | ImportSpecifier>;
isTypeOnly?: boolean;
moduleSpecifier: string | ts.Expression;
}): ts.ImportDeclaration {
return ts.factory.createImportDeclaration(
decorators,
modifiers,
ts.factory.createImportClause(
isTypeOnly || false,
Expand All @@ -277,22 +251,19 @@ export function createNamedImportDeclaration({
}

export function createDefaultImportDeclaration({
decorators,
modifiers,
name,
bindings,
isTypeOnly,
moduleSpecifier
}: {
decorators?: ts.Decorator[];
modifiers?: ts.Modifier[];
name: ts.Identifier | string;
bindings?: Array<ts.Identifier | string | ImportSpecifier>;
isTypeOnly?: boolean;
moduleSpecifier: string | ts.Expression;
}): ts.ImportDeclaration {
return ts.factory.createImportDeclaration(
decorators,
modifiers,
ts.factory.createImportClause(
isTypeOnly || false,
Expand All @@ -308,20 +279,17 @@ export function createDefaultImportDeclaration({
}

export function createNamespaceImportDeclaration({
decorators,
modifiers,
name,
isTypeOnly,
moduleSpecifier
}: {
decorators?: ts.Decorator[];
modifiers?: ts.Modifier[];
name: ts.Identifier | string;
isTypeOnly?: boolean;
moduleSpecifier: string | ts.Expression;
}): ts.ImportDeclaration {
return ts.factory.createImportDeclaration(
decorators,
modifiers,
ts.factory.createImportClause(
isTypeOnly || false,
Expand All @@ -334,19 +302,16 @@ export function createNamespaceImportDeclaration({

export function createTypeOrInterfaceDeclaration({
modifiers,
decorators,
name,
type
}: {
modifiers?: ts.Modifier[];
decorators?: ts.Decorator[];
name: string | ts.Identifier;
type: ts.TypeNode;
}): ts.InterfaceDeclaration | ts.TypeAliasDeclaration {
if (ts.isTypeLiteralNode(type)) {
return createInterfaceDeclaration({
modifiers,
decorators,
name,
members: type.members
});
Expand All @@ -370,7 +335,6 @@ export function createTypeOrInterfaceDeclaration({

return createInterfaceDeclaration({
modifiers,
decorators,
name,
members,
heritageClauses: [
Expand All @@ -385,7 +349,6 @@ export function createTypeOrInterfaceDeclaration({

return createTypeAliasDeclaration({
modifiers,
decorators,
name,
type
});
Expand All @@ -404,16 +367,16 @@ export function updateVariableDeclarationInitializer(declaration: ts.VariableDec
export type ImportSpecifier = { name: ts.Identifier | string; propertyName: ts.Identifier | string, type?: boolean; };
export function createImportSpecifier(binding: ts.Identifier | string | ImportSpecifier): ts.ImportSpecifier {
if (typeof binding === "string" || isIdentifier(binding)) {
if (ts.factory.createImportSpecifier.length === 3) {
return (ts.factory.createImportSpecifier as any)(false, undefined, toIdentifier(binding));
if (ts.factory.createImportSpecifier.length === 2) {
return (ts.factory.createImportSpecifier as any)(undefined, toIdentifier(binding));
} else {
return ts.factory.createImportSpecifier(undefined, toIdentifier(binding));
return ts.factory.createImportSpecifier(false, undefined, toIdentifier(binding));
}
}

if (ts.factory.createImportSpecifier.length === 3) {
return (ts.factory.createImportSpecifier as any)(binding.type || false, toIdentifier(binding.propertyName), toIdentifier(binding.name));
if (ts.factory.createImportSpecifier.length === 2) {
return (ts.factory.createImportSpecifier as any)(toIdentifier(binding.propertyName), toIdentifier(binding.name));
} else {
return ts.factory.createImportSpecifier(toIdentifier(binding.propertyName), toIdentifier(binding.name));
return ts.factory.createImportSpecifier(binding.type || false, toIdentifier(binding.propertyName), toIdentifier(binding.name));
}
}
2 changes: 1 addition & 1 deletion packages/core/lib/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function isValidIdentifier(str: string): boolean {
return (
!!node &&
node.kind === ts.SyntaxKind.Identifier &&
!(node as Record<string, any>)["originalKeywordKind"]
!ts.identifierToKeywordKind(node)
);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/jsonschema/lib/core-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,6 @@ function addOrUpdateImport(importPath: string, ref: ParsedReference, context: Pa

const newImportDeclaration = ts.factory.updateImportDeclaration(
importDeclaration,
importDeclaration.decorators,
importDeclaration.modifiers,
ts.factory.updateImportClause(
importDeclaration.importClause,
Expand All @@ -438,7 +437,8 @@ function addOrUpdateImport(importPath: string, ref: ParsedReference, context: Pa
])
)
),
importDeclaration.moduleSpecifier
importDeclaration.moduleSpecifier,
undefined
);

context.imports.splice(context.imports.indexOf(importDeclaration), 1, newImportDeclaration);
Expand Down
2 changes: 1 addition & 1 deletion packages/jsonschema/tests/schema-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ describe("schema-parser", () => {
const schema = loadSchema("nested.schema.json");
const res = await parseSchema(schema, { cwd: getAssetsPath() });

const arr = ts.createNodeArray(res);
const arr = ts.factory.createNodeArray(res);
const importDecla = cg.findNode<ts.ImportDeclaration>(arr, ts.SyntaxKind.ImportDeclaration);

expect(importDecla).toBeDefined();
Expand Down

0 comments on commit 84d2bb0

Please sign in to comment.