-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support 'typeof class' types #41587
base: main
Are you sure you want to change the base?
Support 'typeof class' types #41587
Changes from 5 commits
a0354a3
a9c2c70
c96b328
d787239
4a39972
a2c1adf
bf1ac8a
9d9dea0
7ee42c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2860,10 +2860,24 @@ namespace ts { | |
return type; | ||
} | ||
|
||
function isStartOfTypeofClassExpression() { | ||
return token() === SyntaxKind.ClassKeyword || | ||
token() === SyntaxKind.AbstractKeyword && lookAhead(() => nextToken() === SyntaxKind.ClassKeyword && !scanner.hasPrecedingLineBreak()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I mention in my comment in |
||
} | ||
|
||
function parseTypeofClassExpression(): ClassExpression { | ||
const pos = getNodePos(); | ||
const hasJSDoc = hasPrecedingJSDocComment(); | ||
const modifiers = parseModifiers(); | ||
return <ClassExpression>parseClassDeclarationOrExpression(pos, hasJSDoc, /*decorators*/ undefined, modifiers, SyntaxKind.ClassExpression); | ||
} | ||
|
||
function parseTypeQuery(): TypeQueryNode { | ||
const pos = getNodePos(); | ||
parseExpected(SyntaxKind.TypeOfKeyword); | ||
return finishNode(factory.createTypeQueryNode(parseEntityName(/*allowReservedWords*/ true)), pos); | ||
return finishNode(factory.createTypeQueryNode(isStartOfTypeofClassExpression() ? | ||
doInsideOfContext(NodeFlags.Ambient, parseTypeofClassExpression) : | ||
parseEntityName(/*allowReservedWords*/ true)), pos); | ||
} | ||
|
||
function parseTypeParameter(): TypeParameterDeclaration { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,7 +157,7 @@ namespace ts.refactor { | |
return true; | ||
} | ||
} | ||
else if (isTypeQueryNode(node)) { | ||
else if (isTypeQueryNode(node) && node.exprName.kind !== SyntaxKind.ClassExpression) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just wondering out loud: it might be useful to make an |
||
if (isIdentifier(node.exprName)) { | ||
const symbol = checker.resolveName(node.exprName.text, node.exprName, SymbolFlags.Value, /* excludeGlobals */ false); | ||
if (symbol && rangeContainsSkipTrivia(statement, symbol.valueDeclaration, file) && !rangeContainsSkipTrivia(selection, symbol.valueDeclaration, file)) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't that also make it valid for a regular class expression? If not, could we consider allowing it in parse? It's never made sense to me that you can write this:
But not this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We certainly could allow it, but it would in effect be an expression syntax extension and we generally try to avoid those.