-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Review PR: Abstract classes #2946
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d4e97ae
Added support for the abstract keyword on classes
DickvdBrink dc7d9cf
Accept baselines
DickvdBrink 651658d
Added testcase for abstract class
DickvdBrink 30bdb52
Added new test cases for abstract keyword and corrected diagnosticmes…
DickvdBrink 42f197a
Accepted baselines
DickvdBrink 48fa9f9
Fixed ncoding issue in abstractClass1.ts
DickvdBrink File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
tests/cases/compiler/abstractClass1.ts(15,9): error TS2502: Cannot create an instance of the abstract class 'Foo' | ||
tests/cases/compiler/abstractClass1.ts(16,9): error TS2346: Supplied parameters do not match any signature of call target. | ||
tests/cases/compiler/abstractClass1.ts(16,9): error TS2502: Cannot create an instance of the abstract class 'Foo' | ||
tests/cases/compiler/abstractClass1.ts(25,1): error TS2502: Cannot create an instance of the abstract class 'Qux' | ||
tests/cases/compiler/abstractClass1.ts(35,1): error TS2346: Supplied parameters do not match any signature of call target. | ||
tests/cases/compiler/abstractClass1.ts(35,1): error TS2502: Cannot create an instance of the abstract class 'Foo' | ||
|
||
|
||
==== tests/cases/compiler/abstractClass1.ts (6 errors) ==== | ||
|
||
abstract class Foo { | ||
constructor(f: any) { } | ||
public static bar(): void { } | ||
|
||
public empty() { } | ||
} | ||
|
||
class Bar extends Foo { | ||
constructor(f: any) { | ||
super(f); | ||
} | ||
} | ||
|
||
var a = new Foo(1); // Error | ||
~~~~~~~~~~ | ||
!!! error TS2502: Cannot create an instance of the abstract class 'Foo' | ||
var b = new Foo(); // Error because of invalid constructor arguments | ||
~~~~~~~~~ | ||
!!! error TS2346: Supplied parameters do not match any signature of call target. | ||
~~~~~~~~~ | ||
!!! error TS2502: Cannot create an instance of the abstract class 'Foo' | ||
|
||
module baz { | ||
export abstract class Qux { | ||
} | ||
export class Quz extends Qux { | ||
} | ||
} | ||
|
||
new baz.Qux(); | ||
~~~~~~~~~~~~~ | ||
!!! error TS2502: Cannot create an instance of the abstract class 'Qux' | ||
|
||
// Valid | ||
var c = new Bar(1); | ||
c.empty(); | ||
|
||
// Calling a static method on a abstract class is valid | ||
Foo.bar(); | ||
|
||
var Copy = Foo; | ||
new Copy(); | ||
~~~~~~~~~~ | ||
!!! error TS2346: Supplied parameters do not match any signature of call target. | ||
~~~~~~~~~~ | ||
!!! error TS2502: Cannot create an instance of the abstract class 'Foo' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
//// [abstractClass1.ts] | ||
|
||
abstract class Foo { | ||
constructor(f: any) { } | ||
public static bar(): void { } | ||
|
||
public empty() { } | ||
} | ||
|
||
class Bar extends Foo { | ||
constructor(f: any) { | ||
super(f); | ||
} | ||
} | ||
|
||
var a = new Foo(1); // Error | ||
var b = new Foo(); // Error because of invalid constructor arguments | ||
|
||
module baz { | ||
export abstract class Qux { | ||
} | ||
export class Quz extends Qux { | ||
} | ||
} | ||
|
||
new baz.Qux(); | ||
|
||
// Valid | ||
var c = new Bar(1); | ||
c.empty(); | ||
|
||
// Calling a static method on a abstract class is valid | ||
Foo.bar(); | ||
|
||
var Copy = Foo; | ||
new Copy(); | ||
|
||
|
||
//// [abstractClass1.js] | ||
var __extends = this.__extends || function (d, b) { | ||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
function __() { this.constructor = d; } | ||
__.prototype = b.prototype; | ||
d.prototype = new __(); | ||
}; | ||
var Foo = (function () { | ||
function Foo(f) { | ||
} | ||
Foo.bar = function () { }; | ||
Foo.prototype.empty = function () { }; | ||
return Foo; | ||
})(); | ||
var Bar = (function (_super) { | ||
__extends(Bar, _super); | ||
function Bar(f) { | ||
_super.call(this, f); | ||
} | ||
return Bar; | ||
})(Foo); | ||
var a = new Foo(1); // Error | ||
var b = new Foo(); // Error because of invalid constructor arguments | ||
var baz; | ||
(function (baz) { | ||
var Qux = (function () { | ||
function Qux() { | ||
} | ||
return Qux; | ||
})(); | ||
baz.Qux = Qux; | ||
var Quz = (function (_super) { | ||
__extends(Quz, _super); | ||
function Quz() { | ||
_super.apply(this, arguments); | ||
} | ||
return Quz; | ||
})(Qux); | ||
baz.Quz = Quz; | ||
})(baz || (baz = {})); | ||
new baz.Qux(); | ||
// Valid | ||
var c = new Bar(1); | ||
c.empty(); | ||
// Calling a static method on a abstract class is valid | ||
Foo.bar(); | ||
var Copy = Foo; | ||
new Copy(); | ||
|
||
|
||
//// [abstractClass1.d.ts] | ||
declare abstract class Foo { | ||
constructor(f: any); | ||
static bar(): void; | ||
empty(): void; | ||
} | ||
declare class Bar extends Foo { | ||
constructor(f: any); | ||
} | ||
declare var a: Foo; | ||
declare var b: any; | ||
declare module baz { | ||
abstract class Qux { | ||
} | ||
class Quz extends Qux { | ||
} | ||
} | ||
declare var c: Bar; | ||
declare var Copy: typeof Foo; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//// [abstractClassIdentifierName.ts] | ||
class abstract { | ||
|
||
abstract(): void { } | ||
} | ||
|
||
|
||
//// [abstractClassIdentifierName.js] | ||
var abstract = (function () { | ||
function abstract() { | ||
} | ||
abstract.prototype.abstract = function () { }; | ||
return abstract; | ||
})(); |
8 changes: 8 additions & 0 deletions
8
tests/baselines/reference/abstractClassIdentifierName.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
=== tests/cases/compiler/abstractClassIdentifierName.ts === | ||
class abstract { | ||
>abstract : Symbol(abstract, Decl(abstractClassIdentifierName.ts, 0, 0)) | ||
|
||
abstract(): void { } | ||
>abstract : Symbol(abstract, Decl(abstractClassIdentifierName.ts, 0, 16)) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
=== tests/cases/compiler/abstractClassIdentifierName.ts === | ||
class abstract { | ||
>abstract : abstract | ||
|
||
abstract(): void { } | ||
>abstract : () => void | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//// [abstractIdentifierName.ts] | ||
var abstract = true; | ||
|
||
function foo() { | ||
"use strict"; | ||
var abstract = true; | ||
} | ||
|
||
//// [abstractIdentifierName.js] | ||
var abstract = true; | ||
function foo() { | ||
"use strict"; | ||
var abstract = true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
=== tests/cases/compiler/abstractIdentifierName.ts === | ||
var abstract = true; | ||
>abstract : Symbol(abstract, Decl(abstractIdentifierName.ts, 0, 3)) | ||
|
||
function foo() { | ||
>foo : Symbol(foo, Decl(abstractIdentifierName.ts, 0, 20)) | ||
|
||
"use strict"; | ||
var abstract = true; | ||
>abstract : Symbol(abstract, Decl(abstractIdentifierName.ts, 4, 7)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
=== tests/cases/compiler/abstractIdentifierName.ts === | ||
var abstract = true; | ||
>abstract : boolean | ||
>true : boolean | ||
|
||
function foo() { | ||
>foo : () => void | ||
|
||
"use strict"; | ||
>"use strict" : string | ||
|
||
var abstract = true; | ||
>abstract : boolean | ||
>true : boolean | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
//// [abstractInterfaceIdentifierName.ts] | ||
|
||
interface abstract { | ||
abstract(): void; | ||
} | ||
|
||
|
||
//// [abstractInterfaceIdentifierName.js] |
9 changes: 9 additions & 0 deletions
9
tests/baselines/reference/abstractInterfaceIdentifierName.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
=== tests/cases/compiler/abstractInterfaceIdentifierName.ts === | ||
|
||
interface abstract { | ||
>abstract : Symbol(abstract, Decl(abstractInterfaceIdentifierName.ts, 0, 0)) | ||
|
||
abstract(): void; | ||
>abstract : Symbol(abstract, Decl(abstractInterfaceIdentifierName.ts, 1, 20)) | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We want this to be allowed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about
I would argue this has to be an error.. else
import {abstractClass} from "module"
is not going to work.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if this is the case, the check should not use type.symbol, it should use resolveName at this location, and only be restricted to identifiers and qualified names.
this looks like a more correct approach though. i think you can always cast to something that is not abstract to get away with it.. it feels a lot like privates in classes that we preserve on the type.