Skip to content
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

Add support for Optional Chaining #33294

Merged
merged 25 commits into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c95daab
Add support for Optional Chaining
rbuckton Sep 6, 2019
c2f53fc
Add grammar error for invalid tagged template, more tests
rbuckton Sep 7, 2019
0f5d5d6
Prototype
andrewbranch Sep 23, 2019
24de747
Merge branch 'master' into optionalChainingStage3
rbuckton Sep 23, 2019
7be47ab
PR feedback
rbuckton Sep 24, 2019
e073c05
Add errors for invalid assignments and a trailing '?.'
rbuckton Sep 24, 2019
72f44d9
Add additional signature help test, fix lint warnings
rbuckton Sep 24, 2019
488c9e6
Merge branch 'master' into optionalChainingStage3
rbuckton Sep 24, 2019
2ffd8e1
Merge branch 'enhancement/auto-insert-question-dot' of github.com:and…
rbuckton Sep 24, 2019
096bb49
Fix to insert text for completions
rbuckton Sep 24, 2019
1bf2d56
Add initial control-flow analysis for optional chains
rbuckton Sep 25, 2019
1d7446f
PR Feedback and more tests
rbuckton Sep 26, 2019
b282b62
Update to control flow
rbuckton Sep 26, 2019
be3e21f
Merge branch 'master' into optionalChainingStage3
rbuckton Sep 26, 2019
fd8c0d4
Remove mangled smart quotes in comments
rbuckton Sep 26, 2019
7c9ef50
Fix lint, PR feedback
rbuckton Sep 27, 2019
ad7c33c
Updates to control flow
rbuckton Sep 28, 2019
6b49a03
Switch to FlowCondition for CFA of optional chains
rbuckton Sep 29, 2019
aaa30f4
Fix ?. insertion for completions on type variables
rbuckton Sep 29, 2019
5ea7cb5
Accept API baseline change
rbuckton Sep 29, 2019
7463860
Clean up types
rbuckton Sep 29, 2019
0828674
improve control-flow debug output
rbuckton Sep 30, 2019
d408e81
Merge branch 'master' into optionalChainingStage3
rbuckton Sep 30, 2019
c2070be
Revert Debug.formatControlFlowGraph helper
rbuckton Sep 30, 2019
dfc798f
Merge branch 'master' into optionalChainingStage3
rbuckton Sep 30, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 128 additions & 12 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ namespace ts {
let currentReturnTarget: FlowLabel | undefined;
let currentTrueTarget: FlowLabel | undefined;
let currentFalseTarget: FlowLabel | undefined;
let currentPresentTarget: FlowLabel | undefined;
let currentMissingTarget: FlowLabel | undefined;
let preSwitchCaseFlow: FlowNode | undefined;
let activeLabels: ActiveLabel[] | undefined;
let hasExplicitReturn: boolean;
Expand Down Expand Up @@ -261,6 +263,8 @@ namespace ts {
currentReturnTarget = undefined;
currentTrueTarget = undefined;
currentFalseTarget = undefined;
currentPresentTarget = undefined;
currentMissingTarget = undefined;
activeLabels = undefined!;
hasExplicitReturn = false;
emitFlags = NodeFlags.None;
Expand Down Expand Up @@ -797,6 +801,10 @@ namespace ts {
case SyntaxKind.VariableDeclaration:
bindVariableDeclarationFlow(<VariableDeclaration>node);
break;
case SyntaxKind.PropertyAccessExpression:
case SyntaxKind.ElementAccessExpression:
bindAccessExpressionFlow(<AccessExpression>node);
break;
case SyntaxKind.CallExpression:
bindCallExpressionFlow(<CallExpression>node);
break;
Expand Down Expand Up @@ -932,6 +940,22 @@ namespace ts {
}
}

function findAntecedent(flowNode: FlowNode, flags: FlowFlags) {
if (flowNode.flags & flags) {
return flowNode;
}
if (flowNode.flags & FlowFlags.BranchLabel) {
const branch = flowNode as FlowLabel;
if (branch.antecedents) {
for (const antecedent of branch.antecedents) {
if (antecedent.flags & flags) {
return antecedent;
}
}
}
}
}

function createFlowCondition(flags: FlowFlags, antecedent: FlowNode, expression: Expression | undefined): FlowNode {
if (antecedent.flags & FlowFlags.Unreachable) {
return antecedent;
Expand All @@ -943,13 +967,36 @@ namespace ts {
expression.kind === SyntaxKind.FalseKeyword && flags & FlowFlags.TrueCondition) {
return unreachableFlow;
}

if (flags & FlowFlags.TrueCondition) {
// If the antecedent is an optional chain, only the Present branch can exist on the `true` condition
const presentFlow = findAntecedent(currentFlow, FlowFlags.Present);
rbuckton marked this conversation as resolved.
Show resolved Hide resolved
if (presentFlow) {
antecedent = presentFlow;
}
}

if (!isNarrowingExpression(expression)) {
return antecedent;
}
setFlowNodeReferenced(antecedent);
return flowNodeCreated({ flags, antecedent, node: expression });
}

function createFlowOptionalChain(flags: FlowFlags, antecedent: FlowNode, expression: Expression): FlowNode {
if (antecedent.flags & FlowFlags.Unreachable) {
return antecedent;
}
if (flags & FlowFlags.Present) {
const presentFlow = findAntecedent(currentFlow, FlowFlags.Present);
if (presentFlow) {
antecedent = presentFlow;
}
}
setFlowNodeReferenced(antecedent);
return flowNodeCreated({ flags, antecedent, node: expression });
}

function createFlowSwitchClause(antecedent: FlowNode, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number): FlowNode {
if (!isNarrowingExpression(switchStatement.expression)) {
return antecedent;
Expand Down Expand Up @@ -1542,22 +1589,79 @@ namespace ts {
}
}

function bindCallExpressionFlow(node: CallExpression) {
// If the target of the call expression is a function expression or arrow function we have
// an immediately invoked function expression (IIFE). Initialize the flowNode property to
// the current control flow (which includes evaluation of the IIFE arguments).
let expr: Expression = node.expression;
while (expr.kind === SyntaxKind.ParenthesizedExpression) {
expr = (<ParenthesizedExpression>expr).expression;
}
if (expr.kind === SyntaxKind.FunctionExpression || expr.kind === SyntaxKind.ArrowFunction) {
bindEach(node.typeArguments);
bindEach(node.arguments);
bind(node.expression);
function bindOptionalExpression(node: Expression, presentTarget: FlowLabel, missingTarget: FlowLabel) {
const savedPresentTarget = currentPresentTarget;
const savedMissingTarget = currentMissingTarget;
currentPresentTarget = presentTarget;
currentMissingTarget = missingTarget;
bind(node);
currentPresentTarget = savedPresentTarget;
currentMissingTarget = savedMissingTarget;
if (!isValidOptionalChain(node) || isOutermostOptionalChain(node)) {
addAntecedent(presentTarget, createFlowOptionalChain(FlowFlags.Present, currentFlow, node));
addAntecedent(missingTarget, createFlowOptionalChain(FlowFlags.Missing, currentFlow, node));
}
}

function isOutermostOptionalChain(node: ValidOptionalChain) {
return !isOptionalChain(node.parent) || !!node.parent.questionDotToken || node.parent.expression !== node;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this definition of outermost handle parenthesis? eg, (((a.b)?.c)?.["d"])?.() (do optional chains support that? they do, right?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional chains stop at parentheses (i.e. (a?.b).c could throw if a is undefined). Each optional chain in the CFA graph gets a Present/Missing branch from the ?. until the end of the chain.

}

function bindOptionalChainFlow(node: ValidOptionalChain) {
const postExpressionLabel = isOutermostOptionalChain(node) ? createBranchLabel() : undefined;
const presentTarget = postExpressionLabel ? createBranchLabel() : Debug.assertDefined(currentPresentTarget);
const missingTarget = postExpressionLabel ? createBranchLabel() : Debug.assertDefined(currentMissingTarget);
bindOptionalExpression(node.expression, presentTarget, missingTarget);
if (!isValidOptionalChain(node.expression) || node.questionDotToken) {
currentFlow = finishFlowLabel(presentTarget);
}
bind(node.questionDotToken);
switch (node.kind) {
case SyntaxKind.PropertyAccessExpression:
bind(node.name);
break;
case SyntaxKind.ElementAccessExpression:
bind(node.argumentExpression);
break;
case SyntaxKind.CallExpression:
bindEach(node.typeArguments);
bindEach(node.arguments);
break;
}
if (postExpressionLabel) {
addAntecedent(postExpressionLabel, currentFlow);
addAntecedent(postExpressionLabel, finishFlowLabel(missingTarget));
currentFlow = finishFlowLabel(postExpressionLabel);
}
}

function bindAccessExpressionFlow(node: AccessExpression) {
if (isValidOptionalChain(node)) {
bindOptionalChainFlow(node);
}
else {
bindEachChild(node);
}
}

function bindCallExpressionFlow(node: CallExpression) {
if (isValidOptionalChain(node)) {
bindOptionalChainFlow(node);
}
else {
// If the target of the call expression is a function expression or arrow function we have
// an immediately invoked function expression (IIFE). Initialize the flowNode property to
// the current control flow (which includes evaluation of the IIFE arguments).
const expr = skipParentheses(node.expression);
if (expr.kind === SyntaxKind.FunctionExpression || expr.kind === SyntaxKind.ArrowFunction) {
bindEach(node.typeArguments);
bindEach(node.arguments);
bind(node.expression);
}
else {
bindEachChild(node);
}
}
if (node.expression.kind === SyntaxKind.PropertyAccessExpression) {
const propertyAccess = <PropertyAccessExpression>node.expression;
if (isNarrowableOperand(propertyAccess.expression) && isPushOrUnshiftIdentifier(propertyAccess.name)) {
Expand Down Expand Up @@ -3245,6 +3349,10 @@ namespace ts {
const callee = skipOuterExpressions(node.expression);
const expression = node.expression;

if (node.flags & NodeFlags.OptionalChain) {
transformFlags |= TransformFlags.ContainsESNext;
}

if (node.typeArguments) {
transformFlags |= TransformFlags.AssertTypeScript;
}
Expand Down Expand Up @@ -3640,6 +3748,10 @@ namespace ts {
function computePropertyAccess(node: PropertyAccessExpression, subtreeFlags: TransformFlags) {
let transformFlags = subtreeFlags;

if (node.flags & NodeFlags.OptionalChain) {
transformFlags |= TransformFlags.ContainsESNext;
}

// If a PropertyAccessExpression starts with a super keyword, then it is
// ES6 syntax, and requires a lexical `this` binding.
if (node.expression.kind === SyntaxKind.SuperKeyword) {
Expand All @@ -3655,6 +3767,10 @@ namespace ts {
function computeElementAccess(node: ElementAccessExpression, subtreeFlags: TransformFlags) {
let transformFlags = subtreeFlags;

if (node.flags & NodeFlags.OptionalChain) {
transformFlags |= TransformFlags.ContainsESNext;
}

// If an ElementAccessExpression starts with a super keyword, then it is
// ES6 syntax, and requires a lexical `this` binding.
if (node.expression.kind === SyntaxKind.SuperKeyword) {
Expand Down
Loading