Skip to content

Commit 6c4e995

Browse files
author
Evgeniy Timokhov
committed
Added handling casting of object literals
Fixes #25
1 parent 85012d0 commit 6c4e995

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

src/transformer.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,21 @@ function createTransformerFactory(program: ts.Program, options?: Partial<RenameO
358358
// tslint:disable-next-line:cyclomatic-complexity
359359
function getNodeVisibilityType(node: ts.Expression | ts.Identifier | ts.StringLiteral | ts.BindingElement): VisibilityType {
360360
if (ts.isPropertyAssignment(node.parent) || ts.isShorthandPropertyAssignment(node.parent)) {
361-
const type = typeChecker.getContextualType(node.parent.parent);
361+
let expressionToGetTypeFrom: ts.Expression = node.parent.parent;
362+
let lastKnownExpression: ts.AsExpression | ts.ObjectLiteralExpression = node.parent.parent;
363+
let currentNode: ts.Node = node.parent.parent.parent;
364+
365+
while (ts.isParenthesizedExpression(currentNode) || ts.isAsExpression(currentNode)) {
366+
if (ts.isAsExpression(currentNode)) {
367+
expressionToGetTypeFrom = lastKnownExpression;
368+
lastKnownExpression = currentNode;
369+
}
370+
371+
currentNode = currentNode.parent;
372+
}
373+
374+
// to get correct contextual type we need to provide the previous last expression rather than the last one
375+
const type = typeChecker.getContextualType(expressionToGetTypeFrom);
362376
if (type !== undefined && isTypePropertyExternal(type, node.getText())) {
363377
return VisibilityType.External;
364378
}

tests/test-cases/any/input.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function func(obj1: PublicInterface, obj2: InternalInterface, obj3: PublicByJSDo
1717

1818
function func2(): any {
1919
const b = (({ value: 321 }) as any);
20-
const c = (({ field: 222 }) as any) as InternalInterface;
20+
const c = (((((((((((((({ field: 222 })))))) as any)) as any as any as PublicByJSDocInterface as any)))) as InternalInterface));
2121
return {
2222
foo: 1,
2323
bar: b,

tests/test-cases/any/output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function func(obj1, obj2, obj3) {
55
}
66
function func2() {
77
var b = ({ value: 321 });
8-
var c = ({ _internal_field: 222 });
8+
var c = (((((({ _internal_field: 222 }))))));
99
return {
1010
foo: 1,
1111
bar: b,

tests/test-cases/unknown/input.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ interface InternalInterface {
22
int: number;
33
}
44

5+
/** @public */
6+
interface PublicInterface {
7+
int: number;
8+
}
9+
510
function func(): unknown {
611
const b = (({ value: 321 }) as unknown);
7-
const c = (({ int: 222 }) as unknown) as InternalInterface;
12+
const c = ((((((((({ int: 222 })))) as unknown)) as unknown as any as PublicInterface) as InternalInterface));
813

914
return {
1015
foo: 1,

tests/test-cases/unknown/output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function func() {
22
var b = ({ value: 321 });
3-
var c = ({ _internal_int: 222 });
3+
var c = (((({ _internal_int: 222 }))));
44
return {
55
foo: 1,
66
bar: b,

0 commit comments

Comments
 (0)