Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(eslint-plugin): added safe getTypeOfPropertyOfType wrapper (#2567)
- Loading branch information
Josh Goldberg
committed
Sep 27, 2020
1 parent
2b2224b
commit 7cba2de
Showing
5 changed files
with
75 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as ts from 'typescript'; | ||
|
||
export function getTypeOfPropertyOfName( | ||
checker: ts.TypeChecker, | ||
type: ts.Type, | ||
name: string, | ||
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('__')) { | ||
return checker.getTypeOfPropertyOfType(type, name); | ||
} | ||
|
||
// Symbolic names may differ in their escaped name compared to their human-readable name | ||
// https://github.com/typescript-eslint/typescript-eslint/issues/2143 | ||
const escapedProperty = type | ||
.getProperties() | ||
.find(property => property.escapedName === escapedName); | ||
|
||
return escapedProperty | ||
? checker.getDeclaredTypeOfSymbol(escapedProperty) | ||
: undefined; | ||
} | ||
|
||
export function getTypeOfPropertyOfType( | ||
checker: ts.TypeChecker, | ||
type: ts.Type, | ||
property: ts.Symbol, | ||
): ts.Type | undefined { | ||
return getTypeOfPropertyOfName( | ||
checker, | ||
type, | ||
property.getName(), | ||
property.getEscapedName(), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters