Skip to content

Commit 159a0a2

Browse files
author
Andy
authored
Use NodeFlags to detect nodes inside with statements instead of climbing ancestors (#17721)
* Use NodeFlags to detect nodes inside with statements instead of climbing ancestors * Add to ContextFlags
1 parent f916e38 commit 159a0a2

File tree

3 files changed

+8
-20
lines changed

3 files changed

+8
-20
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13440,7 +13440,7 @@ namespace ts {
1344013440
// exists. Otherwise, it is the type of the string index signature in T, if one exists.
1344113441
function getContextualTypeForObjectLiteralMethod(node: MethodDeclaration): Type {
1344213442
Debug.assert(isObjectLiteralMethod(node));
13443-
if (isInsideWithStatementBody(node)) {
13443+
if (node.flags & NodeFlags.InWithStatement) {
1344413444
// We cannot answer semantic questions within a with block, do not proceed any further
1344513445
return undefined;
1344613446
}
@@ -13559,7 +13559,7 @@ namespace ts {
1355913559
* @returns the contextual type of an expression.
1356013560
*/
1356113561
function getContextualType(node: Expression): Type | undefined {
13562-
if (isInsideWithStatementBody(node)) {
13562+
if (node.flags & NodeFlags.InWithStatement) {
1356313563
// We cannot answer semantic questions within a with block, do not proceed any further
1356413564
return undefined;
1356513565
}
@@ -23124,21 +23124,8 @@ namespace ts {
2312423124

2312523125
// Language service support
2312623126

23127-
function isInsideWithStatementBody(node: Node): boolean {
23128-
if (node) {
23129-
while (node.parent) {
23130-
if (node.parent.kind === SyntaxKind.WithStatement && (<WithStatement>node.parent).statement === node) {
23131-
return true;
23132-
}
23133-
node = node.parent;
23134-
}
23135-
}
23136-
23137-
return false;
23138-
}
23139-
2314023127
function getSymbolsInScope(location: Node, meaning: SymbolFlags): Symbol[] {
23141-
if (isInsideWithStatementBody(location)) {
23128+
if (location.flags & NodeFlags.InWithStatement) {
2314223129
// We cannot answer semantic questions within a with block, do not proceed any further
2314323130
return [];
2314423131
}
@@ -23438,7 +23425,7 @@ namespace ts {
2343823425
return isExternalModule(<SourceFile>node) ? getMergedSymbol(node.symbol) : undefined;
2343923426
}
2344023427

23441-
if (isInsideWithStatementBody(node)) {
23428+
if (node.flags & NodeFlags.InWithStatement) {
2344223429
// We cannot answer semantic questions within a with block, do not proceed any further
2344323430
return undefined;
2344423431
}
@@ -23546,7 +23533,7 @@ namespace ts {
2354623533
}
2354723534

2354823535
function getTypeOfNode(node: Node): Type {
23549-
if (isInsideWithStatementBody(node)) {
23536+
if (node.flags & NodeFlags.InWithStatement) {
2355023537
// We cannot answer semantic questions within a with block, do not proceed any further
2355123538
return unknownType;
2355223539
}

src/compiler/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4724,7 +4724,7 @@ namespace ts {
47244724
parseExpected(SyntaxKind.OpenParenToken);
47254725
node.expression = allowInAnd(parseExpression);
47264726
parseExpected(SyntaxKind.CloseParenToken);
4727-
node.statement = parseStatement();
4727+
node.statement = doInsideOfContext(NodeFlags.InWithStatement, parseStatement);
47284728
return finishNode(node);
47294729
}
47304730

src/compiler/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,14 +451,15 @@ namespace ts {
451451
/* @internal */
452452
PossiblyContainsDynamicImport = 1 << 19,
453453
JSDoc = 1 << 20, // If node was parsed inside jsdoc
454+
InWithStatement = 1 << 21, // If any ancestor of node was the `statement` of a WithStatement (not the `expression`)
454455

455456
BlockScoped = Let | Const,
456457

457458
ReachabilityCheckFlags = HasImplicitReturn | HasExplicitReturn,
458459
ReachabilityAndEmitFlags = ReachabilityCheckFlags | HasAsyncFunctions,
459460

460461
// Parsing context flags
461-
ContextFlags = DisallowInContext | YieldContext | DecoratorContext | AwaitContext | JavaScriptFile,
462+
ContextFlags = DisallowInContext | YieldContext | DecoratorContext | AwaitContext | JavaScriptFile | InWithStatement,
462463

463464
// Exclude these flags when parsing a Type
464465
TypeExcludesFlags = YieldContext | AwaitContext,

0 commit comments

Comments
 (0)