Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript: Improvements #2280

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 41 additions & 6 deletions components/prism-typescript.js
@@ -1,7 +1,42 @@
Prism.languages.typescript = Prism.languages.extend('javascript', {
// From JavaScript Prism keyword list and TypeScript language spec: https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#221-reserved-words
'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/,
'builtin': /\b(?:string|Function|any|number|boolean|Array|symbol|console|Promise|unknown|never)\b/,
});
(function (Prism) {

Prism.languages.ts = Prism.languages.typescript;
Prism.languages.typescript = Prism.languages.extend('javascript', {
'class-name': {
pattern: /(\b(?:class|extends|implements|instanceof|interface|new|type)\s+)(?!keyof\b)[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?:\s*<(?:[^<>]|<(?:[^<>]|<[^<>]*>)*>)*>)?/,
lookbehind: true,
greedy: true,
inside: null // see below
},
// From JavaScript Prism keyword list and TypeScript language spec: https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#221-reserved-words
'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/,
'builtin': /\b(?:string|Function|any|number|boolean|Array|symbol|console|Promise|unknown|never)\b/,
});

// doesn't work with TS because TS is too complex
delete Prism.languages.typescript['parameter'];

// a version of typescript specifically for highlighting types
var typeInside = Prism.languages.extend('typescript', {});
delete typeInside['class-name'];

Prism.languages.typescript['class-name'].inside = typeInside;

Prism.languages.insertBefore('typescript', 'function', {
'generic-function': {
// e.g. foo<T extends "bar" | "baz">( ...
pattern: /#?[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*\s*<(?:[^<>]|<(?:[^<>]|<[^<>]*>)*>)*>(?=\s*\()/,
greedy: true,
inside: {
'function': /^#?[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*/,
'generic': {
pattern: /<[\s\S]+/, // everything after the first <
alias: 'class-name',
inside: typeInside
}
}
}
});

Prism.languages.ts = Prism.languages.typescript;

}(Prism));
2 changes: 1 addition & 1 deletion components/prism-typescript.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

125 changes: 125 additions & 0 deletions tests/languages/typescript/class-name_feature.test
@@ -0,0 +1,125 @@
interface Dictionary<T> {
[key: string]: T;
}

interface Foo extends Dictionary<number> {}

class Bar<T> extends Baz<T> implements FooBar<number, T | null> {}

type Record<K extends keyof any, T> = {
[P in K]: T;
}
type Diff<T, U> = T extends U ? never : T;

----------------------------------------------------

[
["keyword", "interface"],
["class-name", [
"Dictionary",
["operator", "<"],
["constant", "T"],
["operator", ">"]
]],
["punctuation", "{"],
["punctuation", "["],
"key",
["operator", ":"],
["builtin", "string"],
["punctuation", "]"],
["operator", ":"],
["constant", "T"],
["punctuation", ";"],
["punctuation", "}"],

["keyword", "interface"],
["class-name", [
"Foo"
]],
["keyword", "extends"],
["class-name", [
"Dictionary",
["operator", "<"],
["builtin", "number"],
["operator", ">"]
]],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "class"],
["class-name", [
"Bar",
["operator", "<"],
["constant", "T"],
["operator", ">"]
]],
["keyword", "extends"],
["class-name", [
"Baz",
["operator", "<"],
["constant", "T"],
["operator", ">"]
]],
["keyword", "implements"],
["class-name", [
"FooBar",
["operator", "<"],
["builtin", "number"],
["punctuation", ","],
["constant", "T"],
["operator", "|"],
["keyword", "null"],
["operator", ">"]
]],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "type"],
["class-name", [
"Record",
["operator", "<"],
["constant", "K"],
["keyword", "extends"],
["keyword", "keyof"],
["builtin", "any"],
["punctuation", ","],
["constant", "T"],
["operator", ">"]
]],
["operator", "="],
["punctuation", "{"],
["punctuation", "["],
["constant", "P"],
["keyword", "in"],
["constant", "K"],
["punctuation", "]"],
["operator", ":"],
["constant", "T"],
["punctuation", ";"],
["punctuation", "}"],

["keyword", "type"],
["class-name", [
"Diff",
["operator", "<"],
["constant", "T"],
["punctuation", ","],
["constant", "U"],
["operator", ">"]
]],
["operator", "="],
["constant", "T"],
["keyword", "extends"],
["class-name", [
["constant", "U"]
]],
["operator", "?"],
["builtin", "never"],
["operator", ":"],
["constant", "T"],
["punctuation", ";"]
]

----------------------------------------------------

Checks for class, interface, and other type names.
91 changes: 91 additions & 0 deletions tests/languages/typescript/function_feature.test
@@ -0,0 +1,91 @@
function getProperty<T, K extends keyof T>(o: T, propertyName: K): T[K] { }

declare function f<T extends boolean>(x: T): T extends true ? string : number;

function assert(condition: any, msg?: string): asserts condition { }

----------------------------------------------------

[
["keyword", "function"],
["generic-function", [
["function", "getProperty"],
["generic", [
["operator", "<"],
["constant", "T"],
["punctuation", ","],
["constant", "K"],
["keyword", "extends"],
["keyword", "keyof"],
["constant", "T"],
["operator", ">"]
]]
]],
["punctuation", "("],
"o",
["operator", ":"],
["constant", "T"],
["punctuation", ","],
" propertyName",
["operator", ":"],
["constant", "K"],
["punctuation", ")"],
["operator", ":"],
["constant", "T"],
["punctuation", "["],
["constant", "K"],
["punctuation", "]"],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "declare"],
["keyword", "function"],
["generic-function", [
["function", "f"],
["generic", [
["operator", "<"],
["constant", "T"],
["keyword", "extends"],
["builtin", "boolean"],
["operator", ">"]
]]
]],
["punctuation", "("],
"x",
["operator", ":"],
["constant", "T"],
["punctuation", ")"],
["operator", ":"],
["constant", "T"],
["keyword", "extends"],
["class-name", [
["boolean", "true"]
]],
["operator", "?"],
["builtin", "string"],
["operator", ":"],
["builtin", "number"],
["punctuation", ";"],

["keyword", "function"],
["function", "assert"],
["punctuation", "("],
"condition",
["operator", ":"],
["builtin", "any"],
["punctuation", ","],
" msg",
["operator", "?"],
["operator", ":"],
["builtin", "string"],
["punctuation", ")"],
["operator", ":"],
["keyword", "asserts"],
" condition ",
["punctuation", "{"],
["punctuation", "}"]
]

----------------------------------------------------

Checks for functions.
10 changes: 7 additions & 3 deletions tests/languages/typescript/keyword_feature.test
@@ -1,5 +1,6 @@
abstract
as
asserts
async
await
break
Expand Down Expand Up @@ -51,8 +52,9 @@ switch
this
throw
try
type
type;
typeof
undefined
var
void
while
Expand All @@ -64,6 +66,7 @@ yield
[
["keyword", "abstract"],
["keyword", "as"],
["keyword", "asserts"],
["keyword", "async"],
["keyword", "await"],
["keyword", "break"],
Expand Down Expand Up @@ -115,8 +118,9 @@ yield
["keyword", "this"],
["keyword", "throw"],
["keyword", "try"],
["keyword", "type"],
["keyword", "type"], ["punctuation", ";"],
["keyword", "typeof"],
["keyword", "undefined"],
["keyword", "var"],
["keyword", "void"],
["keyword", "while"],
Expand All @@ -126,4 +130,4 @@ yield

----------------------------------------------------

Checks for keywords.
Checks for keywords.