Skip to content

Commit

Permalink
Handle typedefs with reserved type names
Browse files Browse the repository at this point in the history
  • Loading branch information
Giancarlo Anemone authored and generic-probot-app-workflow[bot] committed Dec 5, 2019
1 parent c0d350e commit 1d75b8d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
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

0 comments on commit 1d75b8d

Please sign in to comment.