Skip to content
Open
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
20 changes: 20 additions & 0 deletions compiler/companion/src/GenerateIds.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,24 @@ NSString *SCValdiIdHelloWorldMySecondId() {
`,
);
});

it('escapes double quotes in ObjC impl when module name contains them', () => {
const iosImpl = generateIds('hello"world', '"SCHelloWorld/Ids.h"', TEST_ID_FILES).ios.impl;
expect(iosImpl).toContain('return @"hello\\"world/my_first_id";');
});

it('escapes backslashes in ObjC impl when module name contains them', () => {
const iosImpl = generateIds('hello\\world', '"SCHelloWorld/Ids.h"', TEST_ID_FILES).ios.impl;
expect(iosImpl).toContain('return @"hello\\\\world/my_first_id";');
});

it('escapes single quotes in JS impl when module name contains them', () => {
const jsImpl = generateIds("hello'world", '"SCHelloWorld/Ids.h"', TEST_ID_FILES).typescript.implementation;
expect(jsImpl).toContain("myFirstId: () => 'hello\\'world/my_first_id',");
});

it('escapes backslashes in JS impl when module name contains them', () => {
const jsImpl = generateIds('hello\\world', '"SCHelloWorld/Ids.h"', TEST_ID_FILES).typescript.implementation;
expect(jsImpl).toContain("myFirstId: () => 'hello\\\\world/my_first_id',");
});
});
22 changes: 20 additions & 2 deletions compiler/companion/src/GenerateIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ interface Id {
description?: string;
}

function escapeObjCString(str: string): string {
return str
.replace(/\\/g, '\\\\')
.replace(/"/g, '\\"')
.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r')
.replace(/\t/g, '\\t');
}

function escapeJSSingleQuotedString(str: string): string {
return str
.replace(/\\/g, '\\\\')
.replace(/'/g, "\\'")
.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r')
.replace(/\t/g, '\\t');
}

function parseIds(moduleName: string, fileContent: string): Id[] {
const idsYaml = yaml.load(fileContent.toString()) as IdsYaml;

Expand Down Expand Up @@ -117,7 +135,7 @@ function generateIOSIds(ids: Id[], iosHeaderImportPath: string): ObjectiveCFile
header.append(`extern ${functionSignature};\n`);
impl.append(`${functionSignature} {\n`);
impl.withIndentation(' ', () => {
impl.append(`return @"${id.identifier}";\n`);
impl.append(`return @"${escapeObjCString(id.identifier)}";\n`);
});
impl.append('}\n');
}
Expand Down Expand Up @@ -150,7 +168,7 @@ function generateTypeScriptIds(ids: Id[]): TypeScriptFile {
}
definition.append(`static ${id.name}(): string;\n`);

implementation.append(`${id.name}: () => '${id.identifier}',\n`);
implementation.append(`${id.name}: () => '${escapeJSSingleQuotedString(id.identifier)}',\n`);
}

definition.endIndent();
Expand Down