Skip to content

Commit

Permalink
feat: add no-unused-exports rule
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Feb 7, 2023
1 parent 87b9799 commit ad22fe4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
14 changes: 8 additions & 6 deletions .README/rules/no-unused-exports.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
### `no-unused-exports`

Identifies unused exports.
Identifies unused TypeScript exports.

> **Note** This rule uses [`ts-unused-exports`](https://github.com/pzavolinsky/ts-unused-exports) program behind the scenes.
> **Note** This rule is implemented using [`ts-unused-exports`](https://github.com/pzavolinsky/ts-unused-exports).
#### Options

|Config|Type|Description|
|---|---|---|
|`tsConfigPath`|string|Path to [tsconfig.json](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html)]
|Config|Type|Required|Default|Description|
|---|---|---|---|---|
|`tsConfigPath`|string|Required||Path to [tsconfig.json](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html)|
|`allowUnusedEnums`|boolean||`false`|Allow unused `enum`s.|
|`allowUnusedTypes`|boolean||`false`|Allow unused `type` and `interface`.|

<!-- assertions noUnusedExports -->
<!-- assertions noUnusedExports -->
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,18 +309,19 @@ The 1st option is an array of strings that cannot be contained in the codebase.
<a name="eslint-plugin-canonical-rules-no-unused-exports"></a>
### <code>no-unused-exports</code>

Identifies unused exports.
Identifies unused TypeScript exports.

> **Note** This rule uses [`ts-unused-exports`](https://github.com/pzavolinsky/ts-unused-exports) program behind the scenes.
> **Note** This rule is implemented using [`ts-unused-exports`](https://github.com/pzavolinsky/ts-unused-exports).
<a name="user-content-eslint-plugin-canonical-rules-no-unused-exports-options-2"></a>
<a name="eslint-plugin-canonical-rules-no-unused-exports-options-2"></a>
#### Options

|Config|Type|Description|
|---|---|---|
|`tsConfigPath`|string|Path to [tsconfig.json](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html)]

|Config|Type|Required|Default|Description|
|---|---|---|---|---|
|`tsConfigPath`|string|Required||Path to [tsconfig.json](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html)|
|`allowUnusedEnums`|boolean||`false`|Allow unused `enum`s.|
|`allowUnusedTypes`|boolean||`false`|Allow unused `type` and `interface`.|


<a name="user-content-eslint-plugin-canonical-rules-no-use-extend-native"></a>
Expand Down
22 changes: 21 additions & 1 deletion src/rules/noUnusedExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import analyzeTsConfig from 'ts-unused-exports';
import { createRule } from '../utilities';

const defaultOptions = {
allowUnusedEnums: false,
allowUnusedTypes: false,
tsConfigPath: '',
};

type Options = [
{
allowUnusedEnums: boolean;
allowUnusedTypes: boolean;
tsConfigPath: string;
},
];
Expand All @@ -17,7 +21,17 @@ export default createRule<Options, MessageIds>({
create: (context) => {
const [options] = context.options;

const result = analyzeTsConfig(options.tsConfigPath);
const tsUnusedOptions: string[] = [];

if (options.allowUnusedEnums) {
tsUnusedOptions.push('--allowUnusedEnums');
}

if (options.allowUnusedTypes) {
tsUnusedOptions.push('--allowUnusedTypes');
}

const result = analyzeTsConfig(options.tsConfigPath, tsUnusedOptions);

return {
Program() {
Expand Down Expand Up @@ -65,6 +79,12 @@ export default createRule<Options, MessageIds>({
{
additionalProperties: false,
properties: {
allowUnusedEnums: {
type: 'boolean',
},
allowUnusedTypes: {
type: 'boolean',
},
tsConfigPath: {
type: 'string',
},
Expand Down

0 comments on commit ad22fe4

Please sign in to comment.