Skip to content

Commit

Permalink
Create types differently now
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Jul 19, 2024
1 parent 166712b commit 9af1465
Show file tree
Hide file tree
Showing 16 changed files with 329 additions and 483 deletions.
6 changes: 3 additions & 3 deletions packages/nitro-codegen/src/syntax/CodeNode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Language } from '../getPlatformSpecs.js'
import type { SourceFile } from './SourceFile.js'
import type { NamedTSType, TSType, VoidType } from './TSType.js'
import type { NamedType, Type } from './types/Type.js'

export interface CodeNode {
/**
Expand All @@ -15,8 +15,8 @@ export interface CodeNode {
}

export interface CppMethodSignature {
returnType: TSType | VoidType
parameters: NamedTSType[]
returnType: Type
parameters: NamedType[]
rawName: string
name: string
type: 'getter' | 'setter' | 'method'
Expand Down
27 changes: 12 additions & 15 deletions packages/nitro-codegen/src/syntax/Method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import { escapeCppName, removeDuplicates, toReferenceType } from './helpers.js'
import type { Language } from '../getPlatformSpecs.js'
import type { SourceFile } from './SourceFile.js'
import { Parameter } from './Parameter.js'
import { TSType } from './TSType.js'
import type { MethodSignature } from 'ts-morph'
import type { Type } from './types/Type.js'
import { createType } from './createType.js'

export class Method implements CodeNode {
readonly name: string
readonly returnType: TSType
readonly returnType: Type
readonly parameters: Parameter[]

constructor(prop: MethodSignature) {
this.name = prop.getSymbolOrThrow().getEscapedName()
const returnType = prop.getReturnTypeNodeOrThrow()
const type = returnType.getType()
const isOptional = type.isNullable()
this.returnType = new TSType(type, isOptional)
const returnType = prop.getReturnType()
const isOptional = returnType.isNullable()
this.returnType = createType(returnType, isOptional)
this.parameters = prop.getParameters().map((p) => new Parameter(p))
}

Expand All @@ -36,19 +36,16 @@ export class Method implements CodeNode {
case 'c++': {
const signature = this.cppSignature
const params = signature.parameters.map((p) => {
const paramType =
p.passByConvention === 'by-reference'
? toReferenceType(p.getCode())
: p.getCode()
const paramType = p.canBePassedByReference
? toReferenceType(p.getCode('c++'))
: p.getCode('c++')
return `${paramType} ${p.name}`
})
return `virtual ${signature.returnType.getCode()} ${signature.name}(${params.join(', ')}) = 0;`
return `virtual ${signature.returnType.getCode('c++')} ${signature.name}(${params.join(', ')}) = 0;`
}
case 'swift': {
const params = this.parameters.map((p) => p.getCode('swift'))
const returnType = this.returnType.isNothing
? ''
: `-> ${this.returnType.getCode()}`
const returnType = this.returnType.getCode('swift')
return `public func ${this.name}(${params.join(', ')}) throws ${returnType}`
}
default:
Expand All @@ -62,7 +59,7 @@ export class Method implements CodeNode {
const parametersDefinitionFiles = this.parameters.flatMap((p) =>
p.getDefinitionFiles()
)
const returnTypeDefinitionFiles = this.returnType.getDefinitionFiles()
const returnTypeDefinitionFiles = this.returnType.getExtraFiles()
const allFiles = [
...returnTypeDefinitionFiles,
...parametersDefinitionFiles,
Expand Down
20 changes: 12 additions & 8 deletions packages/nitro-codegen/src/syntax/Parameter.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
import type { ParameterDeclaration } from 'ts-morph'
import type { CodeNode } from './CodeNode.js'
import { NamedTSType } from './TSType.js'
import { escapeCppName, removeDuplicates } from './helpers.js'
import type { Language } from '../getPlatformSpecs.js'
import type { SourceFile } from './SourceFile.js'
import type { NamedType } from './types/Type.js'
import { createNamedType } from './createType.js'

export class Parameter implements CodeNode {
readonly name: string
readonly type: NamedTSType
readonly type: NamedType

constructor(param: ParameterDeclaration) {
this.name = param.getSymbolOrThrow().getEscapedName()
const name = param.getSymbolOrThrow().getEscapedName()
const type = param.getTypeNodeOrThrow().getType()
const isOptional =
param.hasQuestionToken() || param.isOptional() || type.isNullable()
this.type = new NamedTSType(type, isOptional, this.name)
this.type = createNamedType(name, param.getType(), isOptional)
}

get name(): string {
return this.type.name
}

getCode(language: Language): string {
const cppName = escapeCppName(this.name)
switch (language) {
case 'c++':
return `${this.type.getCode()} ${cppName}`
return `${this.type.getCode(language)} ${cppName}`
case 'swift':
return `${cppName}: ${this.type.getCode()}`
return `${cppName}: ${this.type.getCode(language)}`
default:
throw new Error(
`Language ${language} is not yet supported for parameters!`
Expand All @@ -33,7 +37,7 @@ export class Parameter implements CodeNode {

getDefinitionFiles(): SourceFile[] {
return removeDuplicates(
this.type.getDefinitionFiles(),
this.type.getExtraFiles(),
(a, b) => a.name === b.name
)
}
Expand Down
22 changes: 11 additions & 11 deletions packages/nitro-codegen/src/syntax/Property.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { ts, type PropertySignature } from 'ts-morph'
import type { CodeNode, CppMethodSignature } from './CodeNode.js'
import { NamedTSType, VoidType } from './TSType.js'
import { capitalizeName } from '../stringUtils.js'
import { escapeCppName, removeDuplicates, toReferenceType } from './helpers.js'
import type { SourceFile } from './SourceFile.js'
import type { Language } from '../getPlatformSpecs.js'
import type { NamedType } from './types/Type.js'
import { createNamedType, createVoidType } from './createType.js'

export class Property implements CodeNode {
readonly name: string
readonly type: NamedTSType
readonly type: NamedType
readonly isReadonly: boolean

constructor(prop: PropertySignature) {
this.name = prop.getSymbolOrThrow().getEscapedName()
this.isReadonly = prop.hasModifier(ts.SyntaxKind.ReadonlyKeyword)
const type = prop.getTypeNodeOrThrow().getType()
const isOptional = prop.hasQuestionToken() || type.isNullable()
this.type = new NamedTSType(type, isOptional, this.name)
this.type = createNamedType(this.name, type, isOptional)
}

get cppSignatures(): CppMethodSignature[] {
Expand All @@ -33,7 +34,7 @@ export class Property implements CodeNode {
if (!this.isReadonly) {
// setter
signatures.push({
returnType: new VoidType(),
returnType: createVoidType(),
rawName: this.name,
name: `set${capitalizedName}`,
parameters: [this.type],
Expand All @@ -45,7 +46,7 @@ export class Property implements CodeNode {

getDefinitionFiles(): SourceFile[] {
return removeDuplicates(
this.type.getDefinitionFiles(),
this.type.getExtraFiles(),
(a, b) => a.name === b.name
)
}
Expand All @@ -56,17 +57,16 @@ export class Property implements CodeNode {
const signatures = this.cppSignatures
const codeLines = signatures.map((s) => {
const params = s.parameters.map((p) => {
const paramType =
p.passByConvention === 'by-reference'
? toReferenceType(p.getCode())
: p.getCode()
const paramType = p.canBePassedByReference
? toReferenceType(p.getCode('c++'))
: p.getCode('c++')
return `${paramType} ${p.name}`
})
return `virtual ${s.returnType.getCode()} ${s.name}(${params.join(', ')}) = 0;`
return `virtual ${s.returnType.getCode('c++')} ${s.name}(${params.join(', ')}) = 0;`
})
return codeLines.join('\n')
case 'swift':
const type = this.type.getCode()
const type = this.type.getCode('swift')
if (this.isReadonly) return `public var ${this.name}: ${type} { get }`
else return `public var ${this.name}: ${type} { get set }`
default:
Expand Down
Loading

0 comments on commit 9af1465

Please sign in to comment.