js-import-fixer

build-main npm version

A script that cleans up javascript / typescript duplicates or unused imports in a deterministic way.

Background Information

That’s why I came up with this tool to clean up unused imports and organize imports in a way that are more deterministic.

Features

Requirement

How to use?

Run this script in your project root.

Run it directly

npx import-fixer

Run it as part of preformat in package.json

It’s best to use this script as part of your preformat script in node / frontend project

Say you if you have a format script like this

...
"format": "npx prettier --config ./.prettierrc --write **/*.{ts,tsx,js,jsx,scss,yml,html} *.{json,MD}",
...

Then it will become

...
"preformat": "npx import-fixer --groupImport",
"format": "npx prettier --config ./.prettierrc --write **/*.{ts,tsx,js,jsx,scss,yml,html} *.{json,MD}",
...

Flags

--groupImport

When this flag is turned on, the following import lines

import { databaseActionScripts as RmdbDatabaseActionScripts } from 'src/scripts/rmdb';
import { tableActionScripts as RmdbTableActionScripts } from 'src/scripts/rmdb';

Will become

import {
  databaseActionScripts as RmdbDatabaseActionScripts,
  tableActionScripts as RmdbTableActionScripts,
} from 'src/scripts/rmdb';

When this flag is turned off (by default), imports will be separated into each individual line. So the following imports

import {
  databaseActionScripts as RmdbDatabaseActionScripts,
  tableActionScripts as RmdbTableActionScripts,
} from 'src/scripts/rmdb';

will become

import { databaseActionScripts as RmdbDatabaseActionScripts } from 'src/scripts/rmdb';
import { tableActionScripts as RmdbTableActionScripts } from 'src/scripts/rmdb';

--filter

The full command will look something like this

npx import-fixer --filter=App.tsx,Header.tsx

--ignored

The full command will look something like this

npx import-fixer --ignored=__mocks__

--aggressive

The full command will look something like this

npx import-fixer --aggressive

--transformRelativeImport

For these examples, we will consider the original import line as followed

npx import-fixer --transformRelativeImport
npx import-fixer --transformRelativeImport="src/"

Refer to this table for more information:

Option Original After Transformation
--transformRelativeImport import IDataAdapter from './IDataAdapter'; import IDataAdapter from 'commons/adapters/IDataAdapter';
--transformRelativeImport="src" import IDataAdapter from './IDataAdapter'; import IDataAdapter from 'src/commons/adapters/IDataAdapter';

--importQuote

npx import-fixer --importQuote=single

Refer to this table for more information:

Option Output
--importQuote=single (Default) import { SqluiCore } from 'typings';
--importQuote=double import { SqluiCore } from "typings";

--parseLegacyImports

By default, we don’t parse legacy import lines (with require), ie. const fs = require('fs'). To enable this feature you need to pass in --parseLegacyImports parameter.

How to use this as a library?

The following code shows how to use this library programmatically.

npm install --save-dev import-fixer
const { fixImport } = require('import-fixer');

const actual = fixImport(
  'abc.js',
  `
import externalLib1 from 'externalLib1';
import {methodLib1} from 'externalLib1';
import {constant1, aliasMethodLib1 as myAliasMethod1, unUsedAliasMethod1 as unusedMethod1} from 'externalLib1';
import {aliasMethodLib1 as myAliasMethod1} from 'externalLib1';
import {unUsedAliasMethod1 as unusedMethod1} from 'externalLib1';
import externalLib2 from "externalLib2";
import {methodLib2, constant2} from "externalLib2";

var a1 = constant1;
methodLib1();
externalLib1();
myAliasMethod1();

var a2 = constant2;
var temp2 = externalLib2();
  `,
);

console.log(actual);

Limitations

TODO’s

Examples Run

I used this on my other project sqlui-native. You can refer to this Sample Pull Request with import-fix script run to see the detailed changes in action

demo image

Contributing?

If you are interested in contributing, you can refer to this doc to get started