Skip to content

Commit

Permalink
Use NodeFlags to detect nodes inside with statements instead of climb…
Browse files Browse the repository at this point in the history
…ing ancestors (#17721)

* Use NodeFlags to detect nodes inside with statements instead of climbing ancestors

* Add to ContextFlags
  • Loading branch information
Andy authored Oct 23, 2017
1 parent f916e38 commit 159a0a2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 20 deletions.
23 changes: 5 additions & 18 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13440,7 +13440,7 @@ namespace ts {
// exists. Otherwise, it is the type of the string index signature in T, if one exists.
function getContextualTypeForObjectLiteralMethod(node: MethodDeclaration): Type {
Debug.assert(isObjectLiteralMethod(node));
if (isInsideWithStatementBody(node)) {
if (node.flags & NodeFlags.InWithStatement) {
// We cannot answer semantic questions within a with block, do not proceed any further
return undefined;
}
Expand Down Expand Up @@ -13559,7 +13559,7 @@ namespace ts {
* @returns the contextual type of an expression.
*/
function getContextualType(node: Expression): Type | undefined {
if (isInsideWithStatementBody(node)) {
if (node.flags & NodeFlags.InWithStatement) {
// We cannot answer semantic questions within a with block, do not proceed any further
return undefined;
}
Expand Down Expand Up @@ -23124,21 +23124,8 @@ namespace ts {

// Language service support

function isInsideWithStatementBody(node: Node): boolean {
if (node) {
while (node.parent) {
if (node.parent.kind === SyntaxKind.WithStatement && (<WithStatement>node.parent).statement === node) {
return true;
}
node = node.parent;
}
}

return false;
}

function getSymbolsInScope(location: Node, meaning: SymbolFlags): Symbol[] {
if (isInsideWithStatementBody(location)) {
if (location.flags & NodeFlags.InWithStatement) {
// We cannot answer semantic questions within a with block, do not proceed any further
return [];
}
Expand Down Expand Up @@ -23438,7 +23425,7 @@ namespace ts {
return isExternalModule(<SourceFile>node) ? getMergedSymbol(node.symbol) : undefined;
}

if (isInsideWithStatementBody(node)) {
if (node.flags & NodeFlags.InWithStatement) {
// We cannot answer semantic questions within a with block, do not proceed any further
return undefined;
}
Expand Down Expand Up @@ -23546,7 +23533,7 @@ namespace ts {
}

function getTypeOfNode(node: Node): Type {
if (isInsideWithStatementBody(node)) {
if (node.flags & NodeFlags.InWithStatement) {
// We cannot answer semantic questions within a with block, do not proceed any further
return unknownType;
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4724,7 +4724,7 @@ namespace ts {
parseExpected(SyntaxKind.OpenParenToken);
node.expression = allowInAnd(parseExpression);
parseExpected(SyntaxKind.CloseParenToken);
node.statement = parseStatement();
node.statement = doInsideOfContext(NodeFlags.InWithStatement, parseStatement);
return finishNode(node);
}

Expand Down
3 changes: 2 additions & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,14 +451,15 @@ namespace ts {
/* @internal */
PossiblyContainsDynamicImport = 1 << 19,
JSDoc = 1 << 20, // If node was parsed inside jsdoc
InWithStatement = 1 << 21, // If any ancestor of node was the `statement` of a WithStatement (not the `expression`)

BlockScoped = Let | Const,

ReachabilityCheckFlags = HasImplicitReturn | HasExplicitReturn,
ReachabilityAndEmitFlags = ReachabilityCheckFlags | HasAsyncFunctions,

// Parsing context flags
ContextFlags = DisallowInContext | YieldContext | DecoratorContext | AwaitContext | JavaScriptFile,
ContextFlags = DisallowInContext | YieldContext | DecoratorContext | AwaitContext | JavaScriptFile | InWithStatement,

// Exclude these flags when parsing a Type
TypeExcludesFlags = YieldContext | AwaitContext,
Expand Down

0 comments on commit 159a0a2

Please sign in to comment.