Giter Club home page Giter Club logo

ts-react-loader's Introduction

ts-react-loader

Warning this code is proof-of-concept quality at best! If you find the idea compelling and would like to contribute to making it something reliably shippable, say hi.

What it does

Given a file like this:

import * as React from "react";

interface Props {
  foo: string;
}
class FooComponent extends React.Component<Props> {
  context: {
    store: {
      dispatch: (action: any) => any;
    };
  };

  render() {
    return <strong>Your foo is: {this.props.foo}</strong>;
  }
}

This loader will emit new TypeScript with prop & context types added:

import * as React from "react";
import * as PropTypes from "prop-types";

interface Props {
  foo: string;
}

class FooComponent extends React.Component<Props> {
  static propTypes = {
    foo: PropTypes.string.isRequired
  };

  static contextTypes = {
    store: PropTypes.shape({
      dispatch: PropTypes.func.isRequired
    }).isRequired
  };

  context: {
    store: {
      dispatch: Function;
    };
  };

  render() {
    return <strong>Your foo is: {this.props.foo}</strong>;
  }
}

Look ma! no repetition!

ts-react-loader's People

Contributors

grncdr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

ts-react-loader's Issues

Fix up hacky detection of `React.Component` and `React.ReactNode`

Currently the code just stringifies types and compares them against constant strings. This doesn't work if the import isn't named React.

Investigation needed: can we do reliable symbol comparisons with ts.Symbol.equals?

Tests should cover:

  • import {Component, ReactNode} from 'react' && class Foo extends Component<{n: ReactNode}> {}
  • import * as R from 'react': class Foo extends R.Component<{n: R.ReactNode}>

Set up testing harness

Probably worth looking into what other Webpack loaders do, but even just something that verifies the output of compiling an example output would be a good start.

Implement as TypeScript compiler transform

The TypeScript compiler API supports transforms for a while now. It should be possible to implement the main functionality independent of webpack, and then expose it as a compiler transform as well as a webpack loader.

Keep an eye on microsoft/TypeScript#14419 to see if a common approach to using typescript with compiler transforms comes about.

Add a code-of-conduct.

I don't have one in mind yet, would like to ask a few people for their recommendations

Contributors wanted!

If you're interested in helping develop this library into something more "production ready", or even just interested in using it, say hi below.

Add support for stateless function components

Scan the AST for functions matching the following criteria:

  • either declared or assigned to top-level constants
  • return type includes React.Element
  • takes one or two arguments (2 for context)

Then insert statements after the containing statement adding propTypes and contextTypes properties.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.