Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

feat: Ignore Unimported Files Mentioned in .gitignore #184

Merged
merged 4 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Save the file as `.unimportedrc.json` in the root of your project (next to `pack
"ignoreUnresolved": ["some-npm-dependency"],
"ignoreUnimported": ["src/i18n/locales/en.ts", "src/i18n/locales/nl.ts"],
"ignoreUnused": ["bcrypt", "create-emotion"],
"respectGitignore": true,
"scannedDirs": ["./modules"]
}
```
Expand Down Expand Up @@ -298,6 +299,14 @@ tests/**

To specify custom ignore paths, add your own patterns to `.unimportedrc.json#ignorePatterns`. Note that `unimported` won't merge settings! The custom list needs to be the full list of patterns that you want to ignore.

In addition `unimported` will also ignore files that match your `.gitignore` patterns. To disable this behavior, set `respectGitignore` to `false` in your `.unimportedrc.json` file.

```json
{
"respectGitignore": false
}
```

## Troubleshooting

Common issues or known limitations of unimported.
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"file-entry-cache": "^6.0.1",
"flow-remove-types": "2.156.0",
"glob": "^7.1.6",
"ignore": "^5.2.4",
"json5": "^2.2.0",
"ora": "^5.3.0",
"read-pkg-up": "^7.0.1",
Expand Down
18 changes: 17 additions & 1 deletion src/__tests__/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,19 @@ cases(
exitCode: 1,
stdout: /1 unimported files.*bar.js/s,
},
{
name: 'should ignore unimported file matching `.gitignore`',
files: [
{ name: 'package.json', content: '{ "main": "index.js" }' },
{ name: 'index.js', content: `import foo from './foo';` },
{ name: 'foo.js', content: '' },
{ name: 'bar.js', content: '' },
{ name: 'ignore.js', content: '' },
{ name: '.gitignore', content: '**/ignore*' },
],
exitCode: 1,
stdout: /1 unimported files.*bar.js/s,
},
{
name: 'should support JSON config',
files: [
Expand Down Expand Up @@ -854,7 +867,8 @@ export default promise
"ignoreUnresolved": [],
"ignoreUnimported": ["src/setup{Proxy,Tests}.js"],
"ignoreUnused": [],
"ignorePatterns": ["**/node_modules/**", "**/*.d.ts"]
"ignorePatterns": ["**/node_modules/**", "**/*.d.ts"],
"respectGitignore": true
}`,
},
{ name: 'src/index.tsx', content: `import './imported';` },
Expand Down Expand Up @@ -1118,6 +1132,7 @@ cases(
ignoreUnresolved: [],
ignoreUnimported: [],
ignoreUnused: [],
respectGitignore: true,
},
},
{
Expand Down Expand Up @@ -1151,6 +1166,7 @@ cases(
ignoreUnresolved: [],
ignoreUnimported: [],
ignoreUnused: ['@babel/runtime', 'meteor-node-stubs'],
respectGitignore: true,
},
},
],
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('printResults', () => {
ignoreUnimported: [],
ignoreUnused: [],
ignoreUnresolved: [],
respectGitignore: false,
},
showUnresolvedImports: false,
showUnusedDeps: false,
Expand Down
4 changes: 4 additions & 0 deletions src/__tests__/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('processResults', () => {
ignoreUnimported: [],
ignoreUnused: [],
ignoreUnresolved: [],
respectGitignore: false,
},
showUnusedFiles: false,
showUnusedDeps: false,
Expand Down Expand Up @@ -64,6 +65,7 @@ describe('processResults', () => {
ignoreUnimported: [],
ignoreUnused: [],
ignoreUnresolved: [],
respectGitignore: false,
},
showUnusedFiles: false,
showUnusedDeps: false,
Expand Down Expand Up @@ -104,6 +106,7 @@ describe('processResults', () => {
ignoreUnimported: [],
ignoreUnused: [],
ignoreUnresolved: [],
respectGitignore: false,
},
showUnusedFiles: false,
showUnusedDeps: true,
Expand Down Expand Up @@ -154,6 +157,7 @@ describe('processResults', () => {
ignoreUnimported: [],
ignoreUnused: [],
ignoreUnresolved: [],
respectGitignore: false,
},
showUnusedFiles: true,
showUnusedDeps: false,
Expand Down
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface UnimportedConfig {
ignoreUnresolved: string[];
ignoreUnimported: string[];
ignoreUnused: string[];
respectGitignore?: boolean;
moduleDirectory?: string[];
rootDir?: string;
extensions?: string[];
Expand Down Expand Up @@ -70,6 +71,7 @@ export interface Config {
ignoreUnresolved: string[];
ignoreUnimported: string[];
ignoreUnused: string[];
respectGitignore: boolean;
moduleDirectory?: string[];
rootDir?: string;
extensions: string[];
Expand Down Expand Up @@ -157,6 +159,7 @@ export async function getConfig(args?: CliArguments): Promise<Config> {
),
ignoreUnused: configFile?.ignoreUnused ?? preset?.ignoreUnused ?? [],
ignorePatterns: configFile?.ignorePatterns ?? preset?.ignorePatterns ?? [],
respectGitignore: configFile?.respectGitignore ?? true,
moduleDirectory: configFile?.moduleDirectory ?? preset?.moduleDirectory,
entryFiles: [],
extensions: [],
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export async function main(args: CliArguments): Promise<void> {
ignoreUnimported: config.ignoreUnimported,
ignoreUnused: config.ignoreUnused,
ignoreUnresolved: config.ignoreUnresolved,
respectGitignore: config.respectGitignore,
});

spinner.stop();
Expand Down
17 changes: 16 additions & 1 deletion src/process.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import ignore from 'ignore';
import { TraverseResult } from './traverse';
import { Context } from './index';
import { ensureArray } from './ensureArray';
import path from 'path';
import { exists, readText } from './fs';

export interface ProcessedResult {
unresolved: [string, string[]][];
Expand Down Expand Up @@ -41,11 +44,23 @@ export async function processResults(
!ignoreUnusedIdx[x],
);

const unimported = files
let unimported = files
.filter((x) => !traverseResult.files.has(x))
.map((x) => x.replace(context.cwd + '/', ''))
.filter((x) => !ignoreUnimportedIdx[x]);

if (context.config.respectGitignore && (await exists('.gitignore'))) {
const gitignore = (await readText('.gitignore')).split('\n');
const ig = ignore().add(gitignore);
unimported = ig
.filter(
unimported.map((x) =>
path.relative(context.cwd, path.resolve(context.cwd, x)),
),
)
.map((x) => path.join(context.cwd, x));
}

const formatTypeResultMap: { [P in FormatTypes]: boolean } = {
showUnusedFiles: !unimported.length,
showUnusedDeps: !unused.length,
Expand Down
Loading