Skip to content

fix(40632): Don't emit abstract members #40699

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 1 commit into from
Nov 5, 2020
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
5 changes: 4 additions & 1 deletion src/compiler/transformers/classFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ namespace ts {
}

function isPropertyDeclarationThatRequiresConstructorStatement(member: ClassElement): member is PropertyDeclaration {
if (!isPropertyDeclaration(member) || hasStaticModifier(member)) {
if (!isPropertyDeclaration(member) || hasStaticModifier(member) || hasSyntacticModifier(getOriginalNode(member), ModifierFlags.Abstract)) {
return false;
}
if (context.getCompilerOptions().useDefineForClassFields) {
Expand Down Expand Up @@ -779,6 +779,9 @@ namespace ts {
}

const propertyOriginalNode = getOriginalNode(property);
if (hasSyntacticModifier(propertyOriginalNode, ModifierFlags.Abstract)) {
return undefined;
}
const initializer = property.initializer || emitAssignment ? visitNode(property.initializer, visitor, isExpression) ?? factory.createVoidZero()
: isParameterPropertyDeclaration(propertyOriginalNode, propertyOriginalNode.parent) && isIdentifier(propertyName) ? propertyName
: factory.createVoidZero();
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1900,7 +1900,7 @@ namespace ts {
}

function visitPropertyDeclaration(node: PropertyDeclaration) {
if (node.flags & NodeFlags.Ambient) {
if (node.flags & NodeFlags.Ambient || hasSyntacticModifier(node, ModifierFlags.Abstract)) {
return undefined;
}
const updated = factory.updatePropertyDeclaration(
Expand Down
38 changes: 38 additions & 0 deletions tests/baselines/reference/abstractProperty(target=es2015).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//// [abstractProperty.ts]
abstract class A {
protected abstract x: string;
public foo() {
console.log(this.x);
}
}

class B extends A {
protected x = 'B.x';
}

class C extends A {
protected get x() { return 'C.x' };
}


//// [abstractProperty.js]
class A {
foo() {
console.log(this.x);
}
}
class B extends A {
constructor() {
super(...arguments);
Object.defineProperty(this, "x", {
enumerable: true,
configurable: true,
writable: true,
value: 'B.x'
});
}
}
class C extends A {
get x() { return 'C.x'; }
;
}
36 changes: 36 additions & 0 deletions tests/baselines/reference/abstractProperty(target=es2015).symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
=== tests/cases/conformance/classes/propertyMemberDeclarations/abstractProperty.ts ===
abstract class A {
>A : Symbol(A, Decl(abstractProperty.ts, 0, 0))

protected abstract x: string;
>x : Symbol(A.x, Decl(abstractProperty.ts, 0, 18))

public foo() {
>foo : Symbol(A.foo, Decl(abstractProperty.ts, 1, 33))

console.log(this.x);
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>this.x : Symbol(A.x, Decl(abstractProperty.ts, 0, 18))
>this : Symbol(A, Decl(abstractProperty.ts, 0, 0))
>x : Symbol(A.x, Decl(abstractProperty.ts, 0, 18))
}
}

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

protected x = 'B.x';
>x : Symbol(B.x, Decl(abstractProperty.ts, 7, 19))
}

class C extends A {
>C : Symbol(C, Decl(abstractProperty.ts, 9, 1))
>A : Symbol(A, Decl(abstractProperty.ts, 0, 0))

protected get x() { return 'C.x' };
>x : Symbol(C.x, Decl(abstractProperty.ts, 11, 19))
}

39 changes: 39 additions & 0 deletions tests/baselines/reference/abstractProperty(target=es2015).types
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
=== tests/cases/conformance/classes/propertyMemberDeclarations/abstractProperty.ts ===
abstract class A {
>A : A

protected abstract x: string;
>x : string

public foo() {
>foo : () => void

console.log(this.x);
>console.log(this.x) : void
>console.log : (...data: any[]) => void
>console : Console
>log : (...data: any[]) => void
>this.x : string
>this : this
>x : string
}
}

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

protected x = 'B.x';
>x : string
>'B.x' : "B.x"
}

class C extends A {
>C : C
>A : A

protected get x() { return 'C.x' };
>x : string
>'C.x' : "C.x"
}

30 changes: 30 additions & 0 deletions tests/baselines/reference/abstractProperty(target=esnext).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//// [abstractProperty.ts]
abstract class A {
protected abstract x: string;
public foo() {
console.log(this.x);
}
}

class B extends A {
protected x = 'B.x';
}

class C extends A {
protected get x() { return 'C.x' };
}


//// [abstractProperty.js]
class A {
foo() {
console.log(this.x);
}
}
class B extends A {
x = 'B.x';
}
class C extends A {
get x() { return 'C.x'; }
;
}
36 changes: 36 additions & 0 deletions tests/baselines/reference/abstractProperty(target=esnext).symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
=== tests/cases/conformance/classes/propertyMemberDeclarations/abstractProperty.ts ===
abstract class A {
>A : Symbol(A, Decl(abstractProperty.ts, 0, 0))

protected abstract x: string;
>x : Symbol(A.x, Decl(abstractProperty.ts, 0, 18))

public foo() {
>foo : Symbol(A.foo, Decl(abstractProperty.ts, 1, 33))

console.log(this.x);
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>this.x : Symbol(A.x, Decl(abstractProperty.ts, 0, 18))
>this : Symbol(A, Decl(abstractProperty.ts, 0, 0))
>x : Symbol(A.x, Decl(abstractProperty.ts, 0, 18))
}
}

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

protected x = 'B.x';
>x : Symbol(B.x, Decl(abstractProperty.ts, 7, 19))
}

class C extends A {
>C : Symbol(C, Decl(abstractProperty.ts, 9, 1))
>A : Symbol(A, Decl(abstractProperty.ts, 0, 0))

protected get x() { return 'C.x' };
>x : Symbol(C.x, Decl(abstractProperty.ts, 11, 19))
}

39 changes: 39 additions & 0 deletions tests/baselines/reference/abstractProperty(target=esnext).types
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
=== tests/cases/conformance/classes/propertyMemberDeclarations/abstractProperty.ts ===
abstract class A {
>A : A

protected abstract x: string;
>x : string

public foo() {
>foo : () => void

console.log(this.x);
>console.log(this.x) : void
>console.log : (...data: any[]) => void
>console : Console
>log : (...data: any[]) => void
>this.x : string
>this : this
>x : string
}
}

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

protected x = 'B.x';
>x : string
>'B.x' : "B.x"
}

class C extends A {
>C : C
>A : A

protected get x() { return 'C.x' };
>x : string
>'C.x' : "C.x"
}

6 changes: 0 additions & 6 deletions tests/baselines/reference/accessorsOverrideProperty7.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ var __extends = (this && this.__extends) || (function () {
})();
var A = /** @class */ (function () {
function A() {
Object.defineProperty(this, "p", {
enumerable: true,
configurable: true,
writable: true,
value: 'yep'
});
}
return A;
}());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ abstract class B {

//// [privateNamesIncompatibleModifiers.js]
"use strict";
var _foo, _bar, _baz, _qux, _quux;
var _foo, _bar, _baz, _qux;
class A {
constructor() {
_foo.set(this, 3); // Error
Expand All @@ -25,8 +25,4 @@ class A {
}
_foo = new WeakMap(), _bar = new WeakMap(), _baz = new WeakMap(), _qux = new WeakMap();
class B {
constructor() {
_quux.set(this, 3); // Error
}
}
_quux = new WeakMap();
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @target: es2015,esnext
// @useDefineForClassFields: true
abstract class A {
protected abstract x: string;
public foo() {
console.log(this.x);
}
}

class B extends A {
protected x = 'B.x';
}

class C extends A {
protected get x() { return 'C.x' };
}