Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/quicktype-core/src/language/Swift/SwiftRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,7 @@ export class SwiftRenderer extends ConvenienceRenderer {

if (
this._options.sendable &&
(!this._options.mutableProperties || !isClass) &&
!this._options.objcSupport
(!this._options.mutableProperties || !isClass)
) {
protocols.push("Sendable");
}
Expand Down Expand Up @@ -501,7 +500,11 @@ export class SwiftRenderer extends ConvenienceRenderer {
const isClass = this._options.useClasses || this.isCycleBreakerType(c);
const structOrClass = isClass ? "class" : "struct";
const finalPrefix =
isClass && this._options.finalClasses ? "final " : "";
isClass &&
(this._options.finalClasses ||
(this._options.sendable && !this._options.mutableProperties))
? "final "
: "";

if (isClass && this._options.objcSupport) {
// [Michael Fey (@MrRooni), 2019-4-24] Swift 5 or greater, must come before the access declaration for the class.
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,10 @@ export const allFixtures: Fixture[] = [
new JSONFixture(languages.PythonLanguage),
new JSONFixture(languages.ElmLanguage),
new JSONFixture(languages.SwiftLanguage),
new JSONFixture(
languages.SwiftSendableObjectiveCSupportLanguage,
"swift-sendable-objective-c",
),
new JSONFixture(languages.ObjectiveCLanguage),
new JSONFixture(languages.TypeScriptLanguage),
new JSONFixture(languages.TypeScriptZodLanguage),
Expand Down
37 changes: 37 additions & 0 deletions test/fixtures/swift/verify-sendable.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const fs = require("fs");

const source = fs.readFileSync("quicktype.swift", "utf8");
const declarations = source
.split("\n")
.filter((line) =>
/^(?:@objcMembers )?(?:final )?(?:class|struct|enum) /.test(line),
);

if (declarations.length === 0) {
throw new Error("No generated type declarations found");
}

if (!declarations.some((line) => line.startsWith("enum "))) {
throw new Error("No generated enum declaration found");
}

if (
!declarations.some(
(line) => line.includes("class ") || line.startsWith("struct "),
)
) {
throw new Error("No generated class or struct declaration found");
}

for (const declaration of declarations) {
if (!declaration.includes("Sendable")) {
throw new Error(`Generated type is not Sendable: ${declaration}`);
}

if (
declaration.includes("class ") &&
!declaration.startsWith("@objcMembers final class ")
) {
throw new Error(`Generated Sendable class is not final: ${declaration}`);
}
}
18 changes: 18 additions & 0 deletions test/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,24 @@ export const SwiftLanguage: Language = {
sourceFiles: ["src/language/Swift/index.ts"],
};

export const SwiftSendableObjectiveCSupportLanguage: Language = {
...SwiftLanguage,
compileCommand: "node verify-sendable.cjs",
diffViaSchema: false,
includeJSON: ["pokedex.json"],
rendererOptions: {
...SwiftLanguage.rendererOptions,
sendable: "true",
"struct-or-class": "class",
"objective-c-support": "true",
},
quickTestRendererOptions: [
["pokedex.json", { "struct-or-class": "struct" }],
],
runCommand: undefined,
skipMiscJSON: true,
};

export const ObjectiveCLanguage: Language = {
name: "objective-c",
base: "test/fixtures/objective-c",
Expand Down
Loading