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 component generator #990

Merged
merged 3 commits into from
Jun 8, 2019
Merged

Typescript component generator #990

merged 3 commits into from
Jun 8, 2019

Conversation

delkopiso
Copy link
Contributor

Summary

Allow the generation of typescript components by passing the --ts flag to the generator

resolves #965

Other information

Also handles all the special prop types like instanceOf, oneOf, and oneOfType:

  • Treat instanceOf prop types as reference to predefined types.
  • Treat oneOf prop types as union types, and define the union type.
  • Treat oneOfType prop types as union of primitives and custom types, and define both custom type and union type

Example

$ bin/rails g react:component HelloWorld greeting:string \
  'food:oneOf{meat,cheese,vegetable}' \
  'cuisine:oneOfType{string,Food}' \
  owner:instanceOf{Person} --ts
      create  app/javascript/components/HelloWorld.tsx
// app/javascript/components/HelloWorld.tsx
import * as React from "react"

type Food = 'meat' | 'cheese' | 'vegetable'
type Cuisine = string | Food

interface IHelloWorldProps {
  greeting: string;
  food: Food;
  cuisine: Cuisine;
  owner: Person;
}

interface IHelloWorldState {
}

class HelloWorld extends React.Component <IHelloWorldProps, IHelloWorldState> {
  render() {
    return (
      <React.Fragment>
        Greeting: {this.props.greeting}
        Food: {this.props.food}
        Cuisine: {this.props.cuisine}
        Owner: {this.props.owner}
      </React.Fragment>
    );
  }
}

export default HelloWorld

delkopiso and others added 3 commits May 26, 2019 22:26
- Treat instanceOf prop types as reference to predefined types.

- Treat oneOf prop types as union types, and define the union type.

- Treat oneOfType prop types as union of primitives and custom types, and define both custom type and union type
Copy link
Member

@BookOfGreg BookOfGreg left a comment

Choose a reason for hiding this comment

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

I've not completed the review yet but this looks very good and high quality so far! Thank you very much for this contribution. I'll review the rest through this weekend.

end

def union?(args = '')
return args.to_s.gsub(/[{}]/, '').split(',').count > 1
Copy link
Member

Choose a reason for hiding this comment

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

I was wondering how Rails manages to parse out it's command line generators with curly braces such as rails g migration add_thing_to_table thing:string{10}
Turns out it's very similar to what you've done here already:
https://github.com/rails/rails/blob/1a5381ff0cf04af68a50bd04f265b9b8199e37b4/railties/lib/rails/generators/generated_attribute.rb#L45
But yours is a little easier to read 👍

Copy link
Member

@BookOfGreg BookOfGreg left a comment

Choose a reason for hiding this comment

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

This is a really good PR.
Tested locally and this generator works as intended and no changes for the other generators.

Only thing missing would be a test in test/generators/typescript_component_generator_test.rb which I will add myself after merging.

Thank you for this contribution, hope to see you contributing again some day :)

@BookOfGreg BookOfGreg merged commit 17f0bfb into reactjs:master Jun 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generate Typescript based component
2 participants