From 03bd0187f14fa3c3ee5b1d3c1c95e78cf67a00d9 Mon Sep 17 00:00:00 2001 From: Ker Lee Yap Date: Wed, 4 Dec 2019 13:36:25 -0800 Subject: [PATCH 1/2] fix Flow inexact type --- src/main/convert.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/convert.js b/src/main/convert.js index faf8e34..ef8794c 100644 --- a/src/main/convert.js +++ b/src/main/convert.js @@ -363,7 +363,7 @@ export class ThriftFileConverter { `export type ${name} = ${this.generateStructContents(fields)};`; generateStructContents = (fields: Array) => - `{|${fields + `$ReadOnly<{|${fields .map((field: Field) => { const valueType = field.valueType; let optionalPrefix = this.isOptional(field) ? '?' : ''; @@ -371,9 +371,9 @@ export class ThriftFileConverter { valueType.type === 'Identifier' ? this.getIdentifier(valueType.name, 'type') : this.convertType(valueType); - return `${field.name}${optionalPrefix}: ${optionalPrefix}${value};`; + return `${field.name}${optionalPrefix}: $ReadOnly<${optionalPrefix}${value}>;`; }) - .join('\n')}|}`; + .join('\n')}|}>`; generateUnion = ({id: {name}, fields}: Union) => `export type ${name} = ${this.generateUnionContents(fields)};`; From 8c455c8436c1ab09263b210b853df9dbb32a23e3 Mon Sep 17 00:00:00 2001 From: klyap Date: Wed, 4 Dec 2019 14:44:36 -0800 Subject: [PATCH 2/2] Only add $Readme<> to objects in function args --- src/main/convert.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/convert.js b/src/main/convert.js index ef8794c..b3ae25c 100644 --- a/src/main/convert.js +++ b/src/main/convert.js @@ -191,7 +191,7 @@ export class ThriftFileConverter { generateFunction = (fn: FunctionDefinition) => `${fn.id.name}: (${ - fn.fields.length ? this.generateStructContents([...fn.fields]) : '' + fn.fields.length ? this.generateStructContents([...fn.fields], true) : '' }) => ${this.convertType(fn.returns)}`; generateTypedef = (def: Typedef) => { @@ -362,18 +362,18 @@ export class ThriftFileConverter { generateStruct = ({id: {name}, fields}: Struct | Exception) => `export type ${name} = ${this.generateStructContents(fields)};`; - generateStructContents = (fields: Array) => - `$ReadOnly<{|${fields + generateStructContents = (fields: Array, readOnly?: boolean) => + `${readOnly === true ? '$ReadOnly<' : ''}{|${fields .map((field: Field) => { const valueType = field.valueType; let optionalPrefix = this.isOptional(field) ? '?' : ''; let value = valueType.type === 'Identifier' ? this.getIdentifier(valueType.name, 'type') - : this.convertType(valueType); - return `${field.name}${optionalPrefix}: $ReadOnly<${optionalPrefix}${value}>;`; + : this.convertType(valueType, null, readOnly); + return `${field.name}${optionalPrefix}:${optionalPrefix}${value};`; }) - .join('\n')}|}>`; + .join('\n')}|}${readOnly === true ? '>' : ''}`; generateUnion = ({id: {name}, fields}: Union) => `export type ${name} = ${this.generateUnionContents(fields)};`; @@ -557,7 +557,7 @@ export class ThriftFileConverter { return false; }; - convertBaseType(t: AstNode, def?: Definition): string | void { + convertBaseType(t: AstNode, def?: ?Definition): string | void { if (t.type !== 'BaseType') { return undefined; } @@ -605,7 +605,7 @@ export class ThriftFileConverter { return undefined; } - convertMapType(t: AstNode, def?: Definition): string | void { + convertMapType(t: AstNode, def?: ?Definition, readOnly?: ?boolean): string | void { if (t.type === 'Map' && def) { const valueType = this.convertType(t.valueType); if (def.type === 'Const' && def.value.type === 'ConstMap') { @@ -627,7 +627,7 @@ export class ThriftFileConverter { ); } } else if (entry.key.type === 'Literal') { - return `'${entry.key.value}': ${valueType}`; + return readOnly === true ? `'${entry.key.value}': $ReadOnly<${valueType}>` : `'${entry.key.value}': ${valueType}`; } else { throw new Error('unsupported'); } @@ -651,13 +651,13 @@ export class ThriftFileConverter { return this.identifiersTable[def.name].type === 'Enum'; } - convertType(t: AstNode, def?: Definition): string { + convertType(t: AstNode, def?: ?Definition, readOnly?: boolean): string { if (!t) { throw new Error(`Assertion failed. t is not defined.`); } let type: string | void = this.convertArrayType(t) || - this.convertMapType(t, def) || + this.convertMapType(t, def, readOnly) || this.convertEnumType(t) || this.convertBaseType(t, def); if (type !== undefined) {