Skip to content

Commit 74ed470

Browse files
committed
Added handling mapping types
Fixes #15
1 parent cb4aaf3 commit 74ed470

File tree

5 files changed

+74
-1
lines changed

5 files changed

+74
-1
lines changed

src/transformer.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,22 @@ 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+
289291
if (type.flags & ts.TypeFlags.Object) {
290292
const objectType = type as ts.ObjectType;
291293
// treat any tuple property as "external"
292294
if (objectType.objectFlags & ts.ObjectFlags.Tuple) {
293295
return true;
294296
}
297+
298+
// because of we can't get where a property come from in mapped types
299+
// let's check the whole its type explicitly
300+
// thus let's treat any property of a mapped type as "external" if its parent type is external
301+
// perhaps it would be awesome to handle exactly property we have, but ¯\_(ツ)_/¯
302+
if ((objectType.objectFlags & ts.ObjectFlags.Mapped) && symbol !== undefined && getSymbolVisibilityType(symbol) === VisibilityType.External) {
303+
return true;
304+
}
295305
}
296306

297307
if (type.isUnionOrIntersection()) {
@@ -301,7 +311,6 @@ function createTransformerFactory(program: ts.Program, options?: Partial<RenameO
301311
}
302312
}
303313

304-
const symbol = type.getSymbol();
305314
if (symbol !== undefined) {
306315
const declarations = getDeclarationsForSymbol(symbol);
307316
for (const declaration of declarations) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
type TestKey = 'a';
2+
3+
export type MappedType = {
4+
[P in TestKey]?: {};
5+
};
6+
7+
export interface Interface {
8+
a?: {};
9+
}
10+
11+
class TestMappedClass {
12+
private interfaced: Interface = {
13+
a: {},
14+
};
15+
16+
private mapped: MappedType = {
17+
a: {},
18+
};
19+
20+
public constructor() {
21+
this.interfaced.a = {};
22+
this.mapped.a = {};
23+
}
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
var TestMappedClass = /** @class */ (function () {
4+
function TestMappedClass() {
5+
this._private_interfaced = {
6+
a: {},
7+
};
8+
this._private_mapped = {
9+
a: {},
10+
};
11+
this._private_interfaced.a = {};
12+
this._private_mapped.a = {};
13+
}
14+
return TestMappedClass;
15+
}());
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
type TestKey = 'a' | 'b';
2+
3+
/** @public */
4+
type TestMappedType = {
5+
[P in TestKey]?: {};
6+
};
7+
8+
class TestMappedClass {
9+
private mapped: TestMappedType = {
10+
a: {},
11+
};
12+
13+
public constructor() {
14+
this.mapped.a = {};
15+
}
16+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var TestMappedClass = /** @class */ (function () {
2+
function TestMappedClass() {
3+
this._private_mapped = {
4+
a: {},
5+
};
6+
this._private_mapped.a = {};
7+
}
8+
return TestMappedClass;
9+
}());

0 commit comments

Comments
 (0)