Skip to content

Commit 9eaca4a

Browse files
authored
chore: refine typescript-eslint config (#705)
* chore: remove unnecessary ESLint config * chore: move ESLint config file to js * chore: remove disabled ESLint import rule * chore: linting with type information Configured typescript-eslint to lint with type information: https://typescript-eslint.io/linting/typed-linting * chore: apply a custom message to restricted import
1 parent 25f73c5 commit 9eaca4a

File tree

7 files changed

+84
-66
lines changed

7 files changed

+84
-66
lines changed

.eslintrc.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
module.exports = {
2+
root: true,
3+
extends: [
4+
'kentcdodds',
5+
'plugin:jest/recommended',
6+
'plugin:jest-formatting/recommended',
7+
'prettier',
8+
],
9+
rules: {
10+
// Base
11+
'max-lines-per-function': 'off',
12+
'no-restricted-imports': [
13+
'error',
14+
{
15+
patterns: [
16+
{
17+
group: ['@typescript-eslint/utils/dist/*'],
18+
message: 'Import from `@typescript-eslint/utils` instead.',
19+
},
20+
],
21+
},
22+
],
23+
24+
// Import
25+
'import/order': [
26+
'warn',
27+
{
28+
groups: ['builtin', 'external', 'parent', 'sibling', 'index'],
29+
'newlines-between': 'always',
30+
alphabetize: {
31+
order: 'asc',
32+
caseInsensitive: false,
33+
},
34+
},
35+
],
36+
},
37+
overrides: [
38+
{
39+
// TypeScript
40+
files: ['**/*.ts?(x)'],
41+
parser: '@typescript-eslint/parser',
42+
parserOptions: {
43+
tsconfigRootDir: __dirname,
44+
project: ['./tsconfig.eslint.json'],
45+
},
46+
extends: [
47+
'plugin:@typescript-eslint/recommended',
48+
'plugin:@typescript-eslint/recommended-requiring-type-checking',
49+
],
50+
rules: {
51+
'@typescript-eslint/explicit-function-return-type': 'off',
52+
'@typescript-eslint/no-unused-vars': [
53+
'warn',
54+
{ argsIgnorePattern: '^_' },
55+
],
56+
'@typescript-eslint/no-use-before-define': 'off',
57+
},
58+
},
59+
],
60+
};

.eslintrc.json

Lines changed: 0 additions & 57 deletions
This file was deleted.

lib/rules/no-await-sync-events.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,16 @@ export default createTestingLibraryRule<Options, MessageIds>({
151151
return;
152152
}
153153

154+
const eventModuleName = getPropertyIdentifierNode(node)?.name;
155+
const eventFullName = eventModuleName
156+
? `${eventModuleName}.${simulateEventFunctionName}`
157+
: simulateEventFunctionName;
158+
154159
context.report({
155160
node,
156161
messageId: 'noAwaitSyncEvents',
157162
data: {
158-
name: `${
159-
getPropertyIdentifierNode(node)?.name
160-
}.${simulateEventFunctionName}`,
163+
name: eventFullName,
161164
},
162165
});
163166
},

lib/rules/no-dom-import.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,22 @@ const DOM_TESTING_LIBRARY_MODULES = [
1212
'@testing-library/dom',
1313
];
1414

15-
const correctModuleNameByFramework = {
15+
const CORRECT_MODULE_NAME_BY_FRAMEWORK: Record<
16+
'angular' | 'marko' | string,
17+
string | undefined
18+
> = {
1619
angular: '@testing-library/angular', // ATL is *always* called `@testing-library/angular`
1720
marko: '@marko/testing-library', // Marko TL is called `@marko/testing-library`
1821
};
19-
const getCorrectModuleName = (moduleName: string, framework: string): string =>
20-
correctModuleNameByFramework[framework] ||
21-
moduleName.replace('dom', framework);
22+
const getCorrectModuleName = (
23+
moduleName: string,
24+
framework: string
25+
): string => {
26+
return (
27+
CORRECT_MODULE_NAME_BY_FRAMEWORK[framework] ??
28+
moduleName.replace('dom', framework)
29+
);
30+
};
2231

2332
export default createTestingLibraryRule<Options, MessageIds>({
2433
name: RULE_NAME,

lib/rules/no-wait-for-snapshot.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ export default createTestingLibraryRule<Options, MessageIds>({
6868
}
6969

7070
return {
71-
[`Identifier[name=${SNAPSHOT_REGEXP}]`](node: TSESTree.Identifier) {
71+
[`Identifier[name=${String(SNAPSHOT_REGEXP)}]`](
72+
node: TSESTree.Identifier
73+
) {
7274
const closestAsyncUtil = getClosestAsyncUtil(node);
7375
if (closestAsyncUtil === null) {
7476
return;

lib/utils/file-import.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copied from https://github.com/babel/babel/blob/b35c78f08dd854b08575fc66ebca323fdbc59dab/packages/babel-helpers/src/helpers.js#L615-L619
22
// eslint-disable-next-line @typescript-eslint/no-explicit-any
33
const interopRequireDefault = <T>(obj: any): { default: T } =>
4-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment
4+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-return
55
obj?.__esModule ? obj : { default: obj };
66

77
export const importDefault = <T>(moduleName: string): T =>

tests/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ it('should export configs that refer to actual rules', () => {
6767
expect(rule.startsWith(ruleNamePrefix)).toBe(true);
6868
expect(ruleNames).toContain(ruleName);
6969

70+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
7071
expect(() => require(`../lib/rules/${ruleName}`)).not.toThrow();
7172
});
7273
});

0 commit comments

Comments
 (0)