Skip to content

Commit f7f21ca

Browse files
committed
Create local alias for super class in --closure mode (issue #312)
Closure compiler chokes on super classes that aren't qualified paths. (example: http://goo.gl/5mHC7S) BUG= [email protected] Review URL: https://codereview.chromium.org/1638533004 .
1 parent 7632658 commit f7f21ca

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

pkg/dev_compiler/lib/runtime/dart/_runtime.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,8 @@ dart_library.library('dart/_runtime', null, /* Imports */[
732732
const _mixins = Symbol("mixins");
733733
const implements_ = Symbol("implements");
734734
const metadata = Symbol("metadata");
735-
const TypeRep = class TypeRep extends LazyTagged(() => core.Type) {
735+
const _TypeRepBase = LazyTagged(() => core.Type);
736+
const TypeRep = class TypeRep extends _TypeRepBase {
736737
get name() {
737738
return this.toString();
738739
}

pkg/dev_compiler/lib/src/codegen/js_codegen.dart

+26-4
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,11 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ClosureAnnotator {
379379
}
380380
}
381381

382-
var classDecl = new JS.ClassDeclaration(new JS.ClassExpression(
383-
new JS.Identifier(element.name), _classHeritage(element), body));
382+
var classExpr = new JS.ClassExpression(
383+
new JS.Identifier(element.name), _classHeritage(element), body);
384384

385-
return _finishClassDef(element.type, classDecl);
385+
return _finishClassDef(
386+
element.type, _emitClassHeritageWorkaround(classExpr));
386387
}
387388

388389
JS.Statement _emitJsType(String dartClassName, DartObject jsName) {
@@ -674,6 +675,27 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ClosureAnnotator {
674675
}
675676
}
676677

678+
_isQualifiedPath(JS.Expression node) =>
679+
node is JS.Identifier ||
680+
node is JS.PropertyAccess &&
681+
_isQualifiedPath(node.receiver) &&
682+
node.selector is JS.LiteralString;
683+
684+
/// Workaround for Closure: super classes must be qualified paths.
685+
JS.Statement _emitClassHeritageWorkaround(JS.ClassExpression cls) {
686+
if (options.closure &&
687+
cls.heritage != null &&
688+
!_isQualifiedPath(cls.heritage)) {
689+
var superVar = new JS.TemporaryId(cls.name.name + r'$super');
690+
return _statement([
691+
js.statement('const # = #;', [superVar, cls.heritage]),
692+
new JS.ClassDeclaration(
693+
new JS.ClassExpression(cls.name, superVar, cls.methods))
694+
]);
695+
}
696+
return new JS.ClassDeclaration(cls);
697+
}
698+
677699
/// Emit class members that need to come after the class declaration, such
678700
/// as static fields. See [_emitClassMethods] for things that are emitted
679701
/// inside the ES6 `class { ... }` node.
@@ -702,7 +724,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ClosureAnnotator {
702724
}
703725
}
704726

705-
body.add(new JS.ClassDeclaration(cls));
727+
body.add(_emitClassHeritageWorkaround(cls));
706728

707729
// TODO(jmesserly): we should really just extend native Array.
708730
if (jsPeerName != null && classElem.typeParameters.isNotEmpty) {

pkg/dev_compiler/test/codegen/expect/closure.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ dart_library.library('closure', null, /* Imports */[
117117
});
118118
let Foo = Foo$();
119119
class Bar extends core.Object {}
120-
class Baz extends dart.mixin(Foo$(core.int), Bar) {
120+
const Baz$super = dart.mixin(Foo$(core.int), Bar);
121+
class Baz extends Baz$super {
121122
/** @param {?number} i */
122123
Baz(i) {
123124
super.Foo(i, 123);

pkg/dev_compiler/tool/input_sdk/private/types.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ final metadata = JS('', 'Symbol("metadata")');
3838
/// String toString();
3939
///
4040
///
41+
final _TypeRepBase = JS('', '$LazyTagged(() => $Type)');
4142
final TypeRep = JS('', '''
42-
class TypeRep extends $LazyTagged(() => $Type) {
43+
class TypeRep extends $_TypeRepBase {
4344
get name() {return this.toString();}
4445
}
4546
''');

0 commit comments

Comments
 (0)