-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Improve error messages for property declarations #5559
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
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -1134,6 +1134,14 @@ namespace ts { | |
return token === t && tryParse(nextTokenCanFollowModifier); | ||
} | ||
|
||
function nextTokenIsOnSameLineAndCanFollowModifier() { | ||
nextToken(); | ||
if (scanner.hasPrecedingLineBreak()) { | ||
return false; | ||
} | ||
return canFollowModifier(); | ||
} | ||
|
||
function nextTokenCanFollowModifier() { | ||
if (token === SyntaxKind.ConstKeyword) { | ||
// 'const' is only a modifier if followed by 'enum'. | ||
|
@@ -1154,11 +1162,7 @@ namespace ts { | |
return canFollowModifier(); | ||
} | ||
|
||
nextToken(); | ||
if (scanner.hasPrecedingLineBreak()) { | ||
return false; | ||
} | ||
return canFollowModifier(); | ||
return nextTokenIsOnSameLineAndCanFollowModifier(); | ||
} | ||
|
||
function parseAnyContextualModifier(): boolean { | ||
|
@@ -4923,15 +4927,31 @@ namespace ts { | |
return decorators; | ||
} | ||
|
||
function parseModifiers(): ModifiersArray { | ||
/* | ||
* There are situations in which a modifier like 'const' will appear unexpectedly, such as on a class member. | ||
* In those situations, if we are entirely sure that 'const' is not valid on its own (such as when ASI takes effect | ||
* and turns it into a standalone declaration), then it is better to parse it and report an error later. | ||
* | ||
* In such situations, 'permitInvalidConstAsModifier' should be set to true. | ||
*/ | ||
function parseModifiers(permitInvalidConstAsModifier?: boolean): ModifiersArray { | ||
let flags = 0; | ||
let modifiers: ModifiersArray; | ||
while (true) { | ||
const modifierStart = scanner.getStartPos(); | ||
const modifierKind = token; | ||
|
||
if (!parseAnyContextualModifier()) { | ||
break; | ||
if (token === SyntaxKind.ConstKeyword && permitInvalidConstAsModifier) { | ||
// We need to ensure that any subsequent modifiers appear on the same line | ||
// so that when 'const' is a standalone declaration, we don't issue an error. | ||
if (!tryParse(nextTokenIsOnSameLineAndCanFollowModifier)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a comment above: // We need to ensure that any subsequent modifiers appear on the same line
// so that when 'const' is a standalone declaration, we don't issue an error. |
||
break; | ||
} | ||
} | ||
else { | ||
if (!parseAnyContextualModifier()) { | ||
break; | ||
} | ||
} | ||
|
||
if (!modifiers) { | ||
|
@@ -4976,7 +4996,7 @@ namespace ts { | |
|
||
const fullStart = getNodePos(); | ||
const decorators = parseDecorators(); | ||
const modifiers = parseModifiers(); | ||
const modifiers = parseModifiers(/*permitInvalidConstAsModifier*/ true); | ||
|
||
const accessor = tryParseAccessorDeclaration(fullStart, decorators, modifiers); | ||
if (accessor) { | ||
|
This file contains hidden or 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,23 @@ | ||
tests/cases/compiler/ClassDeclaration26.ts(2,22): error TS1005: ';' expected. | ||
tests/cases/compiler/ClassDeclaration26.ts(4,5): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected. | ||
tests/cases/compiler/ClassDeclaration26.ts(4,20): error TS1005: '=' expected. | ||
tests/cases/compiler/ClassDeclaration26.ts(4,23): error TS1005: '=>' expected. | ||
tests/cases/compiler/ClassDeclaration26.ts(5,1): error TS1128: Declaration or statement expected. | ||
|
||
|
||
==== tests/cases/compiler/ClassDeclaration26.ts (5 errors) ==== | ||
class C { | ||
public const var export foo = 10; | ||
~~~~~~ | ||
!!! error TS1005: ';' expected. | ||
|
||
var constructor() { } | ||
~~~ | ||
!!! error TS1068: Unexpected token. A constructor, method, accessor, or property was expected. | ||
~ | ||
!!! error TS1005: '=' expected. | ||
~ | ||
!!! error TS1005: '=>' expected. | ||
} | ||
~ | ||
!!! error TS1128: Declaration or statement expected. |
This file contains hidden or 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 @@ | ||
//// [ClassDeclaration26.ts] | ||
class C { | ||
public const var export foo = 10; | ||
|
||
var constructor() { } | ||
} | ||
|
||
//// [ClassDeclaration26.js] | ||
var C = (function () { | ||
function C() { | ||
this.foo = 10; | ||
} | ||
return C; | ||
})(); | ||
var constructor = function () { }; |
9 changes: 9 additions & 0 deletions
9
tests/baselines/reference/ClassDeclarationWithInvalidConstOnPropertyDeclaration.errors.txt
This file contains hidden or 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/ClassDeclarationWithInvalidConstOnPropertyDeclaration.ts(2,3): error TS1248: A class member cannot have the 'const' keyword. | ||
|
||
|
||
==== tests/cases/compiler/ClassDeclarationWithInvalidConstOnPropertyDeclaration.ts (1 errors) ==== | ||
class AtomicNumbers { | ||
static const H = 1; | ||
~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS1248: A class member cannot have the 'const' keyword. | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/baselines/reference/ClassDeclarationWithInvalidConstOnPropertyDeclaration.js
This file contains hidden or 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,12 @@ | ||
//// [ClassDeclarationWithInvalidConstOnPropertyDeclaration.ts] | ||
class AtomicNumbers { | ||
static const H = 1; | ||
} | ||
|
||
//// [ClassDeclarationWithInvalidConstOnPropertyDeclaration.js] | ||
var AtomicNumbers = (function () { | ||
function AtomicNumbers() { | ||
} | ||
AtomicNumbers.H = 1; | ||
return AtomicNumbers; | ||
})(); |
13 changes: 13 additions & 0 deletions
13
tests/baselines/reference/ClassDeclarationWithInvalidConstOnPropertyDeclaration2.js
This file contains hidden or 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,13 @@ | ||
//// [ClassDeclarationWithInvalidConstOnPropertyDeclaration2.ts] | ||
class C { | ||
const | ||
x = 10; | ||
} | ||
|
||
//// [ClassDeclarationWithInvalidConstOnPropertyDeclaration2.js] | ||
var C = (function () { | ||
function C() { | ||
this.x = 10; | ||
} | ||
return C; | ||
})(); |
10 changes: 10 additions & 0 deletions
10
tests/baselines/reference/ClassDeclarationWithInvalidConstOnPropertyDeclaration2.symbols
This file contains hidden or 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,10 @@ | ||
=== tests/cases/compiler/ClassDeclarationWithInvalidConstOnPropertyDeclaration2.ts === | ||
class C { | ||
>C : Symbol(C, Decl(ClassDeclarationWithInvalidConstOnPropertyDeclaration2.ts, 0, 0)) | ||
|
||
const | ||
>const : Symbol(const, Decl(ClassDeclarationWithInvalidConstOnPropertyDeclaration2.ts, 0, 9)) | ||
|
||
x = 10; | ||
>x : Symbol(x, Decl(ClassDeclarationWithInvalidConstOnPropertyDeclaration2.ts, 1, 9)) | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/baselines/reference/ClassDeclarationWithInvalidConstOnPropertyDeclaration2.types
This file contains hidden or 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/ClassDeclarationWithInvalidConstOnPropertyDeclaration2.ts === | ||
class C { | ||
>C : C | ||
|
||
const | ||
>const : any | ||
|
||
x = 10; | ||
>x : number | ||
>10 : number | ||
} |
This file contains hidden or 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,5 @@ | ||
class C { | ||
public const var export foo = 10; | ||
|
||
var constructor() { } | ||
} |
3 changes: 3 additions & 0 deletions
3
tests/cases/compiler/ClassDeclarationWithInvalidConstOnPropertyDeclaration.ts
This file contains hidden or 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,3 @@ | ||
class AtomicNumbers { | ||
static const H = 1; | ||
} |
4 changes: 4 additions & 0 deletions
4
tests/cases/compiler/ClassDeclarationWithInvalidConstOnPropertyDeclaration2.ts
This file contains hidden or 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,4 @@ | ||
class C { | ||
const | ||
x = 10; | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -1,12 +1,14 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////export const class C { | ||
//// private static c/*1*/onst foo; | ||
//// constructor(public con/*2*/st foo) { | ||
//// private static c/*1*/onst f/*2*/oo; | ||
//// constructor(public con/*3*/st foo) { | ||
//// } | ||
////} | ||
|
||
goTo.marker("1"); | ||
verify.occurrencesAtPositionCount(1); | ||
verify.occurrencesAtPositionCount(0); | ||
goTo.marker("2"); | ||
verify.occurrencesAtPositionCount(1); | ||
goTo.marker("3"); | ||
verify.occurrencesAtPositionCount(0); |
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.
So we have nextTokenIsOnSameLineAndCanFollowModifier and nextTokenCanFollowModifier...
These are supremely easy to confuse. When would i want one, versus the other?
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.
It was
nextTokenCanFollowModifierWithoutGrammarRules
before. It seemed more clear to me. Basically, I just neednextTokenCanFollowModifier
without the leading checks. I can think of several solutions here:nextTokenIsOnSameLineAndCanFollowModifier
toparseModifiers
(It leads to code duplication).nextTokenIsOnSameLineAndCanFollowModifier
and use it innextTokenCanFollowModifier
as now.tryParse
. It seems like a big change becausetryParse
is a generic function that is used everywhere.The option 2 seems reasonable overall.
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.
But why do some people needs checks, and others do not? That's the part I find confusing. How would I know from my code?
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.
Well, to be frank, I don't know why the if statements with
SyntaxKind
in the following code are not a part ofchecker.ts
file:That way, one would always have all modifiers for a property or a class (possibly something else?) and they would be checked in
checker.ts
if they are correct. Then there would be no need for special handling ofconst
which now seems a bit like a hack. But maybe I'm missing something because I don't know have deep knowledge of the compiler.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.
I think what Cyrus is sort of getting at is that more than anything else, you should be prescriptive and document why this function exists and where it should be used.