UNPKG

6.76 kBJavaScriptView Raw
1const KEYWORDS = [
2 "as", // for exports
3 "in",
4 "of",
5 "if",
6 "for",
7 "while",
8 "finally",
9 "var",
10 "new",
11 "function",
12 "do",
13 "return",
14 "void",
15 "else",
16 "break",
17 "catch",
18 "instanceof",
19 "with",
20 "throw",
21 "case",
22 "default",
23 "try",
24 "switch",
25 "continue",
26 "typeof",
27 "delete",
28 "let",
29 "yield",
30 "const",
31 "class",
32 // JS handles these with a special rule
33 // "get",
34 // "set",
35 "debugger",
36 "async",
37 "await",
38 "static",
39 "import",
40 "from",
41 "export",
42 "extends"
43];
44const LITERALS = [
45 "true",
46 "false",
47 "null",
48 "undefined",
49 "NaN",
50 "Infinity"
51];
52
53// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
54const TYPES = [
55 // Fundamental objects
56 "Object",
57 "Function",
58 "Boolean",
59 "Symbol",
60 // numbers and dates
61 "Math",
62 "Date",
63 "Number",
64 "BigInt",
65 // text
66 "String",
67 "RegExp",
68 // Indexed collections
69 "Array",
70 "Float32Array",
71 "Float64Array",
72 "Int8Array",
73 "Uint8Array",
74 "Uint8ClampedArray",
75 "Int16Array",
76 "Int32Array",
77 "Uint16Array",
78 "Uint32Array",
79 "BigInt64Array",
80 "BigUint64Array",
81 // Keyed collections
82 "Set",
83 "Map",
84 "WeakSet",
85 "WeakMap",
86 // Structured data
87 "ArrayBuffer",
88 "SharedArrayBuffer",
89 "Atomics",
90 "DataView",
91 "JSON",
92 // Control abstraction objects
93 "Promise",
94 "Generator",
95 "GeneratorFunction",
96 "AsyncFunction",
97 // Reflection
98 "Reflect",
99 "Proxy",
100 // Internationalization
101 "Intl",
102 // WebAssembly
103 "WebAssembly"
104];
105
106const ERROR_TYPES = [
107 "Error",
108 "EvalError",
109 "InternalError",
110 "RangeError",
111 "ReferenceError",
112 "SyntaxError",
113 "TypeError",
114 "URIError"
115];
116
117const BUILT_IN_GLOBALS = [
118 "setInterval",
119 "setTimeout",
120 "clearInterval",
121 "clearTimeout",
122
123 "require",
124 "exports",
125
126 "eval",
127 "isFinite",
128 "isNaN",
129 "parseFloat",
130 "parseInt",
131 "decodeURI",
132 "decodeURIComponent",
133 "encodeURI",
134 "encodeURIComponent",
135 "escape",
136 "unescape"
137];
138
139const BUILT_INS = [].concat(
140 BUILT_IN_GLOBALS,
141 TYPES,
142 ERROR_TYPES
143);
144
145/*
146Language: CoffeeScript
147Author: Dmytrii Nagirniak <dnagir@gmail.com>
148Contributors: Oleg Efimov <efimovov@gmail.com>, Cédric Néhémie <cedric.nehemie@gmail.com>
149Description: CoffeeScript is a programming language that transcompiles to JavaScript. For info about language see http://coffeescript.org/
150Category: scripting
151Website: https://coffeescript.org
152*/
153
154/** @type LanguageFn */
155function coffeescript(hljs) {
156 const COFFEE_BUILT_INS = [
157 'npm',
158 'print'
159 ];
160 const COFFEE_LITERALS = [
161 'yes',
162 'no',
163 'on',
164 'off'
165 ];
166 const COFFEE_KEYWORDS = [
167 'then',
168 'unless',
169 'until',
170 'loop',
171 'by',
172 'when',
173 'and',
174 'or',
175 'is',
176 'isnt',
177 'not'
178 ];
179 const NOT_VALID_KEYWORDS = [
180 "var",
181 "const",
182 "let",
183 "function",
184 "static"
185 ];
186 const excluding = (list) =>
187 (kw) => !list.includes(kw);
188 const KEYWORDS$1 = {
189 keyword: KEYWORDS.concat(COFFEE_KEYWORDS).filter(excluding(NOT_VALID_KEYWORDS)),
190 literal: LITERALS.concat(COFFEE_LITERALS),
191 built_in: BUILT_INS.concat(COFFEE_BUILT_INS)
192 };
193 const JS_IDENT_RE = '[A-Za-z$_][0-9A-Za-z$_]*';
194 const SUBST = {
195 className: 'subst',
196 begin: /#\{/,
197 end: /\}/,
198 keywords: KEYWORDS$1
199 };
200 const EXPRESSIONS = [
201 hljs.BINARY_NUMBER_MODE,
202 hljs.inherit(hljs.C_NUMBER_MODE, {
203 starts: {
204 end: '(\\s*/)?',
205 relevance: 0
206 }
207 }), // a number tries to eat the following slash to prevent treating it as a regexp
208 {
209 className: 'string',
210 variants: [
211 {
212 begin: /'''/,
213 end: /'''/,
214 contains: [hljs.BACKSLASH_ESCAPE]
215 },
216 {
217 begin: /'/,
218 end: /'/,
219 contains: [hljs.BACKSLASH_ESCAPE]
220 },
221 {
222 begin: /"""/,
223 end: /"""/,
224 contains: [
225 hljs.BACKSLASH_ESCAPE,
226 SUBST
227 ]
228 },
229 {
230 begin: /"/,
231 end: /"/,
232 contains: [
233 hljs.BACKSLASH_ESCAPE,
234 SUBST
235 ]
236 }
237 ]
238 },
239 {
240 className: 'regexp',
241 variants: [
242 {
243 begin: '///',
244 end: '///',
245 contains: [
246 SUBST,
247 hljs.HASH_COMMENT_MODE
248 ]
249 },
250 {
251 begin: '//[gim]{0,3}(?=\\W)',
252 relevance: 0
253 },
254 {
255 // regex can't start with space to parse x / 2 / 3 as two divisions
256 // regex can't start with *, and it supports an "illegal" in the main mode
257 begin: /\/(?![ *]).*?(?![\\]).\/[gim]{0,3}(?=\W)/
258 }
259 ]
260 },
261 {
262 begin: '@' + JS_IDENT_RE // relevance booster
263 },
264 {
265 subLanguage: 'javascript',
266 excludeBegin: true,
267 excludeEnd: true,
268 variants: [
269 {
270 begin: '```',
271 end: '```'
272 },
273 {
274 begin: '`',
275 end: '`'
276 }
277 ]
278 }
279 ];
280 SUBST.contains = EXPRESSIONS;
281
282 const TITLE = hljs.inherit(hljs.TITLE_MODE, {
283 begin: JS_IDENT_RE
284 });
285 const POSSIBLE_PARAMS_RE = '(\\(.*\\)\\s*)?\\B[-=]>';
286 const PARAMS = {
287 className: 'params',
288 begin: '\\([^\\(]',
289 returnBegin: true,
290 /* We need another contained nameless mode to not have every nested
291 pair of parens to be called "params" */
292 contains: [{
293 begin: /\(/,
294 end: /\)/,
295 keywords: KEYWORDS$1,
296 contains: ['self'].concat(EXPRESSIONS)
297 }]
298 };
299
300 return {
301 name: 'CoffeeScript',
302 aliases: [
303 'coffee',
304 'cson',
305 'iced'
306 ],
307 keywords: KEYWORDS$1,
308 illegal: /\/\*/,
309 contains: [
310 ...EXPRESSIONS,
311 hljs.COMMENT('###', '###'),
312 hljs.HASH_COMMENT_MODE,
313 {
314 className: 'function',
315 begin: '^\\s*' + JS_IDENT_RE + '\\s*=\\s*' + POSSIBLE_PARAMS_RE,
316 end: '[-=]>',
317 returnBegin: true,
318 contains: [
319 TITLE,
320 PARAMS
321 ]
322 },
323 {
324 // anonymous function start
325 begin: /[:\(,=]\s*/,
326 relevance: 0,
327 contains: [{
328 className: 'function',
329 begin: POSSIBLE_PARAMS_RE,
330 end: '[-=]>',
331 returnBegin: true,
332 contains: [PARAMS]
333 }]
334 },
335 {
336 className: 'class',
337 beginKeywords: 'class',
338 end: '$',
339 illegal: /[:="\[\]]/,
340 contains: [
341 {
342 beginKeywords: 'extends',
343 endsWithParent: true,
344 illegal: /[:="\[\]]/,
345 contains: [TITLE]
346 },
347 TITLE
348 ]
349 },
350 {
351 begin: JS_IDENT_RE + ':',
352 end: ':',
353 returnBegin: true,
354 returnEnd: true,
355 relevance: 0
356 }
357 ]
358 };
359}
360
361export { coffeescript as default };