Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a197cfc

Browse files
Typescript: Added asserts keyword and other improvements (PrismJS#2280)
1 parent 7a554b5 commit a197cfc

File tree

5 files changed

+265
-10
lines changed

5 files changed

+265
-10
lines changed

‎components/prism-typescript.js

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,42 @@
1-
Prism.languages.typescript = Prism.languages.extend('javascript', {
2-
// From JavaScript Prism keyword list and TypeScript language spec: https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#221-reserved-words
3-
'keyword': /\b(?:abstract|as|async|await|break|case|catch|class|const|constructor|continue|debugger|declare|default|delete|do|else|enum|export|extends|finally|for|from|function|get|if|implements|import|in|instanceof|interface|is|keyof|let|module|namespace|new|null|of|package|private|protected|public|readonly|return|require|set|static|super|switch|this|throw|try|type|typeof|undefined|var|void|while|with|yield)\b/,
4-
'builtin': /\b(?:string|Function|any|number|boolean|Array|symbol|console|Promise|unknown|never)\b/,
5-
});
1+
(function (Prism) {
62

7-
Prism.languages.ts = Prism.languages.typescript;
3+
Prism.languages.typescript = Prism.languages.extend('javascript', {
4+
'class-name': {
5+
pattern: /(\b(?:class|extends|implements|instanceof|interface|new|type)\s+)(?!keyof\b)[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?:\s*<(?:[^<>]|<(?:[^<>]|<[^<>]*>)*>)*>)?/,
6+
lookbehind: true,
7+
greedy: true,
8+
inside: null // see below
9+
},
10+
// From JavaScript Prism keyword list and TypeScript language spec: https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#221-reserved-words
11+
'keyword': /\b(?:abstract|as|asserts|async|await|break|case|catch|class|const|constructor|continue|debugger|declare|default|delete|do|else|enum|export|extends|finally|for|from|function|get|if|implements|import|in|instanceof|interface|is|keyof|let|module|namespace|new|null|of|package|private|protected|public|readonly|return|require|set|static|super|switch|this|throw|try|type|typeof|undefined|var|void|while|with|yield)\b/,
12+
'builtin': /\b(?:string|Function|any|number|boolean|Array|symbol|console|Promise|unknown|never)\b/,
13+
});
14+
15+
// doesn't work with TS because TS is too complex
16+
delete Prism.languages.typescript['parameter'];
17+
18+
// a version of typescript specifically for highlighting types
19+
var typeInside = Prism.languages.extend('typescript', {});
20+
delete typeInside['class-name'];
21+
22+
Prism.languages.typescript['class-name'].inside = typeInside;
23+
24+
Prism.languages.insertBefore('typescript', 'function', {
25+
'generic-function': {
26+
// e.g. foo<T extends "bar" | "baz">( ...
27+
pattern: /#?[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*\s*<(?:[^<>]|<(?:[^<>]|<[^<>]*>)*>)*>(?=\s*\()/,
28+
greedy: true,
29+
inside: {
30+
'function': /^#?[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*/,
31+
'generic': {
32+
pattern: /<[\s\S]+/, // everything after the first <
33+
alias: 'class-name',
34+
inside: typeInside
35+
}
36+
}
37+
}
38+
});
39+
40+
Prism.languages.ts = Prism.languages.typescript;
41+
42+
}(Prism));

‎components/prism-typescript.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
interface Dictionary<T> {
2+
[key: string]: T;
3+
}
4+
5+
interface Foo extends Dictionary<number> {}
6+
7+
class Bar<T> extends Baz<T> implements FooBar<number, T | null> {}
8+
9+
type Record<K extends keyof any, T> = {
10+
[P in K]: T;
11+
}
12+
type Diff<T, U> = T extends U ? never : T;
13+
14+
----------------------------------------------------
15+
16+
[
17+
["keyword", "interface"],
18+
["class-name", [
19+
"Dictionary",
20+
["operator", "<"],
21+
["constant", "T"],
22+
["operator", ">"]
23+
]],
24+
["punctuation", "{"],
25+
["punctuation", "["],
26+
"key",
27+
["operator", ":"],
28+
["builtin", "string"],
29+
["punctuation", "]"],
30+
["operator", ":"],
31+
["constant", "T"],
32+
["punctuation", ";"],
33+
["punctuation", "}"],
34+
35+
["keyword", "interface"],
36+
["class-name", [
37+
"Foo"
38+
]],
39+
["keyword", "extends"],
40+
["class-name", [
41+
"Dictionary",
42+
["operator", "<"],
43+
["builtin", "number"],
44+
["operator", ">"]
45+
]],
46+
["punctuation", "{"],
47+
["punctuation", "}"],
48+
49+
["keyword", "class"],
50+
["class-name", [
51+
"Bar",
52+
["operator", "<"],
53+
["constant", "T"],
54+
["operator", ">"]
55+
]],
56+
["keyword", "extends"],
57+
["class-name", [
58+
"Baz",
59+
["operator", "<"],
60+
["constant", "T"],
61+
["operator", ">"]
62+
]],
63+
["keyword", "implements"],
64+
["class-name", [
65+
"FooBar",
66+
["operator", "<"],
67+
["builtin", "number"],
68+
["punctuation", ","],
69+
["constant", "T"],
70+
["operator", "|"],
71+
["keyword", "null"],
72+
["operator", ">"]
73+
]],
74+
["punctuation", "{"],
75+
["punctuation", "}"],
76+
77+
["keyword", "type"],
78+
["class-name", [
79+
"Record",
80+
["operator", "<"],
81+
["constant", "K"],
82+
["keyword", "extends"],
83+
["keyword", "keyof"],
84+
["builtin", "any"],
85+
["punctuation", ","],
86+
["constant", "T"],
87+
["operator", ">"]
88+
]],
89+
["operator", "="],
90+
["punctuation", "{"],
91+
["punctuation", "["],
92+
["constant", "P"],
93+
["keyword", "in"],
94+
["constant", "K"],
95+
["punctuation", "]"],
96+
["operator", ":"],
97+
["constant", "T"],
98+
["punctuation", ";"],
99+
["punctuation", "}"],
100+
101+
["keyword", "type"],
102+
["class-name", [
103+
"Diff",
104+
["operator", "<"],
105+
["constant", "T"],
106+
["punctuation", ","],
107+
["constant", "U"],
108+
["operator", ">"]
109+
]],
110+
["operator", "="],
111+
["constant", "T"],
112+
["keyword", "extends"],
113+
["class-name", [
114+
["constant", "U"]
115+
]],
116+
["operator", "?"],
117+
["builtin", "never"],
118+
["operator", ":"],
119+
["constant", "T"],
120+
["punctuation", ";"]
121+
]
122+
123+
----------------------------------------------------
124+
125+
Checks for class, interface, and other type names.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
function getProperty<T, K extends keyof T>(o: T, propertyName: K): T[K] { }
2+
3+
declare function f<T extends boolean>(x: T): T extends true ? string : number;
4+
5+
function assert(condition: any, msg?: string): asserts condition { }
6+
7+
----------------------------------------------------
8+
9+
[
10+
["keyword", "function"],
11+
["generic-function", [
12+
["function", "getProperty"],
13+
["generic", [
14+
["operator", "<"],
15+
["constant", "T"],
16+
["punctuation", ","],
17+
["constant", "K"],
18+
["keyword", "extends"],
19+
["keyword", "keyof"],
20+
["constant", "T"],
21+
["operator", ">"]
22+
]]
23+
]],
24+
["punctuation", "("],
25+
"o",
26+
["operator", ":"],
27+
["constant", "T"],
28+
["punctuation", ","],
29+
" propertyName",
30+
["operator", ":"],
31+
["constant", "K"],
32+
["punctuation", ")"],
33+
["operator", ":"],
34+
["constant", "T"],
35+
["punctuation", "["],
36+
["constant", "K"],
37+
["punctuation", "]"],
38+
["punctuation", "{"],
39+
["punctuation", "}"],
40+
41+
["keyword", "declare"],
42+
["keyword", "function"],
43+
["generic-function", [
44+
["function", "f"],
45+
["generic", [
46+
["operator", "<"],
47+
["constant", "T"],
48+
["keyword", "extends"],
49+
["builtin", "boolean"],
50+
["operator", ">"]
51+
]]
52+
]],
53+
["punctuation", "("],
54+
"x",
55+
["operator", ":"],
56+
["constant", "T"],
57+
["punctuation", ")"],
58+
["operator", ":"],
59+
["constant", "T"],
60+
["keyword", "extends"],
61+
["class-name", [
62+
["boolean", "true"]
63+
]],
64+
["operator", "?"],
65+
["builtin", "string"],
66+
["operator", ":"],
67+
["builtin", "number"],
68+
["punctuation", ";"],
69+
70+
["keyword", "function"],
71+
["function", "assert"],
72+
["punctuation", "("],
73+
"condition",
74+
["operator", ":"],
75+
["builtin", "any"],
76+
["punctuation", ","],
77+
" msg",
78+
["operator", "?"],
79+
["operator", ":"],
80+
["builtin", "string"],
81+
["punctuation", ")"],
82+
["operator", ":"],
83+
["keyword", "asserts"],
84+
" condition ",
85+
["punctuation", "{"],
86+
["punctuation", "}"]
87+
]
88+
89+
----------------------------------------------------
90+
91+
Checks for functions.

‎tests/languages/typescript/keyword_feature.test

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
abstract
22
as
3+
asserts
34
async
45
await
56
break
@@ -51,8 +52,9 @@ switch
5152
this
5253
throw
5354
try
54-
type
55+
type;
5556
typeof
57+
undefined
5658
var
5759
void
5860
while
@@ -64,6 +66,7 @@ yield
6466
[
6567
["keyword", "abstract"],
6668
["keyword", "as"],
69+
["keyword", "asserts"],
6770
["keyword", "async"],
6871
["keyword", "await"],
6972
["keyword", "break"],
@@ -115,8 +118,9 @@ yield
115118
["keyword", "this"],
116119
["keyword", "throw"],
117120
["keyword", "try"],
118-
["keyword", "type"],
121+
["keyword", "type"], ["punctuation", ";"],
119122
["keyword", "typeof"],
123+
["keyword", "undefined"],
120124
["keyword", "var"],
121125
["keyword", "void"],
122126
["keyword", "while"],
@@ -126,4 +130,4 @@ yield
126130

127131
----------------------------------------------------
128132

129-
Checks for keywords.
133+
Checks for keywords.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /