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

Improved type syntax #205

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
58 changes: 51 additions & 7 deletions syntax/basic/type.vim
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ syntax region typescriptTypeArguments matchgroup=typescriptTypeBrackets
\ start=/\></ end=/>/
\ contains=@typescriptType
\ nextgroup=typescriptFuncCallArg,@typescriptTypeOperator
\ contained skipwhite
\ contained skipwhite skipempty


syntax cluster typescriptType contains=
Expand All @@ -42,11 +42,13 @@ syntax cluster typescriptType contains=
" type indexing A['key']
syntax region typescriptTypeBracket contained
\ start=/\[/ end=/\]/
\ contains=typescriptString,typescriptNumber
\ contains=typescriptStringLiteralType,typescriptNumericLiteralType,typescriptTypeReference
\ nextgroup=@typescriptTypeOperator
\ skipwhite skipempty

syntax cluster typescriptPrimaryType contains=
\ typescriptCommentBeforeType,
\ typescriptLineCommentBeforeType,
\ typescriptParenthesizedType,
\ typescriptPredefinedType,
\ typescriptTypeReference,
Expand All @@ -55,29 +57,59 @@ syntax cluster typescriptPrimaryType contains=
\ typescriptTypeQuery,
\ typescriptStringLiteralType,
\ typescriptTemplateLiteralType,
\ typescriptNumericLiteralType,
\ typescriptReadonlyArrayKeyword,
\ typescriptInferTypeKeyword,
\ typescriptAssertType

syntax match typescriptLineCommentBeforeType "//.*"
\ contained
\ nextgroup=@typescriptPrimaryType
\ skipempty skipwhite

syntax region typescriptCommentBeforeType
\ start="/\*" end="\*/"
\ contained
\ nextgroup=@typescriptPrimaryType
\ skipempty skipwhite

syntax match typescriptLineCommentBeforeTypeOperator "//.*"
\ contained
\ nextgroup=@typescriptTypeOperator
\ skipempty skipwhite

syntax region typescriptCommentBeforeTypeOperator
\ start="/\*" end="\*/"
\ contained
\ nextgroup=@typescriptTypeOperator
\ skipempty skipwhite

syntax region typescriptStringLiteralType contained
\ start=/\z(["']\)/ skip=/\\\\\|\\\z1\|\\\n/ end=/\z1\|$/
\ nextgroup=typescriptUnion
\ nextgroup=@typescriptTypeOperator
\ skipwhite skipempty

syntax region typescriptTemplateLiteralType contained
\ start=/`/ skip=/\\\\\|\\`\|\n/ end=/`\|$/
\ contains=typescriptTemplateSubstitutionType
\ nextgroup=typescriptTypeOperator
\ nextgroup=@typescriptTypeOperator
\ skipwhite skipempty

syntax region typescriptTemplateSubstitutionType matchgroup=typescriptTemplateSB
\ start=/\${/ end=/}/
\ contains=@typescriptType
\ contained

syntax match typescriptNumericLiteralType /\<0[bB][01][01_]*\>/ nextgroup=@typescriptTypeOperator skipwhite skipempty contained
syntax match typescriptNumericLiteralType /\<0[oO][0-7][0-7_]*\>/ nextgroup=@typescriptTypeOperator skipwhite skipempty contained
syntax match typescriptNumericLiteralType /\<0[xX][0-9a-fA-F][0-9a-fA-F_]*\>/ nextgroup=@typescriptTypeOperator skipwhite skipempty contained
syntax match typescriptNumericLiteralType /\<\%(\d[0-9_]*\%(\.\d[0-9_]*\)\=\|\.\d[0-9_]*\)\%([eE][+-]\=\d[0-9_]*\)\=\>/
\ nextgroup=@typescriptTypeOperator skipwhite skipempty contained

syntax region typescriptParenthesizedType matchgroup=typescriptParens
\ start=/(/ end=/)/
\ contains=@typescriptType
\ nextgroup=@typescriptTypeOperator
\ nextgroup=@typescriptTypeOperator,typescriptFuncTypeArrow
\ contained skipwhite skipempty fold

syntax match typescriptTypeReference /\K\k*\(\.\K\k*\)*/
Expand Down Expand Up @@ -110,10 +142,17 @@ syntax match typescriptTupleLable /\K\k*?\?:/
syntax region typescriptTupleType matchgroup=typescriptBraces
\ start=/\[/ end=/\]/
\ contains=@typescriptType,@typescriptComments,typescriptRestOrSpread,typescriptTupleLable
\ contained skipwhite
\ nextgroup=@typescriptTypeOperator
\ contained skipwhite skipempty

syntax cluster typescriptTypeOperator
\ contains=typescriptUnion,typescriptTypeBracket,typescriptConstraint,typescriptConditionalType
\ contains=
\ typescriptCommentBeforeTypeOperator,
\ typescriptLineCommentBeforeTypeOperator,
\ typescriptUnion,
\ typescriptTypeBracket,
\ typescriptConstraint,
\ typescriptConditionalType

syntax match typescriptUnion /|\|&/ contained nextgroup=@typescriptPrimaryType skipwhite skipempty

Expand Down Expand Up @@ -212,3 +251,8 @@ syntax region typescriptAliasDeclaration matchgroup=typescriptUnion
syntax keyword typescriptReadonlyArrayKeyword readonly
\ nextgroup=@typescriptPrimaryType
\ skipwhite

syntax keyword typescriptInferTypeKeyword infer
\ contained
\ nextgroup=typescriptTypeReference
\ skipwhite
6 changes: 6 additions & 0 deletions syntax/common.vim
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ if exists("did_typescript_hilink")
HiLink typescriptDocComment Comment
HiLink typescriptCommentTodo Todo
HiLink typescriptMagicComment SpecialComment
HiLink typescriptCommentBeforeType Comment
HiLink typescriptLineCommentBeforeType Comment
HiLink typescriptCommentBeforeTypeOperator Comment
HiLink typescriptLineCommentBeforeTypeOperator Comment
HiLink typescriptRef Include
HiLink typescriptDocNotation SpecialComment
HiLink typescriptDocTags SpecialComment
Expand All @@ -70,6 +74,7 @@ if exists("did_typescript_hilink")
HiLink typescriptSpecial Special
HiLink typescriptStringLiteralType String
HiLink typescriptTemplateLiteralType String
HiLink typescriptNumericLiteralType Number
HiLink typescriptStringMember String
HiLink typescriptTemplate String
HiLink typescriptEventString String
Expand Down Expand Up @@ -146,6 +151,7 @@ if exists("did_typescript_hilink")
HiLink typescriptConstraint Keyword
HiLink typescriptPredefinedType Type
HiLink typescriptReadonlyArrayKeyword Keyword
HiLink typescriptInferTypeKeyword Keyword
HiLink typescriptUnion Operator
HiLink typescriptFuncTypeArrow Function
HiLink typescriptConstructorType Function
Expand Down
86 changes: 83 additions & 3 deletions test/syntax.vader
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,13 @@ Given typescript (string literal type):
Execute:
AssertEqual 'typescriptPredefinedType', SyntaxAt(2, 23)

Given typescript (numeric literal type):
type T = 1 | string
Execute:
AssertEqual 'typescriptNumericLiteralType', SyntaxAt(1, 10)
AssertEqual 'typescriptUnion', SyntaxAt(1, 12)
AssertEqual 'typescriptPredefinedType', SyntaxAt(1, 14)

Given typescript (array return type):
function prop<T>(t: T): T[] {
let a = 123
Expand Down Expand Up @@ -576,7 +583,7 @@ Given typescript (multiline tuple type):
/* Name */ string,
]
Execute:
AssertEqual 'typescriptComment', SyntaxAt(2, 8)
AssertEqual 'typescriptCommentBeforeType', SyntaxAt(2, 8)
AssertEqual 'typescriptPredefinedType', SyntaxAt(2, 18)

Given typescript (assertion functions for nullish assertion):
Expand Down Expand Up @@ -764,6 +771,11 @@ Given typescript (variadic tuple type):
Execute:
AssertEqual 'typescriptRestOrSpread', SyntaxAt(1, 54)

Given typescript (type operator after tuple):
type T = [U] | V
Execute:
AssertEqual 'typescriptUnion', SyntaxAt(1, 14)

Given typescript (array destructuring variable declaration):
const [foo, [bar, baz = 1], ...rest] = arr;
let [foo, [bar, baz], ...rest] = arr;
Expand Down Expand Up @@ -867,7 +879,75 @@ Given typescript (conditional types):
Execute:
AssertEqual 'typescriptConstraint', SyntaxAt(1, 12)
AssertEqual 'typescriptPredefinedType', SyntaxAt(1, 20)
AssertEqual 'typescriptConditional', SyntaxAt(1, 27)
AssertEqual 'typescriptConditionalType', SyntaxAt(1, 27)
AssertEqual 'typescriptPredefinedType', SyntaxAt(1, 29)
AssertEqual 'typescriptConditional', SyntaxAt(1, 36)
AssertEqual 'typescriptConditionalType', SyntaxAt(1, 36)
AssertEqual 'typescriptTypeReference', SyntaxAt(1, 38)

Given typescript (indexed access operator for types):
type T = X[1]
type T = X['foo']
type T = X[K]
type T = X[1 | 2][3]
Execute:
AssertEqual 'typescriptTypeBracket', SyntaxAt(1, 11)
AssertEqual 'typescriptNumericLiteralType', SyntaxAt(1, 12)
AssertEqual 'typescriptStringLiteralType', SyntaxAt(2, 12)
AssertEqual 'typescriptTypeReference', SyntaxAt(3, 12)
AssertEqual 'typescriptUnion', SyntaxAt(4, 14)
AssertEqual 'typescriptNumericLiteralType', SyntaxAt(4, 19)

Given typescript (types with newline):
type T = A extends B
? 'foo'
: A extends C
? 123
: A extends D
? Foo<X>
: A extends E
? {a: number}
: A extends F
? (A)
: A extends G
? `foo${A}`
: A extends H
? [A]
: never
var a;
Execute:
AssertEqual 'typescriptStringLiteralType', SyntaxAt(2, 5)
AssertEqual 'typescriptNumericLiteralType', SyntaxAt(4, 5)
AssertEqual 'typescriptTypeReference', SyntaxAt(6, 5)
AssertEqual 'typescriptPredefinedType', SyntaxAt(8, 9)
AssertEqual 'typescriptTypeReference', SyntaxAt(10, 6)
AssertEqual 'typescriptTemplateLiteralType', SyntaxAt(12, 5)
AssertEqual 'typescriptTypeReference', SyntaxAt(14, 6)
AssertEqual 'typescriptPredefinedType', SyntaxAt(15, 5)
AssertEqual 'typescriptVariable', SyntaxAt(16, 1)

Given typescript (infer keyword):
type T = A extends F<infer B> ? C : D
Execute:
AssertEqual 'typescriptInferTypeKeyword', SyntaxAt(1, 22)

Given typescript (function type with newline):
type T = (
arg: A
) => B
var a
Execute:
" This is not precise(`arg` should typescriptFuncType) but better than before.
" And more precise analysis is difficult
AssertEqual 'typescriptTypeReference', SyntaxAt(2, 3)
AssertEqual 'typescriptTypeReference', SyntaxAt(3, 6)
AssertEqual 'typescriptVariable', SyntaxAt(4, 1)

Given typescript (comment in type):
type A =
// A is X
X
/* ... or Y */
| B
Execute:
AssertEqual 'typescriptTypeReference', SyntaxAt(3, 3)
AssertEqual 'typescriptTypeReference', SyntaxAt(5, 5)
2 changes: 1 addition & 1 deletion test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,4 @@ const {
const {foo: [a, {b, 'bar': d}, ...x], ...y} = obj;

type Greeting = `hello ${World}`;
type X = Y extends string ? string : Y
type X = Y extends string ? string : Y