Skip to content

Commit 3d99938

Browse files
committed
feat(typescript): 添加现代 TypeScript 特性测试
新增 TypeScript 4.9+、5.0+、5.2+ 等现代特性的测试用例,包括 satisfies 操作符、const 类型参数、auto-accessors、using 声明和 override 修饰符等。同时更新了词法和语法分析器以支持这些特性。
1 parent 9b5b094 commit 3d99938

3 files changed

Lines changed: 568 additions & 2 deletions

File tree

chapi-ast-typescript/src/main/antlr/TypeScriptLexer.g4

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,21 @@ Abstract: 'abstract';
224224

225225
Is: 'is';
226226

227+
// TypeScript 4.9+ keywords
228+
Satisfies: 'satisfies';
229+
Accessor: 'accessor';
230+
Override: 'override';
231+
232+
// TypeScript 5.0+ keywords
233+
Infer: 'infer';
234+
Asserts: 'asserts';
235+
236+
// TypeScript 5.2+ explicit resource management
237+
Using: 'using';
238+
239+
// TypeScript 5.4+ NoInfer utility type keyword (contextual)
240+
Out: 'out';
241+
227242
//
228243
// Ext.2 Additions to 1.8: Decorators
229244
//

chapi-ast-typescript/src/main/antlr/TypeScriptParser.g4

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ typeParameterList
6262

6363
typeParameter
6464
// Compatibility with old grammar: support both constraint and default type
65-
: identifierName constraint? ('=' type_)?
65+
// TypeScript 5.0+: const type parameters <const T>
66+
: Const? (In | Out)? identifierName constraint? ('=' type_)?
6667
| typeParameters
6768
;
6869

@@ -372,6 +373,7 @@ statement
372373
| generatorFunctionDeclaration
373374
| typeAliasDeclaration //ADDED
374375
| enumDeclaration //ADDED
376+
| usingStatement // TypeScript 5.2+ explicit resource management
375377
;
376378

377379
block
@@ -568,6 +570,11 @@ debuggerStatement
568570
: Debugger eos
569571
;
570572

573+
// TypeScript 5.2+ explicit resource management
574+
usingStatement
575+
: Await? Using identifier typeAnnotation? '=' singleExpression eos
576+
;
577+
571578
functionDeclaration
572579
: Async? Function_ '*'? identifierName callSignature (('{' functionBody '}') | SemiColon)
573580
;
@@ -617,10 +624,13 @@ propertyMemberDeclaration
617624
| propertyMemberBase propertyName callSignature (('{' functionBody '}') | SemiColon) # MethodDeclarationExpression
618625
| propertyMemberBase (getAccessor | setAccessor) # GetterSetterDeclarationExpression
619626
| abstractDeclaration # AbstractMemberDeclaration
627+
// TypeScript 4.9+ auto-accessors
628+
| propertyMemberBase Accessor propertyName ('?' | '!')? typeAnnotation? initializer? SemiColon # AutoAccessorDeclaration
620629
;
621630

622631
propertyMemberBase
623-
: accessibilityModifier? Async? Static? ReadOnly?
632+
// TypeScript 4.3+ override modifier
633+
: accessibilityModifier? Override? Async? Static? ReadOnly?
624634
;
625635

626636
indexMemberDeclaration
@@ -807,6 +817,8 @@ singleExpression
807817
| singleExpression As asExpression # CastAsExpression
808818
// TypeScript v2.0
809819
| singleExpression '!' # NonNullAssertionExpression
820+
// TypeScript v4.9+
821+
| singleExpression Satisfies type_ # SatisfiesExpression
810822
;
811823

812824
asExpression
@@ -929,6 +941,16 @@ identifier
929941
| Constructor
930942
| Namespace
931943
| Abstract
944+
// TypeScript 4.9+ contextual keywords
945+
| Satisfies
946+
| Accessor
947+
| Override
948+
// TypeScript 5.0+ contextual keywords
949+
| Infer
950+
| Asserts
951+
| Out
952+
// TypeScript 5.2+ contextual keywords
953+
| Using
932954
;
933955

934956
identifierOrKeyWord

0 commit comments

Comments
 (0)