Skip to content

Commit

Permalink
Put parameter property initialiser into defineProperty's value
Browse files Browse the repository at this point in the history
  • Loading branch information
sandersn committed Nov 11, 2019
1 parent 5810765 commit ec79590
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 36 deletions.
31 changes: 12 additions & 19 deletions src/compiler/transformers/classFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,6 @@ namespace ts {
if (constructor) {
indexOfFirstStatement = addPrologueDirectivesAndInitialSuperCall(constructor, statements, visitor);
}
if (useDefineForClassFields) {
addPropertyStatements(statements, properties, createThis());
}

// Add the property initializers. Transforms this:
//
// public x = 1;
Expand All @@ -341,23 +337,18 @@ namespace ts {
// }
//
if (constructor?.body) {
let parameterPropertyDeclarationCount = 0;
for (let i = indexOfFirstStatement; i < constructor.body.statements.length; i++) {
if (isParameterPropertyDeclaration(getOriginalNode(constructor.body.statements[i]), constructor)) {
parameterPropertyDeclarationCount++;
}
else {
break;
}
let afterParameterProperties = findIndex(constructor.body.statements, s => !isParameterPropertyDeclaration(getOriginalNode(s), constructor), indexOfFirstStatement);
if (afterParameterProperties === -1) {
afterParameterProperties = constructor.body.statements.length;
}
if (parameterPropertyDeclarationCount > 0) {
addRange(statements, visitNodes(constructor.body.statements, visitor, isStatement, indexOfFirstStatement, parameterPropertyDeclarationCount));
indexOfFirstStatement += parameterPropertyDeclarationCount;
if (afterParameterProperties > indexOfFirstStatement) {
if (!useDefineForClassFields) {
addRange(statements, visitNodes(constructor.body.statements, visitor, isStatement, indexOfFirstStatement, afterParameterProperties - indexOfFirstStatement));
}
indexOfFirstStatement = afterParameterProperties;
}
}
if (!useDefineForClassFields) {
addPropertyStatements(statements, properties, createThis());
}
addPropertyStatements(statements, properties, createThis());

// Add existing statements, skipping the initial super call.
if (constructor) {
Expand Down Expand Up @@ -427,7 +418,9 @@ namespace ts {
? updateComputedPropertyName(property.name, getGeneratedNameForNode(property.name))
: property.name;

const initializer = property.initializer || emitAssignment ? visitNode(property.initializer, visitor, isExpression) : createVoidZero();
const initializer = property.initializer || emitAssignment ? visitNode(property.initializer, visitor, isExpression)
: hasModifier(getOriginalNode(property), ModifierFlags.ParameterPropertyModifier) && isIdentifier(propertyName) ? propertyName
: createVoidZero();
if (emitAssignment) {
const memberAccess = createMemberAccessForPropertyName(receiver, propertyName, /*location*/ propertyName);
return createAssignment(memberAccess, initializer);
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -900,13 +900,13 @@ namespace ts {
if (parametersWithPropertyAssignments) {
for (const parameter of parametersWithPropertyAssignments) {
if (isIdentifier(parameter.name)) {
members.push(aggregateTransformFlags(createProperty(
members.push(setOriginalNode(aggregateTransformFlags(createProperty(
/*decorators*/ undefined,
/*modifiers*/ undefined,
parameter.name,
/*questionOrExclamationToken*/ undefined,
/*type*/ undefined,
/*initializer*/ undefined)));
/*initializer*/ undefined)), parameter));
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions tests/baselines/reference/definePropertyES5.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//// [definePropertyES5.ts]
var x: "p" = "p"
class A {
a = 12
a = this.y
b
["computed"] = 13
;[x] = 14
m() { }
constructor(public readonly y: number) { }
z = this.y
}


Expand All @@ -19,13 +20,13 @@ var A = /** @class */ (function () {
enumerable: true,
configurable: true,
writable: true,
value: void 0
value: y
});
Object.defineProperty(this, "a", {
enumerable: true,
configurable: true,
writable: true,
value: 12
value: this.y
});
Object.defineProperty(this, "b", {
enumerable: true,
Expand All @@ -45,7 +46,12 @@ var A = /** @class */ (function () {
writable: true,
value: 14
});
this.y = y;
Object.defineProperty(this, "z", {
enumerable: true,
configurable: true,
writable: true,
value: this.y
});
}
Object.defineProperty(A.prototype, "m", {
enumerable: false,
Expand Down
13 changes: 11 additions & 2 deletions tests/baselines/reference/definePropertyES5.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ var x: "p" = "p"
class A {
>A : Symbol(A, Decl(definePropertyES5.ts, 0, 16))

a = 12
a = this.y
>a : Symbol(A.a, Decl(definePropertyES5.ts, 1, 9))
>this.y : Symbol(A.y, Decl(definePropertyES5.ts, 7, 16))
>this : Symbol(A, Decl(definePropertyES5.ts, 0, 16))
>y : Symbol(A.y, Decl(definePropertyES5.ts, 7, 16))

b
>b : Symbol(A.b, Decl(definePropertyES5.ts, 2, 10))
>b : Symbol(A.b, Decl(definePropertyES5.ts, 2, 14))

["computed"] = 13
>["computed"] : Symbol(A["computed"], Decl(definePropertyES5.ts, 3, 5))
Expand All @@ -23,6 +26,12 @@ class A {
>m : Symbol(A.m, Decl(definePropertyES5.ts, 5, 13))

constructor(public readonly y: number) { }
>y : Symbol(A.y, Decl(definePropertyES5.ts, 7, 16))

z = this.y
>z : Symbol(A.z, Decl(definePropertyES5.ts, 7, 46))
>this.y : Symbol(A.y, Decl(definePropertyES5.ts, 7, 16))
>this : Symbol(A, Decl(definePropertyES5.ts, 0, 16))
>y : Symbol(A.y, Decl(definePropertyES5.ts, 7, 16))
}

12 changes: 10 additions & 2 deletions tests/baselines/reference/definePropertyES5.types
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ var x: "p" = "p"
class A {
>A : A

a = 12
a = this.y
>a : number
>12 : 12
>this.y : number
>this : this
>y : number

b
>b : any
Expand All @@ -27,6 +29,12 @@ class A {
>m : () => void

constructor(public readonly y: number) { }
>y : number

z = this.y
>z : number
>this.y : number
>this : this
>y : number
}

6 changes: 4 additions & 2 deletions tests/baselines/reference/definePropertyESNext.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ class A {
class B {
}
class C extends B {
z = 1
z = this.ka
constructor(public ka: number) {
super()
}
ki = this.ka
}


Expand All @@ -35,9 +36,10 @@ class B {
}
class C extends B {
ka;
z = 1;
z = this.ka;
constructor(ka) {
super();
this.ka = ka;
}
ki = this.ka;
}
10 changes: 9 additions & 1 deletion tests/baselines/reference/definePropertyESNext.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,22 @@ class C extends B {
>C : Symbol(C, Decl(definePropertyESNext.ts, 10, 1))
>B : Symbol(B, Decl(definePropertyESNext.ts, 8, 1))

z = 1
z = this.ka
>z : Symbol(C.z, Decl(definePropertyESNext.ts, 11, 19))
>this.ka : Symbol(C.ka, Decl(definePropertyESNext.ts, 13, 16))
>this : Symbol(C, Decl(definePropertyESNext.ts, 10, 1))
>ka : Symbol(C.ka, Decl(definePropertyESNext.ts, 13, 16))

constructor(public ka: number) {
>ka : Symbol(C.ka, Decl(definePropertyESNext.ts, 13, 16))

super()
>super : Symbol(B, Decl(definePropertyESNext.ts, 8, 1))
}
ki = this.ka
>ki : Symbol(C.ki, Decl(definePropertyESNext.ts, 15, 5))
>this.ka : Symbol(C.ka, Decl(definePropertyESNext.ts, 13, 16))
>this : Symbol(C, Decl(definePropertyESNext.ts, 10, 1))
>ka : Symbol(C.ka, Decl(definePropertyESNext.ts, 13, 16))
}

11 changes: 9 additions & 2 deletions tests/baselines/reference/definePropertyESNext.types
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ class C extends B {
>C : C
>B : B

z = 1
z = this.ka
>z : number
>1 : 1
>this.ka : number
>this : this
>ka : number

constructor(public ka: number) {
>ka : number
Expand All @@ -47,5 +49,10 @@ class C extends B {
>super() : void
>super : typeof B
}
ki = this.ka
>ki : number
>this.ka : number
>this : this
>ka : number
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// @useDefineForClassFields: true
var x: "p" = "p"
class A {
a = 12
a = this.y
b
["computed"] = 13
;[x] = 14
m() { }
constructor(public readonly y: number) { }
z = this.y
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ class A {
class B {
}
class C extends B {
z = 1
z = this.ka
constructor(public ka: number) {
super()
}
ki = this.ka
}

0 comments on commit ec79590

Please sign in to comment.