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

Replace import equals declarations with const and type alias statements #155

Merged
merged 5 commits into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
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 @@ -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