Skip to content

Commit

Permalink
Escape Swift identifiers in cases of enum declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnwalraven committed Feb 7, 2017
1 parent a32ed2e commit cb28707
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/swift/codeGeneration.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ function enumerationDeclaration(generator, type) {
generator.printOnNewline(`public enum ${name}: String`);
generator.withinBlock(() => {
values.forEach(value =>
generator.printOnNewline(`case ${camelCase(value.name)} = "${value.value}"${wrap(' /// ', value.description)}`)
generator.printOnNewline(`case ${escapeIdentifierIfNeeded(camelCase(value.name))} = "${value.value}"${wrap(' /// ', value.description)}`)
);
});
generator.printNewline();
Expand Down
23 changes: 22 additions & 1 deletion test/swift/codeGeneration.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
GraphQLInt,
GraphQLList,
GraphQLNonNull,
GraphQLInputObjectType
GraphQLInputObjectType,
GraphQLEnumType
} from 'graphql';

import {
Expand Down Expand Up @@ -841,6 +842,26 @@ describe('Swift code generation', function() {
`);
});

it('should escape identifiers in cases of enum declaration for a GraphQLEnumType', function() {
const generator = new CodeGenerator();

const albumPrivaciesEnum = new GraphQLEnumType({
name: 'AlbumPrivacies',
values: { PUBLIC: { value: "PUBLIC" }, PRIVATE: { value: "PRIVATE" } }
});

typeDeclarationForGraphQLType(generator, albumPrivaciesEnum);

expect(generator.output).to.equal(stripIndent`
public enum AlbumPrivacies: String {
case \`public\` = "PUBLIC"
case \`private\` = "PRIVATE"
}
extension AlbumPrivacies: JSONDecodable, JSONEncodable {}
`);
});

it('should generate a struct declaration for a GraphQLInputObjectType', function() {
const generator = new CodeGenerator();

Expand Down

0 comments on commit cb28707

Please sign in to comment.