GithubHelp home page GithubHelp logo

jamiemason / ts-import-types-cli Goto Github PK

View Code? Open in Web Editor NEW
36.0 3.0 3.0 23 KB

Autofix TypeScript types to be imported using `import type`

JavaScript 18.15% TypeScript 81.85%
typescript cli codemod codemods ts-morph jscodeshift type-only-imports refactor refactoring-tools

ts-import-types-cli's Introduction

ts-import-types-cli

Autofix TypeScript types to be imported using import type

Installation

npm install -g ts-import-types-cli

Usage

Usage: ts-import-types-cli [options] [patterns...]

Options:
  -V, --version              output the version number
  -d, --dry-run              write output to stdout instead of overwriting files
  -p, --project [path]       path to tsconfig.json
  -O, --no-organise-imports  disable use of VS Code's organise imports refactoring
  -h, --help                 display help for command

Example

$ ts-import-types-cli --project ./tsconfig.json 'src/**/*.ts' 'src/**/*.tsx'
import { interpret, StateValue } from 'xstate';
import { sendSearch } from '../../services/search-client/send-search';
import { createAlgoliaMachine } from '../machine';
import { AlgoliaInterpreter, AlgoliaMachine } from '../machine/types';

// ...the rest of the file

↓↓↓↓↓

import type { StateValue } from 'xstate'
import { interpret } from 'xstate'
import { sendSearch } from '../../services/search-client/send-search'
import { createAlgoliaMachine } from '../machine'
import type { AlgoliaInterpreter, AlgoliaMachine } from '../machine/types'

// ...the rest of the file

Triple-Slash Directives

Moving triple-slash directives such as /// <reference lib="webworker" /> back to the top of the file is not yet supported. If you know how to do this using https://ts-morph.com please open a PR or otherwise let me know.

Unfortunately until then, files will need their triple-slash directives manually moving back to the top of the file.

ts-import-types-cli will output a list of which files are affected.

ts-import-types-cli's People

Contributors

jamiemason 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

Watchers

 avatar  avatar  avatar

ts-import-types-cli's Issues

How to submit failing / unexpected transforms

I ran this on a project recently and hit a few types of failing or unexpected behavior. I don't know how best to iterate on this so I figured I'll submit a few steps & screenshots and we can decide split them into new tickets or whatever.

  1. After working around #1, it ran successfully Screen Shot 2021-02-26 at 10 28 47 AM

  2. but failed a lint check with 2 types of errorsScreen Shot 2021-02-26 at 11 15 48 AM

  3. The parsing error was due to this (note import { "path/to/file" ...)Screen Shot 2021-02-26 at 11 17 12 AM

  4. The "restricted path" errors were because a transform lost a comment that was above it Screen Shot 2021-02-26 at 11 25 38 AM

After that, I went through the changes and found a found a few patterns of errors

Duplicate/repeated member imports Screen Shot 2021-02-26 at 12 25 31 PM Screen Shot 2021-02-26 at 11 46 04 AM Screen Shot 2021-02-26 at 11 53 19 AM

Lost members
Screen Shot 2021-02-26 at 12 01 02 PM

Lost `import * as` Screen Shot 2021-02-26 at 11 51 28 AM Screen Shot 2021-02-26 at 12 09 19 PM Screen Shot 2021-02-26 at 12 11 31 PM

I'm not sure what correct behavior is for 4. It wasn't hard to fix, just pointing it out. That said,

I don't understand this comment move
Screen Shot 2021-02-26 at 12 07 15 PM

There might be 5-20 different examples for the lost import and duplicated members patterns. There might be some others, but let's start with these few. Let me know where and how best I can help and I'm glad to do it.

Thanks for this. Even with these issues it was still very useful.

Support comments and other valid tsconfig.json features

I ran ts-import-types-cli -p path/to/tsconfig.json -d and it failed with

SyntaxError: /Users/jfsiii/work/kibana/x-pack/plugins/fleet/tsconfig.json: Unexpected token } in JSON at position 212
    at parse (<anonymous>)
    at Object.Module._extensions..json (internal/modules/cjs/loader.js:1106:22)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/usr/local/lib/node_modules/ts-import-types-cli/dist/bin.js:45:5)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
ts-import-types-cli --project /Users/jfsiii/work/kibana/x-pack/plugins/fleet/tsconfig.json is not a tsconfig.json file

The config file had comments. Removing them fixed the error.

Comments are supported in tsconfig.json (e.g. microsoft/TypeScript#5450)

I think the CLI should fail using valid config files.

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.