-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ClosureRewriteClass.java
686 lines (604 loc) · 23.2 KB
/
ClosureRewriteClass.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/*
* Copyright 2012 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 static com.google.common.base.Preconditions.checkState;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.JSDocInfo.Visibility;
import com.google.javascript.rhino.JSDocInfoBuilder;
import com.google.javascript.rhino.JSTypeExpression;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.Token;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Rewrites "goog.defineClass" into a form that is suitable for
* type checking and dead code elimination.
*
* @author johnlenz@google.com (John Lenz)
*/
class ClosureRewriteClass extends AbstractPostOrderCallback
implements HotSwapCompilerPass {
// Errors
static final DiagnosticType GOOG_CLASS_TARGET_INVALID = DiagnosticType.error(
"JSC_GOOG_CLASS_TARGET_INVALID",
"Unsupported class definition expression.");
static final DiagnosticType GOOG_CLASS_SUPER_CLASS_NOT_VALID = DiagnosticType.error(
"JSC_GOOG_CLASS_SUPER_CLASS_NOT_VALID",
"The super class must be null or a valid name reference");
static final DiagnosticType GOOG_CLASS_DESCRIPTOR_NOT_VALID = DiagnosticType.error(
"JSC_GOOG_CLASS_DESCRIPTOR_NOT_VALID",
"The class must be defined by an object literal");
static final DiagnosticType GOOG_CLASS_CONSTRUCTOR_MISSING = DiagnosticType.error(
"JSC_GOOG_CLASS_CONSTRUCTOR_MISSING",
"The 'constructor' property is missing for the class definition");
static final DiagnosticType GOOG_CLASS_CONSTRUCTOR_NOT_VALID = DiagnosticType.error(
"JSC_GOOG_CLASS_CONSTRUCTOR_NOT_VALID",
"The 'constructor' expression must be a function literal");
static final DiagnosticType GOOG_CLASS_CONSTRUCTOR_ON_INTERFACE = DiagnosticType.error(
"JSC_GOOG_CLASS_CONSTRUCTOR_ON_INTERFACE",
"An interface definition should not have a 'constructor' property");
static final DiagnosticType GOOG_CLASS_STATICS_NOT_VALID = DiagnosticType.error(
"JSC_GOOG_CLASS_STATICS_NOT_VALID",
"The class 'statics' property must be an object or function literal");
static final DiagnosticType GOOG_CLASS_UNEXPECTED_PARAMS = DiagnosticType.error(
"JSC_GOOG_CLASS_UNEXPECTED_PARAMS",
"Too many arguments to goog.defineClass.");
static final DiagnosticType GOOG_CLASS_ES6_COMPUTED_PROP_NAMES_NOT_SUPPORTED =
DiagnosticType.error(
"JSC_GOOG_CLASS_ES6_COMPUTED_PROP_NAMES_NOT_SUPPORTED",
"Computed property names not supported in goog.defineClass.");
static final DiagnosticType GOOG_CLASS_ES6_ARROW_FUNCTION_NOT_SUPPORTED =
DiagnosticType.error(
"JSC_GOOG_CLASS_ES6_ARROW_FUNCTION_NOT_SUPPORTED",
"Arrow functions not supported in goog.defineClass. Object literal method"
+ " definition may be an alternative.");
// Warnings
static final DiagnosticType GOOG_CLASS_NG_INJECT_ON_CLASS = DiagnosticType.warning(
"JSC_GOOG_CLASS_NG_INJECT_ON_CLASS",
"@ngInject should be declared on the constructor, not on the class.");
private final AbstractCompiler compiler;
public ClosureRewriteClass(AbstractCompiler compiler) {
this.compiler = compiler;
}
@Override
public void process(Node externs, Node root) {
hotSwapScript(root, null);
}
@Override
public void hotSwapScript(Node scriptRoot, Node originalRoot) {
NodeTraversal.traverseEs6(compiler, scriptRoot, this);
}
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
if (n.isCall() && isGoogDefineClass(n) && !validateUsage(n)) {
compiler.report(JSError.make(n, GOOG_CLASS_TARGET_INVALID));
}
maybeRewriteClassDefinition(t, n);
}
private boolean validateUsage(Node n) {
// There are only three valid usage patterns for of goog.defineClass
// var ClassName = googDefineClass
// namespace.ClassName = googDefineClass
// and within an objectlit, used by the goog.defineClass.
Node parent = n.getParent();
switch (parent.getToken()) {
case NAME:
return true;
case ASSIGN:
return n == parent.getLastChild() && parent.getParent().isExprResult();
case STRING_KEY:
return isContainedInGoogDefineClass(parent);
default:
break;
}
return false;
}
private boolean isContainedInGoogDefineClass(Node n) {
while (n != null) {
n = n.getParent();
if (n.isCall()) {
if (isGoogDefineClass(n)) {
return true;
}
} else if (!n.isObjectLit() && !n.isStringKey()) {
break;
}
}
return false;
}
private void maybeRewriteClassDefinition(NodeTraversal t, Node n) {
if (NodeUtil.isNameDeclaration(n)) {
Node target = n.getFirstChild();
Node value = target.getFirstChild();
maybeRewriteClassDefinition(t, n, target, value);
} else if (NodeUtil.isExprAssign(n)) {
Node assign = n.getFirstChild();
Node target = assign.getFirstChild();
Node value = assign.getLastChild();
maybeRewriteClassDefinition(t, n, target, value);
}
}
private void maybeRewriteClassDefinition(
NodeTraversal t, Node n, Node target, Node value) {
if (isGoogDefineClass(value)) {
if (!target.isQualifiedName()) {
compiler.report(JSError.make(n, GOOG_CLASS_TARGET_INVALID));
}
ClassDefinition def = extractClassDefinition(target, value);
if (def != null) {
value.detach();
target.detach();
rewriteGoogDefineClass(t, n, def);
}
}
}
private static class MemberDefinition {
final JSDocInfo info;
final Node name;
final Node value;
MemberDefinition(JSDocInfo info, Node name, Node value) {
this.info = info;
this.name = name;
this.value = value;
}
}
private static final class ClassDefinition {
final Node name;
final JSDocInfo classInfo;
final Node superClass;
final MemberDefinition constructor;
final List<MemberDefinition> staticProps;
final List<MemberDefinition> props;
final Node classModifier;
ClassDefinition(
Node name,
JSDocInfo classInfo,
Node superClass,
MemberDefinition constructor,
List<MemberDefinition> staticProps,
List<MemberDefinition> props,
Node classModifier) {
this.name = name;
this.classInfo = classInfo;
this.superClass = superClass;
this.constructor = constructor;
this.staticProps = staticProps;
this.props = props;
this.classModifier = classModifier;
}
}
/**
* Validates the class definition and if valid, destructively extracts
* the class definition from the AST.
*/
private ClassDefinition extractClassDefinition(
Node targetName, Node callNode) {
JSDocInfo classInfo = NodeUtil.getBestJSDocInfo(targetName);
// name = goog.defineClass(superClass, {...}, [modifier, ...])
Node superClass = NodeUtil.getArgumentForCallOrNew(callNode, 0);
if (superClass == null ||
(!superClass.isNull() && !superClass.isQualifiedName())) {
compiler.report(JSError.make(callNode, GOOG_CLASS_SUPER_CLASS_NOT_VALID));
return null;
}
if (NodeUtil.isNullOrUndefined(superClass)
|| superClass.matchesQualifiedName("Object")) {
superClass = null;
}
Node description = NodeUtil.getArgumentForCallOrNew(callNode, 1);
if (!validateObjLit(description, callNode)) {
// Errors will be reported in the validate method. Keeping here clean
return null;
}
int paramCount = callNode.getChildCount() - 1;
if (paramCount > 2) {
compiler.report(JSError.make(callNode, GOOG_CLASS_UNEXPECTED_PARAMS));
return null;
}
Node constructor = extractProperty(description, "constructor");
if (classInfo != null && classInfo.isInterface()) {
if (constructor != null) {
compiler.report(JSError.make(description, GOOG_CLASS_CONSTRUCTOR_ON_INTERFACE));
return null;
}
} else if (constructor == null) {
// report missing constructor
compiler.report(JSError.make(description, GOOG_CLASS_CONSTRUCTOR_MISSING));
return null;
} else {
if (!constructor.isFunction()) {
compiler.report(JSError.make(constructor, GOOG_CLASS_CONSTRUCTOR_NOT_VALID));
}
}
if (constructor == null) {
constructor = IR.function(
IR.name("").srcref(callNode),
IR.paramList().srcref(callNode),
IR.block().srcref(callNode));
constructor.srcref(callNode);
compiler.reportChangeToChangeScope(constructor);
}
JSDocInfo info = NodeUtil.getBestJSDocInfo(constructor);
Node classModifier = null;
Node statics = null;
Node staticsProp = extractProperty(description, "statics");
if (staticsProp != null) {
if (staticsProp.isObjectLit()){
if (!validateObjLit(staticsProp, staticsProp.getParent())) {
// Errors will be reported in the validate method. Keeping here clean
return null;
}
statics = staticsProp;
} else if (staticsProp.isFunction()) {
classModifier = staticsProp;
} else {
compiler.report(
JSError.make(staticsProp, GOOG_CLASS_STATICS_NOT_VALID));
return null;
}
}
if (statics == null) {
statics = IR.objectlit();
}
// Ok, now rip apart the definition into its component pieces.
// Remove the "special" property key nodes.
maybeDetach(constructor.getParent());
maybeDetach(statics.getParent());
if (classModifier != null) {
maybeDetach(classModifier.getParent());
}
ClassDefinition def = new ClassDefinition(
targetName,
classInfo,
maybeDetach(superClass),
new MemberDefinition(info, null, maybeDetach(constructor)),
objectLitToList(maybeDetach(statics)),
objectLitToList(description),
maybeDetach(classModifier));
return def;
}
private static Node maybeDetach(Node node) {
if (node != null && node.getParent() != null) {
node.detach();
}
return node;
}
/**
* @param objlit the object literal being checked.
* @param parent the parent of the object literal node
* @return false if the node is not an object literal, or if it contains any
* property that is neither unquoted plain property nor member
* function definition (ES6 feature)
*/
private boolean validateObjLit(Node objlit, Node parent) {
if (objlit == null || !objlit.isObjectLit()) {
reportErrorOnContext(parent);
return false;
}
for (Node key : objlit.children()) {
if (key.isMemberFunctionDef()) {
continue;
}
if (key.isComputedProp()) {
// report using computed property name
compiler.report(JSError.make(objlit,
GOOG_CLASS_ES6_COMPUTED_PROP_NAMES_NOT_SUPPORTED));
return false;
}
if (key.isStringKey()
&& key.hasChildren()
&& key.getFirstChild().isArrowFunction()){
// report using arrow function
compiler.report(JSError.make(objlit,
GOOG_CLASS_ES6_ARROW_FUNCTION_NOT_SUPPORTED));
return false;
}
if (!key.isStringKey() || key.isQuotedString()) {
reportErrorOnContext(parent);
return false;
}
}
return true;
}
private void reportErrorOnContext(Node parent){
if (parent.isStringKey()){
compiler.report(JSError.make(parent, GOOG_CLASS_STATICS_NOT_VALID));
} else {
// Report error in the context that the objlit is an
// argument of goog.defineClass call.
checkState(parent.isCall());
compiler.report(JSError.make(parent, GOOG_CLASS_DESCRIPTOR_NOT_VALID));
}
}
/**
* @return The first property in the objlit that matches the key.
*/
private static Node extractProperty(Node objlit, String keyName) {
for (Node keyNode : objlit.children()) {
if (keyNode.getString().equals(keyName)) {
return keyNode.getFirstChild();
}
}
return null;
}
private static List<MemberDefinition> objectLitToList(
Node objlit) {
List<MemberDefinition> result = new ArrayList<>();
for (Node keyNode : objlit.children()) {
Node name = keyNode;
// The span of a member function def is the whole function. The NAME node should be the
// first-first child, which will have a span for just the name of the function.
if (keyNode.isMemberFunctionDef()) {
name = keyNode.getFirstFirstChild().cloneNode();
name.setString(keyNode.getString());
}
result.add(
new MemberDefinition(
NodeUtil.getBestJSDocInfo(keyNode), name, keyNode.removeFirstChild()));
}
objlit.detachChildren();
return result;
}
private void rewriteGoogDefineClass(NodeTraversal t, Node exprRoot, final ClassDefinition cls) {
// For simplicity add everything into a block, before adding it to the AST.
Node block = IR.block();
// remove the original jsdoc info if it was attached to the value.
cls.constructor.value.setJSDocInfo(null);
if (NodeUtil.isNameDeclaration(exprRoot)) {
// example: var ctr = function(){}
Node decl =
IR.declaration(cls.name.cloneTree(), cls.constructor.value, exprRoot.getToken())
.srcref(exprRoot);
JSDocInfo mergedClassInfo = mergeJsDocFor(cls, decl);
decl.setJSDocInfo(mergedClassInfo);
block.addChildToBack(decl);
} else {
// example: ns.ctr = function(){}
Node assign = IR.assign(cls.name.cloneTree(), cls.constructor.value)
.srcref(exprRoot)
.setJSDocInfo(cls.constructor.info);
JSDocInfo mergedClassInfo = mergeJsDocFor(cls, assign);
assign.setJSDocInfo(mergedClassInfo);
Node expr = IR.exprResult(assign).srcref(exprRoot);
block.addChildToBack(expr);
}
if (cls.superClass != null) {
// example: goog.inherits(ctr, superClass)
block.addChildToBack(
fixupSrcref(IR.exprResult(
IR.call(
NodeUtil.newQName(compiler, "goog.inherits")
.srcrefTree(cls.superClass),
cls.name.cloneTree(),
cls.superClass.cloneTree()).srcref(cls.superClass))));
}
for (MemberDefinition def : cls.staticProps) {
// remove the original jsdoc info if it was attached to the value.
def.value.setJSDocInfo(null);
// example: ctr.prop = value
block.addChildToBack(
fixupSrcref(IR.exprResult(
fixupSrcref(IR.assign(
IR.getprop(cls.name.cloneTree(),
IR.string(def.name.getString()).srcref(def.name))
.srcref(def.name),
def.value)).setJSDocInfo(def.info))));
// Handle inner class definitions.
maybeRewriteClassDefinition(t, block.getLastChild());
}
for (MemberDefinition def : cls.props) {
// remove the original jsdoc info if it was attached to the value.
def.value.setJSDocInfo(null);
// example: ctr.prototype.prop = value
Node exprResult =
IR.exprResult(
IR.assign(
NodeUtil.newQName(
compiler,
cls.name.getQualifiedName() + ".prototype." + def.name.getString()),
def.value)
.setJSDocInfo(def.info));
exprResult.useSourceInfoIfMissingFromForTree(def.name);
// The length needs to be set explicitly to include the string key node and the function node.
// If we just used the length of def.name or def.value alone, then refactorings which try to
// delete the method would not work correctly.
exprResult.setLength(
def.value.getSourceOffset() + def.value.getLength() - def.name.getSourceOffset());
block.addChildToBack(exprResult);
// Handle inner class definitions.
maybeRewriteClassDefinition(t, block.getLastChild());
}
if (cls.classModifier != null) {
// Inside the modifier function, replace references to the argument
// with the class name.
// function(cls) { cls.Foo = bar; }
// becomes
// function(cls) { theClassName.Foo = bar; }
// The cls parameter is unused, but leave it there so that it
// matches the JsDoc.
// TODO(tbreisacher): Add a warning if the param is shadowed or reassigned.
Node argList = cls.classModifier.getSecondChild();
Node arg = argList.getFirstChild();
final String argName = arg.getString();
NodeTraversal.traverseEs6(
compiler,
cls.classModifier.getLastChild(),
new AbstractPostOrderCallback() {
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
if (n.isName() && n.getString().equals(argName)) {
Node newName = cls.name.cloneTree();
parent.replaceChild(n, newName);
compiler.reportChangeToEnclosingScope(newName);
}
}
});
block.addChildToBack(
IR.exprResult(
fixupFreeCall(
IR.call(
cls.classModifier,
cls.name.cloneTree())
.srcref(cls.classModifier)))
.srcref(cls.classModifier));
}
Node parent = exprRoot.getParent();
Node stmts = block.removeChildren();
parent.addChildrenAfter(stmts, exprRoot);
parent.removeChild(exprRoot);
// compiler.reportChangeToEnclosingScope(parent);
t.reportCodeChange();
}
private static Node fixupSrcref(Node node) {
node.srcref(node.getFirstChild());
return node;
}
private static Node fixupFreeCall(Node call) {
checkState(call.isCall());
call.putBooleanProp(Node.FREE_CALL, true);
return call;
}
/**
* @return Whether the call represents a class definition.
*/
static boolean isGoogDefineClass(Node value) {
if (value != null && value.isCall()) {
return value.getFirstChild().matchesQualifiedName("goog.defineClass");
}
return false;
}
static final String VIRTUAL_FILE = "<ClosureRewriteClass.java>";
private JSDocInfo mergeJsDocFor(ClassDefinition cls, Node associatedNode) {
// avoid null checks
JSDocInfo classInfo = (cls.classInfo != null)
? cls.classInfo
: new JSDocInfoBuilder(true).build(true);
JSDocInfo ctorInfo = (cls.constructor.info != null)
? cls.constructor.info
: new JSDocInfoBuilder(true).build(true);
Node superNode = cls.superClass;
// Start with a clone of the constructor info if there is one.
JSDocInfoBuilder mergedInfo = cls.constructor.info != null
? JSDocInfoBuilder.copyFrom(ctorInfo)
: new JSDocInfoBuilder(true);
// merge block description
String blockDescription = Joiner.on("\n").skipNulls().join(
classInfo.getBlockDescription(),
ctorInfo.getBlockDescription());
if (!blockDescription.isEmpty()) {
mergedInfo.recordBlockDescription(blockDescription);
}
// merge suppressions
Set<String> suppressions = new HashSet<>();
suppressions.addAll(classInfo.getSuppressions());
suppressions.addAll(ctorInfo.getSuppressions());
if (!suppressions.isEmpty()) {
mergedInfo.recordSuppressions(suppressions);
}
// Use class deprecation if set.
if (classInfo.isDeprecated()) {
mergedInfo.recordDeprecated();
}
String deprecationReason = null;
if (classInfo.getDeprecationReason() != null) {
deprecationReason = classInfo.getDeprecationReason();
mergedInfo.recordDeprecationReason(deprecationReason);
}
// Use class visibility if specifically set
Visibility visibility = classInfo.getVisibility();
if (visibility != null && visibility != JSDocInfo.Visibility.INHERITED) {
mergedInfo.recordVisibility(classInfo.getVisibility());
}
if (classInfo.isAbstract()) {
mergedInfo.recordAbstract();
}
if (classInfo.isConstant()) {
mergedInfo.recordConstancy();
}
if (classInfo.isExport()) {
mergedInfo.recordExport();
}
// If @ngInject is on the ctor, it's already been copied above.
if (classInfo.isNgInject()) {
compiler.report(JSError.make(associatedNode, GOOG_CLASS_NG_INJECT_ON_CLASS));
mergedInfo.recordNgInject(true);
}
if (classInfo.makesUnrestricted() || ctorInfo.makesUnrestricted()) {
mergedInfo.recordUnrestricted();
} else if (classInfo.makesDicts() || ctorInfo.makesDicts()) {
mergedInfo.recordDict();
} else {
// @struct by default
mergedInfo.recordStruct();
}
// @constructor is implied, @interface must be explicit
boolean isInterface = classInfo.isInterface() || ctorInfo.isInterface();
if (isInterface) {
if (classInfo.usesImplicitMatch() || ctorInfo.usesImplicitMatch()) {
mergedInfo.recordImplicitMatch();
} else {
mergedInfo.recordInterface();
}
List<JSTypeExpression> extendedInterfaces = null;
if (classInfo.getExtendedInterfacesCount() > 0) {
extendedInterfaces = classInfo.getExtendedInterfaces();
} else if (ctorInfo.getExtendedInterfacesCount() == 0
&& superNode != null) {
extendedInterfaces = ImmutableList.of(new JSTypeExpression(
new Node(Token.BANG,
IR.string(superNode.getQualifiedName())),
VIRTUAL_FILE));
}
if (extendedInterfaces != null) {
for (JSTypeExpression extend : extendedInterfaces) {
mergedInfo.recordExtendedInterface(extend);
}
}
} else {
// @constructor by default
mergedInfo.recordConstructor();
if (classInfo.getBaseType() != null) {
mergedInfo.recordBaseType(classInfo.getBaseType());
} else if (superNode != null) {
// a "super" implies @extends, build a default.
JSTypeExpression baseType = new JSTypeExpression(
new Node(Token.BANG,
IR.string(superNode.getQualifiedName())),
VIRTUAL_FILE);
mergedInfo.recordBaseType(baseType);
}
// @implements from the class if they exist
List<JSTypeExpression> interfaces = classInfo.getImplementedInterfaces();
for (JSTypeExpression implemented : interfaces) {
mergedInfo.recordImplementedInterface(implemented);
}
}
// merge @template types if they exist
List<String> templateNames = new ArrayList<>();
templateNames.addAll(classInfo.getTemplateTypeNames());
templateNames.addAll(ctorInfo.getTemplateTypeNames());
for (String typeName : templateNames) {
mergedInfo.recordTemplateTypeName(typeName);
}
return mergedInfo.build();
}
}