Skip to content

Commit

Permalink
misc tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwind committed Jul 18, 2024
1 parent e96db1a commit 7091ee0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 39 deletions.
7 changes: 4 additions & 3 deletions docs/rules/no-unused-modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ This rule takes the following option:

- **`missingExports`**: if `true`, files without any exports are reported (defaults to `false`)
- **`unusedExports`**: if `true`, exports without any static usage within other modules are reported (defaults to `false`)
- **`ignoreUnusedTypeExports`**: if `true`, type exports without any static usage within other modules are reported (defaults to `false` and has no effect unless `unusedExports` is `true`)
- `src`: an array with files/paths to be analyzed. It only applies to unused exports. Defaults to `process.cwd()`, if not provided
- **`ignoreUnusedTypeExports`**: if `true`, TypeScript type exports without any static usage within other modules are reported (defaults to `false` and has no effect unless `unusedExports` is `true`) - `src`: an array with files/paths to be analyzed. It only applies to unused exports. Defaults to `process.cwd()`, if not provided
- `ignoreExports`: an array with files/paths for which unused exports will not be reported (e.g module entry points in a published package)

### Example for missing exports
Expand Down Expand Up @@ -117,7 +116,9 @@ export function doAnything() {
export default 5 // will not be reported
```

### Example for unused exports with `ignoreUnusedTypeExports` set to `true`
### Unused exports with `ignoreUnusedTypeExports` set to `true`

The following will not be reported:

```ts
export type Foo = {}; // will not be reported
Expand Down
96 changes: 60 additions & 36 deletions tests/src/rules/no-unused-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -1148,24 +1148,6 @@ context('TypeScript', function () {
parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-e-used-as-type.ts'),
}),
test({
options: unusedExportsTypescriptIgnoreUnusedTypesOptions,
code: `export interface c {};`,
parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-c-used-as-type.ts'),
}),
test({
options: unusedExportsTypescriptIgnoreUnusedTypesOptions,
code: `export type d = {};`,
parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-d-used-as-type.ts'),
}),
test({
options: unusedExportsTypescriptIgnoreUnusedTypesOptions,
code: `export enum e { f };`,
parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-e-used-as-type.ts'),
}),
// Should also be valid when the exporting files are linted before the importing ones
isESLint4TODO ? [] : test({
options: unusedExportsTypescriptOptions,
Expand All @@ -1191,24 +1173,6 @@ context('TypeScript', function () {
parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-f-import-type.ts'),
}),
test({
options: unusedExportsTypescriptIgnoreUnusedTypesOptions,
code: `export interface c {};`,
parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-c-unused.ts'),
}),
test({
options: unusedExportsTypescriptIgnoreUnusedTypesOptions,
code: `export type d = {};`,
parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-d-unused.ts'),
}),
test({
options: unusedExportsTypescriptIgnoreUnusedTypesOptions,
code: `export enum e { f };`,
parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-e-unused.ts'),
}),
),
invalid: [].concat(
test({
Expand Down Expand Up @@ -1252,6 +1216,66 @@ context('TypeScript', function () {
});
});

describe('ignoreUnusedTypeExports', () => {
getTSParsers().forEach((parser) => {
typescriptRuleTester.run('no-unused-modules', rule, {
valid: [
// unused vars should not report
test({
options: unusedExportsTypescriptIgnoreUnusedTypesOptions,
code: `export interface c {};`,
parser,
filename: testFilePath(
'./no-unused-modules/typescript/file-ts-c-unused.ts',
),
}),
test({
options: unusedExportsTypescriptIgnoreUnusedTypesOptions,
code: `export type d = {};`,
parser,
filename: testFilePath(
'./no-unused-modules/typescript/file-ts-d-unused.ts',
),
}),
test({
options: unusedExportsTypescriptIgnoreUnusedTypesOptions,
code: `export enum e { f };`,
parser,
filename: testFilePath(
'./no-unused-modules/typescript/file-ts-e-unused.ts',
),
}),
// used vars should not report
test({
options: unusedExportsTypescriptIgnoreUnusedTypesOptions,
code: `export interface c {};`,
parser,
filename: testFilePath(
'./no-unused-modules/typescript/file-ts-c-used-as-type.ts',
),
}),
test({
options: unusedExportsTypescriptIgnoreUnusedTypesOptions,
code: `export type d = {};`,
parser,
filename: testFilePath(
'./no-unused-modules/typescript/file-ts-d-used-as-type.ts',
),
}),
test({
options: unusedExportsTypescriptIgnoreUnusedTypesOptions,
code: `export enum e { f };`,
parser,
filename: testFilePath(
'./no-unused-modules/typescript/file-ts-e-used-as-type.ts',
),
}),
],
invalid: [],
});
});
});

describe('correctly work with JSX only files', () => {
jsxRuleTester.run('no-unused-modules', rule, {
valid: [
Expand Down

0 comments on commit 7091ee0

Please sign in to comment.