Skip to content
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

Allow for class static vars to be called static #44813

Merged
merged 3 commits into from
Oct 1, 2021
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
11 changes: 7 additions & 4 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1817,7 +1817,6 @@ namespace ts {
case SyntaxKind.DefaultKeyword:
return nextTokenCanFollowDefaultKeyword();
case SyntaxKind.StaticKeyword:
return nextTokenIsOnSameLineAndCanFollowModifier();
case SyntaxKind.GetKeyword:
case SyntaxKind.SetKeyword:
nextToken();
Expand Down Expand Up @@ -6701,7 +6700,7 @@ namespace ts {
return list && createNodeArray(list, pos);
}

function tryParseModifier(permitInvalidConstAsModifier?: boolean, stopOnStartOfClassStaticBlock?: boolean): Modifier | undefined {
function tryParseModifier(permitInvalidConstAsModifier?: boolean, stopOnStartOfClassStaticBlock?: boolean, hasSeenStaticModifier?: boolean): Modifier | undefined {
const pos = getNodePos();
const kind = token();

Expand All @@ -6715,6 +6714,9 @@ namespace ts {
else if (stopOnStartOfClassStaticBlock && token() === SyntaxKind.StaticKeyword && lookAhead(nextTokenIsOpenBrace)) {
return undefined;
}
else if (hasSeenStaticModifier && token() === SyntaxKind.StaticKeyword) {
return undefined;
}
else {
if (!parseAnyContextualModifier()) {
return undefined;
Expand All @@ -6733,8 +6735,9 @@ namespace ts {
*/
function parseModifiers(permitInvalidConstAsModifier?: boolean, stopOnStartOfClassStaticBlock?: boolean): NodeArray<Modifier> | undefined {
const pos = getNodePos();
let list, modifier;
while (modifier = tryParseModifier(permitInvalidConstAsModifier, stopOnStartOfClassStaticBlock)) {
let list, modifier, hasSeenStatic = false;
while (modifier = tryParseModifier(permitInvalidConstAsModifier, stopOnStartOfClassStaticBlock, hasSeenStatic)) {
if (modifier.kind === SyntaxKind.StaticKeyword) hasSeenStatic = true;
list = append(list, modifier);
}
return list && createNodeArray(list, pos);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
tests/cases/compiler/multipleClassPropertyModifiersErrors.ts(2,9): error TS1028: Accessibility modifier already seen.
tests/cases/compiler/multipleClassPropertyModifiersErrors.ts(3,10): error TS1028: Accessibility modifier already seen.
tests/cases/compiler/multipleClassPropertyModifiersErrors.ts(4,9): error TS1030: 'static' modifier already seen.
tests/cases/compiler/multipleClassPropertyModifiersErrors.ts(5,9): error TS1028: Accessibility modifier already seen.
tests/cases/compiler/multipleClassPropertyModifiersErrors.ts(6,10): error TS1028: Accessibility modifier already seen.
tests/cases/compiler/multipleClassPropertyModifiersErrors.ts(4,16): error TS1005: ';' expected.


==== tests/cases/compiler/multipleClassPropertyModifiersErrors.ts (5 errors) ====
==== tests/cases/compiler/multipleClassPropertyModifiersErrors.ts (1 errors) ====
class C {
public public p1;
~~~~~~
!!! error TS1028: Accessibility modifier already seen.
private private p2;
~~~~~~~
!!! error TS1028: Accessibility modifier already seen.
static static p3;
~~~~~~
!!! error TS1030: 'static' modifier already seen.
~~
!!! error TS1005: ';' expected.
public private p4;
~~~~~~~
!!! error TS1028: Accessibility modifier already seen.
private public p5;
~~~~~~
!!! error TS1028: Accessibility modifier already seen.
public static p6;
private static p7;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class C {
>p2 : Symbol(C.p2, Decl(multipleClassPropertyModifiersErrors.ts, 1, 18))

static static p3;
>p3 : Symbol(C.p3, Decl(multipleClassPropertyModifiersErrors.ts, 2, 20))
>static : Symbol(C.static, Decl(multipleClassPropertyModifiersErrors.ts, 2, 20))
>p3 : Symbol(C.p3, Decl(multipleClassPropertyModifiersErrors.ts, 3, 14))

public private p4;
>p4 : Symbol(C.p4, Decl(multipleClassPropertyModifiersErrors.ts, 3, 18))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class C {
>p2 : any

static static p3;
>static : any
>p3 : any

public private p4;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
tests/cases/conformance/parser/ecmascript5/IndexMemberDeclarations/parserIndexMemberDeclaration10.ts(2,11): error TS1030: 'static' modifier already seen.
tests/cases/conformance/parser/ecmascript5/IndexMemberDeclarations/parserIndexMemberDeclaration10.ts(2,18): error TS1005: ';' expected.


==== tests/cases/conformance/parser/ecmascript5/IndexMemberDeclarations/parserIndexMemberDeclaration10.ts (1 errors) ====
class C {
static static [x: string]: string;
~~~~~~
!!! error TS1030: 'static' modifier already seen.
~
!!! error TS1005: ';' expected.
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ class C {
>C : Symbol(C, Decl(parserIndexMemberDeclaration10.ts, 0, 0))

static static [x: string]: string;
>static : Symbol(C.static, Decl(parserIndexMemberDeclaration10.ts, 0, 9))
>x : Symbol(x, Decl(parserIndexMemberDeclaration10.ts, 1, 18))
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ class C {
>C : C

static static [x: string]: string;
>static : any
>x : string
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
tests/cases/conformance/parser/ecmascript5/MemberAccessorDeclarations/parserMemberAccessorDeclaration8.ts(2,12): error TS1030: 'static' modifier already seen.
tests/cases/conformance/parser/ecmascript5/MemberAccessorDeclarations/parserMemberAccessorDeclaration8.ts(2,19): error TS1005: ';' expected.
tests/cases/conformance/parser/ecmascript5/MemberAccessorDeclarations/parserMemberAccessorDeclaration8.ts(2,23): error TS2378: A 'get' accessor must return a value.


==== tests/cases/conformance/parser/ecmascript5/MemberAccessorDeclarations/parserMemberAccessorDeclaration8.ts (2 errors) ====
class C {
static static get Foo() { }
~~~~~~
!!! error TS1030: 'static' modifier already seen.
~~~
!!! error TS1005: ';' expected.
~~~
!!! error TS2378: A 'get' accessor must return a value.
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class C {
var C = /** @class */ (function () {
function C() {
}
Object.defineProperty(C, "Foo", {
Object.defineProperty(C.prototype, "Foo", {
get: function () { },
enumerable: false,
configurable: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ class C {
>C : Symbol(C, Decl(parserMemberAccessorDeclaration8.ts, 0, 0))

static static get Foo() { }
>Foo : Symbol(C.Foo, Decl(parserMemberAccessorDeclaration8.ts, 0, 9))
>static : Symbol(C.static, Decl(parserMemberAccessorDeclaration8.ts, 0, 9))
>Foo : Symbol(C.Foo, Decl(parserMemberAccessorDeclaration8.ts, 1, 17))
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ class C {
>C : C

static static get Foo() { }
>static : any
>Foo : void
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
tests/cases/conformance/parser/ecmascript5/MemberFunctionDeclarations/parserMemberFunctionDeclaration2.ts(2,12): error TS1030: 'static' modifier already seen.
tests/cases/conformance/parser/ecmascript5/MemberFunctionDeclarations/parserMemberFunctionDeclaration2.ts(2,19): error TS1005: ';' expected.


==== tests/cases/conformance/parser/ecmascript5/MemberFunctionDeclarations/parserMemberFunctionDeclaration2.ts (1 errors) ====
class C {
static static Foo() { }
~~~~~~
!!! error TS1030: 'static' modifier already seen.
~~~
!!! error TS1005: ';' expected.
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ class C {
var C = /** @class */ (function () {
function C() {
}
C.Foo = function () { };
C.prototype.Foo = function () { };
return C;
}());
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ class C {
>C : Symbol(C, Decl(parserMemberFunctionDeclaration2.ts, 0, 0))

static static Foo() { }
>Foo : Symbol(C.Foo, Decl(parserMemberFunctionDeclaration2.ts, 0, 9))
>static : Symbol(C.static, Decl(parserMemberFunctionDeclaration2.ts, 0, 9))
>Foo : Symbol(C.Foo, Decl(parserMemberFunctionDeclaration2.ts, 1, 17))
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ class C {
>C : C

static static Foo() { }
>static : any
>Foo : () => void
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
tests/cases/conformance/parser/ecmascript5/MemberVariableDeclarations/parserMemberVariableDeclaration2.ts(2,10): error TS1030: 'static' modifier already seen.
tests/cases/conformance/parser/ecmascript5/MemberVariableDeclarations/parserMemberVariableDeclaration2.ts(2,17): error TS1005: ';' expected.


==== tests/cases/conformance/parser/ecmascript5/MemberVariableDeclarations/parserMemberVariableDeclaration2.ts (1 errors) ====
class C {
static static Foo;
~~~~~~
!!! error TS1030: 'static' modifier already seen.
~~~
!!! error TS1005: ';' expected.
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ class C {
>C : Symbol(C, Decl(parserMemberVariableDeclaration2.ts, 0, 0))

static static Foo;
>Foo : Symbol(C.Foo, Decl(parserMemberVariableDeclaration2.ts, 0, 9))
>static : Symbol(C.static, Decl(parserMemberVariableDeclaration2.ts, 0, 9))
>Foo : Symbol(C.Foo, Decl(parserMemberVariableDeclaration2.ts, 1, 15))
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ class C {
>C : C

static static Foo;
>static : any
>Foo : any
}
27 changes: 21 additions & 6 deletions tests/baselines/reference/staticAsIdentifier.errors.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests/cases/compiler/staticAsIdentifier.ts(12,12): error TS1030: 'static' modifier already seen.
tests/cases/compiler/staticAsIdentifier.ts(16,12): error TS1030: 'static' modifier already seen.
tests/cases/compiler/staticAsIdentifier.ts(12,19): error TS1005: ';' expected.
tests/cases/compiler/staticAsIdentifier.ts(16,19): error TS1005: ';' expected.


==== tests/cases/compiler/staticAsIdentifier.ts (2 errors) ====
Expand All @@ -15,13 +15,28 @@ tests/cases/compiler/staticAsIdentifier.ts(16,12): error TS1030: 'static' modifi

class C3 {
static static p: string;
~~~~~~
!!! error TS1030: 'static' modifier already seen.
~
!!! error TS1005: ';' expected.
}

class C4 {
static static foo() {}
~~~~~~
!!! error TS1030: 'static' modifier already seen.
~~~
!!! error TS1005: ';' expected.
}

class C5 {
static static
Copy link
Member

Choose a reason for hiding this comment

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

can I see an example with an initialiser, and one with a type?

}

class C6 {
static
static
}

class C7 extends C6 {
static override static
}



49 changes: 48 additions & 1 deletion tests/baselines/reference/staticAsIdentifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,39 @@ class C3 {
class C4 {
static static foo() {}
}

class C5 {
static static
}

class C6 {
static
static
}

class C7 extends C6 {
static override static
}




//// [staticAsIdentifier.js]
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var C1 = /** @class */ (function () {
function C1() {
}
Expand All @@ -38,6 +68,23 @@ var C3 = /** @class */ (function () {
var C4 = /** @class */ (function () {
function C4() {
}
C4.foo = function () { };
C4.prototype.foo = function () { };
return C4;
}());
var C5 = /** @class */ (function () {
function C5() {
}
return C5;
}());
var C6 = /** @class */ (function () {
function C6() {
}
return C6;
}());
var C7 = /** @class */ (function (_super) {
__extends(C7, _super);
function C7() {
return _super !== null && _super.apply(this, arguments) || this;
}
return C7;
}(C6));
31 changes: 29 additions & 2 deletions tests/baselines/reference/staticAsIdentifier.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,40 @@ class C3 {
>C3 : Symbol(C3, Decl(staticAsIdentifier.ts, 8, 1))

static static p: string;
>p : Symbol(C3.p, Decl(staticAsIdentifier.ts, 10, 10))
>static : Symbol(C3.static, Decl(staticAsIdentifier.ts, 10, 10))
>p : Symbol(C3.p, Decl(staticAsIdentifier.ts, 11, 17))
}

class C4 {
>C4 : Symbol(C4, Decl(staticAsIdentifier.ts, 12, 1))

static static foo() {}
>foo : Symbol(C4.foo, Decl(staticAsIdentifier.ts, 14, 10))
>static : Symbol(C4.static, Decl(staticAsIdentifier.ts, 14, 10))
>foo : Symbol(C4.foo, Decl(staticAsIdentifier.ts, 15, 17))
}

class C5 {
>C5 : Symbol(C5, Decl(staticAsIdentifier.ts, 16, 1))

static static
>static : Symbol(C5.static, Decl(staticAsIdentifier.ts, 18, 10))
}

class C6 {
>C6 : Symbol(C6, Decl(staticAsIdentifier.ts, 20, 1))

static
static
>static : Symbol(C6.static, Decl(staticAsIdentifier.ts, 22, 10))
}

class C7 extends C6 {
>C7 : Symbol(C7, Decl(staticAsIdentifier.ts, 25, 1))
>C6 : Symbol(C6, Decl(staticAsIdentifier.ts, 20, 1))

static override static
>static : Symbol(C7.static, Decl(staticAsIdentifier.ts, 27, 21))
}



Loading