Skip to content

Commit

Permalink
Merge pull request #546 from DanielXMoore/ts-readonly
Browse files Browse the repository at this point in the history
Fix readonly support in interfaces
  • Loading branch information
edemaine authored Jun 4, 2023
2 parents 8a7e35d + 1bf9ea1 commit 2cc4a0b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
14 changes: 10 additions & 4 deletions source/parser.hera
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ ClassSignatureElement
Static ClassSignatureBody

AccessModifier
( ( Public / Private / Protected ) __ )? ( Readonly __ )? ->
( ( Public / Private / Protected ) NotDedented )? ( Readonly NotDedented )? ->
if (!($1 || $2)) return $skip

return {
Expand Down Expand Up @@ -5617,7 +5617,7 @@ InterfaceProperty
MethodSignature InterfacePropertyDelimiter

BasicInterfaceProperty
( TypeIndexSignature / PropertyName ) TypeSuffix InterfacePropertyDelimiter
( TypeIndexSignature / TypeProperty ) _? TypeSuffix InterfacePropertyDelimiter

InterfacePropertyDelimiter
_? ( Semicolon / Comma )
Expand Down Expand Up @@ -5750,8 +5750,12 @@ EnumProperty
children: $0,
}

TypeProperty
( Readonly NotDedented )? PropertyName

TypeIndexSignature
( [+-]? "readonly" __ )? OpenBracket TypeIndex CloseBracket ( __ [+-] QuestionMark )?
# NOTE: QuestionMark will be parsed by following TypeSuffix
( [+-]? Readonly NotDedented )? OpenBracket TypeIndex CloseBracket ( __ [+-] &( _? QuestionMark ) )?

TypeIndex
__ Identifier TypeSuffix
Expand Down Expand Up @@ -5926,7 +5930,9 @@ InlineInterfaceLiteral
InsertInlineOpenBrace InlineBasicInterfaceProperty ( ( IndentedFurther / _? ) InlineBasicInterfaceProperty )* InsertCloseBrace

InlineBasicInterfaceProperty
( TypeIndexSignature / PropertyName ) QuestionMark? Colon Type InlineInterfacePropertyDelimiter
# NOTE: Not using TypeSuffix here to require a Colon, and to forbid spaces
# before the colon (to enable conditional types like prop ? T1 : T2).
( TypeIndexSignature / TypeProperty ) QuestionMark? Colon Type InlineInterfacePropertyDelimiter

InlineInterfacePropertyDelimiter
( _? Semicolon ) / CommaDelimiter
Expand Down
28 changes: 28 additions & 0 deletions test/types/interface.civet
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,34 @@ describe "[TS] interface", ->
}
"""

testCase """
spaces
---
interface ParseResult<T>
loc : Loc
pos ? : number
value:T
---
interface ParseResult<T> {
loc : Loc
pos ? : number
value:T
}
"""

testCase """
readonly
---
interface Point
readonly x: number
readonly y?: number
---
interface Point {
readonly x: number
readonly y?: number
}
"""

testCase """
callable
---
Expand Down
18 changes: 17 additions & 1 deletion test/types/type-declaration.civet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{testCase} from ../helper.civet
{testCase, throws} from ../helper.civet

describe "[TS] type declaration", ->
testCase """
Expand Down Expand Up @@ -239,6 +239,22 @@ describe "[TS] type declaration", ->
}
"""

throws """
mapped type modifiers need to modify something
---
type OptionsFlags<Type> = {
-readonly [Property in keyof Type]-: boolean
}
"""

throws """
mapped type modifiers need to modify something
---
type OptionsFlags<Type> = {
-[Property in keyof Type]-?: boolean
}
"""

testCase """
remapped type
---
Expand Down

0 comments on commit 2cc4a0b

Please sign in to comment.