Skip to content

Upgrade and support new decorators #38

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

Merged
merged 3 commits into from
May 8, 2024
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
17 changes: 8 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@
"trailingComma": "all"
},
"devDependencies": {
"@types/jest": "^26.0.23",
"@types/node": "^12.20.13",
"jest": "^26.5.6",
"lerna": "^4.0.0",
"mobx": "^4.5.0",
"prettier": "^2.3.0",
"ts-jest": "^26.5.6",
"typescript": "^5.0.4",
"@types/jest": "^29.5.12",
"@types/node": "^16",
"jest": "^29.7.0",
"mobx": "^6.0.0",
"prettier": "^3.2.5",
"ts-jest": "^29.1.2",
"typescript": "^5.4.5",
"typescript4": "npm:[email protected]"
},
"scripts": {
"test": "lerna run test"
"test": "pnpm -r test"
},
"workspaces": [
"packages/*"
Expand Down
81 changes: 49 additions & 32 deletions packages/examples/README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
# An example transforming with ttypescript
# An example transforming with ts-patch

## Usage

```
yarn install
yarn build
pnpm install
pnpm build
```

The `build` command will create the output the `lib` folder

## Input

```ts
export const fn = transformToMobxFlow(async input => {
export const fn = transformToMobxFlow(async (input) => {
return await Promise.resolve(input);
});

export const fn2 = transformToMobxFlow(async function test(input) {
return await Promise.resolve(input);
});

export const fn3 = transformToMobxFlow(async function(input) {
export const fn3 = transformToMobxFlow(async function (input) {
return await Promise.resolve(input);
});

Expand All @@ -42,47 +43,63 @@ export class Test {

@transformToMobxFlow
funcBound = async (input: string) => {
this.value = await Promise.resolve(input);
this.value = await Promise.resolve(input);
};

@transformToMobxFlow
funcNonBound = async function(input: string) {
funcNonBound = async function (input: string) {
return await Promise.resolve(input);
};
}

```

## Output ES2015

```js
import { flow as flow_1 } from "mobx";
export const fn = (input) => { return flow_1(function* fn() {
import { flow as flow_1 } from 'mobx';
export const fn = (input) => {
return flow_1(function* fn() {
return yield Promise.resolve(input);
}).call(this); };
export const fn2 = function test(input) { return flow_1(function* test() {
}).call(this);
};
export const fn2 = function test(input) {
return flow_1(function* test() {
return yield Promise.resolve(input);
}).call(this); };
export const fn3 = function (input) { return flow_1(function* fn3() {
}).call(this);
};
export const fn3 = function (input) {
return flow_1(function* fn3() {
return yield Promise.resolve(input);
}).call(this); };
}).call(this);
};
export class Test {
constructor() {
this.value = '';
this.funcBound = (input) => { return flow_1(function* funcBound() {
this.value = yield Promise.resolve(input);
}).call(this); };
this.funcNonBound = function (input) { return flow_1(function* funcNonBound() {
return yield Promise.resolve(input);
}).call(this); };
var nestedFlow = () => { return flow_1(function* nestedFlow() {
var anotherNestedFlow = () => { return flow_1(function* anotherNestedFlow() {
return yield Promise.resolve('5');
}).call(this); };
yield anotherNestedFlow();
}).call(this); };
}
func(input) { return flow_1(function* func() {
constructor() {
this.value = '';
this.funcBound = (input) => {
return flow_1(function* funcBound() {
this.value = yield Promise.resolve(input);
}).call(this); }
}).call(this);
};
this.funcNonBound = function (input) {
return flow_1(function* funcNonBound() {
return yield Promise.resolve(input);
}).call(this);
};
var nestedFlow = () => {
return flow_1(function* nestedFlow() {
var anotherNestedFlow = () => {
return flow_1(function* anotherNestedFlow() {
return yield Promise.resolve('5');
}).call(this);
};
yield anotherNestedFlow();
}).call(this);
};
}
func(input) {
return flow_1(function* func() {
this.value = yield Promise.resolve(input);
}).call(this);
}
}
```
2 changes: 1 addition & 1 deletion packages/examples/global.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/// <reference path="node_modules/ts-transform-async-to-mobx-flow/transformToMobxFlow.d.ts" />
/// <reference path="node_modules/ts-transform-async-to-mobx-flow/transformToMobxFlow.d.ts" />
9 changes: 5 additions & 4 deletions packages/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
"main": "lib/index.js",
"repository": "[email protected]:aurornz/ts-transform-async-to-mobx-flow.git",
"scripts": {
"build": "rm -rf lib && ttsc"
"prepare": "ts-patch install -s",
"build": "rm -rf lib && tsc"
},
"bugs": {
"url": "https://github.com/aurornz/ts-transform-async-to-mobx-flow/issues"
},
"devDependencies": {
"ttypescript": "^1.5.12",
"typescript": "^4.2.4"
"ts-patch": "^3.1.2",
"typescript": "^5.4.5"
},
"dependencies": {
"ts-transform-async-to-mobx-flow": "./packages/ts-transform-async-to-mobx-flow"
"ts-transform-async-to-mobx-flow": "workspace:ts-transform-async-to-mobx-flow"
}
}
2 changes: 1 addition & 1 deletion packages/examples/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"target": "es2015",
"moduleResolution": "node",
"lib": ["es6", "dom", "esnext.asynciterable"],
"experimentalDecorators": true,
"experimentalDecorators": false,
"strict": true,
"esModuleInterop": true,
"outDir": "lib",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Typescript version: TypeScript-4 Converts function marked as transformToMobxFlow 1`] = `
"import * as mobx from \\"mobx\\";
/// <reference path=\\"../../transformToMobxFlow.d.ts\\" />
"import * as mobx from "mobx";
/// <reference path="../../transformToMobxFlow.d.ts" />
declare let randomDecorator: any;
export const fn = (input) => { return mobx.flow(function* fn_mobxFlow() {
return yield Promise.resolve(input);
Expand Down Expand Up @@ -104,7 +104,7 @@ export class Test {

exports[`Typescript version: TypeScript-4 Transpiled correctly to ES5 1`] = `
"var _this = this;
var mobx = require(\\"mobx\\");
var mobx = require("mobx");
var fn = function (input) { return mobx.flow(function fn_mobxFlow() {
return __generator(this, function (_a) {
switch (_a.label) {
Expand All @@ -117,8 +117,8 @@ var fn = function (input) { return mobx.flow(function fn_mobxFlow() {
`;

exports[`Typescript version: TypeScript-5 Converts function marked as transformToMobxFlow 1`] = `
"import * as mobx from \\"mobx\\";
/// <reference path=\\"../../transformToMobxFlow.d.ts\\" />
"import * as mobx from "mobx";
/// <reference path="../../transformToMobxFlow.d.ts" />
declare let randomDecorator: any;
export const fn = (input) => { return mobx.flow(function* fn_mobxFlow() {
return yield Promise.resolve(input);
Expand Down Expand Up @@ -220,7 +220,7 @@ export class Test {

exports[`Typescript version: TypeScript-5 Transpiled correctly to ES5 1`] = `
"var _this = this;
var mobx = require(\\"mobx\\");
import * as mobx from "mobx";
var fn = function (input) { return mobx.flow(function fn_mobxFlow() {
return __generator(this, function (_a) {
switch (_a.label) {
Expand Down
2 changes: 1 addition & 1 deletion packages/ts-transform-async-to-mobx-flow/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-transform-async-to-mobx-flow",
"version": "0.1.0",
"version": "1.0.0",
"description": "Typescript transformer for converting async functions into generators wrapped with mobx.flow",
"keywords": [
"mobx",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ declare function transformToMobxFlow<T extends (...args: any[]) => Promise<any>>
propertyKey: string | symbol,
descriptor: TypedPropertyDescriptor<T>,
): TypedPropertyDescriptor<T> | void;
declare function transformToMobxFlow<TThis, TMethod extends (...args: any[]) => Promise<any>>(
target: TMethod,
context: ClassMethodDecoratorContext<TThis, TMethod>,
): TMethod;

/**
* Marks an `async` property function to transform into a generator function wrapped with `mobx.flow`
Expand Down Expand Up @@ -82,3 +86,7 @@ class Test {
```
*/
declare function transformToMobxFlow(target: Object, propertyKey: string | symbol): void;
declare function transformToMobxFlow<TThis, TValue extends (...args: any[]) => Promise<any>>(
target: undefined,
context: ClassFieldDecoratorContext<TThis, TValue>,
): void;
5 changes: 2 additions & 3 deletions packages/ts-transform-async-to-mobx-flow/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
"target": "esnext",
"module": "commonjs",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": false,
"declaration": true,
"strict": true,
"esModuleInterop": true,
"outDir": "lib",
"sourceMap": true,
"skipLibCheck": true
"skipLibCheck": false
},
"exclude": [
"node_modules"
Expand Down
Loading