Skip to content

Commit

Permalink
always dead-code-eliminate optional chains (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed May 9, 2020
1 parent 285cb89 commit 0783585
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
16 changes: 12 additions & 4 deletions internal/parser/parser.go
Expand Up @@ -7224,6 +7224,7 @@ func (p *parser) lowerOptionalChain(expr ast.Expr, in exprIn, out exprOut, thisA
valueWhenUndefined := ast.Expr{expr.Loc, &ast.EUndefined{}}
endsWithPropertyAccess := false
startsWithCall := false
originalExpr := expr
chain := []ast.Expr{}
loc := expr.Loc

Expand Down Expand Up @@ -7275,6 +7276,13 @@ flatten:
return valueWhenUndefined, exprOut{}
}

// Don't lower this if we don't need to. This check must be done here instead
// of earlier so we can do the dead code elimination above when the target is
// null or undefined.
if p.target >= ES2020 {
return originalExpr, exprOut{}
}

// Step 2: Figure out if we need to capture the value for "this" for the
// initial ECall. This will be passed to ".call(this, ...args)" later.
var thisArg ast.Expr
Expand Down Expand Up @@ -7779,7 +7787,7 @@ func (p *parser) visitExprInOut(expr ast.Expr, in exprIn) (ast.Expr, exprOut) {
// Lower optional chaining if we're the top of the chain
containsOptionalChain := e.IsOptionalChain || out.childContainsOptionalChain
isEndOfChain := e.IsParenthesized || !in.hasChainParent
if p.target < ES2020 && containsOptionalChain && isEndOfChain {
if containsOptionalChain && isEndOfChain {
return p.lowerOptionalChain(expr, in, out, nil)
}

Expand Down Expand Up @@ -7824,7 +7832,7 @@ func (p *parser) visitExprInOut(expr ast.Expr, in exprIn) (ast.Expr, exprOut) {

// Lower optional chaining if present since we're guaranteed to be the
// end of the chain
if p.target < ES2020 && out.childContainsOptionalChain {
if out.childContainsOptionalChain {
return p.lowerOptionalChain(expr, in, out, nil)
}

Expand Down Expand Up @@ -7879,7 +7887,7 @@ func (p *parser) visitExprInOut(expr ast.Expr, in exprIn) (ast.Expr, exprOut) {
// Lower optional chaining if we're the top of the chain
containsOptionalChain := e.IsOptionalChain || out.childContainsOptionalChain
isEndOfChain := e.IsParenthesized || !in.hasChainParent
if p.target < ES2020 && containsOptionalChain && isEndOfChain {
if containsOptionalChain && isEndOfChain {
return p.lowerOptionalChain(expr, in, out, nil)
}

Expand Down Expand Up @@ -8012,7 +8020,7 @@ func (p *parser) visitExprInOut(expr ast.Expr, in exprIn) (ast.Expr, exprOut) {
// Lower optional chaining if we're the top of the chain
containsOptionalChain := e.IsOptionalChain || out.childContainsOptionalChain
isEndOfChain := e.IsParenthesized || !in.hasChainParent
if p.target < ES2020 && containsOptionalChain && isEndOfChain {
if containsOptionalChain && isEndOfChain {
result, out := p.lowerOptionalChain(expr, in, out, thisArgFunc)
if thisArgWrapFunc != nil {
result = thisArgWrapFunc(result)
Expand Down
12 changes: 6 additions & 6 deletions internal/parser/parser_test.go
Expand Up @@ -1477,13 +1477,13 @@ func TestLowerOptionalChain(t *testing.T) {
expectPrintedTarget(t, ES2020, "x?.[y]", "x?.[y];\n")
expectPrintedTarget(t, ES2020, "x?.(y)", "x?.(y);\n")

expectPrintedTarget(t, ES2020, "null?.x", "null?.x;\n")
expectPrintedTarget(t, ES2020, "null?.[x]", "null?.[x];\n")
expectPrintedTarget(t, ES2020, "null?.(x)", "null?.(x);\n")
expectPrintedTarget(t, ES2020, "null?.x", "void 0;\n")
expectPrintedTarget(t, ES2020, "null?.[x]", "void 0;\n")
expectPrintedTarget(t, ES2020, "null?.(x)", "void 0;\n")

expectPrintedTarget(t, ES2020, "undefined?.x", "(void 0)?.x;\n")
expectPrintedTarget(t, ES2020, "undefined?.[x]", "(void 0)?.[x];\n")
expectPrintedTarget(t, ES2020, "undefined?.(x)", "(void 0)?.(x);\n")
expectPrintedTarget(t, ES2020, "undefined?.x", "void 0;\n")
expectPrintedTarget(t, ES2020, "undefined?.[x]", "void 0;\n")
expectPrintedTarget(t, ES2020, "undefined?.(x)", "void 0;\n")

// Check multiple levels of nesting
expectPrintedTarget(t, ES2019, "a?.b?.c?.d", `var _a, _b;
Expand Down

0 comments on commit 0783585

Please sign in to comment.