Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle typedefs with reserved type names #99

Merged
merged 1 commit into from
Dec 5, 2019
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
15 changes: 15 additions & 0 deletions src/__tests__/typedefs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,21 @@ struct UserActivitiesRequest {
});
});

test('typedef reserved type', () => {
const root = tmp.dirSync().name;
const p = path.resolve(root, 'types.thrift');
fs.writeFileSync(p, `typedef string Symbol`);
let output = new ThriftFileConverter(p, true).generateFlowFile();
expect(output).toMatchInlineSnapshot(`
"// @flow

// Source: /var/folders/sz/v9gcp6kx09g3b49qqnll68lc0000gp/T/tmp-35121caTR4AtzFmXM/types.thrift

export type _Symbol = string;
"
`);
});

test('typedef long in global scope', () => {
let files = {
// language=thrift
Expand Down
16 changes: 9 additions & 7 deletions src/main/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export class ThriftFileConverter {
};

generateService = (def: Service) =>
`export type ${def.id.name} = {\n${def.functions
`export type ${id(def.id.name)} = {\n${def.functions
.map(this.generateFunction)
.join(',')}};`;

Expand All @@ -201,7 +201,9 @@ export class ThriftFileConverter {
return this.generateEnum(otherDef, def.id.name);
}
}
return `export type ${def.id.name} = ${this.convertType(def.valueType)};`;
return `export type ${id(def.id.name)} = ${this.convertType(
def.valueType
)};`;
};

generateEnumUnion = (def: Enum) => {
Expand Down Expand Up @@ -269,12 +271,12 @@ export class ThriftFileConverter {
value !== undefined
) {
const numValue = Number(value) > 0 ? Number(value) : -Number(value);
return `export const ${def.id.name}: ${String(numValue)} = ${String(
return `export const ${id(def.id.name)}: ${String(numValue)} = ${String(
numValue
)};`;
}
if (def.value.type === 'Identifier') {
return `export const ${def.id.name} = ${def.value.name}`;
return `export const ${id(def.id.name)} = ${id(def.value.name)}`;
}
}
if (value === undefined) {
Expand All @@ -286,7 +288,7 @@ export class ThriftFileConverter {
}
}
const fieldType = enumType || this.convertType(def.fieldType, def);
return `export const ${def.id.name}: ${
return `export const ${id(def.id.name)}: ${
readOnly ? `$ReadOnly<${fieldType}>` : fieldType
} = ${value};`;
};
Expand Down Expand Up @@ -360,7 +362,7 @@ export class ThriftFileConverter {
};

generateStruct = ({id: {name}, fields}: Struct | Exception) =>
`export type ${name} = ${this.generateStructContents(fields)};`;
`export type ${id(name)} = ${this.generateStructContents(fields)};`;

generateStructContents = (fields: Array<Field>) =>
`{|${fields
Expand All @@ -376,7 +378,7 @@ export class ThriftFileConverter {
.join('\n')}|}`;

generateUnion = ({id: {name}, fields}: Union) =>
`export type ${name} = ${this.generateUnionContents(fields)};`;
`export type ${id(name)} = ${this.generateUnionContents(fields)};`;

generateUnionContents = (fields: Array<Field>) => {
if (!fields.length) {
Expand Down
10 changes: 9 additions & 1 deletion src/main/identifier.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// @flow
const reservedTypes = ['any', 'mixed', 'number', 'throw', 'Class', 'Object'];
const reservedTypes = [
'any',
'mixed',
'number',
'throw',
'Class',
'Object',
'Symbol',
];

export function id(s: string): string {
const split = s.split('.');
Expand Down