UNPKG

apollo-codegen

Version:

Generate API code or type annotations based on a GraphQL schema and query documents

123 lines (104 loc) 5.11 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.comment = comment; exports.namespaceDeclaration = namespaceDeclaration; exports.classDeclaration = classDeclaration; exports.structDeclaration = structDeclaration; exports.propertyDeclaration = propertyDeclaration; exports.propertyDeclarations = propertyDeclarations; exports.protocolDeclaration = protocolDeclaration; exports.protocolPropertyDeclaration = protocolPropertyDeclaration; exports.protocolPropertyDeclarations = protocolPropertyDeclarations; exports.escapeIdentifierIfNeeded = escapeIdentifierIfNeeded; var _printing = require('../utilities/printing'); function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } function comment(generator, comment) { comment && comment.split('\n').forEach(function (line) { generator.printOnNewline(`/// ${line.trim()}`); }); } function namespaceDeclaration(generator, namespace, closure) { if (namespace) { generator.printNewlineIfNeeded(); generator.printOnNewline(`/// ${namespace} namespace`); generator.printOnNewline(`public enum ${namespace}`); generator.pushScope({ typeName: namespace }); generator.withinBlock(closure); generator.popScope(); } else { closure(); } } function classDeclaration(generator, _ref, closure) { var className = _ref.className, modifiers = _ref.modifiers, superClass = _ref.superClass, _ref$adoptedProtocols = _ref.adoptedProtocols, adoptedProtocols = _ref$adoptedProtocols === undefined ? [] : _ref$adoptedProtocols, properties = _ref.properties; generator.printNewlineIfNeeded(); generator.printOnNewline((0, _printing.wrap)('', (0, _printing.join)(modifiers, ' '), ' ') + `class ${className}`); generator.print((0, _printing.wrap)(': ', (0, _printing.join)([superClass].concat(_toConsumableArray(adoptedProtocols)), ', '))); generator.pushScope({ typeName: className }); generator.withinBlock(closure); generator.popScope(); } function structDeclaration(generator, _ref2, closure) { var structName = _ref2.structName, description = _ref2.description, _ref2$adoptedProtocol = _ref2.adoptedProtocols, adoptedProtocols = _ref2$adoptedProtocol === undefined ? [] : _ref2$adoptedProtocol; generator.printNewlineIfNeeded(); comment(generator, description); generator.printOnNewline(`public struct ${structName}`); generator.print((0, _printing.wrap)(': ', (0, _printing.join)(adoptedProtocols, ', '))); generator.pushScope({ typeName: structName }); generator.withinBlock(closure); generator.popScope(); } function propertyDeclaration(generator, _ref3) { var propertyName = _ref3.propertyName, typeName = _ref3.typeName, description = _ref3.description; comment(generator, description); generator.printOnNewline(`public var ${propertyName}: ${typeName}`); } function propertyDeclarations(generator, properties) { if (!properties) return; properties.forEach(function (property) { return propertyDeclaration(generator, property); }); } function protocolDeclaration(generator, _ref4, closure) { var protocolName = _ref4.protocolName, adoptedProtocols = _ref4.adoptedProtocols, properties = _ref4.properties; generator.printNewlineIfNeeded(); generator.printOnNewline(`public protocol ${protocolName}`); generator.print((0, _printing.wrap)(': ', (0, _printing.join)(adoptedProtocols, ', '))); generator.pushScope({ typeName: protocolName }); generator.withinBlock(closure); generator.popScope(); } function protocolPropertyDeclaration(generator, _ref5) { var propertyName = _ref5.propertyName, typeName = _ref5.typeName; generator.printOnNewline(`var ${propertyName}: ${typeName} { get }`); } function protocolPropertyDeclarations(generator, properties) { if (!properties) return; properties.forEach(function (property) { return protocolPropertyDeclaration(generator, property); }); } var reservedKeywords = new Set(['associatedtype', 'class', 'deinit', 'enum', 'extension', 'fileprivate', 'func', 'import', 'init', 'inout', 'internal', 'let', 'open', 'operator', 'private', 'protocol', 'public', 'static', 'struct', 'subscript', 'typealias', 'var', 'break', 'case', 'continue', 'default', 'defer', 'do', 'else', 'fallthrough', 'for', 'guard', 'if', 'in', 'repeat', 'return', 'switch', 'where', 'while', 'as', 'Any', 'catch', 'false', 'is', 'nil', 'rethrows', 'super', 'self', 'Self', 'throw', 'throws', 'true', 'try', 'associativity', 'convenience', 'dynamic', 'didSet', 'final', 'get', 'infix', 'indirect', 'lazy', 'left', 'mutating', 'none', 'nonmutating', 'optional', 'override', 'postfix', 'precedence', 'prefix', 'Protocol', 'required', 'right', 'set', 'Type', 'unowned', 'weak', 'willSet']); function escapeIdentifierIfNeeded(identifier) { if (reservedKeywords.has(identifier)) { return '`' + identifier + '`'; } else { return identifier; } } //# sourceMappingURL=language.js.map