Skip to content

Commit cd3edbe

Browse files
BridgeJS: Remove BridgeType usage from ts2swift (#520)
1 parent 1496944 commit cd3edbe

File tree

2 files changed

+38
-73
lines changed

2 files changed

+38
-73
lines changed
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
export type BridgeType =
2-
| { "int": {} }
3-
| { "float": {} }
4-
| { "double": {} }
5-
| { "string": {} }
6-
| { "bool": {} }
7-
| { "jsObject": { "_0": string } | {} }
8-
| { "void": {} }
9-
101
export type Parameter = {
112
name: string;
12-
type: BridgeType;
3+
type: string;
134
}

Plugins/BridgeJS/Sources/TS2Swift/JavaScript/src/processor.js

Lines changed: 37 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import ts from 'typescript';
88

99
/** @typedef {import('./index.d.ts').Parameter} Parameter */
10-
/** @typedef {import('./index.d.ts').BridgeType} BridgeType */
1110

1211
/**
1312
* @typedef {{
@@ -48,7 +47,7 @@ export class TypeProcessor {
4847
this.diagnosticEngine = diagnosticEngine;
4948
this.options = options;
5049

51-
/** @type {Map<ts.Type, BridgeType>} */
50+
/** @type {Map<ts.Type, string>} */
5251
this.processedTypes = new Map();
5352
/** @type {Map<ts.Type, ts.Node>} Seen position by type */
5453
this.seenTypes = new Map();
@@ -202,7 +201,7 @@ export class TypeProcessor {
202201
if (!signature) return;
203202

204203
const params = this.renderParameters(signature.getParameters(), node);
205-
const returnType = this.renderBridgeType(this.visitType(signature.getReturnType(), node), node);
204+
const returnType = this.visitType(signature.getReturnType(), node);
206205
const effects = this.renderEffects({ isAsync: false });
207206
const swiftName = this.renderIdentifier(name);
208207

@@ -242,7 +241,7 @@ export class TypeProcessor {
242241

243242
/**
244243
* @param {ts.PropertyDeclaration | ts.PropertySignature} node
245-
* @returns {{ name: string, type: BridgeType, isReadonly: boolean, documentation: string | undefined } | null}
244+
* @returns {{ name: string, type: string, isReadonly: boolean, documentation: string | undefined } | null}
246245
*/
247246
visitPropertyDecl(node) {
248247
if (!node.name) return null;
@@ -253,10 +252,10 @@ export class TypeProcessor {
253252
}
254253

255254
const type = this.checker.getTypeAtLocation(node)
256-
const bridgeType = this.visitType(type, node);
255+
const swiftType = this.visitType(type, node);
257256
const isReadonly = node.modifiers?.some(m => m.kind === ts.SyntaxKind.ReadonlyKeyword) ?? false;
258257
const documentation = this.getFullJSDocText(node);
259-
return { name: propertyName, type: bridgeType, isReadonly, documentation };
258+
return { name: propertyName, type: swiftType, isReadonly, documentation };
260259
}
261260

262261
/**
@@ -266,8 +265,8 @@ export class TypeProcessor {
266265
*/
267266
visitSignatureParameter(symbol, node) {
268267
const type = this.checker.getTypeOfSymbolAtLocation(symbol, node);
269-
const bridgeType = this.visitType(type, node);
270-
return { name: symbol.name, type: bridgeType };
268+
const swiftType = this.visitType(type, node);
269+
return { name: symbol.name, type: swiftType };
271270
}
272271

273272
/**
@@ -375,10 +374,10 @@ export class TypeProcessor {
375374
}
376375

377376
/**
378-
* Convert TypeScript type string to BridgeType
379-
* @param {ts.Type} type - TypeScript type string
377+
* Convert TypeScript type to Swift type string
378+
* @param {ts.Type} type - TypeScript type
380379
* @param {ts.Node} node - Node
381-
* @returns {BridgeType} Bridge type
380+
* @returns {string} Swift type string
382381
* @private
383382
*/
384383
visitType(type, node) {
@@ -392,56 +391,52 @@ export class TypeProcessor {
392391
}
393392
/**
394393
* @param {ts.Type} type
395-
* @returns {BridgeType}
394+
* @returns {string}
396395
*/
397396
const convert = (type) => {
398-
/** @type {Record<string, BridgeType>} */
397+
/** @type {Record<string, string>} */
399398
const typeMap = {
400-
"number": { "double": {} },
401-
"string": { "string": {} },
402-
"boolean": { "bool": {} },
403-
"void": { "void": {} },
404-
"any": { "jsObject": {} },
405-
"unknown": { "jsObject": {} },
406-
"null": { "void": {} },
407-
"undefined": { "void": {} },
408-
"bigint": { "int": {} },
409-
"object": { "jsObject": {} },
410-
"symbol": { "jsObject": {} },
411-
"never": { "void": {} },
412-
"Promise": {
413-
"jsObject": {
414-
"_0": "JSPromise"
415-
}
416-
},
399+
"number": "Double",
400+
"string": "String",
401+
"boolean": "Bool",
402+
"void": "Void",
403+
"any": "JSObject",
404+
"unknown": "JSObject",
405+
"null": "Void",
406+
"undefined": "Void",
407+
"bigint": "Int",
408+
"object": "JSObject",
409+
"symbol": "JSObject",
410+
"never": "Void",
411+
"Promise": "JSPromise",
417412
};
418413
const typeString = type.getSymbol()?.name ?? this.checker.typeToString(type);
419414
if (typeMap[typeString]) {
420415
return typeMap[typeString];
421416
}
422417

423418
if (this.checker.isArrayType(type) || this.checker.isTupleType(type) || type.getCallSignatures().length > 0) {
424-
return { "jsObject": {} };
419+
return "JSObject";
425420
}
426421
// "a" | "b" -> string
427422
if (this.checker.isTypeAssignableTo(type, this.checker.getStringType())) {
428-
return { "string": {} };
423+
return "String";
429424
}
430425
if (type.isTypeParameter()) {
431-
return { "jsObject": {} };
426+
return "JSObject";
432427
}
433428

434429
const typeName = this.deriveTypeName(type);
435430
if (!typeName) {
436431
this.diagnosticEngine.print("warning", `Unknown non-nominal type: ${typeString}`, node);
437-
return { "jsObject": {} };
432+
return "JSObject";
438433
}
439434
this.seenTypes.set(type, node);
440-
return { "jsObject": { "_0": typeName } };
435+
return this.renderIdentifier(typeName);
441436
}
442-
const bridgeType = convert(type);
443-
this.processedTypes.set(type, bridgeType);
444-
return bridgeType;
437+
const swiftType = convert(type);
438+
this.processedTypes.set(type, swiftType);
439+
return swiftType;
445440
}
446441

447442
/**
@@ -471,7 +466,7 @@ export class TypeProcessor {
471466
const property = this.visitPropertyDecl(node);
472467
if (!property) return;
473468

474-
const type = this.renderBridgeType(property.type, node);
469+
const type = property.type;
475470
const name = this.renderIdentifier(property.name);
476471

477472
// Always render getter
@@ -501,7 +496,7 @@ export class TypeProcessor {
501496
if (!signature) return;
502497

503498
const params = this.renderParameters(signature.getParameters(), node);
504-
const returnType = this.renderBridgeType(this.visitType(signature.getReturnType(), node), node);
499+
const returnType = this.visitType(signature.getReturnType(), node);
505500
const effects = this.renderEffects({ isAsync: false });
506501
const swiftName = this.renderIdentifier(name);
507502

@@ -538,35 +533,14 @@ export class TypeProcessor {
538533
renderParameters(parameters, node) {
539534
const params = [];
540535
for (const p of parameters) {
541-
const bridgeType = this.visitSignatureParameter(p, node);
536+
const param = this.visitSignatureParameter(p, node);
542537
const paramName = this.renderIdentifier(p.name);
543-
const type = this.renderBridgeType(bridgeType.type, node);
538+
const type = param.type;
544539
params.push(`_ ${paramName}: ${type}`);
545540
}
546541
return params.join(", ");
547542
}
548543

549-
/**
550-
* Render bridge type to Swift type
551-
* @param {BridgeType} bridgeType
552-
* @param {ts.Node} node
553-
* @returns {string}
554-
* @private
555-
*/
556-
renderBridgeType(bridgeType, node) {
557-
if ("int" in bridgeType) return "Int";
558-
if ("float" in bridgeType) return "Float";
559-
if ("double" in bridgeType) return "Double";
560-
if ("string" in bridgeType) return "String";
561-
if ("bool" in bridgeType) return "Bool";
562-
if ("void" in bridgeType) return "Void";
563-
if ("jsObject" in bridgeType) {
564-
const name = "_0" in bridgeType.jsObject ? bridgeType.jsObject._0 : undefined;
565-
return name ? this.renderIdentifier(name) : "JSObject";
566-
}
567-
return "JSObject";
568-
}
569-
570544
/**
571545
* Render effects (async/throws)
572546
* @param {{ isAsync: boolean }} effects

0 commit comments

Comments
 (0)