-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
simone
committed
Oct 29, 2024
1 parent
476c121
commit 40cde0a
Showing
5 changed files
with
61 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import path = require('path'); | ||
import { analyzeTsConfig } from './app'; | ||
import { Analysis } from './types'; | ||
|
||
interface UnusedOptions { | ||
tsConfigPath?: string; | ||
ignoreVars?: string[]; | ||
ignoreFiles?: string[]; | ||
} | ||
|
||
/** Find all the unused variables in your code. */ | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
export const findUnusedExports = ({ | ||
ignoreFiles, | ||
ignoreVars, | ||
tsConfigPath = path.resolve('.', 'tsconfig.json'), | ||
}: UnusedOptions = {}) => { | ||
const analyzed = analyzeTsConfig(tsConfigPath); | ||
const response: Analysis = {}; | ||
for (const [key, value] of Object.entries(analyzed)) { | ||
const filename = path.basename(key); | ||
if (ignoreFiles?.includes(filename)) { | ||
ignoreFiles.splice(ignoreFiles.indexOf(filename), 1); | ||
continue; | ||
} | ||
const filteredExports = []; | ||
for (const v of value) { | ||
if (ignoreVars?.includes(v.exportName)) { | ||
ignoreVars.splice(ignoreVars.indexOf(v.exportName), 1); | ||
} else { | ||
filteredExports.push(v); | ||
} | ||
} | ||
|
||
if (filteredExports.length > 0) { | ||
response[key] = filteredExports; | ||
} | ||
} | ||
|
||
if (ignoreFiles && ignoreFiles.length > 0) { | ||
throw new Error( | ||
`The following ignore entries are no longer needed: | ||
${ignoreFiles.length > 0 ? `Files: ${ignoreFiles.join(',\n')}` : ''}`, | ||
); | ||
} | ||
|
||
if (ignoreVars && ignoreVars.length > 0) { | ||
throw new Error( | ||
`The following ignore entries are no longer needed: | ||
${ignoreVars.length > 0 ? `Variables: ${ignoreVars.join(', ')}` : ''}`, | ||
); | ||
} | ||
|
||
return Object.keys(response).length > 0 ? response : undefined; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './analyzer'; | ||
export * from './app'; | ||
export * from './find-unused'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters