GithubHelp home page GithubHelp logo

ts-smoosh's Introduction

TS Smoosh

Combine type decls with related source files.

Use

Given a JavaScript file (or list of files) like so:

$ node ts-smoosh/bin ./src/some-file.js

Will produce .ts files using nearby .d.ts files.

Supported syntax

ts-smoosh parses .d.ts file, collects type declarations and runs typescript parser on .js file, injects types wherever a JsDoc type declaration is available. It supports most of the common JSDoc type declarations we use.

1. Type imports

If importing from matching .d.ts, will inline type declaration:

from

/** @typedef {import('./geokey-reducers').GeoKey} GeoKey */

to

export type GeoKey = {
  id: string,
  label: string,
  dataId: string
};

If importing from files other than matching .d.ts, will convert to type import.

from

/** @typedef {import('./kepler-types').VisState} VisState */

to

import type {VisState} from './kepler-types';

2. Functions

Functions with JSDoc type declaration using the @type {typeof import('./').updater} will be converted

From

// js
/** @type {typeof import('./project-reducers').createMapUpdater} */
const createMapUpdater = (state, action) => state

// d.ts
export declare function createMapUpdater(state: State, action: Action<void>): State;

To

export function createMapUpdater(state: State, action: Action<void>): State {
   ...
}

Running Tests

$ node test

Test cases live in various directories in ./tests. Tests run by comparing the output against golden master .tsx files. To update the masters, run node ts-smoosh/bin ./test/0X-dir/somefile.js.

Known issues

Here are some limitations and drawbacks of the scripts

1. Missing empty line inside functions

Typescript compiler doesn't preserve empty lines. When ts-smoosh injects types into functions, it will replace the existing JSDoc comment with an empty line, but empty lines inside the functions will be missing. So are empty lines between variable declarations that does not have JSDoc types. More improvements can be made such as insert empty before return statements.

2. Mixed import and export statement.

When type decl are injected, they might come between import statements. Some manual cleanup is required.

3. Unsupported JSDoc @returns @params tags

Only type declarations with the @type tag are supported. Some functions use separate tags such as below, future work can be done to support it.

/**
 * Currently not supported
 * @param {KeplerDataset} dataset
 * @param {GeoKey} geoKey
 * @returns {Array<any>} layers
 */
function defaultLayersForGeoKey(dataset, geoKey) {...}

Notes

Here are articles and docs that were helpful when writing this script.

ts-smoosh's People

Contributors

heshan0131 avatar sequence-studio avatar

Watchers

 avatar  avatar  avatar  avatar

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.