Repository: alecthomas/chroma
Pull request #375 (2020-06-24 00:33:52)

Fix TypeScript keywords
Many of the highlighted keywords for TypeScript were just taken directly from Java, and many modern TS keywords were missing altogether. This would make the following changes to the TypeScript keywords:

- strict mode reserved keywords to highlight:
    - `yield`
    - `package`
- contextual keywords to highlight:
    - Keyword:
        - `asserts`
        - `await`
        - `infer`
        - `is`
        - `keyof`
        - `of`
    - KeywordReserved:
        - `async`
        - `get`
        - `namespace`
        - `readonly`
        - `require`
        - `set`
        - `type`
        - `from`
        - `global`
    - KeywordType:
        - `any`
        - `never`
        - `object`
        - `symbol`
        - `unique`
        - `unknown`
        - `bigint`
- stop highlighting:
    - `AS`
    - `char`
    - `byte`
    - `short`
    - `int`
    - `float`
    - `long`
    - `double`
    - `synchronized`
    - `throws`
    - `transient`
    - `volatile`
    - `native`
    - `sun`
    - `netscape`


The list of keywords was taken [directly from the TypeScript compiler source](https://github.com/microsoft/TypeScript/blob/fbd1c958e5c913120e6870022e5bd373870d4db2/src/compiler/types.ts#L111), since there doesn't seem to be an up-to-date list anywhere else.

I tried to be conservative in removing keywords, but hopefully I didn't miss any unnecessary ones.

Any feedback is welcome!


Patch:
diff --git a/lexers/t/typescript.go b/lexers/t/typescript.go
index 3b425ed..5205e01 100644
--- a/lexers/t/typescript.go
+++ b/lexers/t/typescript.go
@@ -38,14 +38,14 @@ var TypeScript = internal.Register(MustNewLexer(
 			{`\+\+|--|~|&&|\?|:|\|\||\\(?=\n)|(<<|>>>?|==?|!=?|[-<>+*%&|^/])=?`, Operator, Push("slashstartsregex")},
 			{`[{(\[;,]`, Punctuation, Push("slashstartsregex")},
 			{`[})\].]`, Punctuation, nil},
-			{`(for|in|while|do|break|return|continue|switch|case|default|if|else|throw|try|catch|finally|new|delete|typeof|instanceof|void|this)\b`, Keyword, Push("slashstartsregex")},
+			{`(for|in|of|while|do|break|return|yield|continue|switch|case|default|if|else|throw|try|catch|finally|new|delete|typeof|instanceof|keyof|asserts|is|infer|await|void|this)\b`, Keyword, Push("slashstartsregex")},
 			{`(var|let|with|function)\b`, KeywordDeclaration, Push("slashstartsregex")},
-			{`(abstract|boolean|byte|char|class|const|debugger|double|enum|export|extends|final|float|goto|implements|import|int|interface|long|native|package|private|protected|public|short|static|super|synchronized|throws|transient|volatile)\b`, KeywordReserved, nil},
+			{`(abstract|async|boolean|class|const|debugger|enum|export|extends|from|get|global|goto|implements|import|interface|namespace|package|private|protected|public|readonly|require|set|static|super|type)\b`, KeywordReserved, nil},
 			{`(true|false|null|NaN|Infinity|undefined)\b`, KeywordConstant, nil},
-			{`(Array|Boolean|Date|Error|Function|Math|netscape|Number|Object|Packages|RegExp|String|sun|decodeURI|decodeURIComponent|encodeURI|encodeURIComponent|Error|eval|isFinite|isNaN|parseFloat|parseInt|document|this|window)\b`, NameBuiltin, nil},
+			{`(Array|Boolean|Date|Error|Function|Math|Number|Object|Packages|RegExp|String|decodeURI|decodeURIComponent|encodeURI|encodeURIComponent|eval|isFinite|isNaN|parseFloat|parseInt|document|this|window)\b`, NameBuiltin, nil},
 			{`\b(module)(\s*)(\s*[\w?.$][\w?.$]*)(\s*)`, ByGroups(KeywordReserved, Text, NameOther, Text), Push("slashstartsregex")},
-			{`\b(string|bool|number)\b`, KeywordType, nil},
-			{`\b(constructor|declare|interface|as|AS)\b`, KeywordReserved, nil},
+			{`\b(string|bool|number|any|never|object|symbol|unique|unknown|bigint)\b`, KeywordType, nil},
+			{`\b(constructor|declare|interface|as)\b`, KeywordReserved, nil},
 			{`(super)(\s*)(\([\w,?.$\s]+\s*\))`, ByGroups(KeywordReserved, Text), Push("slashstartsregex")},
 			{`([a-zA-Z_?.$][\w?.$]*)\(\) \{`, NameOther, Push("slashstartsregex")},
 			{`([\w?.$][\w?.$]*)(\s*:\s*)([\w?.$][\w?.$]*)`, ByGroups(NameOther, Text, KeywordType), nil},


Test patch:
diff --git a/lexers/testdata/tsx.expected b/lexers/testdata/tsx.expected
index 69acfc4..c498ff3 100644
--- a/lexers/testdata/tsx.expected
+++ b/lexers/testdata/tsx.expected
@@ -3,7 +3,7 @@
   {"type":"Text","value":" "},
   {"type":"NameOther","value":"React"},
   {"type":"Text","value":" "},
-  {"type":"NameOther","value":"from"},
+  {"type":"KeywordReserved","value":"from"},
   {"type":"Text","value":" "},
   {"type":"LiteralStringSingle","value":"'react'"},
   {"type":"Text","value":"\n"},
@@ -11,7 +11,7 @@
   {"type":"Text","value":" "},
   {"type":"NameOther","value":"ReactDOM"},
   {"type":"Text","value":" "},
-  {"type":"NameOther","value":"from"},
+  {"type":"KeywordReserved","value":"from"},
   {"type":"Text","value":" "},
   {"type":"LiteralStringSingle","value":"'react-dom'"},
   {"type":"Text","value":"\n"},
@@ -27,7 +27,7 @@
   {"type":"Text","value":" "},
   {"type":"Punctuation","value":"}"},
   {"type":"Text","value":" "},
-  {"type":"NameOther","value":"from"},
+  {"type":"KeywordReserved","value":"from"},
   {"type":"Text","value":" "},
   {"type":"LiteralStringSingle","value":"'./App'"},
   {"type":"Text","value":"\n\n"},