diff --git a/syntax/basic/type.vim b/syntax/basic/type.vim index d2a24f3d..86208ef6 100644 --- a/syntax/basic/type.vim +++ b/syntax/basic/type.vim @@ -29,7 +29,7 @@ syntax region typescriptTypeArguments matchgroup=typescriptTypeBrackets \ start=/\>/ \ contains=@typescriptType \ nextgroup=typescriptFuncCallArg,@typescriptTypeOperator - \ contained skipwhite + \ contained skipwhite skipempty syntax cluster typescriptType contains= @@ -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, @@ -55,18 +57,42 @@ 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 @@ -74,10 +100,16 @@ syntax region typescriptTemplateSubstitutionType matchgroup=typescriptTemplateS \ 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*\)*/ @@ -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 @@ -212,3 +251,8 @@ syntax region typescriptAliasDeclaration matchgroup=typescriptUnion syntax keyword typescriptReadonlyArrayKeyword readonly \ nextgroup=@typescriptPrimaryType \ skipwhite + +syntax keyword typescriptInferTypeKeyword infer + \ contained + \ nextgroup=typescriptTypeReference + \ skipwhite diff --git a/syntax/common.vim b/syntax/common.vim index 5f493a7a..0bb88896 100644 --- a/syntax/common.vim +++ b/syntax/common.vim @@ -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 @@ -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 @@ -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 diff --git a/test/syntax.vader b/test/syntax.vader index ba763649..27334dcb 100644 --- a/test/syntax.vader +++ b/test/syntax.vader @@ -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[] { let a = 123 @@ -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): @@ -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; @@ -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 + : 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 ? 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) diff --git a/test/test.ts b/test/test.ts index 713bb8eb..2417d986 100644 --- a/test/test.ts +++ b/test/test.ts @@ -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 \ No newline at end of file +type X = Y extends string ? string : Y