1.0.0 • Published 2 years ago

ts-autofix v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

TS AutoFix

npm version

Automatically fix TS Errors when codefixes are available.

Usage

For most use cases you should be able to use ts-autofix directly through npx.

  • npx ts-autofix list from the root of your TS project will list available fixes.
  • npx ts-autofix will attempt to run all available fixes.

If your tsconfig is not at ./tsconfig.json, you should add arg --project <pathToTsConfig> to both commands.

You can also filter which fixes are applied by using the --fixes or --errors args.

ArgumentDescription
--projectThe path to your tsconfig file. e.g. npx ts-autofix [list] --project foo/tsconfig.json
-f--fixesThe name of one or more TS fixes to apply. Use npx ts-autofix list to find available fixes. e.g. npx ts-autofix --fixes unusedIdentifier inferFromUsage
-e--errorsThe TypeScript error codes to look for fixes for. e.g. npx ts-autofix --errors TS6133 7006
--tsCliArgsAdditional CLI args for tsc to override compilerOptions. e.g. if you are trying to increase strictness of your project you might pass the additional compiler flag you are trying to enforce. e.g. npx ts-autofix [list] --tsCliArgs "--noImplicitAny"

Advanced Usage

It's also possible to use ts-autofix as a library, which gives you more control over how it runs and applies fixes.

e.g.

import { tsAutoFix } from `ts-autofix`;

tsAutoFix({
  tsConfigPath: "foo/tsconfig.json",
  diagnosticsFilter: (diag) => diag.code === 6133,
});

The input to tsAutoFix is a configuration object of type TsAutoFixOptions.

type TsAutoFixOptions = {
  tsconfigPath: string;
  compilerOptionsOverrides?: ts.CompilerOptions;
  diagnosticsFilter?: (diagnostic: ts.Diagnostic) => boolean;
  codeFixFilter?: (codeFixAction: ts.CodeFixAction) => boolean;
  preprocessCodeChanges?: (changes: ts.TextChange[], sourceFile: ts.SourceFile, diagnostic: ts.Diagnostic) => ts.TextChange[];
};
OptionDescription
tsconfigPath(required) The path to your project's tsconfig.json.
compilerOptionsOverridesOptional overrides to the compilerOptions in your tsconfig.
diagnosticsFilterAn optional callback to filter which TypeScript diagnostics/errors to attempt to find fixes for. If not defined, all diagnostics are used. Return true to include that diagnostic.
codeFixFilterAn optional callback to filter which fixes to apply. If not defined, all fixes are applied. Return true to include that fix.
preprocessCodeChangesAn optional callback to modify fixes before they are applied. This can return modified changes, or skip individual changes, but cannot modify sourceFile or diagnostic directly.

For exaple, you could use preprocessCodeChanges to modify the suggested replacements so that line comments are preserved when removing a variable.

import { tsAutoFix } from "ts-autofix";
import type * as ts from "typescript"

const preprocessCodeChanges = (
  changes: ts.TextChange[],
  sourceFile: ts.SourceFile,
  diagnostic: ts.Diagnostic
): ts.TextChange[] => {
  for (const change of changes) {
    // If the change is purely a deletion
    if (!change.newText) {
      let { start, length } = change.span;
      const removedText = sourceFile.text.substring(start, start + length);

      // Skip leading line comments when removing code.
      const match = removedText.match(/^(\s*\/\/.*\r?\n)+/);
      if (match) {
        change.span.start += match[0].length;
        change.span.length -= match[0].length;
      }
    }
  }
  return changes;
};


tsAutoFix({
  tsConfigPath: "foo/tsconfig.json",
  preprocessCodeChanges
});
1.0.0

2 years ago

0.2.0

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago