Skip to content

chore: upgrade @typescript-eslint #852

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

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
36 changes: 33 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
module.exports = {
root: true,
env: {
es6: true,
node: true,
},
extends: [
'kentcdodds',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eslint-config-kentcdodds uses some outdated packages which block us from upgrading TypeScript dependencies properly. It didn't bring many rules so decided just use eslint:recommended, plugin:import/recommended and configure some rules manually.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this change! We should apply it anyway.

'eslint:recommended',
'plugin:import/recommended',
'plugin:jest/recommended',
'plugin:jest-formatting/recommended',
'prettier',
Expand Down Expand Up @@ -33,6 +38,12 @@ module.exports = {
},
},
],
'import/first': 'error',
'import/no-empty-named-blocks': 'error',
'import/no-extraneous-dependencies': 'error',
'import/no-mutable-exports': 'error',
'import/no-named-default': 'error',
'import/no-relative-packages': 'warn',
},
overrides: [
{
Expand All @@ -44,8 +55,8 @@ module.exports = {
project: ['./tsconfig.eslint.json'],
},
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
'plugin:@typescript-eslint/strict-type-checked',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also add plugin:@typescript-eslint/stylistic-type-checked which brings some useful rules but I wasn't sure what rules should be used in the project.

'plugin:import/typescript',
],
rules: {
'@typescript-eslint/explicit-function-return-type': 'off',
Expand All @@ -54,6 +65,25 @@ module.exports = {
{ argsIgnorePattern: '^_' },
],
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/no-unnecessary-condition': 'off',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disabled that rule because we have a lot of errors from it in the project and fixing it might cause some issues. If we want to enable this rule, we need to fix it carefully.


// Import
// Rules enabled by `import/recommended` but are better handled by
// TypeScript and @typescript-eslint.
'import/default': 'off',
'import/export': 'off',
'import/namespace': 'off',
'import/no-unresolved': 'off',
},
settings: {
'import/resolver': {
node: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
},
typescript: {
alwaysTryTypes: true,
},
},
},
},
],
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
fail-fast: false
matrix:
# The .x indicates "the most recent one"
node: [19.x, 18.x, 17.x, 16.x, 14.x, 14.17.0, 12.x, 12.22.0]
Copy link
Contributor Author

@lesha1201 lesha1201 Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped support for Node.js versions lower than 16. They're not maintained for a long time and don't support package.json exports which is used at least in @typescript-eslint.

node: [21.x, 20.x, 18.x, 16.x]
eslint: [7.5, 7, 8]

steps:
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16
18
6 changes: 6 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Files
package-lock.json

# Folders
node_modules/
dist/
Comment on lines +1 to +6
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add .prettierignore to ignore dist folder.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
const SETTING_OPTION_OFF = 'off' as const;

export type TestingLibrarySettings = {
'testing-library/utils-module'?: string | typeof SETTING_OPTION_OFF;
'testing-library/utils-module'?: string;
'testing-library/custom-renders'?: string[] | typeof SETTING_OPTION_OFF;
'testing-library/custom-queries'?: string[] | typeof SETTING_OPTION_OFF;
};
Expand Down
3 changes: 0 additions & 3 deletions lib/create-testing-library-rule/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ export function createTestingLibraryRule<
...meta,
docs: {
...meta.docs,
// We're using our own recommendedConfig meta to tell our build tools
// if the rule is recommended on a config basis
recommended: false,
},
},
});
Expand Down
5 changes: 2 additions & 3 deletions lib/node-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
AST_NODE_TYPES,
ASTUtils,
TSESLint,
TSESLintScope,
TSESTree,
} from '@typescript-eslint/utils';

Expand Down Expand Up @@ -270,7 +269,7 @@ export function getVariableReferences(
return [];
}

interface InnermostFunctionScope extends TSESLintScope.FunctionScope {
interface InnermostFunctionScope extends TSESLint.Scope.Scopes.FunctionScope {
block:
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionDeclaration
Expand All @@ -287,7 +286,7 @@ export function getInnermostFunctionScope(
);

if (
innermostScope.type === 'function' &&
innermostScope.type === TSESLint.Scope.ScopeType.function &&
ASTUtils.isFunction(innermostScope.block)
) {
return innermostScope as unknown as InnermostFunctionScope;
Expand Down
3 changes: 3 additions & 0 deletions lib/rules/no-await-sync-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
property.id.name === 'delay' &&
isLiteral(property.init) &&
property.init.value &&
// @ts-expect-error -- TODO: fix me
property.init.value > 0
Comment on lines +79 to 80
Copy link
Contributor Author

@lesha1201 lesha1201 Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, we had suppressImplicitAnyIndexErrors: true in tsconfig.json which allowed such code. But in TypeScript v5, it complains about this configuration so I removed it.

I think we should fix such places but it's better to do it in a separate PR.

);
},
Expand All @@ -88,6 +89,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
isLiteral(node.right) &&
node.right.value !== null
) {
// @ts-expect-error -- TODO: fix me
hasDelayDeclarationOrAssignmentGTZero = node.right.value > 0;
}
},
Expand Down Expand Up @@ -141,6 +143,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
property.key.name === 'delay' &&
isLiteral(property.value) &&
!!property.value.value &&
// @ts-expect-error -- TODO: fix me
property.value.value > 0
);

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-debugging-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
utilsToCheckFor: {
type: 'object',
properties: DEBUG_UTILS.reduce<
Record<string, JSONSchema.JSONSchema7>
Record<string, JSONSchema.JSONSchema4>
>(
(obj, name) => ({
[name]: { type: 'boolean' },
Expand Down
8 changes: 3 additions & 5 deletions lib/rules/no-dom-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ const DOM_TESTING_LIBRARY_MODULES = [
'@testing-library/dom',
];

const CORRECT_MODULE_NAME_BY_FRAMEWORK: Record<
'angular' | 'marko' | string,
string | undefined
> = {
const CORRECT_MODULE_NAME_BY_FRAMEWORK: Record<string, string | undefined> = {
angular: '@testing-library/angular', // ATL is *always* called `@testing-library/angular`
marko: '@marko/testing-library', // Marko TL is called `@marko/testing-library`
};
Expand Down Expand Up @@ -60,10 +57,11 @@ export default createTestingLibraryRule<Options, MessageIds>({
moduleName: string
) {
if (!framework) {
return context.report({
context.report({
node,
messageId: 'noDomImport',
});
return;
}

const correctModuleName = getCorrectModuleName(moduleName, framework);
Expand Down
1 change: 1 addition & 0 deletions lib/rules/no-render-in-lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
type: 'object',
properties: {
allowTestingFrameworkSetupHook: {
type: 'string',
enum: TESTING_FRAMEWORK_SETUP_HOOKS,
},
},
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/no-wait-for-side-effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,12 @@ export default createTestingLibraryRule<Options, MessageIds>({
return;
}

getSideEffectNodes(node.body).forEach((sideEffectNode) =>
getSideEffectNodes(node.body).forEach((sideEffectNode) => {
context.report({
node: sideEffectNode,
messageId: 'noSideEffectsWaitFor',
})
);
});
});
}

function reportImplicitReturnSideEffect(
Expand Down
8 changes: 4 additions & 4 deletions lib/rules/prefer-explicit-assert.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TSESTree, ASTUtils } from '@typescript-eslint/utils';
import { TSESTree, ASTUtils, AST_NODE_TYPES } from '@typescript-eslint/utils';

import { createTestingLibraryRule } from '../create-testing-library-rule';
import {
Expand All @@ -21,10 +21,10 @@ type Options = [

const isAtTopLevel = (node: TSESTree.Node) =>
(!!node.parent?.parent &&
node.parent.parent.type === 'ExpressionStatement') ||
(node.parent?.parent?.type === 'AwaitExpression' &&
node.parent.parent.type === AST_NODE_TYPES.ExpressionStatement) ||
(node.parent?.parent?.type === AST_NODE_TYPES.AwaitExpression &&
!!node.parent.parent.parent &&
node.parent.parent.parent.type === 'ExpressionStatement');
node.parent.parent.parent.type === AST_NODE_TYPES.ExpressionStatement);

const isVariableDeclaration = (node: TSESTree.Node) => {
if (
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/prefer-implicit-assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const reportError = (
queryType: string
) => {
if (node) {
return context.report({
context.report({
node,
messageId: 'preferImplicitAssert',
data: {
Expand Down Expand Up @@ -107,7 +107,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
AST_NODE_TYPES.Identifier &&
helpers.isPresenceAssert(node.parent.parent.parent.parent)
) {
return reportError(context, node, 'findBy*');
reportError(context, node, 'findBy*');
}
}
}
Expand All @@ -125,7 +125,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
AST_NODE_TYPES.Identifier &&
helpers.isPresenceAssert(node.parent.parent.parent)
) {
return reportError(context, node, 'getBy*');
reportError(context, node, 'getBy*');
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ const DEBUG_UTILS = [
'prettyFormat',
] as const;

const EVENTS_SIMULATORS = ['fireEvent', 'userEvent'] as const;
const EVENTS_SIMULATORS = ['fireEvent', 'userEvent'] as (
| 'fireEvent'
| 'userEvent'
)[];

const TESTING_FRAMEWORK_SETUP_HOOKS = ['beforeEach', 'beforeAll'];

Expand Down
6 changes: 4 additions & 2 deletions lib/utils/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { type TSESLint } from '@typescript-eslint/utils';

type RecommendedConfigRuleLevel = 'error' | 'warn' | false;

type RecommendedConfig<TOptions extends readonly unknown[]> =
| TSESLint.RuleMetaDataDocs['recommended']
| [TSESLint.RuleMetaDataDocs['recommended'], ...TOptions];
| RecommendedConfigRuleLevel
| [RecommendedConfigRuleLevel, ...TOptions];

// These 2 types are copied from @typescript-eslint/utils' CreateRuleMeta
// and modified to our needs
Expand Down
Loading