From 33a35b04569a8e16fb73d24829b12ce5b4542e4f Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Tue, 15 Nov 2022 20:52:47 +0800 Subject: [PATCH] fix(ts): add numberic support --- .../src/main/antlr/TypeScriptLexer.g4 | 17 +- .../src/main/antlr/TypeScriptLexer.tokens | 276 ------------------ .../typescriptast/TypeScriptAnalyserTest.kt | 2 +- .../typescriptast/TypeScriptRegressionTest.kt | 20 +- 4 files changed, 31 insertions(+), 284 deletions(-) delete mode 100644 chapi-ast-typescript/src/main/antlr/TypeScriptLexer.tokens diff --git a/chapi-ast-typescript/src/main/antlr/TypeScriptLexer.g4 b/chapi-ast-typescript/src/main/antlr/TypeScriptLexer.g4 index 5a8ad493..f90bc6ec 100644 --- a/chapi-ast-typescript/src/main/antlr/TypeScriptLexer.g4 +++ b/chapi-ast-typescript/src/main/antlr/TypeScriptLexer.g4 @@ -81,17 +81,22 @@ BooleanLiteral: 'true' /// Numeric Literals -DecimalLiteral: DecimalIntegerLiteral '.' [0-9]* ExponentPart? - | '.' [0-9]+ ExponentPart? +DecimalLiteral: DecimalIntegerLiteral '.' [0-9] [0-9_]* ExponentPart? + | '.' [0-9] [0-9_]* ExponentPart? | DecimalIntegerLiteral ExponentPart? ; /// Numeric Literals -HexIntegerLiteral: '0' [xX] HexDigit+; +HexIntegerLiteral: '0' [xX] [0-9a-fA-F] HexDigit*; OctalIntegerLiteral: '0' [0-7]+ {!this.IsStrictMode()}?; -OctalIntegerLiteral2: '0' [oO] [0-7]+; -BinaryIntegerLiteral: '0' [bB] [01]+; +OctalIntegerLiteral2: '0' [oO] [0-7] [_0-7]*; +BinaryIntegerLiteral: '0' [bB] [01] [_01]*; + +BigHexIntegerLiteral: '0' [xX] [0-9a-fA-F] HexDigit* 'n'; +BigOctalIntegerLiteral: '0' [oO] [0-7] [_0-7]* 'n'; +BigBinaryIntegerLiteral: '0' [bB] [01] [_01]* 'n'; +BigDecimalIntegerLiteral: DecimalIntegerLiteral 'n'; /// Keywords @@ -376,7 +381,7 @@ fragment HexDigit fragment DecimalIntegerLiteral : '0' - | [1-9] [0-9]* + | [1-9] [0-9_]* ; fragment ExponentPart diff --git a/chapi-ast-typescript/src/main/antlr/TypeScriptLexer.tokens b/chapi-ast-typescript/src/main/antlr/TypeScriptLexer.tokens deleted file mode 100644 index 6868eae4..00000000 --- a/chapi-ast-typescript/src/main/antlr/TypeScriptLexer.tokens +++ /dev/null @@ -1,276 +0,0 @@ -JsxComment=1 -MultiLineComment=2 -SingleLineComment=3 -RegularExpressionLiteral=4 -OpenBracket=5 -CloseBracket=6 -OpenParen=7 -CloseParen=8 -OpenBrace=9 -TemplateCloseBrace=10 -CloseBrace=11 -SemiColon=12 -Comma=13 -Assign=14 -QuestionMark=15 -Colon=16 -Ellipsis=17 -Dot=18 -PlusPlus=19 -MinusMinus=20 -Plus=21 -Minus=22 -BitNot=23 -Not=24 -Multiply=25 -Divide=26 -Modulus=27 -Power=28 -NullCoalesce=29 -Hashtag=30 -RightShiftArithmetic=31 -LeftShiftArithmetic=32 -RightShiftLogical=33 -LessThan=34 -MoreThan=35 -LessThanEquals=36 -GreaterThanEquals=37 -Equals_=38 -NotEquals=39 -IdentityEquals=40 -IdentityNotEquals=41 -BitAnd=42 -BitXOr=43 -BitOr=44 -And=45 -Or=46 -MultiplyAssign=47 -DivideAssign=48 -ModulusAssign=49 -PlusAssign=50 -MinusAssign=51 -LeftShiftArithmeticAssign=52 -RightShiftArithmeticAssign=53 -RightShiftLogicalAssign=54 -BitAndAssign=55 -BitXorAssign=56 -BitOrAssign=57 -ARROW=58 -Lodash=59 -Dollar=60 -NullLiteral=61 -BooleanLiteral=62 -DecimalLiteral=63 -HexIntegerLiteral=64 -OctalIntegerLiteral=65 -OctalIntegerLiteral2=66 -BinaryIntegerLiteral=67 -Break=68 -Do=69 -Instanceof=70 -Typeof=71 -Keyof=72 -Case=73 -Else=74 -New=75 -Var=76 -Catch=77 -Finally=78 -Return=79 -Void=80 -Continue=81 -For=82 -Switch=83 -While=84 -Debugger=85 -Function_=86 -This=87 -With=88 -Default=89 -If=90 -Throw=91 -Delete=92 -In=93 -Try=94 -As=95 -From=96 -ReadOnly=97 -Async=98 -Of=99 -Await=100 -Class=101 -Enum=102 -Extends=103 -Super=104 -Const=105 -Export=106 -Import=107 -Implements=108 -Let=109 -Private=110 -Public=111 -Interface=112 -Package=113 -Protected=114 -Static=115 -Yield=116 -Any=117 -Number=118 -Boolean=119 -String=120 -Symbol=121 -TypeAlias=122 -Get=123 -Set=124 -Constructor=125 -Namespace=126 -Require=127 -Module=128 -Declare=129 -Unknown=130 -Undefined=131 -Abstract=132 -Is=133 -At=134 -Identifier=135 -StringLiteral=136 -BackTick=137 -WhiteSpaces=138 -LineTerminator=139 -HtmlComment=140 -CDataComment=141 -UnexpectedCharacter=142 -TemplateStringStartExpression=143 -TemplateStringAtom=144 -TagOpen=145 -TagClose=146 -TagSlashClose=147 -TagSlash=148 -TagName=149 -AttributeValue=150 -Attribute=151 -'['=5 -']'=6 -'('=7 -')'=8 -'{'=9 -'}'=11 -';'=12 -','=13 -'='=14 -'?'=15 -':'=16 -'...'=17 -'.'=18 -'++'=19 -'--'=20 -'+'=21 -'-'=22 -'~'=23 -'!'=24 -'*'=25 -'/'=26 -'%'=27 -'**'=28 -'??'=29 -'#'=30 -'>>'=31 -'<<'=32 -'>>>'=33 -'<'=34 -'>'=35 -'<='=36 -'>='=37 -'=='=38 -'!='=39 -'==='=40 -'!=='=41 -'&'=42 -'^'=43 -'|'=44 -'&&'=45 -'||'=46 -'*='=47 -'/='=48 -'%='=49 -'+='=50 -'-='=51 -'<<='=52 -'>>='=53 -'>>>='=54 -'&='=55 -'^='=56 -'|='=57 -'=>'=58 -'_'=59 -'$'=60 -'null'=61 -'break'=68 -'do'=69 -'instanceof'=70 -'typeof'=71 -'keyof'=72 -'case'=73 -'else'=74 -'new'=75 -'var'=76 -'catch'=77 -'finally'=78 -'return'=79 -'void'=80 -'continue'=81 -'for'=82 -'switch'=83 -'while'=84 -'debugger'=85 -'function'=86 -'this'=87 -'with'=88 -'default'=89 -'if'=90 -'throw'=91 -'delete'=92 -'in'=93 -'try'=94 -'as'=95 -'from'=96 -'readonly'=97 -'async'=98 -'of'=99 -'await'=100 -'class'=101 -'enum'=102 -'extends'=103 -'super'=104 -'const'=105 -'export'=106 -'import'=107 -'implements'=108 -'let'=109 -'private'=110 -'public'=111 -'interface'=112 -'package'=113 -'protected'=114 -'static'=115 -'yield'=116 -'any'=117 -'number'=118 -'boolean'=119 -'string'=120 -'symbol'=121 -'type'=122 -'get'=123 -'set'=124 -'constructor'=125 -'namespace'=126 -'require'=127 -'module'=128 -'declare'=129 -'unknown'=130 -'undefined'=131 -'abstract'=132 -'is'=133 -'@'=134 -'${'=143 -'/>'=147 diff --git a/chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptAnalyserTest.kt b/chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptAnalyserTest.kt index efba6fa0..f05ece91 100644 --- a/chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptAnalyserTest.kt +++ b/chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptAnalyserTest.kt @@ -70,7 +70,7 @@ class TypeScriptAnalyserTest { } @Test -// @Disabled + @Disabled fun someBug() { val dir = File("/Users/phodal/bug-ui-system") dir.walkTopDown().forEach { diff --git a/chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptRegressionTest.kt b/chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptRegressionTest.kt index c17a3cd0..6081baa4 100644 --- a/chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptRegressionTest.kt +++ b/chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptRegressionTest.kt @@ -103,7 +103,25 @@ export class DemoComponent implements OnInit, ControlValueAccessor { val code = """export type Exact = { [K in keyof T]: T[K] };""" TypeScriptAnalyser().analysis(code, "index.tsx") - val code2 = """export type MakeOptional = Omit & { [SubKey in K]?: Maybe };""" + val code2 = + """export type MakeOptional = Omit & { [SubKey in K]?: Maybe };""" TypeScriptAnalyser().analysis(code2, "index.tsx") } + + @Test + fun numeric_separators() { + val code = """if (+value > 1_000_000_000) { + }""" + + TypeScriptAnalyser().analysis(code, "index.tsx") + } + + @Test + fun annotated_in_constructor() { + val code = """export class DemoComponent implements OnInit { + constructor(@Optional() @Inject(GROUP) private group: Component) {} +}""" + + TypeScriptAnalyser().analysis(code, "index.tsx") + } }