Skip to content

Fix #113: Make generated function names inside arrow-function deterministic in watch-mode runs and in recompiles #114

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

Merged
merged 1 commit into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/transform-inline/transform-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { sliceMapValues } from './utils';
function createArrowFunction(type: ts.Type, rootName: string, optional: boolean, partialVisitorContext: PartialVisitorContext) {
const functionMap: VisitorContext['functionMap'] = new Map();
const functionNames: VisitorContext['functionNames'] = new Set();
const visitorContext = { ...partialVisitorContext, functionNames, functionMap };
const typeIdMap: VisitorContext['typeIdMap'] = new Map();
const visitorContext = { ...partialVisitorContext, functionNames, functionMap, typeIdMap };
const functionName = partialVisitorContext.options.shortCircuit
? visitShortCircuit(visitorContext)
: (optional
Expand Down
1 change: 1 addition & 0 deletions src/transform-inline/visitor-context.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface Options {
export interface VisitorContext extends PartialVisitorContext {
functionNames: Set<string>;
functionMap: Map<string, ts.FunctionDeclaration>;
typeIdMap: Map<string, string>;
}

export interface PartialVisitorContext {
Expand Down
31 changes: 21 additions & 10 deletions src/transform-inline/visitor-type-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,19 @@ function visitArrayObjectType(type: ts.ObjectType, visitorContext: VisitorContex
return `sa_${numberIndexName}_ea`;
}

function visitRegularObjectType(type: ts.ObjectType) {
const id: string = (type as unknown as { id: string }).id;
return `_${id}`;
function getTypeIndexById(type: ts.Type, { typeIdMap }: VisitorContext) {
const id = (type as unknown as { id: string | number }).id.toString();
let index = typeIdMap.get(id);
if (index === undefined) {
index = typeIdMap.size.toString();
typeIdMap.set(id, index);
}
return index;
}

function visitRegularObjectType(type: ts.Type, visitorContext: VisitorContext) {
const index = getTypeIndexById(type, visitorContext);
return `_${index}`;
}

function visitTypeReference(type: ts.TypeReference, visitorContext: VisitorContext, mode: NameMode) {
Expand Down Expand Up @@ -71,7 +81,7 @@ function visitObjectType(type: ts.ObjectType, visitorContext: VisitorContext, mo
} else if (checkIsDateClass(type)) {
return '_date';
} else {
return visitRegularObjectType(type);
return visitRegularObjectType(type, visitorContext);
}
}

Expand All @@ -98,7 +108,7 @@ function visitIndexedAccessType(type: ts.IndexedAccessType, visitorContext: Visi

export function visitType(type: ts.Type, visitorContext: VisitorContext, mode: NameMode): string {
let name: string;
const id: string = (type as unknown as { id: string }).id;
const index = getTypeIndexById(type, visitorContext);
if ((ts.TypeFlags.Any & type.flags) !== 0) {
name = VisitorUtils.getAnyFunction(visitorContext);
} else if ((ts.TypeFlags.Unknown & type.flags) !== 0) {
Expand All @@ -118,25 +128,25 @@ export function visitType(type: ts.Type, visitorContext: VisitorContext, mode: N
} else if ((ts.TypeFlags.String & type.flags) !== 0) {
name = VisitorUtils.getStringFunction(visitorContext);
} else if ((ts.TypeFlags.BooleanLiteral & type.flags) !== 0) {
name = `_${id}`;
name = `_${index}`;
} else if (tsutils.isTypeReference(type) && visitorContext.previousTypeReference !== type) {
name = visitTypeReference(type, visitorContext, mode);
} else if ((ts.TypeFlags.TypeParameter & type.flags) !== 0) {
name = visitTypeParameter(type, visitorContext, mode);
} else if (tsutils.isObjectType(type)) {
name = visitObjectType(type, visitorContext, mode);
} else if (tsutils.isLiteralType(type)) {
name = `_${id}`;
name = `_${index}`;
} else if (tsutils.isUnionOrIntersectionType(type)) {
name = visitUnionOrIntersectionType(type, visitorContext, mode);
} else if ((ts.TypeFlags.NonPrimitive & type.flags) !== 0) {
name = `_${id}`;
name = `_${index}`;
} else if ((ts.TypeFlags.Index & type.flags) !== 0) {
name = visitIndexType(type, visitorContext);
} else if (tsutils.isIndexedAccessType(type)) {
name = visitIndexedAccessType(type, visitorContext);
} else if ((ts.TypeFlags.TemplateLiteral & type.flags) !== 0) {
name = `_${id}`;
name = `_${index}`;
} else {
throw new Error('Could not generate type-check; unsupported type with flags: ' + type.flags);
}
Expand All @@ -153,7 +163,8 @@ export function visitType(type: ts.Type, visitorContext: VisitorContext, mode: N
if (tsutils.isTypeReference(type) && type.typeArguments !== undefined) {
for (const typeArgument of type.typeArguments) {
const resolvedType = VisitorUtils.getResolvedTypeParameter(typeArgument, visitorContext) || typeArgument;
name += `_${(resolvedType as unknown as { id: string }).id}`;
const resolvedTypeIndex = getTypeIndexById(resolvedType, visitorContext);
name += `_${resolvedTypeIndex}`;
}
}
return name;
Expand Down