{"analyzedAt":"2022-07-01T15:51:13.832Z","collected":{"metadata":{"name":"import-fixer","scope":"unscoped","version":"0.8.2204282156","description":"A shell command tool that cleaned up unused imports in a Typescript / Javascript code base.","date":"2022-04-28T21:57:04.570Z","author":{"name":"Sy Le","email":"le.nguyen.sy@gmail.com"},"publisher":{"username":"synle","email":"lenguyensy@gmail.com"},"maintainers":[{"username":"synle","email":"lenguyensy@gmail.com"}],"repository":{"type":"git","url":"git+https://github.com/synle/js-import-fixer.git"},"links":{"npm":"https://www.npmjs.com/package/import-fixer","homepage":"https://github.com/synle/js-import-fixer#readme","repository":"https://github.com/synle/js-import-fixer","bugs":"https://github.com/synle/js-import-fixer/issues"},"license":"MIT","devDependencies":{"@types/jest":"^27.4.1","@types/node":"^17.0.23","jest":"^27.5.1","prettier":"2.5.1","ts-jest":"^27.1.4","ts-loader":"^9.2.8","typescript":"^4.6.3","webpack":"^5.69.1","webpack-cli":"^4.9.2"},"releases":[{"from":"2022-06-01T00:00:00.000Z","to":"2022-07-01T00:00:00.000Z","count":0},{"from":"2022-04-02T00:00:00.000Z","to":"2022-07-01T00:00:00.000Z","count":17},{"from":"2022-01-02T00:00:00.000Z","to":"2022-07-01T00:00:00.000Z","count":53},{"from":"2021-07-01T00:00:00.000Z","to":"2022-07-01T00:00:00.000Z","count":53},{"from":"2020-07-01T00:00:00.000Z","to":"2022-07-01T00:00:00.000Z","count":53}],"hasTestScript":true,"readme":"[![build-main](https://github.com/synle/js-import-fixer/actions/workflows/build-main.yml/badge.svg)](https://github.com/synle/js-import-fixer/actions/workflows/build-main.yml)\n[![npm version](https://badge.fury.io/js/import-fixer.svg)](https://badge.fury.io/js/import-fixer)\n\nA script that cleans up javascript / typescript duplicates or unused imports in a deterministic way.\n\n- [JS Import Fixer - Official Home Page](https://synle.github.io/js-import-fixer/)\n- [JS Import Fixer - Github Page](https://github.com/synle/js-import-fixer)\n\n## Background Information\n\n- There are times where you worked on an existing TypeScript / JavaScript code base with a lot of unused imports. It causes eslint error to show a lot of warning. Removing the unused imports by hands is just not feasible.\n- Another area is that people tend to have no convention when it comes to importing library. Sometimes we imported external libraries first, then we imported local modules.\n\nThat's why I came up with this tool to clean up unused imports and organize imports in a way that are more deterministic.\n\n## Features\n\n- Will look at the JavaScript / TypeScript code for import usages. And remove unused imports.\n- Will also fix duplicate imports issue. Say if you have multiple lines of `import react from 'react';`. So it will consolidate that into a single import and will allow your script to compile and run.\n- There is an option `--groupImport` that will consolidate multiple lines of imports from the same library into a single one.\n\n## Requirement\n\n- Node 12+ (tested with Node 14.18.3)\n\n## How to use?\n\nRun this script in your project root.\n\n### Run it directly\n\n```bash\nnpx import-fixer\n```\n\n### Run it as part of preformat in package.json\n\nIt's best to use this script as part of your preformat script in node / frontend project\n\nSay you if you have a format script like this\n\n```js\n...\n\"format\": \"npx prettier --config ./.prettierrc --write **/*.{ts,tsx,js,jsx,scss,yml,html} *.{json,MD}\",\n...\n```\n\nThen it will become\n\n```js\n...\n\"preformat\": \"npx import-fixer --groupImport\",\n\"format\": \"npx prettier --config ./.prettierrc --write **/*.{ts,tsx,js,jsx,scss,yml,html} *.{json,MD}\",\n...\n```\n\n### Flags\n\n#### `--groupImport`\n\n- `--groupImport` : to group imports from the same library into a single line.\n\nWhen this flag is turned on, the following import lines\n\n```js\nimport { databaseActionScripts as RmdbDatabaseActionScripts } from 'src/scripts/rmdb';\nimport { tableActionScripts as RmdbTableActionScripts } from 'src/scripts/rmdb';\n```\n\nWill become\n\n```js\nimport {\n  databaseActionScripts as RmdbDatabaseActionScripts,\n  tableActionScripts as RmdbTableActionScripts,\n} from 'src/scripts/rmdb';\n```\n\nWhen this flag is turned off (by default), imports will be separated into each individual line. So the following imports\n\n```js\nimport {\n  databaseActionScripts as RmdbDatabaseActionScripts,\n  tableActionScripts as RmdbTableActionScripts,\n} from 'src/scripts/rmdb';\n```\n\nwill become\n\n```js\nimport { databaseActionScripts as RmdbDatabaseActionScripts } from 'src/scripts/rmdb';\nimport { tableActionScripts as RmdbTableActionScripts } from 'src/scripts/rmdb';\n```\n\n#### `--filter`\n\n- `--filter` : to perform the import changes on a set of files with matching filter (aka `--filter=App.tsx`). This param is a CSV, so if you have multiple files, you can add `,` in between them, for example something like this `--filter=App.tsx,Header.tsx`\n\nThe full command will look something like this\n\n```bash\nnpx import-fixer --filter=App.tsx,Header.tsx\n```\n\n#### `--ignored`\n\n- `--ignored`: similar to `--filter` but used to ignore certain files from being formatted `--ignored=__mocks__`\n\nThe full command will look something like this\n\n```bash\nnpx import-fixer --ignored=__mocks__\n```\n\n#### `--aggressive`\n\n- `--aggressive` : when turned on, the script will be more aggressive when checking for usages of the imports. By default this flag is turned off.\n\nThe full command will look something like this\n\n```bash\nnpx import-fixer --aggressive\n```\n\n#### `--transformRelativeImport`\n\n- `--transformRelativeImport` : when turned on, the script will transform relative imports such as `import IDataAdapter from './IDataAdapter';` in a file to an absolute import such as `import IDataAdapter from 'commons/adapters/IDataAdapter';`\n\n- You can add your own path prefix, by default, we will resolve the full path and add this path prefix to the front of the file.\n\nFor these examples, we will consider the original import line as followed\n\n- The minimal command will be like this.\n\n```bash\nnpx import-fixer --transformRelativeImport\n```\n\n- You can also pass in the path prefix for the resolved absolute import paths using `--transformRelativeImport=\"<pathPrefix>\"`.\n\n```bash\nnpx import-fixer --transformRelativeImport=\"src/\"\n```\n\nRefer to this table for more information:\n\n| Option                            | Original                                     | After Transformation                                            |\n| --------------------------------- | -------------------------------------------- | --------------------------------------------------------------- |\n| `--transformRelativeImport`       | `import IDataAdapter from './IDataAdapter';` | `import IDataAdapter from 'commons/adapters/IDataAdapter';`     |\n| `--transformRelativeImport=\"src\"` | `import IDataAdapter from './IDataAdapter';` | `import IDataAdapter from 'src/commons/adapters/IDataAdapter';` |\n\n#### `--importQuote`\n\n- `--importQuote`: can be used to set the import line quote. So it's either double quote or single quote. The default behavior is using single quote.\n\n- The minimal command will look like this.\n\n```bash\nnpx import-fixer --importQuote=single\n```\n\nRefer to this table for more information:\n\n| Option                           | Output                                 |\n| -------------------------------- | -------------------------------------- |\n| `--importQuote=single` (Default) | `import { SqluiCore } from 'typings';` |\n| `--importQuote=double`           | `import { SqluiCore } from \"typings\";` |\n\n#### `--parseLegacyImports`\n\nBy 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.\n\n## How to use this as a library?\n\nThe following code shows how to use this library programmatically.\n\n```bash\nnpm install --save-dev import-fixer\n```\n\n```js\nconst { fixImport } = require('import-fixer');\n\nconst actual = fixImport(\n  'abc.js',\n  `\nimport externalLib1 from 'externalLib1';\nimport {methodLib1} from 'externalLib1';\nimport {constant1, aliasMethodLib1 as myAliasMethod1, unUsedAliasMethod1 as unusedMethod1} from 'externalLib1';\nimport {aliasMethodLib1 as myAliasMethod1} from 'externalLib1';\nimport {unUsedAliasMethod1 as unusedMethod1} from 'externalLib1';\nimport externalLib2 from \"externalLib2\";\nimport {methodLib2, constant2} from \"externalLib2\";\n\nvar a1 = constant1;\nmethodLib1();\nexternalLib1();\nmyAliasMethod1();\n\nvar a2 = constant2;\nvar temp2 = externalLib2();\n  `,\n);\n\nconsole.log(actual);\n```\n\n## Limitations\n\n- The script currently only supports `import` syntax, so if you have `require` syntax in your code base, it will skip those. In the future, I plan to combine the two and give users an option to consolidate the import as `import` or require syntax.\n- The code that checks for usage of library uses contains, if your module contains a common name like Box / Button, there might be a false negative, so you might need to remove those manually.\n\n## TODO's\n\n- [x] Potentially provides option to group imports (Using [`--groupImport`](https://synle.github.io/js-import-fixer/#--groupimport)).\n- [x] Run the script on a files with matching patterns (Using [`--filter`](https://synle.github.io/js-import-fixer/#--filter)).\n- [x] Added an option to do aggressive checks for import usages. This is an opt-in feature using [`--aggressive`](https://synle.github.io/js-import-fixer/#--aggressive).\n- [x] Publish this package to npm registry.\n- [x] Make this package executable with `npx` (Using `npx import-fixer`).\n- [x] Respect the files in `.gitignore` and skip those files when running the script.\n- [x] Added an option to transform relative imports into absolute imports (Using [`--transformRelativeImport`](https://synle.github.io/js-import-fixer/#--transformRelativeImport)).\n- [x] Added an option to control what's the output quote is in the import line. Either single quote or double quote. [`--importQuote`](https://synle.github.io/js-import-fixer/#--importQuote)\n- [x] Added an option to parse legacy import line with `require` (Using [`--parseLegacyImports`](https://synle.github.io/js-import-fixer/#--parseLegacyImports))\n- [ ] Maybe create a VS Code addon or a separate Electron standalone app that visualize the import transformation and allows user to fine tune the translation one by one.\n\n## Examples Run\n\nI used this on my other project `sqlui-native`. You can refer to [this Sample Pull Request with import-fix script run](https://github.com/synle/sqlui-native/pull/103/files) to see the detailed changes in action\n\n![demo](https://user-images.githubusercontent.com/3792401/154776692-15db9288-5192-46aa-bef6-f7105349dd7d.gif)\n![image](https://user-images.githubusercontent.com/3792401/154777798-0cdb9b5c-aa1c-455c-afbd-41a00e6c8166.png)\n\n## Contributing?\n\nIf you are interested in contributing, you can refer to this doc to get started\n\n- [CONTRIBUTING.md](https://github.com/synle/js-import-fixer/blob/main/CONTRIBUTING.md)"},"npm":{"downloads":[{"from":"2022-06-30T00:00:00.000Z","to":"2022-07-01T00:00:00.000Z","count":37},{"from":"2022-06-24T00:00:00.000Z","to":"2022-07-01T00:00:00.000Z","count":229},{"from":"2022-06-01T00:00:00.000Z","to":"2022-07-01T00:00:00.000Z","count":936},{"from":"2022-04-02T00:00:00.000Z","to":"2022-07-01T00:00:00.000Z","count":2662},{"from":"2022-01-02T00:00:00.000Z","to":"2022-07-01T00:00:00.000Z","count":4179},{"from":"2021-07-01T00:00:00.000Z","to":"2022-07-01T00:00:00.000Z","count":4179}],"starsCount":0},"github":{"homepage":"https://synle.github.io/js-import-fixer/","starsCount":1,"forksCount":0,"subscribersCount":1,"issues":{"count":86,"openCount":4,"distribution":{"3600":76,"10800":2,"32400":1,"97200":0,"291600":0,"874800":0,"2624400":3,"7873200":4,"23619600":0,"70858800":0,"212576400":0},"isDisabled":false},"contributors":[{"username":"synle","commitsCount":85}],"commits":[{"from":"2022-06-24T00:00:00.000Z","to":"2022-07-01T00:00:00.000Z","count":0},{"from":"2022-06-01T00:00:00.000Z","to":"2022-07-01T00:00:00.000Z","count":0},{"from":"2022-04-02T00:00:00.000Z","to":"2022-07-01T00:00:00.000Z","count":45},{"from":"2022-01-02T00:00:00.000Z","to":"2022-07-01T00:00:00.000Z","count":85},{"from":"2021-07-01T00:00:00.000Z","to":"2022-07-01T00:00:00.000Z","count":85}]},"source":{"files":{"readmeSize":9540,"testsSize":79843,"hasNpmIgnore":true},"linters":["prettier"]}},"evaluation":{"quality":{"carefulness":0.45999999999999996,"tests":0.6,"health":1,"branding":0},"popularity":{"communityInterest":3,"downloadsCount":887.3333333333334,"downloadsAcceleration":7.8795091324200905,"dependentsCount":0},"maintenance":{"releasesFrequency":1,"commitsFrequency":0.9554452054794521,"openIssues":1,"issuesDistribution":1}},"score":{"final":0.5925319399934781,"detail":{"quality":0.7529586381780047,"popularity":0.047555281543076214,"maintenance":1}}}