forked from highlightjs/highlight.js
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtypescript.js
More file actions
96 lines (85 loc) · 2.4 KB
/
typescript.js
File metadata and controls
96 lines (85 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
Language: TypeScript
Author: Panu Horsmalahti <panu.horsmalahti@iki.fi>
Contributors: Ike Ku <dempfi@yahoo.com>
Description: TypeScript is a strict superset of JavaScript
Website: https://www.typescriptlang.org
Category: common, scripting
*/
import * as ECMAScript from "./lib/ecmascript.js";
import javascript from "./javascript.js";
/** @type LanguageFn */
export default function(hljs) {
const IDENT_RE = ECMAScript.IDENT_RE;
const NAMESPACE = {
beginKeywords: 'namespace', end: /\{/, excludeEnd: true
};
const INTERFACE = {
beginKeywords: 'interface', end: /\{/, excludeEnd: true,
keywords: 'interface extends'
};
const USE_STRICT = {
className: 'meta',
relevance: 10,
begin: /^\s*['"]use strict['"]/
};
const TYPES = [
"any",
"void",
"number",
"boolean",
"string",
"object",
"never",
"enum"
];
const TS_SPECIFIC_KEYWORDS = [
"type",
"namespace",
"typedef",
"interface",
"public",
"private",
"protected",
"implements",
"declare",
"abstract",
"readonly"
];
const KEYWORDS = {
$pattern: ECMAScript.IDENT_RE,
keyword: ECMAScript.KEYWORDS.concat(TS_SPECIFIC_KEYWORDS),
literal: ECMAScript.LITERALS,
built_in: ECMAScript.BUILT_INS.concat(TYPES)
};
const DECORATOR = {
className: 'meta',
begin: '@' + IDENT_RE,
};
const swapMode = (mode, label, replacement) => {
const indx = mode.contains.findIndex(m => m.label === label);
if (indx === -1) { throw new Error("can not find mode to replace"); };
mode.contains.splice(indx, 1, replacement);
};
const tsLanguage = javascript(hljs);
// this should update anywhere keywords is used since
// it will be the same actual JS object
Object.assign(tsLanguage.keywords, KEYWORDS);
tsLanguage.exports.PARAMS_CONTAINS.push(DECORATOR);
tsLanguage.contains = tsLanguage.contains.concat([
DECORATOR,
NAMESPACE,
INTERFACE,
]);
// TS gets a simpler shebang rule than JS
swapMode(tsLanguage, "shebang", hljs.SHEBANG());
// JS use strict rule purposely excludes `asm` which makes no sense
swapMode(tsLanguage, "use_strict", USE_STRICT);
const functionDeclaration = tsLanguage.contains.find(m => m.className === "function");
functionDeclaration.relevance = 0; // () => {} is more typical in TypeScript
Object.assign(tsLanguage, {
name: 'TypeScript',
aliases: ['ts', 'tsx']
});
return tsLanguage;
}