Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
simone committed Oct 29, 2024
1 parent 476c121 commit 40cde0a
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "ts-unused-exports",
"version": "10.1.0",
"description": "ts-unused-exports finds unused exported symbols in your Typescript project",
"main": "lib/app.js",
"main": "lib/index.js",
"repository": {
"type": "git",
"url": "https://github.com/pzavolinsky/ts-unused-exports.git"
Expand Down
4 changes: 1 addition & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as ts from 'typescript';
import analyze, { Analysis } from './analyzer';
import { dirname, resolve } from 'path';

import { TsConfig } from './types';
import type { TsConfig } from './types';
import { extractOptionsFromFiles } from './argsParser';
import parseFiles from './parser';

Expand Down Expand Up @@ -69,5 +69,3 @@ export const analyzeTsConfig = (
};
return analyze(parseFiles(tsConfig, args.options), options);
};

export * from './analyzer';
55 changes: 55 additions & 0 deletions src/find-unused.ts
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;
};
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './analyzer';
export * from './app';
export * from './find-unused';
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
"strictNullChecks": true,
"strict": true
},
"exclude": ["node_modules"],
"include": ["src/**/*.ts"]
"include": ["src"]
}

0 comments on commit 40cde0a

Please sign in to comment.