Skip to content

Commit 0ecfe7e

Browse files
committed
Fixed incorrect handing in operator with string literal
Fixes #5
1 parent ee041fe commit 0ecfe7e

File tree

3 files changed

+94
-8
lines changed

3 files changed

+94
-8
lines changed

src/transformer.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ function createTransformerFactory(program: ts.Program, options?: Partial<RenameO
8787
return handleShorthandObjectBindingElement(node);
8888
}
8989

90+
// 'node' in obj
91+
if (ts.isStringLiteral(node) && ts.isBinaryExpression(node.parent) && node.parent.operatorToken.kind === ts.SyntaxKind.InKeyword) {
92+
return handleInKeyword(node);
93+
}
94+
9095
if (ts.isIdentifier(node) && node.parent) {
9196
// obj.node
9297
if (ts.isPropertyAccessExpression(node.parent) && node.parent.name === node) {
@@ -131,6 +136,21 @@ function createTransformerFactory(program: ts.Program, options?: Partial<RenameO
131136
return node;
132137
}
133138

139+
function handleInKeyword(node: ts.StringLiteral): ts.StringLiteral {
140+
const parent = node.parent as ts.BinaryExpression;
141+
const nodeType = typeChecker.getTypeAtLocation(node);
142+
if (!nodeType.isStringLiteral()) {
143+
throw new Error(`Can't get type for left expression in ${node.parent.getText()}`);
144+
}
145+
146+
const propertyName = nodeType.value;
147+
if (isTypePropertyExternal(typeChecker.getTypeAtLocation(parent.right), propertyName)) {
148+
return node;
149+
}
150+
151+
return createNewNode(propertyName, VisibilityType.Internal, ts.createStringLiteral);
152+
}
153+
134154
// obj.node
135155
function handlePropertyAccessIdentifier(node: ts.Identifier): ts.Identifier {
136156
return createNewIdentifier(node);
@@ -143,7 +163,7 @@ function createTransformerFactory(program: ts.Program, options?: Partial<RenameO
143163
return node;
144164
}
145165

146-
return createNewNode(node, visibilityType, ts.createStringLiteral);
166+
return createNewNodeFromProperty(node, visibilityType, ts.createStringLiteral);
147167
}
148168

149169
// private node
@@ -169,7 +189,7 @@ function createTransformerFactory(program: ts.Program, options?: Partial<RenameO
169189
return node;
170190
}
171191

172-
return createNewNode(node.name, visibilityType, (newName: string) => {
192+
return createNewNodeFromProperty(node.name, visibilityType, (newName: string) => {
173193
return ts.createPropertyAssignment(newName, node.name);
174194
});
175195
}
@@ -185,7 +205,7 @@ function createTransformerFactory(program: ts.Program, options?: Partial<RenameO
185205
return node;
186206
}
187207

188-
return createNewNode(node.name, visibilityType, (newName: string) => {
208+
return createNewNodeFromProperty(node.name, visibilityType, (newName: string) => {
189209
return ts.createBindingElement(node.dotDotDotToken, newName, node.name, node.initializer);
190210
});
191211
}
@@ -203,21 +223,23 @@ function createTransformerFactory(program: ts.Program, options?: Partial<RenameO
203223
return oldIdentifier;
204224
}
205225

206-
return createNewNode(oldIdentifier, visibilityType, ts.createIdentifier);
226+
return createNewNodeFromProperty(oldIdentifier, visibilityType, ts.createIdentifier);
207227
}
208228

209-
function createNewNode<T extends ts.Node>(oldProperty: ts.PropertyName, type: VisibilityType, createNode: (newName: string) => T): T {
229+
function createNewNodeFromProperty<T extends ts.Node>(oldProperty: ts.PropertyName, type: VisibilityType, createNode: (newName: string) => T): T {
210230
const symbol = typeChecker.getSymbolAtLocation(oldProperty);
211231
if (symbol === undefined) {
212232
throw new Error(`Cannot get symbol for node "${oldProperty.getText()}"`);
213233
}
214234

215235
const oldPropertyName = symbol.escapedName as string;
216236

217-
const newPropertyName = getNewName(oldPropertyName, type);
218-
const newProperty = createNode(newPropertyName);
237+
return createNewNode(oldPropertyName, type, createNode);
238+
}
219239

220-
return newProperty;
240+
function createNewNode<T extends ts.Node>(oldPropertyName: string, type: VisibilityType, createNode: (newName: string) => T): T {
241+
const newPropertyName = getNewName(oldPropertyName, type);
242+
return createNode(newPropertyName);
221243
}
222244

223245
function getNewName(originalName: string, type: VisibilityType): string {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
export interface ExportedInterface {
2+
foo: number;
3+
}
4+
5+
export interface AnotherExportedInterface {
6+
bar: number;
7+
isAnother: boolean;
8+
}
9+
10+
export type ExportedType = ExportedInterface | AnotherExportedInterface;
11+
12+
interface NonExportedInterface {
13+
foo: number;
14+
}
15+
16+
interface AnotherNonExportedInterface {
17+
bar: number;
18+
isAnother: boolean;
19+
}
20+
21+
type NonExportedType = NonExportedInterface | AnotherNonExportedInterface;
22+
23+
declare function getNonExported(): NonExportedType;
24+
declare const moveEvent: MouseEvent | TouchEvent;
25+
26+
export function func(type: ExportedType): void {
27+
if ('isAnother' in type) {
28+
console.log(type.bar);
29+
} else {
30+
console.log(type.foo);
31+
}
32+
33+
const nonExportedVar = getNonExported();
34+
if ('isAnother' in nonExportedVar) {
35+
console.log(nonExportedVar.bar);
36+
} else {
37+
console.log(nonExportedVar.foo);
38+
}
39+
40+
if ('onorientationchange' in window || 'button' in moveEvent) {
41+
console.log('check');
42+
}
43+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
function func(type) {
4+
if ('isAnother' in type) {
5+
console.log(type.bar);
6+
}
7+
else {
8+
console.log(type.foo);
9+
}
10+
var nonExportedVar = getNonExported();
11+
if ("_internal_isAnother" in nonExportedVar) {
12+
console.log(nonExportedVar._internal_bar);
13+
}
14+
else {
15+
console.log(nonExportedVar._internal_foo);
16+
}
17+
if ('onorientationchange' in window || 'button' in moveEvent) {
18+
console.log('check');
19+
}
20+
}
21+
exports.func = func;

0 commit comments

Comments
 (0)