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

Adding decorators support #271

Merged
merged 10 commits into from
Apr 8, 2021
Merged

Adding decorators support #271

merged 10 commits into from
Apr 8, 2021

Conversation

aminya
Copy link
Contributor

@aminya aminya commented Apr 6, 2021

This adds decorators support such as for mobx. I have added the test which was taken from a real-world project.

Fixes #143

@codecov
Copy link

codecov bot commented Apr 6, 2021

Codecov Report

Merging #271 (23ced32) into master (ea69a99) will increase coverage by 93.64%.
The diff coverage is n/a.

Impacted file tree graph

@@             Coverage Diff             @@
##           master     #271       +/-   ##
===========================================
+ Coverage        0   93.64%   +93.64%     
===========================================
  Files           0        9        +9     
  Lines           0      614      +614     
  Branches        0       99       +99     
===========================================
+ Hits            0      575      +575     
- Misses          0       39       +39     
Impacted Files Coverage Δ
src/cli.ts 100.00% <ø> (ø)
src/convert.ts 95.23% <ø> (ø)
src/detect-jsx.ts 100.00% <ø> (ø)
src/transform.ts 88.88% <ø> (ø)
src/transforms/declare.ts 97.29% <ø> (ø)
src/transforms/object-type.ts 97.93% <ø> (ø)
src/transforms/react-types.ts 97.56% <ø> (ø)
src/transforms/utility-types.ts 97.56% <ø> (ø)
src/util.ts 94.44% <ø> (ø)
... and 8 more

@aminya aminya marked this pull request as ready for review April 6, 2021 17:54
@aminya
Copy link
Contributor Author

aminya commented Apr 6, 2021

@kevinbarabash Would you please check this out? 😃

@@ -2,6 +2,7 @@ test/fixtures/convert/formatting
test/fixtures/convert/imports/named_import_specifier_kind/
test/fixtures/convert/function-types/predicate03
test/fixtures/convert/imports/dynamic_import
test/fixtures/convert/decorator/
Copy link
Contributor Author

@aminya aminya Apr 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for the record, this was added because prettier removed the @observable from above the markersMapping in the test when the parser is flow.

Fortunately, it works fine with TypeScript, which is what flow-to-ts uses in the end for formatting with prettier.

Copy link
Contributor

@kevinbarabash kevinbarabash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. It's cool that transform.ts didn't need to be changed. There are maybe a few more test cases that might be worth adding:

  • multiple decorators
  • decorator on a class
  • decorator factories

package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
test/fixtures/convert/decorator/mobx/flow.js Outdated Show resolved Hide resolved
@dimitropoulos
Copy link

Just want to chime in that I would benefit quite a lot from this in the conversion of insomnia: Kong/insomnia#3266

For now, we'll be using a minimal implementation of this PR dimitropoulos@41a52dc (I made it a few days ago before this PR existed).

We'll need to be using this code in our conversion so it'd be useful if it could be merged soon.

I'd be happy to help however possible.

@aminya
Copy link
Contributor Author

aminya commented Apr 7, 2021

Just want to chime in that I would benefit quite a lot from this in the conversion of insomnia: Kong/insomnia#3266

@dimitropoulos Could you give us some simple examples of the decorators you use and the expected output so we can add them to the tests?

@aminya
Copy link
Contributor Author

aminya commented Apr 7, 2021

Thanks for the PR. It's cool that transform.ts didn't need to be changed. There are maybe a few more test cases that might be worth adding:

  • multiple decorators
  • decorator on a class
  • decorator factories

Done!

@dimitropoulos
Copy link

Screenshot_20210407_164205

Thankfully we only have exactly one decorator, autoBindMethodsForReact from https://www.npmjs.com/package/class-autobind-decorator, that we use in 148 places.

To pick a random example: https://github.com/Kong/insomnia/blob/develop/packages/insomnia-app/app/ui/components/editors/password-editor.js#L24

@aminya
Copy link
Contributor Author

aminya commented Apr 7, 2021

Thankfully we only have exactly one decorator, autoBindMethodsForReact from npmjs.com/package/class-autobind-decorator, that we use in 148 places.

OK. I added the test for that one too.

Comment on lines +1 to +15
import { observable, computed, action } from "mobx";
export class Store {
@observable
markersMapping: Map<number, MarkerStore> = new Map();

@computed
get filePath(): ?string {
return "/file";
}

@action
newMarkerStore(editorId: number) {
return 1;
}
}
Copy link
Contributor

@kevinbarabash kevinbarabash Apr 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for simplifying these examples. The could be simpler since the code doesn't have to be semantically valid (in this case the getter doesn't return a value, e.g.

class MyClass {
   @property
   num: number = 5;

   @computed
   get foo(): string {}

   @action
   doSomething() {}
}

What you have is fine for now. No action req'd.

Comment on lines +3 to +4
@autoBindMethodsForReact(AUTOBIND_CFG)
class PasswordEditor extends React.PureComponent<Props, State> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the decorator-factory example already covers this, unless I'm missing something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can remove it if you prefer fewer tests, but usually, more tests are better 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine. It'd be a different story if the tests took a long time to run, but that isn't the case.

Copy link
Contributor

@kevinbarabash kevinbarabash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for adding this feature. 🙂

@kevinbarabash kevinbarabash merged commit b2949cf into Khan:master Apr 8, 2021
@kevinbarabash
Copy link
Contributor

I've published 0.5.1 to npm.

@dimitropoulos
Copy link

wow this is great news, now I don't have to publish a fork just for our use-case! thank you all so much!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Conversion fails when syntax uses decorators
3 participants