Skip to content

Commit

Permalink
Scripts: Add @typescript-eslint/no-unused-vars eslint rule (WordPress…
Browse files Browse the repository at this point in the history
…#62925)

Enable @typescript-eslint/no-unused-vars eslint rule in default eslint TypeScript configuration.
Disable redundant `noUnusedLocals` and `noUnusedParameters` from TypeScript configuration.

The TypeScript eslint configuration disabled no-unused-vars rules, resulting in many cases of unused vars appearing in code.

The expectation was that TypeScript handle the linting via configuration such as `noUnusedLocals` and `noUnusedParameters`. This was unreliable because TypeScript may not run on entire projects, for example if `checkJs` is disabled.

---

Co-authored-by: Chrico <chrico@git.wordpress.org>
Co-authored-by: sirreal <jonsurrell@git.wordpress.org>
Co-authored-by: gziolo <gziolo@git.wordpress.org>
  • Loading branch information
4 people authored and carstingaxion committed Jul 18, 2024
1 parent 9a53c47 commit b404003
Show file tree
Hide file tree
Showing 29 changed files with 30 additions and 37 deletions.
1 change: 0 additions & 1 deletion packages/components/src/autocomplete/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
useRef,
useMemo,
} from '@wordpress/element';
import { __, _n } from '@wordpress/i18n';
import { useInstanceId, useMergeRefs, useRefEffect } from '@wordpress/compose';
import {
create,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
Expand Down
1 change: 0 additions & 1 deletion packages/components/src/date-time/date-time/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type { ForwardedRef } from 'react';
* WordPress dependencies
*/
import { forwardRef } from '@wordpress/element';
import { __, _x } from '@wordpress/i18n';

/**
* Internal dependencies
Expand Down
5 changes: 0 additions & 5 deletions packages/components/src/font-size-picker/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/palette-edit/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ async function clearInput( input: HTMLInputElement ) {
await click( input );

// Press backspace as many times as the input's current value
// eslint-disable-next-line @typescript-eslint/no-unused-vars
for ( const _ of Array( input.value.length ) ) {
await press.Backspace();
}
Expand Down
5 changes: 0 additions & 5 deletions packages/components/src/private-apis.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* WordPress dependencies
*/
import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/private-apis';

/**
* Internal dependencies
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/components/src/theme/stories/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ export const ColorScheme: StoryFn< typeof Theme > = ( {
} ) => {
const { colors } = generateThemeVariables( { accent, background } );
const { gray, ...otherColors } = colors;
/* eslint-disable @typescript-eslint/no-unused-vars */
const contrastIssues = Object.entries(
checkContrasts( { accent, background }, colors )
).filter( ( [ _, error ] ) => !! error );
/* eslint-enable @typescript-eslint/no-unused-vars */

const Chip = ( { color, name }: { color: string; name: string } ) => (
<HStack justify="flex-start">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { LayoutGroup } from 'framer-motion';
* WordPress dependencies
*/
import { useInstanceId } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import { useMemo } from '@wordpress/element';

/**
Expand Down
1 change: 0 additions & 1 deletion packages/compose/src/higher-order/test/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ describe( 'compose', () => {
const c = ( value ) => ( value += 'c' );
const d = ( value ) => ( value += 'd' );
const e = ( value ) => ( value += 'e' );
const f = ( value ) => ( value += 'f' );

expect( compose( [ a, b ], c, [ d ], e )( 'test' ) ).toBe(
'testedcba'
Expand Down
1 change: 0 additions & 1 deletion packages/compose/src/higher-order/test/pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ describe( 'pipe', () => {
const c = ( value ) => ( value += 'c' );
const d = ( value ) => ( value += 'd' );
const e = ( value ) => ( value += 'e' );
const f = ( value ) => ( value += 'f' );

expect( pipe( [ a, b ], c, [ d ], e )( 'test' ) ).toBe( 'testabcde' );
} );
Expand Down
1 change: 1 addition & 0 deletions packages/core-data/src/entity-types/menu-location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-reco

declare module './base-entity-records' {
export namespace BaseEntityRecords {
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
export interface MenuLocation< C extends Context > {
/**
* The name of the menu location.
Expand Down
1 change: 1 addition & 0 deletions packages/core-data/src/entity-types/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-reco

declare module './base-entity-records' {
export namespace BaseEntityRecords {
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
export interface Settings< C extends Context > {
/**
* What to show on the front page
Expand Down
1 change: 1 addition & 0 deletions packages/core-data/src/entity-types/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-reco

declare module './base-entity-records' {
export namespace BaseEntityRecords {
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
export interface Theme< C extends Context > {
/**
* The theme's stylesheet. This uniquely identifies the theme.
Expand Down
1 change: 1 addition & 0 deletions packages/core-data/src/entity-types/widget-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-reco

declare module './base-entity-records' {
export namespace BaseEntityRecords {
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
export interface WidgetType< C extends Context > {
/**
* Unique slug identifying the widget type.
Expand Down
6 changes: 6 additions & 0 deletions packages/core-data/src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,9 @@ export function getEntitiesByKind( state: State, kind: string ): Array< any > {
export const getEntitiesConfig = createSelector(
( state: State, kind: string ): Array< any > =>
state.entities.config.filter( ( entity ) => entity.kind === kind ),
/* eslint-disable @typescript-eslint/no-unused-vars */
( state: State, kind: string ) => state.entities.config
/* eslint-enable @typescript-eslint/no-unused-vars */
);
/**
* Returns the entity config given its kind and name.
Expand Down Expand Up @@ -992,6 +994,7 @@ export function getLastEntityDeleteError(
?.error;
}

/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Returns the previous edit from the current undo offset
* for the entity records edits history, if any.
Expand All @@ -1008,7 +1011,9 @@ export function getUndoEdit( state: State ): Optional< any > {
} );
return undefined;
}
/* eslint-enable @typescript-eslint/no-unused-vars */

/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Returns the next edit from the current undo offset
* for the entity records edits history, if any.
Expand All @@ -1025,6 +1030,7 @@ export function getRedoEdit( state: State ): Optional< any > {
} );
return undefined;
}
/* eslint-enable @typescript-eslint/no-unused-vars */

/**
* Returns true if there is a previous edit from the current undo offset
Expand Down
1 change: 0 additions & 1 deletion packages/core-data/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"compilerOptions": {
"rootDir": "src",
"declarationDir": "build-types",
"noUnusedParameters": false,
"checkJs": false,
"noImplicitAny": false
},
Expand Down
1 change: 0 additions & 1 deletion packages/data/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"compilerOptions": {
"rootDir": "src",
"declarationDir": "build-types",
"noUnusedParameters": false,
"checkJs": false
},
"references": [
Expand Down
1 change: 0 additions & 1 deletion packages/dataviews/src/dataform.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type { Dispatch, SetStateAction } from 'react';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { TextControl } from '@wordpress/components';
import { useCallback, useMemo } from '@wordpress/element';

Expand Down
3 changes: 1 addition & 2 deletions packages/date/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"declarationDir": "build-types",
"noUnusedParameters": false
"declarationDir": "build-types"
},
"references": [ { "path": "../deprecated" } ],
"include": [ "src/**/*" ]
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/dataviews/actions/delete-post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { trash } from '@wordpress/icons';
import { useDispatch } from '@wordpress/data';
import { __, _n, sprintf, _x } from '@wordpress/i18n';
import { __, _n, sprintf } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
import {
Button,
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/dataviews/actions/reset-post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { backup } from '@wordpress/icons';
import { useDispatch } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { __, _n, sprintf, _x } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
import { useState } from '@wordpress/element';
import {
Expand Down
1 change: 0 additions & 1 deletion packages/editor/src/dataviews/store/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* WordPress dependencies
*/
import { combineReducers } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import type { Action } from '@wordpress/dataviews';

type ReduxAction =
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
## Unreleased

### Breaking Changes

- Add [`@typescript-eslint/method-signature-style` rule](https://typescript-eslint.io/rules/method-signature-style) to the recommended TypeScript ruleset ([#62718](https://github.com/WordPress/gutenberg/pull/62718)).
- Add [`@typescript-eslint/no-unused-vars` rule](https://typescript-eslint.io/rules/no-unused-vars) to the recommended TypeScript ruleset ([#62925](https://github.com/WordPress/gutenberg/pull/62925)).
- Add [`@typescript-eslint/method-signature-style` rule](https://typescript-eslint.io/rules/method-signature-style) to the recommended TypeScript ruleset ([#62718](https://github.com/WordPress/gutenberg/pull/62718)).

## 19.2.0 (2024-06-26)

Expand Down
9 changes: 8 additions & 1 deletion packages/eslint-plugin/configs/recommended.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,15 @@ if ( isPackageInstalled( 'typescript' ) ) {
// Don't require redundant JSDoc types in TypeScript files.
'jsdoc/require-param-type': 'off',
'jsdoc/require-returns-type': 'off',
// Handled by TS itself.
// Use eslint for unused variable and parameter detection.
// This overlaps with TypeScript noUnusedLocals and noUnusedParameters settings.
// TypeScript may only run on a subset of files. Prefer eslint which is more
// likely to run on the entire codebase.
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': [
'error',
{ ignoreRestSiblings: true },
],
// no-shadow doesn't work correctly in TS, so let's use a TS-dedicated version instead.
'no-shadow': 'off',
'@typescript-eslint/no-shadow': 'error',
Expand Down
3 changes: 1 addition & 2 deletions packages/redux-routine/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"declarationDir": "build-types",
"noUnusedParameters": false
"declarationDir": "build-types"
},
"include": [ "src/**/*" ]
}
1 change: 0 additions & 1 deletion packages/report-flaky-tests/src/__tests__/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import * as core from '@actions/core';
import * as github from '@actions/github';

/**
* Internal dependencies
Expand Down
2 changes: 2 additions & 0 deletions packages/token-list/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export default class TokenList {
return true;
}

/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Returns true if `token` is in the associated attribute’s supported
* tokens. Returns false otherwise.
Expand All @@ -207,4 +208,5 @@ export default class TokenList {
supports( _token: string ): boolean {
return true;
}
/* eslint-enable @typescript-eslint/no-unused-vars */
}
2 changes: 1 addition & 1 deletion test/performance/config/performance-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export function curateResults(
return Object.fromEntries(
Object.entries( output )
// Reduce the output to contain taken metrics only.
.filter( ( [ _, value ] ) => value !== undefined )
.filter( ( [ , value ] ) => value !== undefined )
);
}

Expand Down
2 changes: 0 additions & 2 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
"strict": true,

/* Additional Checks */
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,

Expand Down

0 comments on commit b404003

Please sign in to comment.