Skip to content

Commit

Permalink
fix: handle class sharp private field and member
Browse files Browse the repository at this point in the history
  • Loading branch information
lonyele committed Feb 11, 2022
1 parent 9d029a9 commit cf9199f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Expand Up @@ -222,6 +222,21 @@ ruleTester.run('prefer-readonly-parameter-types', rule, {
},
],
},
// PrivateIdentifier is handled without throwing error.
{
code: `
class Foo {
readonly #privateField = 'foo';
#privateMember() {}
}
function foo(arg: Foo) {}
`,
options: [
{
treatMethodsAsReadonly: true,
},
],
},

// parameter properties should work fine
{
Expand Down
17 changes: 16 additions & 1 deletion packages/type-utils/src/propertyTypes.ts
Expand Up @@ -7,7 +7,7 @@ export function getTypeOfPropertyOfName(
escapedName?: ts.__String,
): ts.Type | undefined {
// Most names are directly usable in the checker and aren't different from escaped names
if (!escapedName || !name.startsWith('__')) {
if (!escapedName || !isSymbol(escapedName)) {
return checker.getTypeOfPropertyOfType(type, name);
}

Expand All @@ -34,3 +34,18 @@ export function getTypeOfPropertyOfType(
property.getEscapedName(),
);
}

// Symbolic names need to be specially handled because TS api is not sufficient for these cases.
function isSymbol(escapedName: string): boolean {
return isKnownSymbol(escapedName) || isPrivateIdentifierSymbol(escapedName);
}

// case for escapedName: "__@foo@10", name: "__@foo@10"
function isKnownSymbol(escapedName: string): boolean {
return escapedName.startsWith('__@');
}

// case for escapedName: "__#1@#foo", name: "#foo"
function isPrivateIdentifierSymbol(escapedName: string): boolean {
return escapedName.startsWith('__#');
}

0 comments on commit cf9199f

Please sign in to comment.