Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge the two super-checking passes together.
It was incorrect for one of them to be considered a "transpilation" pass when in fact it ought to run even in non-transpiling compilations. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=184609654
- Loading branch information
Showing
8 changed files
with
199 additions
and
288 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* Copyright 2016 The Closure Compiler Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.javascript.jscomp; | ||
|
||
import com.google.javascript.jscomp.NodeTraversal.Callback; | ||
import com.google.javascript.rhino.Node; | ||
|
||
/** | ||
* Check for errors related to the `super` keyword. | ||
*/ | ||
final class CheckSuper implements HotSwapCompilerPass, Callback { | ||
static final DiagnosticType MISSING_CALL_TO_SUPER = | ||
DiagnosticType.error("JSC_MISSING_CALL_TO_SUPER", "constructor is missing a call to super()"); | ||
|
||
static final DiagnosticType THIS_BEFORE_SUPER = | ||
DiagnosticType.error("JSC_THIS_BEFORE_SUPER", "cannot access this before calling super()"); | ||
|
||
static final DiagnosticType INVALID_SUPER_CALL = DiagnosticType.error( | ||
"JSC_INVALID_SUPER_CALL", | ||
"super() not allowed except in the constructor of a subclass"); | ||
|
||
static final DiagnosticType INVALID_SUPER_CALL_WITH_SUGGESTION = DiagnosticType.error( | ||
"JSC_INVALID_SUPER_CALL_WITH_SUGGESTION", | ||
"super() not allowed here. Did you mean super.{0}?"); | ||
|
||
private final AbstractCompiler compiler; | ||
|
||
public CheckSuper(AbstractCompiler compiler) { | ||
this.compiler = compiler; | ||
} | ||
|
||
@Override | ||
public void process(Node externs, Node root) { | ||
NodeTraversal.traverseEs6(compiler, root, this); | ||
} | ||
|
||
@Override | ||
public void hotSwapScript(Node scriptRoot, Node originalRoot) { | ||
NodeTraversal.traverseEs6(compiler, scriptRoot, this); | ||
} | ||
|
||
@Override | ||
public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) { | ||
if (n.isClass()) { | ||
return visitClass(t, n); | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public void visit(NodeTraversal t, Node n, Node parent) { | ||
if (n.isSuper()) { | ||
visitSuper(t, n, parent); | ||
} | ||
} | ||
|
||
/** | ||
* @return Whether to continue traversing this class | ||
*/ | ||
private boolean visitClass(NodeTraversal t, Node n) { | ||
Node superclass = n.getSecondChild(); | ||
if (superclass.isEmpty()) { | ||
return true; | ||
} | ||
|
||
Node constructor = | ||
NodeUtil.getFirstPropMatchingKey(NodeUtil.getClassMembers(n), "constructor"); | ||
if (constructor == null) { | ||
return true; | ||
} | ||
|
||
FindSuper finder = new FindSuper(); | ||
NodeTraversal.traverseEs6(compiler, NodeUtil.getFunctionBody(constructor), finder); | ||
|
||
if (!finder.found) { | ||
t.report(constructor, MISSING_CALL_TO_SUPER); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
private void visitSuper(NodeTraversal t, Node n, Node parent) { | ||
Node classNode = NodeUtil.getEnclosingClass(n); | ||
if (classNode == null || classNode.getSecondChild().isEmpty()) { | ||
t.report(n, INVALID_SUPER_CALL); | ||
return; | ||
} | ||
|
||
if (parent.isCall()) { | ||
Node fn = NodeUtil.getEnclosingFunction(parent); | ||
if (fn == null) { | ||
t.report(n, INVALID_SUPER_CALL); | ||
return; | ||
} | ||
|
||
Node memberDef = fn.getParent(); | ||
if (memberDef.isMemberFunctionDef()) { | ||
if (memberDef.matchesQualifiedName("constructor")) { | ||
// No error. | ||
} else { | ||
t.report(n, INVALID_SUPER_CALL_WITH_SUGGESTION, memberDef.getString()); | ||
} | ||
} else { | ||
t.report(n, INVALID_SUPER_CALL); | ||
} | ||
} | ||
} | ||
|
||
private static final class FindSuper implements Callback { | ||
boolean found = false; | ||
|
||
@Override | ||
public boolean shouldTraverse(NodeTraversal nodeTraversal, Node n, Node parent) { | ||
// Stop traversal once the super() call is found. Also don't traverse into nested functions | ||
// since this and super() references may not be applicable within those scopes. | ||
return !found && !n.isFunction(); | ||
} | ||
|
||
@Override | ||
public void visit(NodeTraversal t, Node n, Node parent) { | ||
if (n.isThis()) { | ||
t.report(n, THIS_BEFORE_SUPER); | ||
} | ||
if (n.isSuper() && parent.isCall()) { | ||
found = true; | ||
return; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.