Skip to content

Commit

Permalink
TypeScript: Added support for decorators (#2820)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Apr 3, 2021
1 parent 01b7b6f commit 31cc214
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 1 deletion.
10 changes: 10 additions & 0 deletions components/prism-typescript.js
Expand Up @@ -22,6 +22,16 @@
Prism.languages.typescript['class-name'].inside = typeInside;

Prism.languages.insertBefore('typescript', 'function', {
'decorator': {
pattern: /@[$\w\xA0-\uFFFF]+/,
inside: {
'at': {
pattern: /^@/,
alias: 'operator'
},
'function': /^[\s\S]+/
}
},
'generic-function': {
// e.g. foo<T extends "bar" | "baz">( ...
pattern: /#?(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*\s*<(?:[^<>]|<(?:[^<>]|<[^<>]*>)*>)*>(?=\s*\()/,
Expand Down
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.

121 changes: 121 additions & 0 deletions tests/languages/typescript/decorator_feature.test
@@ -0,0 +1,121 @@
@f @g x

@f
@g
x

@sealed
class ExampleClass {

@first()
@second()
method() {}

@enumerable(false)
greet() {
return "Hello, " + this.greeting;
}

@configurable(false)
get y() {
return this._y;
}

}

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

[
["decorator", [
["at", "@"],
["function", "f"]
]],
["decorator", [
["at", "@"],
["function", "g"]
]],
" x\r\n\r\n",

["decorator", [
["at", "@"],
["function", "f"]
]],
["decorator", [
["at", "@"],
["function", "g"]
]],
"\r\nx\r\n\r\n",

["decorator", [
["at", "@"],
["function", "sealed"]
]],
["keyword", "class"], ["class-name", ["ExampleClass"]], ["punctuation", "{"],

["decorator", [
["at", "@"],
["function", "first"]
]],
["punctuation", "("],
["punctuation", ")"],

["decorator", [
["at", "@"],
["function", "second"]
]],
["punctuation", "("],
["punctuation", ")"],

["function", "method"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],

["decorator", [
["at", "@"],
["function", "enumerable"]
]],
["punctuation", "("],
["boolean", "false"],
["punctuation", ")"],

["function", "greet"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],

["keyword", "return"],
["string", "\"Hello, \""],
["operator", "+"],
["keyword", "this"],
["punctuation", "."],
"greeting",
["punctuation", ";"],

["punctuation", "}"],

["decorator", [
["at", "@"],
["function", "configurable"]
]],
["punctuation", "("],
["boolean", "false"],
["punctuation", ")"],

["keyword", "get"],
["function", "y"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],

["keyword", "return"],
["keyword", "this"],
["punctuation", "."],
"_y",
["punctuation", ";"],

["punctuation", "}"],

["punctuation", "}"]
]
38 changes: 38 additions & 0 deletions tests/languages/typescript/issue2819.test
@@ -0,0 +1,38 @@
@Component({
selector: 'my-app',
template: `<div>Hello World!</div>`
})
export class AppComponent {}

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

[
["decorator", [
["at", "@"],
["function", "Component"]
]],
["punctuation", "("],
["punctuation", "{"],

"\r\n selector",
["operator", ":"],
["string", "'my-app'"],
["punctuation", ","],

"\r\n template",
["operator", ":"],
["template-string", [
["template-punctuation", "`"],
["string", "<div>Hello World!</div>"],
["template-punctuation", "`"]
]],

["punctuation", "}"],
["punctuation", ")"],

["keyword", "export"],
["keyword", "class"],
["class-name", ["AppComponent"]],
["punctuation", "{"],
["punctuation", "}"]
]

0 comments on commit 31cc214

Please sign in to comment.