Skip to content

Commit

Permalink
Replace import equals declarations with const and type alias statemen…
Browse files Browse the repository at this point in the history
…ts (#155)

* Replace import equals declarations with const and type alias statements

* Update union re-export

* Update src/tests/unit/fixtures/thrift-server/generated-strict/com/test/common/typedefs.ts

* Fix enum typedefs

* import typedefs last
  • Loading branch information
hayes authored and kevin-greene-ck committed Apr 24, 2019
1 parent 86b571c commit 4f39da1
Show file tree
Hide file tree
Showing 26 changed files with 809 additions and 141 deletions.
22 changes: 11 additions & 11 deletions src/main/render/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,6 @@ export function renderIndex(state: IRenderState): Array<ts.Statement> {
)
}

if (currentNamespace.typedefs.length > 0) {
results.push(
ts.createExportDeclaration(
undefined,
undefined,
undefined,
ts.createLiteral(`./typedefs`),
),
)
}

;[
...currentNamespace.structs,
...currentNamespace.unions,
Expand Down Expand Up @@ -73,5 +62,16 @@ export function renderIndex(state: IRenderState): Array<ts.Statement> {
)
})

if (currentNamespace.typedefs.length > 0) {
results.push(
ts.createExportDeclaration(
undefined,
undefined,
undefined,
ts.createLiteral(`./typedefs`),
),
)
}

return results
}
159 changes: 112 additions & 47 deletions src/main/render/thrift-server/typedef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ import { TypeMapping } from './types'

import { DefinitionType, IRenderState, IResolvedIdentifier } from '../../types'

import { className, looseName, strictName, toolkitName } from './struct/utils'
import {
className,
looseName,
strictName,
tokens,
toolkitName,
} from './struct/utils'

import {
fieldInterfaceName,
Expand All @@ -25,6 +31,7 @@ import {
resolveIdentifierDefinition,
resolveIdentifierName,
} from '../../resolver'
import { createConst } from '../shared/utils'

function renderStrictInterfaceReexport(
id: IResolvedIdentifier,
Expand All @@ -33,14 +40,14 @@ function renderStrictInterfaceReexport(
state: IRenderState,
): ts.Statement {
if (id.pathName !== undefined) {
return ts.createImportEqualsDeclaration(
return ts.createTypeAliasDeclaration(
undefined,
[ts.createToken(ts.SyntaxKind.ExportKeyword)],
ts.createIdentifier(
strictName(node.name.value, definition.type, state),
),
ts.createIdentifier(
`${strictName(id.rawName, definition.type, state)}`,
tokens(true),
strictName(node.name.value, definition.type, state),
undefined,
ts.createTypeReferenceNode(
strictName(id.rawName, definition.type, state),
undefined,
),
)
} else {
Expand Down Expand Up @@ -69,14 +76,14 @@ function renderLooseInterfaceReexport(
state: IRenderState,
): ts.Statement {
if (id.pathName !== undefined) {
return ts.createImportEqualsDeclaration(
return ts.createTypeAliasDeclaration(
undefined,
[ts.createToken(ts.SyntaxKind.ExportKeyword)],
ts.createIdentifier(
looseName(node.name.value, definition.type, state),
),
ts.createIdentifier(
`${looseName(id.rawName, definition.type, state)}`,
tokens(true),
looseName(node.name.value, definition.type, state),
undefined,
ts.createTypeReferenceNode(
looseName(id.rawName, definition.type, state),
undefined,
),
)
} else {
Expand Down Expand Up @@ -104,11 +111,41 @@ function renderClassReexport(
state: IRenderState,
): ts.Statement {
if (id.pathName !== undefined) {
return ts.createImportEqualsDeclaration(
return ts.createVariableStatement(
tokens(true),
createConst(
className(node.name.value, state),
undefined,
ts.createIdentifier(className(id.rawName, state)),
),
)
} else {
return ts.createExportDeclaration(
[],
[],
ts.createNamedExports([
ts.createExportSpecifier(
ts.createIdentifier(`${className(id.rawName, state)}`),
ts.createIdentifier(className(node.name.value, state)),
),
]),
undefined,
)
}
}

function renderUnionReexport(
id: IResolvedIdentifier,
node: TypedefDefinition,
state: IRenderState,
): ts.Statement {
if (id.pathName !== undefined) {
return ts.createTypeAliasDeclaration(
undefined,
[ts.createToken(ts.SyntaxKind.ExportKeyword)],
ts.createIdentifier(className(node.name.value, state)),
ts.createIdentifier(`${className(id.rawName, state)}`),
tokens(true),
className(node.name.value, state),
undefined,
ts.createTypeReferenceNode(className(id.rawName, state), undefined),
)
} else {
return ts.createExportDeclaration(
Expand All @@ -132,11 +169,13 @@ function renderToolkitReexport(
state: IRenderState,
): ts.Statement {
if (id.pathName !== undefined) {
return ts.createImportEqualsDeclaration(
undefined,
[ts.createToken(ts.SyntaxKind.ExportKeyword)],
ts.createIdentifier(toolkitName(node.name.value, state)),
ts.createIdentifier(`${toolkitName(id.rawName, state)}`),
return ts.createVariableStatement(
tokens(true),
createConst(
toolkitName(node.name.value, state),
undefined,
ts.createIdentifier(toolkitName(id.rawName, state)),
),
)
} else {
return ts.createExportDeclaration(
Expand All @@ -159,11 +198,15 @@ function renderUnionTypeReexport(
state: IRenderState,
): ts.Statement {
if (id.pathName !== undefined) {
return ts.createImportEqualsDeclaration(
return ts.createTypeAliasDeclaration(
undefined,
[ts.createToken(ts.SyntaxKind.ExportKeyword)],
ts.createIdentifier(renderUnionTypeName(node.name.value, state)),
ts.createIdentifier(`${renderUnionTypeName(id.rawName, state)}`),
tokens(true),
renderUnionTypeName(node.name.value, state),
undefined,
ts.createTypeReferenceNode(
renderUnionTypeName(id.rawName, state),
undefined,
),
)
} else {
return ts.createExportDeclaration(
Expand Down Expand Up @@ -192,22 +235,18 @@ function renderUnionInterfaceReexports(
): Array<ts.Statement> {
if (id.pathName !== undefined) {
return union.fields.map((next: FieldDefinition) => {
return ts.createImportEqualsDeclaration(
return ts.createTypeAliasDeclaration(
undefined,
[ts.createToken(ts.SyntaxKind.ExportKeyword)],
ts.createIdentifier(
fieldInterfaceName(
node.name.value,
next.name.value,
strict,
),
),
ts.createIdentifier(
tokens(true),
fieldInterfaceName(node.name.value, next.name.value, strict),
undefined,
ts.createTypeReferenceNode(
`${id.pathName}.${fieldInterfaceName(
union.name.value,
next.name.value,
strict,
)}`,
undefined,
),
)
})
Expand Down Expand Up @@ -245,11 +284,15 @@ function renderUnionArgsReexport(
state: IRenderState,
): ts.Statement {
if (id.pathName !== undefined) {
return ts.createImportEqualsDeclaration(
return ts.createTypeAliasDeclaration(
undefined,
tokens(true),
unionTypeName(node.name.value, state, false),
undefined,
[ts.createToken(ts.SyntaxKind.ExportKeyword)],
ts.createIdentifier(unionTypeName(node.name.value, state, false)),
ts.createIdentifier(`${unionTypeName(id.rawName, state, false)}`),
ts.createTypeReferenceNode(
unionTypeName(id.rawName, state, false),
undefined,
),
)
} else {
return ts.createExportDeclaration(
Expand Down Expand Up @@ -282,7 +325,7 @@ function renderTypeDefForIdentifier(
if (state.options.strictUnions) {
return [
renderUnionTypeReexport(resolvedIdentifier, node, state),
renderClassReexport(resolvedIdentifier, node, state),
renderUnionReexport(resolvedIdentifier, node, state),
...renderUnionInterfaceReexports(
resolvedIdentifier,
definition,
Expand Down Expand Up @@ -331,13 +374,35 @@ function renderTypeDefForIdentifier(
]

case SyntaxType.ConstDefinition:
return [
ts.createVariableStatement(
tokens(true),
createConst(
node.name.value,
undefined,
ts.createIdentifier(resolvedIdentifier.fullName),
),
),
]
case SyntaxType.EnumDefinition:
return [
ts.createImportEqualsDeclaration(
ts.createVariableStatement(
tokens(true),
createConst(
node.name.value,
undefined,
ts.createIdentifier(resolvedIdentifier.fullName),
),
),
ts.createTypeAliasDeclaration(
undefined,
[ts.createToken(ts.SyntaxKind.ExportKeyword)],
ts.createIdentifier(node.name.value),
ts.createIdentifier(resolvedIdentifier.fullName),
tokens(true),
node.name.value,
undefined,
ts.createTypeReferenceNode(
resolvedIdentifier.fullName,
undefined,
),
),
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2345,7 +2345,8 @@ export class Processor extends com_test_shared.SharedService.Processor {
constructor(handler: IHandler) {
super({
getStruct: handler.getStruct,
getUnion: handler.getUnion
getUnion: handler.getUnion,
getEnum: handler.getEnum
});
this._handler = handler;
}
Expand All @@ -2363,6 +2364,10 @@ export class Processor extends com_test_shared.SharedService.Processor {
this.process_getUnion(requestId, input, output);
return;
}
case "process_getEnum": {
this.process_getEnum(requestId, input, output);
return;
}
case "process_ping": {
this.process_ping(requestId, input, output);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
* DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
*/
export * from "./constants";
export * from "./typedefs";
export * from "./Work";
export * from "./FirstName";
export * from "./LastName";
export * from "./Choice";
export * from "./NotAGoodIdea";
import * as Calculator from "./Calculator";
export { Calculator as Calculator };
export * from "./typedefs";
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
* Autogenerated by @creditkarma/thrift-typescript v{{VERSION}}
* DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
*/
export * from "./typedefs";
export * from "./OtherCommonUnion";
export * from "./AuthException";
export * from "./typedefs";
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as com_test_shared from "./../shared";
import * as __NAMESPACE__ from "./.";
export import CommonStruct = com_test_shared.SharedStruct;
export import CommonUnion = com_test_shared.SharedUnion;
export import CommonEnum = com_test_shared.SharedEnum;
export import COMMON_INT = com_test_shared.SHARED_INT;
export import NotAllowed = __NAMESPACE__.AuthException;
export import MoreOptions = __NAMESPACE__.OtherCommonUnion;
Loading

0 comments on commit 4f39da1

Please sign in to comment.