mockdeep / typewiz

Automatically discover and add missing types in your TypeScript code
https://medium.com/@urish/manual-typing-is-no-fun-introducing-typewiz-58e3e8813f4c
1.1k stars 46 forks source link

Configuration file support #31

Closed urish closed 6 years ago

urish commented 6 years ago

Once #27 is merged, or #29 is implemented, it will make sense to have some configuration file to toggle these behaviors. A few more things the user may want to configure:

And I am pretty sure we will have more down the road. This also paves the road to adding experimental features, which will be turned off by default, but available for adventurous users.

zoehneto commented 6 years ago

I'm thinking about implementing this since I just switched my build to typewiz-webpack and I really want to take advantage of the newer features which aren't enabled by default. I imagine the following:

Config File

typewiz.json

the result would look something like:

{
    /**
     * If given, all the file paths in the collected type info will be resolved relative to this directory.
     */
    rootDir?: string;

    /**
     * Path to your project's tsconfig file
     */
    tsConfig?: string;

    // You probably never need to touch these two - they are used by the integration tests to setup
    // a virtual file system for TS:
    tsConfigHost?: ts.ParseConfigHost;
    tsCompilerHost?: ts.CompilerHost;

    instrumentCallExpressions: boolean;
    instrumentImplicitThis: boolean;

   /**
     * A prefix that will be added in front of each type applied. You can use a javascript comment
     * to mark the automatically added types. The prefix will be added after the colon character,
     * just before the actual type.
     */
    prefix?: string;
}

Plugin Integration

typewiz-webpack

We can pass the config path to the loader:

use: [
    {
        loader: 'typewiz-webpack',
        options: {
            config: 'some/path/typewiz.json'
        }
    },
    'ts-loader'
],

and to the plugin:

plugins: [
    new TypewizPlugin('some/path/typewiz.json')
]

typewiz-node

We could pass the config path to the normal command or detect it automatically, the former might be difficult when using it with mocha though.

Internal Handling

This leaves the question what the config file actually does. Do we want a central config file handling in typewiz (with instrument() and applyTypes() using this config object) or should the individual plugin interpret the config file and pass the correct options to the existing functions? If the former is chosen, what does this mean for people using the api directly?

urish commented 6 years ago

Great write up @zoehneto, pretty much summarizes my thoughts :)

As for the internal handling part, I see two approaches:

  1. Divide the config file into sections, e.g.
{
  "common": {
     "rootDir": "...",
     "tsConfig": "..."
  },
  "instrument": {
     "instrumentCallExpressions": true,
     "instrumentImplicitThis": true
   },
   "applyTypes": {
      "prefix": "TypeWiz |"
   }
}

Then you can just pass the appropriate section to each of the functions, merged with the "common" section.

Another approach would be to create helper functions that only return the relevant part of configurations for each of the functions, e.g. applyTypesConfig() that will get a typewiz configuration (in any format we decide on), and return the relevant options for applyTypes. This will allow much more flexibility in the config file format.

Thoughts?

zoehneto commented 6 years ago

I like the splitting into different sections from approach 1 since it nicely represents the stages of the typewiz process and also makes it more clear what options do / where they are used ('prefix' by its self doesn't tell you when its used). But we should probably still create the helper functions so we can decouple the config file properties from the options passed to the individual functions.

zoehneto commented 6 years ago

Basic config file support is merged, here are the tasks left for full support:

zoehneto commented 6 years ago

I created a PR for Schema Store at SchemaStore/schemastore#433

zoehneto commented 6 years ago

I just submitted a PR for urish/typewiz-webpack-demo. That means that once all relevant PRs are merged this issue is resolved.