Skip to content

Commit

Permalink
Fix check for ES5 classes extending ES6 classes
Browse files Browse the repository at this point in the history
Previously this only worked on transpiled classes because it looked for a Node property (IS_ES6_CLASS) that was added by an earlier transpilation pass.  Add a Node method to also check for actual untranspiled CLASS nodes.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=204974193
  • Loading branch information
shicks authored and tjgq committed Jul 18, 2018
1 parent 0958b86 commit e968945
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/TypeCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -2048,8 +2048,8 @@ private void checkConstructor(NodeTraversal t, Node n, FunctionType functionType
if (n.isFunction()
&& baseConstructor != null
&& baseConstructor.getSource() != null
&& baseConstructor.getSource().getBooleanProp(Node.IS_ES6_CLASS)
&& !functionType.getSource().getBooleanProp(Node.IS_ES6_CLASS)) {
&& baseConstructor.getSource().isEs6Class()
&& !functionType.getSource().isEs6Class()) {
// Warn if an ES5 class extends an ES6 class.
compiler.report(
t.makeError(
Expand Down
5 changes: 5 additions & 0 deletions src/com/google/javascript/rhino/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -2655,6 +2655,11 @@ public final boolean isYieldAll() {
return getBooleanProp(YIELD_ALL);
}

/** Returns true if this is or ever was a CLASS node (i.e. even after transpilation). */
public final boolean isEs6Class() {
return isClass() || getBooleanProp(IS_ES6_CLASS);
}

// There are four values of interest:
// global state changes
// this state changes
Expand Down
18 changes: 18 additions & 0 deletions test/com/google/javascript/jscomp/TypeCheckNoTranspileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3339,6 +3339,24 @@ public void testClassNewTargetInConstructorNestedArrow() {
"required: null"));
}

public void testClassEs5ClassCannotExtendEs6Class() {
testTypes(
lines(
"class Base {}",
"/** @constructor @extends {Base} */",
"function Sub() {}"),
"ES5 class Sub cannot extend ES6 class Base");
}

public void testClassEs5ClassCanImplementEs6Interface() {
testTypes(
lines(
"/** @interface */",
"class Inter {}",
"/** @constructor @implements {Inter} */",
"function Sub() {}"));
}

public void testAsyncFunctionWithoutJSDoc() {
testTypes("async function f() { return 3; }");
}
Expand Down

0 comments on commit e968945

Please sign in to comment.