Skip to content

Commit

Permalink
chore: refine typescript-eslint config (#705)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Belco90 authored Dec 22, 2022
1 parent 25f73c5 commit 9eaca4a
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 66 deletions.
60 changes: 60 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module.exports = {
root: true,
extends: [
'kentcdodds',
'plugin:jest/recommended',
'plugin:jest-formatting/recommended',
'prettier',
],
rules: {
// Base
'max-lines-per-function': 'off',
'no-restricted-imports': [
'error',
{
patterns: [
{
group: ['@typescript-eslint/utils/dist/*'],
message: 'Import from `@typescript-eslint/utils` instead.',
},
],
},
],

// Import
'import/order': [
'warn',
{
groups: ['builtin', 'external', 'parent', 'sibling', 'index'],
'newlines-between': 'always',
alphabetize: {
order: 'asc',
caseInsensitive: false,
},
},
],
},
overrides: [
{
// TypeScript
files: ['**/*.ts?(x)'],
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.eslint.json'],
},
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
],
rules: {
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-unused-vars': [
'warn',
{ argsIgnorePattern: '^_' },
],
'@typescript-eslint/no-use-before-define': 'off',
},
},
],
};
57 changes: 0 additions & 57 deletions .eslintrc.json

This file was deleted.

9 changes: 6 additions & 3 deletions lib/rules/no-await-sync-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,16 @@ export default createTestingLibraryRule<Options, MessageIds>({
return;
}

const eventModuleName = getPropertyIdentifierNode(node)?.name;
const eventFullName = eventModuleName
? `${eventModuleName}.${simulateEventFunctionName}`
: simulateEventFunctionName;

context.report({
node,
messageId: 'noAwaitSyncEvents',
data: {
name: `${
getPropertyIdentifierNode(node)?.name
}.${simulateEventFunctionName}`,
name: eventFullName,
},
});
},
Expand Down
17 changes: 13 additions & 4 deletions lib/rules/no-dom-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@ const DOM_TESTING_LIBRARY_MODULES = [
'@testing-library/dom',
];

const correctModuleNameByFramework = {
const CORRECT_MODULE_NAME_BY_FRAMEWORK: Record<
'angular' | 'marko' | 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`
};
const getCorrectModuleName = (moduleName: string, framework: string): string =>
correctModuleNameByFramework[framework] ||
moduleName.replace('dom', framework);
const getCorrectModuleName = (
moduleName: string,
framework: string
): string => {
return (
CORRECT_MODULE_NAME_BY_FRAMEWORK[framework] ??
moduleName.replace('dom', framework)
);
};

export default createTestingLibraryRule<Options, MessageIds>({
name: RULE_NAME,
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/no-wait-for-snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ export default createTestingLibraryRule<Options, MessageIds>({
}

return {
[`Identifier[name=${SNAPSHOT_REGEXP}]`](node: TSESTree.Identifier) {
[`Identifier[name=${String(SNAPSHOT_REGEXP)}]`](
node: TSESTree.Identifier
) {
const closestAsyncUtil = getClosestAsyncUtil(node);
if (closestAsyncUtil === null) {
return;
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/file-import.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copied from https://github.com/babel/babel/blob/b35c78f08dd854b08575fc66ebca323fdbc59dab/packages/babel-helpers/src/helpers.js#L615-L619
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const interopRequireDefault = <T>(obj: any): { default: T } =>
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-return
obj?.__esModule ? obj : { default: obj };

export const importDefault = <T>(moduleName: string): T =>
Expand Down
1 change: 1 addition & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ it('should export configs that refer to actual rules', () => {
expect(rule.startsWith(ruleNamePrefix)).toBe(true);
expect(ruleNames).toContain(ruleName);

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

0 comments on commit 9eaca4a

Please sign in to comment.