Skip to content

Commit 8397add

Browse files
authored
Merge pull request #26 from timocov/fix25_any_unknown
2 parents 69acca7 + 6c4e995 commit 8397add

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
lines changed

src/transformer.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,18 @@ function createTransformerFactory(program: ts.Program, options?: Partial<RenameO
286286

287287
// tslint:disable-next-line:cyclomatic-complexity
288288
function isTypePropertyExternal(type: ts.Type, typePropertyName: string): boolean {
289-
const symbol = type.getSymbol();
290-
const propertySymbol = typeChecker.getPropertyOfType(type, typePropertyName);
289+
// if a type is unknown or any - they should be interpret as a public ones
290+
if (type.flags & ts.TypeFlags.Unknown || type.flags & ts.TypeFlags.Any) {
291+
return true;
292+
}
291293

292294
if (type.flags & ts.TypeFlags.IndexedAccess) {
293295
return isTypePropertyExternal(typeChecker.getApparentType(type), typePropertyName);
294296
}
295297

298+
const symbol = type.getSymbol();
299+
const propertySymbol = typeChecker.getPropertyOfType(type, typePropertyName);
300+
296301
if (type.flags & ts.TypeFlags.Object) {
297302
const objectType = type as ts.ObjectType;
298303
// treat any tuple property as "external"
@@ -353,7 +358,21 @@ function createTransformerFactory(program: ts.Program, options?: Partial<RenameO
353358
// tslint:disable-next-line:cyclomatic-complexity
354359
function getNodeVisibilityType(node: ts.Expression | ts.Identifier | ts.StringLiteral | ts.BindingElement): VisibilityType {
355360
if (ts.isPropertyAssignment(node.parent) || ts.isShorthandPropertyAssignment(node.parent)) {
356-
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);
357376
if (type !== undefined && isTypePropertyExternal(type, node.getText())) {
358377
return VisibilityType.External;
359378
}

tests/test-cases/any/input.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,14 @@ interface PublicByJSDocInterface {
1414
function func(obj1: PublicInterface, obj2: InternalInterface, obj3: PublicByJSDocInterface): void {
1515
console.log(obj1.field.f1Any, obj2.field.f2Any, obj3.field.f3Any);
1616
}
17+
18+
function func2(): any {
19+
const b = (({ value: 321 }) as any);
20+
const c = (((((((((((((({ field: 222 })))))) as any)) as any as any as PublicByJSDocInterface as any)))) as InternalInterface));
21+
return {
22+
foo: 1,
23+
bar: b,
24+
baz: 3,
25+
c: c,
26+
};
27+
}

tests/test-cases/any/output.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
33
function func(obj1, obj2, obj3) {
44
console.log(obj1.field.f1Any, obj2._internal_field.f2Any, obj3.field.f3Any);
55
}
6+
function func2() {
7+
var b = ({ value: 321 });
8+
var c = (((((({ _internal_field: 222 }))))));
9+
return {
10+
foo: 1,
11+
bar: b,
12+
baz: 3,
13+
c: c,
14+
};
15+
}

tests/test-cases/unknown/input.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
interface InternalInterface {
2+
int: number;
3+
}
4+
5+
/** @public */
6+
interface PublicInterface {
7+
int: number;
8+
}
9+
10+
function func(): unknown {
11+
const b = (({ value: 321 }) as unknown);
12+
const c = ((((((((({ int: 222 })))) as unknown)) as unknown as any as PublicInterface) as InternalInterface));
13+
14+
return {
15+
foo: 1,
16+
bar: b,
17+
baz: 3,
18+
c: c,
19+
};
20+
}

tests/test-cases/unknown/output.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function func() {
2+
var b = ({ value: 321 });
3+
var c = (((({ _internal_int: 222 }))));
4+
return {
5+
foo: 1,
6+
bar: b,
7+
baz: 3,
8+
c: c,
9+
};
10+
}

0 commit comments

Comments
 (0)