-
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
Allow literal initializers of readonly properties in declaration files #26313
Merged
weswigham
merged 4 commits into
microsoft:master
from
weswigham:fix-declaration-type-widening
Sep 5, 2018
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7623c03
Allow literal initializers of readonly properties in declaration files
weswigham b0a8702
Merge branch 'master' into fix-declaration-type-widening
weswigham ec022ac
Merge branch 'master' into fix-declaration-type-widening
weswigham 399d9ae
Move some conditions a bit
weswigham 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
tests/cases/compiler/ambientErrors1.ts(1,15): error TS1039: Initializers are not allowed in ambient contexts. | ||
tests/cases/compiler/ambientErrors1.ts(1,17): error TS1039: Initializers are not allowed in ambient contexts. | ||
|
||
|
||
==== tests/cases/compiler/ambientErrors1.ts (1 errors) ==== | ||
declare var x = 4; | ||
~ | ||
~ | ||
!!! error TS1039: Initializers are not allowed in ambient contexts. |
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
20 changes: 10 additions & 10 deletions
20
tests/baselines/reference/constDeclarations-ambient-errors.errors.txt
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 |
---|---|---|
@@ -1,30 +1,30 @@ | ||
tests/cases/compiler/constDeclarations-ambient-errors.ts(2,27): error TS1039: Initializers are not allowed in ambient contexts. | ||
tests/cases/compiler/constDeclarations-ambient-errors.ts(3,26): error TS1039: Initializers are not allowed in ambient contexts. | ||
tests/cases/compiler/constDeclarations-ambient-errors.ts(2,29): error TS1039: Initializers are not allowed in ambient contexts. | ||
tests/cases/compiler/constDeclarations-ambient-errors.ts(3,28): error TS1039: Initializers are not allowed in ambient contexts. | ||
tests/cases/compiler/constDeclarations-ambient-errors.ts(4,20): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal. | ||
tests/cases/compiler/constDeclarations-ambient-errors.ts(4,37): error TS1039: Initializers are not allowed in ambient contexts. | ||
tests/cases/compiler/constDeclarations-ambient-errors.ts(4,51): error TS1039: Initializers are not allowed in ambient contexts. | ||
tests/cases/compiler/constDeclarations-ambient-errors.ts(8,22): error TS1039: Initializers are not allowed in ambient contexts. | ||
tests/cases/compiler/constDeclarations-ambient-errors.ts(4,39): error TS1039: Initializers are not allowed in ambient contexts. | ||
tests/cases/compiler/constDeclarations-ambient-errors.ts(4,53): error TS1039: Initializers are not allowed in ambient contexts. | ||
tests/cases/compiler/constDeclarations-ambient-errors.ts(8,24): error TS1039: Initializers are not allowed in ambient contexts. | ||
|
||
|
||
==== tests/cases/compiler/constDeclarations-ambient-errors.ts (6 errors) ==== | ||
// error: no intialization expected in ambient declarations | ||
declare const c1: boolean = true; | ||
~ | ||
~~~~ | ||
!!! error TS1039: Initializers are not allowed in ambient contexts. | ||
declare const c2: number = 0; | ||
~ | ||
~ | ||
!!! error TS1039: Initializers are not allowed in ambient contexts. | ||
declare const c3 = null, c4 :string = "", c5: any = 0; | ||
~~~~ | ||
!!! error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal. | ||
~ | ||
~~ | ||
!!! error TS1039: Initializers are not allowed in ambient contexts. | ||
~ | ||
~ | ||
!!! error TS1039: Initializers are not allowed in ambient contexts. | ||
|
||
declare module M { | ||
const c6 = 0; | ||
const c7: number = 7; | ||
~ | ||
~ | ||
!!! error TS1039: Initializers are not allowed in ambient contexts. | ||
} |
24 changes: 24 additions & 0 deletions
24
tests/baselines/reference/declarationEmitConstantNoWidening.js
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,24 @@ | ||
//// [declarationEmitConstantNoWidening.ts] | ||
export const FOO = 'FOO'; | ||
export class Bar { | ||
readonly type = FOO; // Should be widening literal "FOO" - so either `typeof "FOO"` or = "FOO" | ||
} | ||
|
||
//// [declarationEmitConstantNoWidening.js] | ||
"use strict"; | ||
exports.__esModule = true; | ||
exports.FOO = 'FOO'; | ||
var Bar = /** @class */ (function () { | ||
function Bar() { | ||
this.type = exports.FOO; // Should be widening literal "FOO" - so either `typeof "FOO"` or = "FOO" | ||
} | ||
return Bar; | ||
}()); | ||
exports.Bar = Bar; | ||
|
||
|
||
//// [declarationEmitConstantNoWidening.d.ts] | ||
export declare const FOO = "FOO"; | ||
export declare class Bar { | ||
readonly type = "FOO"; | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/baselines/reference/declarationEmitConstantNoWidening.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,11 @@ | ||
=== tests/cases/compiler/declarationEmitConstantNoWidening.ts === | ||
export const FOO = 'FOO'; | ||
>FOO : Symbol(FOO, Decl(declarationEmitConstantNoWidening.ts, 0, 12)) | ||
|
||
export class Bar { | ||
>Bar : Symbol(Bar, Decl(declarationEmitConstantNoWidening.ts, 0, 25)) | ||
|
||
readonly type = FOO; // Should be widening literal "FOO" - so either `typeof "FOO"` or = "FOO" | ||
>type : Symbol(Bar.type, Decl(declarationEmitConstantNoWidening.ts, 1, 18)) | ||
>FOO : Symbol(FOO, Decl(declarationEmitConstantNoWidening.ts, 0, 12)) | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/baselines/reference/declarationEmitConstantNoWidening.types
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,12 @@ | ||
=== tests/cases/compiler/declarationEmitConstantNoWidening.ts === | ||
export const FOO = 'FOO'; | ||
>FOO : "FOO" | ||
>'FOO' : "FOO" | ||
|
||
export class Bar { | ||
>Bar : Bar | ||
|
||
readonly type = FOO; // Should be widening literal "FOO" - so either `typeof "FOO"` or = "FOO" | ||
>type : "FOO" | ||
>FOO : "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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
tests/cases/compiler/externSemantics.ts(1,14): error TS1039: Initializers are not allowed in ambient contexts. | ||
tests/cases/compiler/externSemantics.ts(3,21): error TS1039: Initializers are not allowed in ambient contexts. | ||
tests/cases/compiler/externSemantics.ts(1,15): error TS1039: Initializers are not allowed in ambient contexts. | ||
tests/cases/compiler/externSemantics.ts(3,22): error TS1039: Initializers are not allowed in ambient contexts. | ||
|
||
|
||
==== tests/cases/compiler/externSemantics.ts (2 errors) ==== | ||
declare var x=10; | ||
~ | ||
~~ | ||
!!! error TS1039: Initializers are not allowed in ambient contexts. | ||
declare var v; | ||
declare var y:number=3; | ||
~ | ||
~ | ||
!!! error TS1039: Initializers are not allowed in ambient contexts. | ||
|
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
4 changes: 2 additions & 2 deletions
4
tests/baselines/reference/missingRequiredDeclare.d.errors.txt
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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
tests/cases/compiler/missingRequiredDeclare.d.ts(1,1): error TS1046: A 'declare' modifier is required for a top level declaration in a .d.ts file. | ||
tests/cases/compiler/missingRequiredDeclare.d.ts(1,7): error TS1039: Initializers are not allowed in ambient contexts. | ||
tests/cases/compiler/missingRequiredDeclare.d.ts(1,9): error TS1039: Initializers are not allowed in ambient contexts. | ||
|
||
|
||
==== tests/cases/compiler/missingRequiredDeclare.d.ts (2 errors) ==== | ||
var x = 1; | ||
~~~ | ||
!!! error TS1046: A 'declare' modifier is required for a top level declaration in a .d.ts file. | ||
~ | ||
~ | ||
!!! error TS1039: Initializers are not allowed in ambient contexts. |
4 changes: 2 additions & 2 deletions
4
tests/baselines/reference/parserModifierOnStatementInBlock2.errors.txt
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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/parserModifierOnStatementInBlock2.ts(2,4): error TS1184: Modifiers cannot appear here. | ||
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/parserModifierOnStatementInBlock2.ts(2,18): error TS1039: Initializers are not allowed in ambient contexts. | ||
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/parserModifierOnStatementInBlock2.ts(2,20): error TS1039: Initializers are not allowed in ambient contexts. | ||
|
||
|
||
==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/parserModifierOnStatementInBlock2.ts (2 errors) ==== | ||
{ | ||
declare var x = this; | ||
~~~~~~~ | ||
!!! error TS1184: Modifiers cannot appear here. | ||
~ | ||
~~~~ | ||
!!! error TS1039: Initializers are not allowed in ambient contexts. | ||
} | ||
|
4 changes: 2 additions & 2 deletions
4
tests/baselines/reference/parserVariableStatement1.d.errors.txt
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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
tests/cases/conformance/parser/ecmascript5/Statements/parserVariableStatement1.d.ts(1,1): error TS1046: A 'declare' modifier is required for a top level declaration in a .d.ts file. | ||
tests/cases/conformance/parser/ecmascript5/Statements/parserVariableStatement1.d.ts(1,7): error TS1039: Initializers are not allowed in ambient contexts. | ||
tests/cases/conformance/parser/ecmascript5/Statements/parserVariableStatement1.d.ts(1,9): error TS1039: Initializers are not allowed in ambient contexts. | ||
|
||
|
||
==== tests/cases/conformance/parser/ecmascript5/Statements/parserVariableStatement1.d.ts (2 errors) ==== | ||
var v = 1; | ||
~~~ | ||
!!! error TS1046: A 'declare' modifier is required for a top level declaration in a .d.ts file. | ||
~ | ||
~ | ||
!!! error TS1039: Initializers are not allowed in ambient contexts. |
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.
How is this readonly declaration?
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's not, the error span location was just normalized for the initializer forbidden error to always be on the initializer instead of sometimes on the initializer and sometimes on the equals token.
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.
@weswigham makes sense. Sorry somehow I perceived as error removed. My bad.