MCPcopy Index your code
hub / github.com/nodejs/node / checkConstructorDeclarationDiagnostics

Function checkConstructorDeclarationDiagnostics

test/fixtures/snapshot/typescript.js:80510–80561  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

80508 !!n.initializer;
80509 }
80510 function checkConstructorDeclarationDiagnostics() {
80511 // TS 1.0 spec (April 2014): 8.3.2
80512 // Constructors of classes with no extends clause may not contain super calls, whereas
80513 // constructors of derived classes must contain at least one super call somewhere in their function body.
80514 var containingClassDecl = node.parent;
80515 if (ts.getClassExtendsHeritageElement(containingClassDecl)) {
80516 captureLexicalThis(node.parent, containingClassDecl);
80517 var classExtendsNull = classDeclarationExtendsNull(containingClassDecl);
80518 var superCall = findFirstSuperCall(node.body);
80519 if (superCall) {
80520 if (classExtendsNull) {
80521 error(superCall, ts.Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_class_extends_null);
80522 }
80523 // A super call must be root-level in a constructor if both of the following are true:
80524 // - The containing class is a derived class.
80525 // - The constructor declares parameter properties
80526 // or the containing class declares instance member variables with initializers.
80527 var superCallShouldBeRootLevel = (ts.getEmitScriptTarget(compilerOptions) !== 99 /* ScriptTarget.ESNext */ || !useDefineForClassFields) &&
80528 (ts.some(node.parent.members, isInstancePropertyWithInitializerOrPrivateIdentifierProperty) ||
80529 ts.some(node.parameters, function (p) { return ts.hasSyntacticModifier(p, 16476 /* ModifierFlags.ParameterPropertyModifier */); }));
80530 if (superCallShouldBeRootLevel) {
80531 // Until we have better flow analysis, it is an error to place the super call within any kind of block or conditional
80532 // See GH #8277
80533 if (!superCallIsRootLevelInConstructor(superCall, node.body)) {
80534 error(superCall, ts.Diagnostics.A_super_call_must_be_a_root_level_statement_within_a_constructor_of_a_derived_class_that_contains_initialized_properties_parameter_properties_or_private_identifiers);
80535 }
80536 // Skip past any prologue directives to check statements for referring to 'super' or 'this' before a super call
80537 else {
80538 var superCallStatement = void 0;
80539 for (var _i = 0, _a = node.body.statements; _i < _a.length; _i++) {
80540 var statement = _a[_i];
80541 if (ts.isExpressionStatement(statement) && ts.isSuperCall(ts.skipOuterExpressions(statement.expression))) {
80542 superCallStatement = statement;
80543 break;
80544 }
80545 if (nodeImmediatelyReferencesSuperOrThis(statement)) {
80546 break;
80547 }
80548 }
80549 // Until we have better flow analysis, it is an error to place the super call within any kind of block or conditional
80550 // See GH #8277
80551 if (superCallStatement === undefined) {
80552 error(node, ts.Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_to_refer_to_super_or_this_when_a_derived_class_contains_initialized_properties_parameter_properties_or_private_identifiers);
80553 }
80554 }
80555 }
80556 }
80557 else if (!classExtendsNull) {
80558 error(node, ts.Diagnostics.Constructors_for_derived_classes_must_contain_a_super_call);
80559 }
80560 }
80561 }
80562 }
80563 function superCallIsRootLevelInConstructor(superCall, body) {
80564 var superCallParent = ts.walkUpParenthesizedExpressions(superCall.parent);

Callers

nothing calls this directly

Calls 7

captureLexicalThisFunction · 0.85
findFirstSuperCallFunction · 0.85
someMethod · 0.80
errorFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…