Skip to content

Commit

Permalink
Fix issue with flow and special reserved words
Browse files Browse the repository at this point in the history
  • Loading branch information
ganemone authored and generic-probot-app-workflow[bot] committed Oct 31, 2018
1 parent 85ca40f commit 7eb73cc
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 23 deletions.
31 changes: 15 additions & 16 deletions src/main/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {Thrift} from 'thriftrw';
import {TypeConverter} from './types';
import prettier from 'prettier';
import path from 'path';

import {id} from './identifier';
import type {Base} from 'bufrw';
import type {
Struct,
Expand Down Expand Up @@ -69,20 +69,19 @@ export class ThriftFileConverter {
this.withsource = withsource;
}

generateFlowFile = () =>
prettier.format(
[
'// @flow',
`// Generated by thrift2flow at ${new Date().toString()}${
this.withsource ? `\n// Source: ${this.thriftPath}` : ''
}`,
this.generateImports(),
...this.thriftAstDefinitions.map(this.convertDefinitionToCode),
]
.filter(Boolean)
.join('\n\n'),
{parser: 'flow'}
);
generateFlowFile = () => {
const result = [
'// @flow',
`// Generated by thrift2flow at ${new Date().toString()}${
this.withsource ? `\n// Source: ${this.thriftPath}` : ''
}`,
this.generateImports(),
...this.thriftAstDefinitions.map(this.convertDefinitionToCode),
]
.filter(Boolean)
.join('\n\n');
return prettier.format(result, {parser: 'flow'});
};

convertDefinitionToCode = (def: any) => {
switch (def.type) {
Expand Down Expand Up @@ -229,7 +228,7 @@ export class ThriftFileConverter {
baseName = `_${baseName}`;
}
}
return `import * as ${baseName} from '${relpath}';`;
return `import * as ${id(baseName)} from '${relpath}';`;
});

if (this.isLongDefined()) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/identifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @flow
const reservedTypes = ['any', 'mixed', 'number'];

export function id(s: string): string {
const split = s.split('.');
if (reservedTypes.includes(s) || reservedTypes.includes(split[0])) {
return `_${s}`;
}
return s;
}
18 changes: 11 additions & 7 deletions src/main/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
// @flow

import {BaseType, Enum, ListType, MapType, SetType} from 'thriftrw/ast';
import {id} from './identifier';

export class TypeConverter {
static primitives = {
Expand Down Expand Up @@ -65,13 +66,16 @@ export class TypeConverter {
return '';
}

convert = (t: BaseType): string =>
this.arrayType(t) ||
this.enumType(t) ||
this.mapType(t) ||
this.annotation(t) ||
TypeConverter.primitives[t.baseType] ||
this.transformName(t.name);
convert = (t: BaseType): string => {
return (
this.arrayType(t) ||
this.enumType(t) ||
this.mapType(t) ||
this.annotation(t) ||
TypeConverter.primitives[t.baseType] ||
this.transformName(id(t.name))
);
};

enumType = (thriftValueType: BaseType) =>
this.isEnum(thriftValueType) && this.transformName(thriftValueType.name);
Expand Down
42 changes: 42 additions & 0 deletions src/test/imports.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,48 @@ struct MyStruct {
)
);

test(
'imports with special type file names',
flowResultTest(
{
// language=thrift
'any.thrift': `
typedef i32 Thing
`,
// language=thrift
'shared.thrift': `
include "./any.thrift"
struct MyStruct {
1: any.Thing a
2: map<string, any.Thing> b
3: map<any.Thing, string> c
}
typedef any.Thing MyTypedef
const any.Thing MyConst = 10;
const set<any.Thing> MySet = [0];
union MyUnion {
1: any.Thing a
2: i32 b
}
`,
// language=JavaScript
'index.js': `
// @flow
import type {MyStructXXX} from './shared';
function go(s : MyStructXXX) {
const numbers : number[] = [s.a];
return [numbers];
}
`,
},
(t: Test, r: FlowResult) => {
t.equal(r.errors.length, 0);
t.end();
}
)
);

test(
'imports in sub directory',
flowResultTest(
Expand Down

0 comments on commit 7eb73cc

Please sign in to comment.