Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit b2949cf

Browse files
authored
Adding decorators support (#271)
* Add decorators support * Add missing babel dependency * Add submodule update to prepare * Add tests for decorators * Simplify the test * Add multi-decorator test * Add decoratory factory test * Add decorator on a class test * Add decorator on a class with argument test * Remove charcodes and add babel install to prepare
1 parent 10beb7d commit b2949cf

File tree

15 files changed

+156
-0
lines changed

15 files changed

+156
-0
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ test/fixtures/convert/formatting
22
test/fixtures/convert/imports/named_import_specifier_kind/
33
test/fixtures/convert/function-types/predicate03
44
test/fixtures/convert/imports/dynamic_import
5+
test/fixtures/convert/decorator/
56
playground/static
67
babel-traverse
78
babel-types

babel.configs.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ const presets = [
44

55
const commonPlugins = [
66
"@babel/plugin-proposal-class-properties",
7+
[
8+
"@babel/plugin-proposal-decorators",
9+
{ legacy: true },
10+
],
711
["module-resolver", {
812
"alias": {
913
"^@babel/(.+)": "./babel/packages/babel-\\1/src/index"

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"devDependencies": {
2727
"@babel/core": "^7.13.10",
2828
"@babel/plugin-proposal-class-properties": "^7.13.0",
29+
"@babel/plugin-proposal-decorators": "^7.13.5",
2930
"@babel/plugin-transform-flow-strip-types": "^7.13.0",
3031
"@babel/plugin-transform-typescript": "^7.13.0",
3132
"@babel/preset-env": "^7.13.10",
@@ -49,6 +50,7 @@
4950
"webpack-cli": "^4.5.0"
5051
},
5152
"scripts": {
53+
"prepare": "git submodule update --init --recursive && cd babel && yarn install && cd ..",
5254
"prepublishOnly": "webpack build",
5355
"build": "webpack build",
5456
"type-check": "tsc --project tsconfig.json",

src/convert.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export const parseOptions: ParserOptions = {
2222
"nullishCoalescingOperator",
2323
"classPrivateProperties",
2424
"classPrivateMethods",
25+
// decorators
26+
"decorators-legacy",
2527
],
2628
};
2729

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function inject(options: {
2+
api_version: string;
3+
}) {
4+
return target => target;
5+
}
6+
7+
@inject({
8+
api_version: "0.3.4"
9+
})
10+
class MyComponent extends React.Component<Props> {
11+
render() {
12+
return document.createElement("div");
13+
}
14+
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function inject(options: {
2+
api_version: string;
3+
}) {
4+
return target => target;
5+
}
6+
7+
@inject({
8+
api_version: "0.3.4"
9+
})
10+
class MyComponent extends React.Component<Props> {
11+
render() {
12+
return document.createElement("div");
13+
}
14+
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { autoBindMethodsForReact } from "class-autobind-decorator";
2+
3+
@autoBindMethodsForReact(AUTOBIND_CFG)
4+
class PasswordEditor extends React.PureComponent<Props, State> {
5+
constructor(props: Props) {
6+
super(props);
7+
this.state = {
8+
showPassword: false
9+
};
10+
}
11+
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { autoBindMethodsForReact } from "class-autobind-decorator";
2+
3+
@autoBindMethodsForReact(AUTOBIND_CFG)
4+
class PasswordEditor extends React.PureComponent<Props, State> {
5+
constructor(props: Props) {
6+
super(props);
7+
this.state = {
8+
showPassword: false
9+
};
10+
}
11+
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { observer } from "mobx";
2+
3+
@observer
4+
class Store {
5+
constructor() {}
6+
7+
}
8+
9+
export default Store;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { observer } from "mobx";
2+
3+
@observer
4+
class Store {
5+
constructor() {}
6+
7+
}
8+
9+
export default Store;

0 commit comments

Comments
 (0)