1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.SwiftGenerator = exports.swift = exports.SwiftSource = void 0;
|
7 | const CodeGenerator_1 = __importDefault(require("apollo-codegen-core/lib/utilities/CodeGenerator"));
|
8 | const printing_1 = require("apollo-codegen-core/lib/utilities/printing");
|
9 | const reservedKeywords = new Set([
|
10 | 'associatedtype', 'class', 'deinit', 'enum', 'extension', 'fileprivate',
|
11 | 'func', 'import', 'init', 'inout', 'internal', 'let', 'open', 'operator',
|
12 | 'private', 'protocol', 'public', 'static', 'struct', 'subscript',
|
13 | 'typealias', 'var',
|
14 | 'break', 'case', 'continue', 'default', 'defer', 'do', 'else', 'fallthrough',
|
15 | 'for', 'guard', 'if', 'in', 'repeat', 'return', 'switch', 'where', 'while',
|
16 | 'as', 'Any', 'catch', 'false', 'is', 'nil', 'rethrows', 'super', 'self',
|
17 | 'Self', 'throw', 'throws', 'true', 'try',
|
18 | '_',
|
19 | 'associativity', 'convenience', 'dynamic', 'didSet', 'final', 'get', 'infix',
|
20 | 'indirect', 'lazy', 'left', 'mutating', 'none', 'nonmutating', 'optional',
|
21 | 'override', 'postfix', 'precedence', 'prefix', 'Protocol', 'required',
|
22 | 'right', 'set', 'Type', 'unowned', 'weak', 'willSet'
|
23 | ]);
|
24 | const reservedMemberKeywords = new Set(["self", "Type", "Protocol"]);
|
25 | class SwiftSource {
|
26 | constructor(source) {
|
27 | this.source = source;
|
28 | }
|
29 | static string(string, trim = false) {
|
30 | if (trim) {
|
31 | string = string
|
32 | .split(/\n/g)
|
33 | .map(line => line.trim())
|
34 | .join(" ");
|
35 | }
|
36 | return new SwiftSource(`"${string.replace(/[\0\\\t\n\r"]/g, c => {
|
37 | switch (c) {
|
38 | case "\0":
|
39 | return "\\0";
|
40 | case "\t":
|
41 | return "\\t";
|
42 | case "\n":
|
43 | return "\\n";
|
44 | case "\r":
|
45 | return "\\r";
|
46 | default:
|
47 | return `\\${c}`;
|
48 | }
|
49 | })}"`);
|
50 | }
|
51 | static multilineString(string) {
|
52 | let rawCount = 0;
|
53 | if (/"""|\\/.test(string)) {
|
54 | let re = /"""(#+)|\\(#+)/g;
|
55 | for (let ary = re.exec(string); ary !== null; ary = re.exec(string)) {
|
56 | rawCount = Math.max(rawCount, (ary[1] || "").length, (ary[2] || "").length);
|
57 | }
|
58 | rawCount += 1;
|
59 | }
|
60 | const rawToken = "#".repeat(rawCount);
|
61 | return new SwiftSource(`${rawToken}"""\n${string.replace(/[\0\r]/g, c => {
|
62 | switch (c) {
|
63 | case "\0":
|
64 | return `\\${rawToken}0`;
|
65 | case "\r":
|
66 | return `\\${rawToken}r`;
|
67 | default:
|
68 | return c;
|
69 | }
|
70 | })}\n"""${rawToken}`);
|
71 | }
|
72 | static identifier(input) {
|
73 | return new SwiftSource(input.replace(/[a-zA-Z_][a-zA-Z0-9_]*/g, (match, offset, fullString) => {
|
74 | if (reservedKeywords.has(match)) {
|
75 | if (offset == 0 ||
|
76 | fullString[offset - 1] !== "." ||
|
77 | reservedMemberKeywords.has(match)) {
|
78 | return `\`${match}\``;
|
79 | }
|
80 | }
|
81 | return match;
|
82 | }));
|
83 | }
|
84 | static memberName(input) {
|
85 | return new SwiftSource(input.replace(/[a-zA-Z_][a-zA-Z0-9_]*/g, (match, offset, fullString) => {
|
86 | if (!reservedMemberKeywords.has(match)) {
|
87 | if (offset == 0 ||
|
88 | fullString[offset - 1] === "." ||
|
89 | !reservedKeywords.has(match)) {
|
90 | return match;
|
91 | }
|
92 | }
|
93 | return `\`${match}\``;
|
94 | }));
|
95 | }
|
96 | static isValidParameterName(input) {
|
97 | return input !== "self";
|
98 | }
|
99 | static raw(literals, ...placeholders) {
|
100 | var result = literals[0];
|
101 | placeholders.forEach((value, i) => {
|
102 | result += `${value}${literals[i + 1]}`;
|
103 | });
|
104 | return new SwiftSource(result);
|
105 | }
|
106 | toString() {
|
107 | return this.source;
|
108 | }
|
109 | concat(...sources) {
|
110 | return new SwiftSource(sources.reduce((accum, value) => accum + value.source, this.source));
|
111 | }
|
112 | append(...sources) {
|
113 | for (let value of sources) {
|
114 | this.source += value.source;
|
115 | }
|
116 | }
|
117 | static wrap(start, maybeSource, end) {
|
118 | const result = printing_1.wrap(start.source, maybeSource !== undefined ? maybeSource.source : undefined, end !== undefined ? end.source : undefined);
|
119 | return result ? new SwiftSource(result) : undefined;
|
120 | }
|
121 | static join(maybeArray, separator) {
|
122 | const result = printing_1.join(maybeArray, separator);
|
123 | return result ? new SwiftSource(result) : undefined;
|
124 | }
|
125 | }
|
126 | exports.SwiftSource = SwiftSource;
|
127 | function swift(literals, ...placeholders) {
|
128 | let result = literals[0];
|
129 | placeholders.forEach((value, i) => {
|
130 | result += _escape(value);
|
131 | result += literals[i + 1];
|
132 | });
|
133 | return new SwiftSource(result);
|
134 | }
|
135 | exports.swift = swift;
|
136 | function _escape(value) {
|
137 | if (value instanceof SwiftSource) {
|
138 | return value.source;
|
139 | }
|
140 | else if (typeof value === "string") {
|
141 | return SwiftSource.identifier(value).source;
|
142 | }
|
143 | else if (Array.isArray(value)) {
|
144 | return value.map(_escape).join();
|
145 | }
|
146 | else if (typeof value === "object") {
|
147 | return SwiftSource.identifier(`${value}`).source;
|
148 | }
|
149 | else if (value === undefined) {
|
150 | return "";
|
151 | }
|
152 | else {
|
153 | return `${value}`;
|
154 | }
|
155 | }
|
156 | const { wrap, join } = SwiftSource;
|
157 | class SwiftGenerator extends CodeGenerator_1.default {
|
158 | constructor(context) {
|
159 | super(context);
|
160 | }
|
161 | multilineString(string, suppressMultilineStringLiterals) {
|
162 | if (suppressMultilineStringLiterals) {
|
163 | this.printOnNewline(SwiftSource.string(string, !string.includes('"""')));
|
164 | }
|
165 | else {
|
166 | SwiftSource.multilineString(string)
|
167 | .source.split("\n")
|
168 | .forEach(line => {
|
169 | this.printOnNewline(new SwiftSource(line));
|
170 | });
|
171 | }
|
172 | }
|
173 | comment(comment, trim = true) {
|
174 | comment &&
|
175 | comment.split("\n").forEach(line => {
|
176 | this.printOnNewline(SwiftSource.raw `/// ${trim ? line.trim() : line}`);
|
177 | });
|
178 | }
|
179 | deprecationAttributes(isDeprecated, deprecationReason) {
|
180 | if (isDeprecated !== undefined && isDeprecated) {
|
181 | deprecationReason =
|
182 | deprecationReason !== undefined && deprecationReason.length > 0
|
183 | ? deprecationReason
|
184 | : "";
|
185 | this.printOnNewline(swift `@available(*, deprecated, message: ${SwiftSource.string(deprecationReason, true)})`);
|
186 | }
|
187 | }
|
188 | namespaceDeclaration(namespace, closure) {
|
189 | if (namespace) {
|
190 | this.printNewlineIfNeeded();
|
191 | this.printOnNewline(SwiftSource.raw `/// ${namespace} namespace`);
|
192 | this.printOnNewline(swift `public enum ${namespace}`);
|
193 | this.pushScope({ typeName: namespace });
|
194 | this.withinBlock(closure);
|
195 | this.popScope();
|
196 | }
|
197 | else {
|
198 | if (closure) {
|
199 | closure();
|
200 | }
|
201 | }
|
202 | }
|
203 | namespaceExtensionDeclaration(namespace, closure) {
|
204 | if (namespace) {
|
205 | this.printNewlineIfNeeded();
|
206 | this.printOnNewline(SwiftSource.raw `/// ${namespace} namespace`);
|
207 | this.printOnNewline(swift `public extension ${namespace}`);
|
208 | this.pushScope({ typeName: namespace });
|
209 | this.withinBlock(closure);
|
210 | this.popScope();
|
211 | }
|
212 | else {
|
213 | if (closure) {
|
214 | closure();
|
215 | }
|
216 | }
|
217 | }
|
218 | classDeclaration({ className, modifiers, superClass, adoptedProtocols = [] }, closure) {
|
219 | this.printNewlineIfNeeded();
|
220 | this.printOnNewline((wrap(swift ``, new SwiftSource(printing_1.join(modifiers, " ")), swift ` `) ||
|
221 | swift ``).concat(swift `class ${className}`));
|
222 | this.print(wrap(swift `: `, join([
|
223 | superClass !== undefined
|
224 | ? SwiftSource.identifier(superClass)
|
225 | : undefined,
|
226 | ...adoptedProtocols.map(SwiftSource.identifier)
|
227 | ], ", ")));
|
228 | this.pushScope({ typeName: className });
|
229 | this.withinBlock(closure);
|
230 | this.popScope();
|
231 | }
|
232 | structDeclaration({ structName, description, adoptedProtocols = [], namespace = undefined }, outputIndividualFiles, closure) {
|
233 | this.printNewlineIfNeeded();
|
234 | this.comment(description);
|
235 | const isRedundant = adoptedProtocols.includes("GraphQLFragment") &&
|
236 | !!namespace &&
|
237 | outputIndividualFiles;
|
238 | const modifier = new SwiftSource(isRedundant ? "" : "public ");
|
239 | this.printOnNewline(swift `${modifier}struct ${structName}`);
|
240 | this.print(wrap(swift `: `, join(adoptedProtocols.map(SwiftSource.identifier), ", ")));
|
241 | this.pushScope({ typeName: structName });
|
242 | this.withinBlock(closure);
|
243 | this.popScope();
|
244 | }
|
245 | propertyDeclaration({ propertyName, typeName, description }) {
|
246 | this.comment(description);
|
247 | this.printOnNewline(swift `public var ${propertyName}: ${typeName}`);
|
248 | }
|
249 | propertyDeclarations(properties) {
|
250 | if (!properties)
|
251 | return;
|
252 | properties.forEach(property => this.propertyDeclaration(property));
|
253 | }
|
254 | protocolDeclaration({ protocolName, adoptedProtocols }, closure) {
|
255 | this.printNewlineIfNeeded();
|
256 | this.printOnNewline(swift `public protocol ${protocolName}`);
|
257 | this.print(wrap(swift `: `, join(adoptedProtocols !== undefined
|
258 | ? adoptedProtocols.map(SwiftSource.identifier)
|
259 | : undefined, ", ")));
|
260 | this.pushScope({ typeName: protocolName });
|
261 | this.withinBlock(closure);
|
262 | this.popScope();
|
263 | }
|
264 | protocolPropertyDeclaration({ propertyName, typeName }) {
|
265 | this.printOnNewline(swift `var ${propertyName}: ${typeName} { get }`);
|
266 | }
|
267 | protocolPropertyDeclarations(properties) {
|
268 | if (!properties)
|
269 | return;
|
270 | properties.forEach(property => this.protocolPropertyDeclaration(property));
|
271 | }
|
272 | }
|
273 | exports.SwiftGenerator = SwiftGenerator;
|
274 |
|
\ | No newline at end of file |