Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no-unused-modules: add flow tests and enhance typescript tests #1905

Closed
Closed
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
1 change: 1 addition & 0 deletions tests/files/no-unused-modules/flow/flow-0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { type FooType, type FooInterface } from './flow-2';
3 changes: 3 additions & 0 deletions tests/files/no-unused-modules/flow/flow-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @flow strict
export type Bar = number;
export interface BarInterface {};
3 changes: 3 additions & 0 deletions tests/files/no-unused-modules/flow/flow-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @flow strict
export type FooType = string;
export interface FooInterface {};
1 change: 1 addition & 0 deletions tests/files/no-unused-modules/flow/flow-3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import type { FooType, FooInterface } from './flow-4';
3 changes: 3 additions & 0 deletions tests/files/no-unused-modules/flow/flow-4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @flow strict
export type FooType = string;
export interface FooInterface {};
6 changes: 3 additions & 3 deletions tests/files/no-unused-modules/typescript/file-ts-a.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import {c} from './file-ts-c';
import {d} from './file-ts-d';
import {e} from './file-ts-e';

export const a = b + 1 + e.f;
export const a2: c = {};
export const a3: d = {};
const a = b + 1 + e.f;
const a2: c = {};
const a3: d = {};
1 change: 1 addition & 0 deletions tests/files/no-unused-modules/typescript/file-ts-b-2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const b = 2;
1 change: 1 addition & 0 deletions tests/files/no-unused-modules/typescript/file-ts-b-3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const b = 2;
1 change: 1 addition & 0 deletions tests/files/no-unused-modules/typescript/file-ts-c-2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export interface c {};
1 change: 1 addition & 0 deletions tests/files/no-unused-modules/typescript/file-ts-c-3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export interface c {};
1 change: 1 addition & 0 deletions tests/files/no-unused-modules/typescript/file-ts-d-2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type d = {};
1 change: 1 addition & 0 deletions tests/files/no-unused-modules/typescript/file-ts-d-3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type d = {};
1 change: 1 addition & 0 deletions tests/files/no-unused-modules/typescript/file-ts-e-2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export enum e { f };
1 change: 1 addition & 0 deletions tests/files/no-unused-modules/typescript/file-ts-e-3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export enum e { f };
9 changes: 9 additions & 0 deletions tests/files/no-unused-modules/typescript/file-ts-f.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type {b} from './file-ts-b-3';
import type {c} from './file-ts-c-3';
import type {d} from './file-ts-d-3';
import type {e} from './file-ts-e-3';

const a: typeof b = 2;
const a2: c = {};
const a3: d = {};
const a4: typeof e = undefined;
178 changes: 131 additions & 47 deletions tests/src/rules/no-unused-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -744,65 +744,96 @@ describe('Avoid errors if re-export all from umd compiled library', () => {
})
})

describe('correctly work with Typescript only files', () => {
typescriptRuleTester.run('no-unused-modules', rule, {
valid: [
test({
options: unusedExportsTypescriptOptions,
code: 'import a from "file-ts-a";',
parser: require.resolve('babel-eslint'),
filename: testFilePath('./no-unused-modules/typescript/file-ts-a.ts'),
}),
],
invalid: [
test({
options: unusedExportsTypescriptOptions,
code: `export const b = 2;`,
parser: require.resolve('babel-eslint'),
filename: testFilePath('./no-unused-modules/typescript/file-ts-b.ts'),
errors: [
error(`exported declaration 'b' not used within other modules`),
],
}),
test({
options: unusedExportsTypescriptOptions,
code: `export interface c {};`,
parser: require.resolve('babel-eslint'),
filename: testFilePath('./no-unused-modules/typescript/file-ts-c.ts'),
errors: [
error(`exported declaration 'c' not used within other modules`),
],
}),
test({
options: unusedExportsTypescriptOptions,
code: `export type d = {};`,
parser: require.resolve('babel-eslint'),
filename: testFilePath('./no-unused-modules/typescript/file-ts-d.ts'),
errors: [
error(`exported declaration 'd' not used within other modules`),
],
}),
],
})
})

context('TypeScript', function () {
getTSParsers().forEach((parser) => {
typescriptRuleTester.run('no-unused-modules', rule, {
valid: [
test({
options: unusedExportsTypescriptOptions,
code: 'import a from "file-ts-a";',
code: `
import {b} from './file-ts-b';
import {c} from './file-ts-c';
import {d} from './file-ts-d';
import {e} from './file-ts-e';

const a = b + 1 + e.f;
const a2: c = {};
const a3: d = {};
`,
parser: parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-a.ts'),
}),
test({
options: unusedExportsTypescriptOptions,
code: `export const b = 2;`,
parser: parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-b.ts'),
}),
test({
options: unusedExportsTypescriptOptions,
code: `export interface c {};`,
parser: parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-c.ts'),
}),
test({
options: unusedExportsTypescriptOptions,
code: `export type d = {};`,
parser: parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-d.ts'),
}),
test({
options: unusedExportsTypescriptOptions,
code: `export enum e { f };`,
parser: parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-e.ts'),
}),
test({
options: unusedExportsTypescriptOptions,
code: `
import type {b} from './file-ts-b-3';
import type {c} from './file-ts-c-3';
import type {d} from './file-ts-d-3';
import type {e} from './file-ts-e-3';

const a: typeof b = 2;
const a2: c = {};
const a3: d = {};
const a4: typeof e = undefined;
`,
parser: parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-f.ts'),
}),
test({
options: unusedExportsTypescriptOptions,
code: `export const b = 2;`,
parser: parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-b-3.ts'),
}),
test({
options: unusedExportsTypescriptOptions,
code: `export interface c {};`,
parser: parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-c-3.ts'),
}),
test({
options: unusedExportsTypescriptOptions,
code: `export type d = {};`,
parser: parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-d-3.ts'),
}),
test({
options: unusedExportsTypescriptOptions,
code: `export enum e { f };`,
parser: parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-e-3.ts'),
}),
],
invalid: [
test({
options: unusedExportsTypescriptOptions,
code: `export const b = 2;`,
parser: parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-b.ts'),
filename: testFilePath('./no-unused-modules/typescript/file-ts-b-2.ts'),
errors: [
error(`exported declaration 'b' not used within other modules`),
],
Expand All @@ -811,7 +842,7 @@ context('TypeScript', function () {
options: unusedExportsTypescriptOptions,
code: `export interface c {};`,
parser: parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-c.ts'),
filename: testFilePath('./no-unused-modules/typescript/file-ts-c-2.ts'),
errors: [
error(`exported declaration 'c' not used within other modules`),
],
Expand All @@ -820,7 +851,7 @@ context('TypeScript', function () {
options: unusedExportsTypescriptOptions,
code: `export type d = {};`,
parser: parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-d.ts'),
filename: testFilePath('./no-unused-modules/typescript/file-ts-d-2.ts'),
errors: [
error(`exported declaration 'd' not used within other modules`),
],
Expand All @@ -829,7 +860,7 @@ context('TypeScript', function () {
options: unusedExportsTypescriptOptions,
code: `export enum e { f };`,
parser: parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-e.ts'),
filename: testFilePath('./no-unused-modules/typescript/file-ts-e-2.ts'),
errors: [
error(`exported declaration 'e' not used within other modules`),
],
Expand Down Expand Up @@ -862,3 +893,56 @@ describe('correctly work with JSX only files', () => {
],
})
})

describe('correctly report flow types', () => {
ruleTester.run('no-unused-modules', rule, {
valid: [
test({
options: unusedExportsOptions,
code: 'import { type FooType, type FooInterface } from "./flow-2";',
parser: require.resolve('babel-eslint'),
filename: testFilePath('./no-unused-modules/flow/flow-0.js'),
}),
test({
options: unusedExportsOptions,
code: `// @flow strict
export type FooType = string;
export interface FooInterface {};
`,
parser: require.resolve('babel-eslint'),
filename: testFilePath('./no-unused-modules/flow/flow-2.js'),
}),
test({
options: unusedExportsOptions,
code: 'import type { FooType, FooInterface } from "./flow-4";',
parser: require.resolve('babel-eslint'),
filename: testFilePath('./no-unused-modules/flow/flow-3.js'),
}),
test({
options: unusedExportsOptions,
code: `// @flow strict
export type FooType = string;
export interface FooInterface {};
`,
parser: require.resolve('babel-eslint'),
filename: testFilePath('./no-unused-modules/flow/flow-4.js'),
}),
],
invalid: [
test({
options: unusedExportsOptions,
code: `// @flow strict
export type Bar = number;
export interface BarInterface {};
`,
parser: require.resolve('babel-eslint'),
filename: testFilePath('./no-unused-modules/flow/flow-1.js'),
errors: [
error(`exported declaration 'Bar' not used within other modules`),
error(`exported declaration 'BarInterface' not used within other modules`),
],
}),
],
})
})