Skip to content

Commit f3c4190

Browse files
authored
Fix reparsing of JSDoc modifiers on constructor declarations (#4229)
1 parent 1f955e9 commit f3c4190

8 files changed

Lines changed: 82 additions & 3 deletions

File tree

internal/ast/ast.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1522,7 +1522,8 @@ func (node *ExportableBase) ExportableData() *ExportableBase { return node }
15221522

15231523
// ModifiersBase
15241524

1525-
func (node *ModifiersBase) Modifiers() *ModifierList { return node.modifiers }
1525+
func (node *ModifiersBase) Modifiers() *ModifierList { return node.modifiers }
1526+
func (node *ModifiersBase) setModifiers(modifiers *ModifierList) { node.modifiers = modifiers }
15261527

15271528
// LocalsContainerBase
15281529

internal/parser/reparser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
470470
parent = parent.Expression()
471471
}
472472
switch parent.Kind {
473-
case ast.KindPropertyDeclaration, ast.KindMethodDeclaration, ast.KindGetAccessor, ast.KindSetAccessor, ast.KindBinaryExpression:
473+
case ast.KindPropertyDeclaration, ast.KindMethodDeclaration, ast.KindConstructor, ast.KindGetAccessor, ast.KindSetAccessor, ast.KindBinaryExpression:
474474
var keyword ast.Kind
475475
switch tag.Kind {
476476
case ast.KindJSDocReadonlyTag:
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
main.js(7,1): error TS2673: Constructor of class 'C' is private and only accessible within the class declaration.
2+
3+
4+
==== main.js (1 errors) ====
5+
// https://github.com/microsoft/typescript-go/issues/4219
6+
7+
class C {
8+
/** @private */
9+
constructor() {}
10+
}
11+
new C();
12+
~~~~~~~
13+
!!! error TS2673: Constructor of class 'C' is private and only accessible within the class declaration.
14+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [tests/cases/compiler/jsDocPrivateConstructor.ts] ////
2+
3+
=== main.js ===
4+
// https://github.com/microsoft/typescript-go/issues/4219
5+
6+
class C {
7+
>C : Symbol(C, Decl(main.js, 0, 0))
8+
9+
/** @private */
10+
constructor() {}
11+
}
12+
new C();
13+
>C : Symbol(C, Decl(main.js, 0, 0))
14+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [tests/cases/compiler/jsDocPrivateConstructor.ts] ////
2+
3+
=== main.js ===
4+
// https://github.com/microsoft/typescript-go/issues/4219
5+
6+
class C {
7+
>C : C
8+
9+
/** @private */
10+
constructor() {}
11+
}
12+
new C();
13+
>new C() : C
14+
>C : typeof C
15+

testdata/baselines/reference/submodule/conformance/jsdocReadonly.errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
jsdocReadonly.js(13,9): error TS1024: 'readonly' modifier can only appear on a property declaration or index signature.
12
jsdocReadonly.js(23,3): error TS2540: Cannot assign to 'y' because it is a read-only property.
23

34

4-
==== jsdocReadonly.js (1 errors) ====
5+
==== jsdocReadonly.js (2 errors) ====
56
class LOL {
67
/**
78
* @readonly
@@ -15,6 +16,8 @@ jsdocReadonly.js(23,3): error TS2540: Cannot assign to 'y' because it is a read-
1516
/** @readonly Definitely not here */
1617
static z = 3
1718
/** @readonly This is OK too */
19+
~~~~~~~~~~~~~~~~~~~~~~~~~
20+
!!! error TS1024: 'readonly' modifier can only appear on a property declaration or index signature.
1821
constructor() {
1922
/** ok */
2023
this.y = 2
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--- old.jsdocReadonly.errors.txt
2+
+++ new.jsdocReadonly.errors.txt
3+
@@= skipped -0, +0 lines =@@
4+
+jsdocReadonly.js(13,9): error TS1024: 'readonly' modifier can only appear on a property declaration or index signature.
5+
jsdocReadonly.js(23,3): error TS2540: Cannot assign to 'y' because it is a read-only property.
6+
7+
8+
-==== jsdocReadonly.js (1 errors) ====
9+
+==== jsdocReadonly.js (2 errors) ====
10+
class LOL {
11+
/**
12+
* @readonly
13+
@@= skipped -14, +15 lines =@@
14+
/** @readonly Definitely not here */
15+
static z = 3
16+
/** @readonly This is OK too */
17+
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
18+
+!!! error TS1024: 'readonly' modifier can only appear on a property declaration or index signature.
19+
constructor() {
20+
/** ok */
21+
this.y = 2
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @checkJs: true
2+
// @noEmit: true
3+
// @filename: main.js
4+
5+
// https://github.com/microsoft/typescript-go/issues/4219
6+
7+
class C {
8+
/** @private */
9+
constructor() {}
10+
}
11+
new C();

0 commit comments

Comments
 (0)