Skip to content

Commit 022ed63

Browse files
author
Pedram Razavi
committed
Fix extra underscore getting added to properties that start with double underscores
Internally, typescript seems to add an extra underscore to identifiers that start with two underscores and based on https://github.com/microsoft/TypeScript-wiki/blob/master/API-Breaking-Changes.md#typescript-30 unescapeLeadingUnderscores is the type-safe way of inverting this: "...the typesafe escapeLeadingUnderscores and unescapeLeadingUnderscores should be used if the types indicate they are required (as they are used to convert to or from branded __String and string types)."
1 parent 2802a61 commit 022ed63

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/transformer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ function createTransformerFactory(program: ts.Program, options?: Partial<RenameO
253253
throw new Error(`Cannot get symbol for node "${oldProperty.getText()}"`);
254254
}
255255

256-
const oldPropertyName = symbol.escapedName as string;
256+
const oldPropertyName = ts.unescapeLeadingUnderscores(symbol.escapedName);
257257

258258
return createNewNode(oldPropertyName, type, createNode);
259259
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class DoubleUnderscoreProperty {
2+
public __foo: string = "bar";
3+
}
4+
5+
const testObject = new DoubleUnderscoreProperty();
6+
export const foo = testObject.__foo + "baz";
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.foo = void 0;
4+
var DoubleUnderscoreProperty = /** @class */ (function () {
5+
function DoubleUnderscoreProperty() {
6+
this._internal___foo = "bar";
7+
}
8+
return DoubleUnderscoreProperty;
9+
}());
10+
var testObject = new DoubleUnderscoreProperty();
11+
exports.foo = testObject._internal___foo + "baz";

0 commit comments

Comments
 (0)