@@ -7,11 +7,25 @@ Website: https://www.typescriptlang.org
7
7
Category: common, scripting
8
8
*/
9
9
10
- import * as ECMAScript from './lib/ecmascript.js' ;
10
+ import * as ECMAScript from "./lib/ecmascript.js" ;
11
+ import javascript from "./javascript.js" ;
11
12
13
+ /** @type LanguageFn */
12
14
export default function ( hljs ) {
13
- var IDENT_RE = ECMAScript . IDENT_RE ;
14
- var TYPES = [
15
+ const IDENT_RE = ECMAScript . IDENT_RE ;
16
+ const NAMESPACE = {
17
+ beginKeywords : 'namespace' , end : / \{ / , excludeEnd : true
18
+ } ;
19
+ const INTERFACE = {
20
+ beginKeywords : 'interface' , end : / \{ / , excludeEnd : true ,
21
+ keywords : 'interface extends'
22
+ } ;
23
+ const USE_STRICT = {
24
+ className : 'meta' ,
25
+ relevance : 10 ,
26
+ begin : / ^ \s * [ ' " ] u s e s t r i c t [ ' " ] /
27
+ } ;
28
+ const TYPES = [
15
29
"any" ,
16
30
"void" ,
17
31
"number" ,
@@ -21,7 +35,7 @@ export default function(hljs) {
21
35
"never" ,
22
36
"enum"
23
37
] ;
24
- var TS_SPECIFIC_KEYWORDS = [
38
+ const TS_SPECIFIC_KEYWORDS = [
25
39
"type" ,
26
40
"namespace" ,
27
41
"typedef" ,
@@ -34,197 +48,49 @@ export default function(hljs) {
34
48
"abstract" ,
35
49
"readonly"
36
50
] ;
37
- var KEYWORDS = {
51
+ const KEYWORDS = {
38
52
$pattern : ECMAScript . IDENT_RE ,
39
53
keyword : ECMAScript . KEYWORDS . concat ( TS_SPECIFIC_KEYWORDS ) . join ( " " ) ,
40
54
literal : ECMAScript . LITERALS . join ( " " ) ,
41
55
built_in : ECMAScript . BUILT_INS . concat ( TYPES ) . join ( " " )
42
56
} ;
43
- var DECORATOR = {
57
+ const DECORATOR = {
44
58
className : 'meta' ,
45
59
begin : '@' + IDENT_RE ,
46
60
} ;
47
- var NUMBER = {
48
- className : 'number' ,
49
- variants : [
50
- { begin : '\\b(0[bB][01]+)n?' } ,
51
- { begin : '\\b(0[oO][0-7]+)n?' } ,
52
- { begin : hljs . C_NUMBER_RE + 'n?' }
53
- ] ,
54
- relevance : 0
55
- } ;
56
- var SUBST = {
57
- className : 'subst' ,
58
- begin : '\\$\\{' , end : '\\}' ,
59
- keywords : KEYWORDS ,
60
- contains : [ ] // defined later
61
- } ;
62
- var HTML_TEMPLATE = {
63
- begin : 'html`' , end : '' ,
64
- starts : {
65
- end : '`' , returnEnd : false ,
66
- contains : [
67
- hljs . BACKSLASH_ESCAPE ,
68
- SUBST
69
- ] ,
70
- subLanguage : 'xml' ,
71
- }
72
- } ;
73
- var CSS_TEMPLATE = {
74
- begin : 'css`' , end : '' ,
75
- starts : {
76
- end : '`' , returnEnd : false ,
77
- contains : [
78
- hljs . BACKSLASH_ESCAPE ,
79
- SUBST
80
- ] ,
81
- subLanguage : 'css' ,
82
- }
83
- } ;
84
- var TEMPLATE_STRING = {
85
- className : 'string' ,
86
- begin : '`' , end : '`' ,
87
- contains : [
88
- hljs . BACKSLASH_ESCAPE ,
89
- SUBST
90
- ]
91
- } ;
92
- SUBST . contains = [
93
- hljs . APOS_STRING_MODE ,
94
- hljs . QUOTE_STRING_MODE ,
95
- HTML_TEMPLATE ,
96
- CSS_TEMPLATE ,
97
- TEMPLATE_STRING ,
98
- NUMBER ,
99
- hljs . REGEXP_MODE
100
- ] ;
101
- var ARGUMENTS =
102
- {
103
- begin : '\\(' ,
104
- end : / \) / ,
105
- keywords : KEYWORDS ,
106
- contains : [
107
- 'self' ,
108
- hljs . QUOTE_STRING_MODE ,
109
- hljs . APOS_STRING_MODE ,
110
- hljs . NUMBER_MODE
111
- ]
112
- } ;
113
- var PARAMS = {
114
- className : 'params' ,
115
- begin : / \( / , end : / \) / ,
116
- excludeBegin : true ,
117
- excludeEnd : true ,
118
- keywords : KEYWORDS ,
119
- contains : [
120
- hljs . C_LINE_COMMENT_MODE ,
121
- hljs . C_BLOCK_COMMENT_MODE ,
122
- DECORATOR ,
123
- ARGUMENTS
124
- ]
61
+
62
+ const swapMode = ( mode , label , replacement ) => {
63
+ const indx = mode . contains . findIndex ( m => m . label === label ) ;
64
+ if ( indx === - 1 ) { throw new Error ( "can not find mode to replace" ) ; } ;
65
+
66
+ mode . contains . splice ( indx , 1 , replacement ) ;
125
67
} ;
126
68
127
- return {
69
+ const tsLanguage = javascript ( hljs ) ;
70
+
71
+ // this should update anywhere keywords is used since
72
+ // it will be the same actual JS object
73
+ Object . assign ( tsLanguage . keywords , KEYWORDS ) ;
74
+
75
+ tsLanguage . exports . PARAMS_CONTAINS . push ( DECORATOR ) ;
76
+ tsLanguage . contains = tsLanguage . contains . concat ( [
77
+ DECORATOR ,
78
+ NAMESPACE ,
79
+ INTERFACE ,
80
+ ] ) ;
81
+
82
+ // TS gets a simpler shebang rule than JS
83
+ swapMode ( tsLanguage , "shebang" , hljs . SHEBANG ( ) ) ;
84
+ // JS use strict rule purposely excludes `asm` which makes no sense
85
+ swapMode ( tsLanguage , "use_strict" , USE_STRICT ) ;
86
+
87
+ const functionDeclaration = tsLanguage . contains . find ( m => m . className === "function" ) ;
88
+ functionDeclaration . relevance = 0 ; // () => {} is more typical in TypeScript
89
+
90
+ Object . assign ( tsLanguage , {
128
91
name : 'TypeScript' ,
129
- aliases : [ 'ts' ] ,
130
- keywords : KEYWORDS ,
131
- contains : [
132
- hljs . SHEBANG ( ) ,
133
- {
134
- className : 'meta' ,
135
- begin : / ^ \s * [ ' " ] u s e s t r i c t [ ' " ] /
136
- } ,
137
- hljs . APOS_STRING_MODE ,
138
- hljs . QUOTE_STRING_MODE ,
139
- HTML_TEMPLATE ,
140
- CSS_TEMPLATE ,
141
- TEMPLATE_STRING ,
142
- hljs . C_LINE_COMMENT_MODE ,
143
- hljs . C_BLOCK_COMMENT_MODE ,
144
- NUMBER ,
145
- { // "value" container
146
- begin : '(' + hljs . RE_STARTERS_RE + '|\\b(case|return|throw)\\b)\\s*' ,
147
- keywords : 'return throw case' ,
148
- contains : [
149
- hljs . C_LINE_COMMENT_MODE ,
150
- hljs . C_BLOCK_COMMENT_MODE ,
151
- hljs . REGEXP_MODE ,
152
- {
153
- className : 'function' ,
154
- // we have to count the parens to make sure we actually have the
155
- // correct bounding ( ) before the =>. There could be any number of
156
- // sub-expressions inside also surrounded by parens.
157
- begin : '(\\([^(]*' +
158
- '(\\([^(]*' +
159
- '(\\([^(]*' +
160
- '\\))?' +
161
- '\\))?' +
162
- '\\)|' + hljs . UNDERSCORE_IDENT_RE + ')\\s*=>' , returnBegin : true ,
163
- end : '\\s*=>' ,
164
- contains : [
165
- {
166
- className : 'params' ,
167
- variants : [
168
- {
169
- begin : hljs . UNDERSCORE_IDENT_RE
170
- } ,
171
- {
172
- className : null ,
173
- begin : / \( \s * \) / ,
174
- skip : true
175
- } ,
176
- {
177
- begin : / \( / , end : / \) / ,
178
- excludeBegin : true , excludeEnd : true ,
179
- keywords : KEYWORDS ,
180
- contains : ARGUMENTS . contains
181
- }
182
- ]
183
- }
184
- ]
185
- }
186
- ] ,
187
- relevance : 0
188
- } ,
189
- {
190
- className : 'function' ,
191
- beginKeywords : 'function' , end : / [ \{ ; ] / , excludeEnd : true ,
192
- keywords : KEYWORDS ,
193
- contains : [
194
- 'self' ,
195
- hljs . inherit ( hljs . TITLE_MODE , { begin : IDENT_RE } ) ,
196
- PARAMS
197
- ] ,
198
- illegal : / % / ,
199
- relevance : 0 // () => {} is more typical in TypeScript
200
- } ,
201
- {
202
- beginKeywords : 'constructor' , end : / [ \{ ; ] / , excludeEnd : true ,
203
- contains : [
204
- 'self' ,
205
- PARAMS
206
- ]
207
- } ,
208
- { // prevent references like module.id from being highlighted as module definitions
209
- begin : / m o d u l e \. / ,
210
- keywords : { built_in : 'module' } ,
211
- relevance : 0
212
- } ,
213
- {
214
- beginKeywords : 'module' , end : / \{ / , excludeEnd : true
215
- } ,
216
- {
217
- beginKeywords : 'interface' , end : / \{ / , excludeEnd : true ,
218
- keywords : 'interface extends'
219
- } ,
220
- {
221
- begin : / \$ [ ( . ] / // relevance booster for a pattern common to JS libs: `$(something)` and `$.something`
222
- } ,
223
- {
224
- begin : '\\.' + hljs . IDENT_RE , relevance : 0 // hack: prevents detection of keywords after dots
225
- } ,
226
- DECORATOR ,
227
- ARGUMENTS
228
- ]
229
- } ;
92
+ aliases : [ 'ts' ]
93
+ } ) ;
94
+
95
+ return tsLanguage ;
230
96
}
0 commit comments