Skip to content

Commit

Permalink
fix imported identifiers in maps
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Hayes committed Sep 14, 2019
1 parent e8931ff commit 5d33f49
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 42 deletions.
20 changes: 16 additions & 4 deletions src/main/resolver/resolveConstValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { ValidationError } from '../errors'
import { INamespace, INamespacePath, IResolveContext } from '../types'
import { stubIdentifier } from '../utils'
import { resolveIdentifier } from './resolveIdentifier'

/**
* It makes things easier to rewrite all const values to their literal values.
Expand Down Expand Up @@ -50,7 +51,11 @@ export function resolveConstValue(
}

case SyntaxType.Identifier:
const [head, ...tail] = value.value.split('.')
const resolvedIdentifier = resolveIdentifier(
value,
context.currentNamespace,
)
const [head, ...tail] = resolvedIdentifier.value.split('.')
if (context.currentNamespace.exports[head]) {
const statement: ThriftStatement =
context.currentNamespace.exports[head]
Expand All @@ -61,7 +66,7 @@ export function resolveConstValue(
context,
)
} else {
return value
return resolvedIdentifier
}
} else {
const nextNamespacePath: INamespacePath | undefined =
Expand All @@ -71,14 +76,21 @@ export function resolveConstValue(
const nextNamespace: INamespace =
context.namespaceMap[nextNamespacePath.accessor]

return resolveConstValue(
stubIdentifier(tail.join('.')),
const stub = stubIdentifier(tail.join('.'))
const resolvedValue = resolveConstValue(
stub,
expectedType,
{
currentNamespace: nextNamespace,
namespaceMap: context.namespaceMap,
},
)

// Check if returned identifier matches stub, if it does, return the correct identifier
return stub.type === resolvedValue.type &&
stub.value === resolvedValue.value
? resolvedIdentifier
: resolvedValue
}
}
throw new ValidationError(
Expand Down
35 changes: 35 additions & 0 deletions src/main/resolver/resolveIdentifier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Identifier, SyntaxType } from '@creditkarma/thrift-parser'
import { INamespace, INamespacePath } from '../types'

export function resolveIdentifier(
id: Identifier,
currentNamespace: INamespace,
): Identifier {
return {
type: SyntaxType.Identifier,
value: resolveName(id.value, currentNamespace),
annotations: id.annotations,
loc: id.loc,
}
}

export function resolveName(
name: string,
currentNamespace: INamespace,
): string {
const [head, ...tail] = name.split('.')

if (currentNamespace.exports[head] !== undefined) {
return name
} else if (currentNamespace.includedNamespaces[head] !== undefined) {
const namespacePath: INamespacePath =
currentNamespace.includedNamespaces[head]
return [namespacePath.accessor, ...tail].join('.')
} else if (currentNamespace.namespaceIncludes[head]) {
const namespaceAccessor: string =
currentNamespace.namespaceIncludes[head]
return [namespaceAccessor, ...tail].join('.')
} else {
return name
}
}
45 changes: 7 additions & 38 deletions src/main/resolver/resolveNamespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
FieldType,
FunctionDefinition,
FunctionType,
Identifier,
IntConstant,
SyntaxType,
} from '@creditkarma/thrift-parser'
Expand All @@ -16,7 +15,6 @@ import {
DefinitionType,
INamespace,
INamespaceMap,
INamespacePath,
IResolveResult,
} from '../types'

Expand All @@ -25,6 +23,7 @@ import { createValidationError, IThriftError, ValidationError } from '../errors'
import { emptyLocation } from '../utils'

import { resolveConstValue } from './resolveConstValue'
import { resolveIdentifier } from './resolveIdentifier'
import { resolveIdentifierDefinition } from './resolveIdentifierDefinition'

/**
Expand Down Expand Up @@ -169,7 +168,10 @@ export function resolveNamespace(
extends:
statement.extends === null
? null
: resolveIdentifier(statement.extends),
: resolveIdentifier(
statement.extends,
currentNamespace,
),
comments: statement.comments,
annotations: statement.annotations,
loc: statement.loc,
Expand All @@ -181,33 +183,6 @@ export function resolveNamespace(
}
}

function resolveIdentifier(id: Identifier): Identifier {
return {
type: SyntaxType.Identifier,
value: resolveName(id.value),
annotations: id.annotations,
loc: id.loc,
}
}

function resolveName(name: string): string {
const [head, ...tail] = name.split('.')

if (currentNamespace.exports[head] !== undefined) {
return name
} else if (currentNamespace.includedNamespaces[head] !== undefined) {
const namespacePath: INamespacePath =
currentNamespace.includedNamespaces[head]
return [namespacePath.accessor, ...tail].join('.')
} else if (currentNamespace.namespaceIncludes[head]) {
const namespaceAccessor: string =
currentNamespace.namespaceIncludes[head]
return [namespaceAccessor, ...tail].join('.')
} else {
return name
}
}

function resolveFunctionType(fieldType: FunctionType): FunctionType {
switch (fieldType.type) {
case SyntaxType.VoidKeyword:
Expand Down Expand Up @@ -256,7 +231,7 @@ export function resolveNamespace(
) {
return definition.definitionType
} else {
return resolveIdentifier(fieldType)
return resolveIdentifier(fieldType, currentNamespace)
}

case SyntaxType.ListType:
Expand Down Expand Up @@ -329,16 +304,10 @@ export function resolveNamespace(
value: ConstValue,
fieldType: FunctionType,
): ConstValue {
const resolvedValue: ConstValue = resolveConstValue(value, fieldType, {
return resolveConstValue(value, fieldType, {
currentNamespace,
namespaceMap,
})

if (resolvedValue.type === SyntaxType.Identifier) {
return resolveIdentifier(resolvedValue)
} else {
return resolvedValue
}
}

function resolveFields(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* tslint:disable */
/* eslint-disable */
/*
* Autogenerated by @creditkarma/thrift-typescript v{{VERSION}}
* DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
*/
import * as __ROOT_NAMESPACE__ from "./../../..";
export const EnumKeyMap: Map<__ROOT_NAMESPACE__.SharedEnum, string> = new Map([[__ROOT_NAMESPACE__.SharedEnum.value1, "value1"], [__ROOT_NAMESPACE__.SharedEnum.value2, "value2"]]);
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Autogenerated by @creditkarma/thrift-typescript v{{VERSION}}
* DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
*/
export * from "./constants";
export * from "./CommonStruct";
export * from "./CommonUnion";
export * from "./CommonEnum";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* tslint:disable */
/* eslint-disable */
/*
* Autogenerated by @creditkarma/thrift-typescript v{{VERSION}}
* DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
*/
import * as __ROOT_NAMESPACE__ from "./../../..";
export const EnumKeyMap: Map<__ROOT_NAMESPACE__.SharedEnum, string> = new Map([[__ROOT_NAMESPACE__.SharedEnum.value1, "value1"], [__ROOT_NAMESPACE__.SharedEnum.value2, "value2"]]);
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Autogenerated by @creditkarma/thrift-typescript v{{VERSION}}
* DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
*/
export * from "./constants";
export * from "./CommonStruct";
export * from "./CommonUnion";
export * from "./CommonEnum";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* tslint:disable */
/* eslint-disable */
/*
* Autogenerated by @creditkarma/thrift-typescript v{{VERSION}}
* DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
*/
import * as __ROOT_NAMESPACE__ from "./../../..";
export const EnumKeyMap: Map<__ROOT_NAMESPACE__.SharedEnum, string> = new Map([[__ROOT_NAMESPACE__.SharedEnum.value1, "value1"], [__ROOT_NAMESPACE__.SharedEnum.value2, "value2"]]);
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Autogenerated by @creditkarma/thrift-typescript v{{VERSION}}
* DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
*/
export * from "./constants";
export * from "./CommonStruct";
export * from "./CommonUnion";
export * from "./CommonEnum";
Expand Down
5 changes: 5 additions & 0 deletions src/tests/unit/fixtures/thrift/common.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ union OtherCommonUnion {

typedef AuthException NotAllowed
typedef OtherCommonUnion MoreOptions

const map<shared.SharedEnum, string> EnumKeyMap = {
shared.SharedEnum.value1: "value1",
shared.SharedEnum.value2: "value2",
}

0 comments on commit 5d33f49

Please sign in to comment.