Skip to content

Fix emitting super-call when using prologue directives #6911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11766,10 +11766,6 @@ namespace ts {
return;
}

function isSuperCallExpression(n: Node): boolean {
return n.kind === SyntaxKind.CallExpression && (<CallExpression>n).expression.kind === SyntaxKind.SuperKeyword;
}

function containsSuperCallAsComputedPropertyName(n: Declaration): boolean {
return n.name && containsSuperCall(n.name);
}
Expand Down
34 changes: 21 additions & 13 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4766,18 +4766,24 @@ const _super = (function (geti, seti) {
emitToken(SyntaxKind.CloseBraceToken, body.statements.end);
}

function findInitialSuperCall(ctor: ConstructorDeclaration): ExpressionStatement {
if (ctor.body) {
const statement = (<Block>ctor.body).statements[0];
if (statement && statement.kind === SyntaxKind.ExpressionStatement) {
const expr = (<ExpressionStatement>statement).expression;
if (expr && expr.kind === SyntaxKind.CallExpression) {
const func = (<CallExpression>expr).expression;
if (func && func.kind === SyntaxKind.SuperKeyword) {
return <ExpressionStatement>statement;
}
}
}
/**
* Return the statement at a given index if it is a super-call statement
* @param ctor a constructor declaration
* @param index an index to constructor's body to check
*/
function getSuperCallAtGivenIndex(ctor: ConstructorDeclaration, index: number): ExpressionStatement {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just take a NodeArray<Statement>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an explicit return at the end?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why doesn't this just return a boolean if there's a super call at that index?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't just return boolean because we should the super-call found here for emitting later

if (!ctor.body) {
return undefined;
}
const statements = ctor.body.statements;

if (!statements || index >= statements.length) {
return undefined;
}

const statement = statements[index];
if (statement.kind === SyntaxKind.ExpressionStatement) {
return isSuperCallExpression((<ExpressionStatement>statement).expression) ? <ExpressionStatement>statement : undefined;
}
}

Expand Down Expand Up @@ -5061,13 +5067,15 @@ const _super = (function (geti, seti) {
if (ctor) {
emitDefaultValueAssignments(ctor);
emitRestParameter(ctor);

if (baseTypeElement) {
superCall = findInitialSuperCall(ctor);
superCall = getSuperCallAtGivenIndex(ctor, startIndex);
if (superCall) {
writeLine();
emit(superCall);
}
}

emitParameterPropertyAssignments(ctor);
}
else {
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,10 @@ namespace ts {
return !!(getCombinedNodeFlags(node) & NodeFlags.Let);
}

export function isSuperCallExpression(n: Node): boolean {
return n.kind === SyntaxKind.CallExpression && (<CallExpression>n).expression.kind === SyntaxKind.SuperKeyword;
}

export function isPrologueDirective(node: Node): boolean {
return node.kind === SyntaxKind.ExpressionStatement && (<ExpressionStatement>node).expression.kind === SyntaxKind.StringLiteral;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//// [emitSuperCallBeforeEmitParameterPropertyDeclaration1.ts]
class A {
blub = 6;
}


class B extends A {
constructor(public x: number) {
"use strict";
'someStringForEgngInject';
super()
}
}


//// [emitSuperCallBeforeEmitParameterPropertyDeclaration1.js]
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var A = (function () {
function A() {
this.blub = 6;
}
return A;
}());
var B = (function (_super) {
__extends(B, _super);
function B(x) {
"use strict";
'someStringForEgngInject';
_super.call(this);
this.x = x;
}
return B;
}(A));
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/compiler/emitSuperCallBeforeEmitParameterPropertyDeclaration1.ts ===
class A {
>A : Symbol(A, Decl(emitSuperCallBeforeEmitParameterPropertyDeclaration1.ts, 0, 0))

blub = 6;
>blub : Symbol(blub, Decl(emitSuperCallBeforeEmitParameterPropertyDeclaration1.ts, 0, 9))
}


class B extends A {
>B : Symbol(B, Decl(emitSuperCallBeforeEmitParameterPropertyDeclaration1.ts, 2, 1))
>A : Symbol(A, Decl(emitSuperCallBeforeEmitParameterPropertyDeclaration1.ts, 0, 0))

constructor(public x: number) {
>x : Symbol(x, Decl(emitSuperCallBeforeEmitParameterPropertyDeclaration1.ts, 6, 16))

"use strict";
'someStringForEgngInject';
super()
>super : Symbol(A, Decl(emitSuperCallBeforeEmitParameterPropertyDeclaration1.ts, 0, 0))
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== tests/cases/compiler/emitSuperCallBeforeEmitParameterPropertyDeclaration1.ts ===
class A {
>A : A

blub = 6;
>blub : number
>6 : number
}


class B extends A {
>B : B
>A : A

constructor(public x: number) {
>x : number

"use strict";
>"use strict" : string

'someStringForEgngInject';
>'someStringForEgngInject' : string

super()
>super() : void
>super : typeof A
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//// [emitSuperCallBeforeEmitParameterPropertyDeclaration1ES6.ts]
class A {
blub = 6;
}


class B extends A {
constructor(public x: number) {
"use strict";
'someStringForEgngInject';
super()
}
}


//// [emitSuperCallBeforeEmitParameterPropertyDeclaration1ES6.js]
class A {
constructor() {
this.blub = 6;
}
}
class B extends A {
constructor(x) {
"use strict";
'someStringForEgngInject';
super();
this.x = x;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/compiler/emitSuperCallBeforeEmitParameterPropertyDeclaration1ES6.ts ===
class A {
>A : Symbol(A, Decl(emitSuperCallBeforeEmitParameterPropertyDeclaration1ES6.ts, 0, 0))

blub = 6;
>blub : Symbol(blub, Decl(emitSuperCallBeforeEmitParameterPropertyDeclaration1ES6.ts, 0, 9))
}


class B extends A {
>B : Symbol(B, Decl(emitSuperCallBeforeEmitParameterPropertyDeclaration1ES6.ts, 2, 1))
>A : Symbol(A, Decl(emitSuperCallBeforeEmitParameterPropertyDeclaration1ES6.ts, 0, 0))

constructor(public x: number) {
>x : Symbol(x, Decl(emitSuperCallBeforeEmitParameterPropertyDeclaration1ES6.ts, 6, 16))

"use strict";
'someStringForEgngInject';
super()
>super : Symbol(A, Decl(emitSuperCallBeforeEmitParameterPropertyDeclaration1ES6.ts, 0, 0))
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== tests/cases/compiler/emitSuperCallBeforeEmitParameterPropertyDeclaration1ES6.ts ===
class A {
>A : A

blub = 6;
>blub : number
>6 : number
}


class B extends A {
>B : B
>A : A

constructor(public x: number) {
>x : number

"use strict";
>"use strict" : string

'someStringForEgngInject';
>'someStringForEgngInject' : string

super()
>super() : void
>super : typeof A
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//// [emitSuperCallBeforeEmitPropertyDeclaration1.ts]
class A {
blub = 6;
}


class B extends A {

blub = 12;

constructor() {
"use strict";
'someStringForEgngInject';
super()
}
}

//// [emitSuperCallBeforeEmitPropertyDeclaration1.js]
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var A = (function () {
function A() {
this.blub = 6;
}
return A;
}());
var B = (function (_super) {
__extends(B, _super);
function B() {
"use strict";
'someStringForEgngInject';
_super.call(this);
this.blub = 12;
}
return B;
}(A));
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/compiler/emitSuperCallBeforeEmitPropertyDeclaration1.ts ===
class A {
>A : Symbol(A, Decl(emitSuperCallBeforeEmitPropertyDeclaration1.ts, 0, 0))

blub = 6;
>blub : Symbol(blub, Decl(emitSuperCallBeforeEmitPropertyDeclaration1.ts, 0, 9))
}


class B extends A {
>B : Symbol(B, Decl(emitSuperCallBeforeEmitPropertyDeclaration1.ts, 2, 1))
>A : Symbol(A, Decl(emitSuperCallBeforeEmitPropertyDeclaration1.ts, 0, 0))

blub = 12;
>blub : Symbol(blub, Decl(emitSuperCallBeforeEmitPropertyDeclaration1.ts, 5, 19))

constructor() {
"use strict";
'someStringForEgngInject';
super()
>super : Symbol(A, Decl(emitSuperCallBeforeEmitPropertyDeclaration1.ts, 0, 0))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
=== tests/cases/compiler/emitSuperCallBeforeEmitPropertyDeclaration1.ts ===
class A {
>A : A

blub = 6;
>blub : number
>6 : number
}


class B extends A {
>B : B
>A : A

blub = 12;
>blub : number
>12 : number

constructor() {
"use strict";
>"use strict" : string

'someStringForEgngInject';
>'someStringForEgngInject' : string

super()
>super() : void
>super : typeof A
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//// [emitSuperCallBeforeEmitPropertyDeclaration1ES6.ts]
class A {
blub = 6;
}


class B extends A {

blub = 12;

constructor() {
'someStringForEgngInject';
super()
}
}

//// [emitSuperCallBeforeEmitPropertyDeclaration1ES6.js]
class A {
constructor() {
this.blub = 6;
}
}
class B extends A {
constructor() {
'someStringForEgngInject';
super();
this.blub = 12;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=== tests/cases/compiler/emitSuperCallBeforeEmitPropertyDeclaration1ES6.ts ===
class A {
>A : Symbol(A, Decl(emitSuperCallBeforeEmitPropertyDeclaration1ES6.ts, 0, 0))

blub = 6;
>blub : Symbol(blub, Decl(emitSuperCallBeforeEmitPropertyDeclaration1ES6.ts, 0, 9))
}


class B extends A {
>B : Symbol(B, Decl(emitSuperCallBeforeEmitPropertyDeclaration1ES6.ts, 2, 1))
>A : Symbol(A, Decl(emitSuperCallBeforeEmitPropertyDeclaration1ES6.ts, 0, 0))

blub = 12;
>blub : Symbol(blub, Decl(emitSuperCallBeforeEmitPropertyDeclaration1ES6.ts, 5, 19))

constructor() {
'someStringForEgngInject';
super()
>super : Symbol(A, Decl(emitSuperCallBeforeEmitPropertyDeclaration1ES6.ts, 0, 0))
}
}
Loading