Code Monkey home page Code Monkey logo

ts-fix's Introduction

This tool is to automate the application of TypeScript codefixes across your TypeScript repositories.

Download

If cloning from GitHub, after cloning, run

npm run build
npm link

ts-fix can then be used in the command line

Example Usage

ts-fix -t path/to/tsconfig.json -f nameOfCodefix ts-fix -e 4114 --write ts-fix --interactiveMode --file relativePathToFile

Flags

Options:
      --help                Show help                                             [boolean]
      --version             Show version number                                   [boolean]
  -t, --tsconfig            Path to project's tsconfig    
                                                      [string] [default: "./tsconfig.json"]
  -e, --errorCode           The error code(s)                        [number] [default: []]
  -f, --fixName             The name(s) of codefixe(s) to apply      [string] [default: []]
  -w, --write               Tool will only emit or overwrite files if --write is included                                                       [boolean] [default: false]
  -o, --outputFolder        Path of output directory                               [string]
      --interactiveMode     Enables interactive mode             [boolean] [default: false]
      --file                Relative paths to files                  [string] [default: []]
      --showMultiple        Shows multiple fixes for a diagnostic    
                                                                 [boolean] [default: false]
      --ignoreGitStatus     Ignore working tree and force the emitting or overwriting of files                                                      [boolean] [default: false]

-t path/to/tsconfig.json or --tsconfig path/to/tsconfig.json Specifies the project to use the tool on. If no arguement given, the tool will use the tsconfig in the current working directory.

-e <number> or --errorCode <number> Specifies the errors to fix. Several error codes can be specified during the same command. The easiest way to find error codes in your repository is to hover over the error in your IDE.

-f <name> or --fixName <name> Specifies the types of codefixes to use. Several names can be specified.

If both error numbers and fix names are given, then in order to be applied, a fix must be generated by a specified error and have a name that was specified.

--write Boolean for if the tool should overwrite previous code files with the codefixed code or emit any files with codefixed code. If --write not included, then the tool will print to console which files would have changed.

--interactiveMode Boolean for enabling interactive CLI to visualize which code fixes to apply to your project. Some special cases to keep in mind are:

  1. One diagnostic might be tied to more than one code fix. A simple example of this is when the paramenters of a functions have any type and the inferFromUsage fix is recommended. Let's say that you want to fix only error code 7019, this could also fix 7006 if the function parameters has both diagnostics.
  2. Some codefixes are applied on a different location from where the actual diagnostic is.Some examples: 2.1 When the code fix is applied on the diagnostic's related information instead. 2.2 When the code fix is applied in an entire different file from the original file.

--file <filename> Relative file path(s) to the file(s) in which to find diagnostics and apply quick fixes. The path is relative to the project folder.

--showMultiple Boolean for enabling showing multiple fixes for diagnostics for which this applies. One consideration when --showMultiple = true is that the tool migth not be able to find consecutives fixes afecting the same span if those diagnostics have mutliple fixes.

--ignoreGitStatus Boolean to force the overwriting of files when --write = true and output folder matches project folder. If you are sure you would like to run ts-fix on top of your current changes provide this flag.

ts-fix's People

Contributors

andrewbranch avatar danay1999 avatar iisaduan avatar microsoft-github-policy-service[bot] 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ts-fix's Issues

Rethink fixing by error code and fixName

Original: iisaduan/ts-fix#3

interface I {}

declare const obj: I;

obj.foo;
//  ^^^ Property 'aa' does not exist on type 'I'.ts(2339)

This error returns two fixes:

image

For each, the fixName is fixMissingMember. They represent two different ways of fixing the same thing. I believe the current code will try to apply them both. Unfortunately, there’s not actually a way to differentiate them besides their description property, which is dynamic.

A minimal fix, probably good enough to start with, would be to only apply the first fix returned for a given error, since we try to sort the most likely fixes first. If we need more granularity, we’d need an API change from TypeScript itself.

Git check fails on MacOS

The path created to the git repo folder for checking its status uses an explicit backslash instead of path.join() which causes it to throw an error on MacOS:

/path/to/repo/node_modules/ts-fix/dist/index.js:90
            throw new Error(err.message);
            ^

Error: Command failed: git --git-dir="/path/to/repo\.git" --work-tree="/path/to/repo" status --porcelain
fatal: not a git repository: '/path/to/repo\.git'

    at /path/to/repo/node_modules/ts-fix/dist/index.js:90:19
    at ChildProcess.exithandler (node:child_process:404:5)
    at ChildProcess.emit (node:events:390:28)
    at maybeClose (node:internal/child_process:1064:16)
    at Socket.<anonymous> (node:internal/child_process:450:11)
    at Socket.emit (node:events:390:28)
    at Pipe.<anonymous> (node:net:687:12)

The path is generated here.

This happens even with the --ignoreGitStatus flag.

git Status testing and additional support

  • gitStatus needs to be tested, unable to use -w when testing integration > cases because the functionality hasn't been implemented in TestHost. In Andrew's words "git status functionality should be done through the Host, and a mock Host can be injected into the test runs that returns fake results for git operations".
  • gitStatus needs support for when the repository and the project folder are on a different absolute path

Possible improvements

  • Add Back option in the showMultiple menu in --interactiveMode
  • Maybe let the user decide when to write to files so they don't have to sit through all of it, if they can't
  • Keep a counter of how many fixes are left after each accept, skip, acceptall, rejectall, etc.
  • Let the user decide how many passes to allow
  • Truncate notappliedFixes, allChangesFiles, etc. in printSummary
  • Implement exclude flags - what if a user wants to exclude some code fixes or some error codes?

Require a fix name / error code

Original issue: iisaduan/ts-fix#15

@andrewbranch commented July 28, 2021
I think we should do away with the default of applying all available fixes. It’s bound to produce a ton of undesired changes.

@iisaduan commented Aug 13, 2021
Currently, behavior is fixes are only applied if at least one of errorCode or fixName is supplied. Otherwise process exits with warning. 26bb5c6

There could be other ways to handle the case where no fixes or errors are given, such as printing what errors and fixes are present

Rethink sorting fixes by start

Currently, the tool accepts all fixes that don't collide, and if there are colliding fixes, the tool then picks the fix that occurs first by start index. This however, can produce several unwanted effects in the resulting code, such as applying multiple fixes for one error, or prioritizing changes by how the code is organized, instead of which fix is most likely to be applied. As written in #5, this is already done by services, and could be used to improve the output of the tool.

Add descriptive error handling

Original issue: iisaduan/ts-fix#7

Currently, this is most needed in createProject for when a project is unable to be created. There are a multitude of reasons this could happen, the most common being the options.tsconfig does not link to a valid tsconfig for a project. Knowing why a project could not be created would be helpful for troubleshooting.

Additional CLI flags/improve functionality

Currently (b3cedc8), the following flags can be added or improved

  • entries for custom --outputFolder should be tested to work on all OS and in bigger repositories with more complicated folder structures
  • --file or similar, so the tool can be run on specified file(s)
  • --verbose or similar, which describes which messages are outputted during runtime

More support for fixes applied to the same span

Currently ts-fix only identifies fixes being applied to the same span if they are consecutive.
It should be able to identify them even when they aren't consecutive.
It gets a little more complicated when multiple diagnostics are showing AND there are fixes being applied to the same span, how to handle that?

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.