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: Added support for decorators #2820

Merged
merged 1 commit into from
Apr 3, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions components/prism-typescript.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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", "}"]
]