File tree Expand file tree Collapse file tree 7 files changed +99
-11
lines changed
create-testing-library-rule Expand file tree Collapse file tree 7 files changed +99
-11
lines changed Original file line number Diff line number Diff line change 8
8
SupportedTestingFramework ,
9
9
} from '../utils' ;
10
10
11
- type LinterConfigRules = Record < string , TSESLint . Linter . RuleEntry > ;
11
+ export type LinterConfigRules = Record < string , TSESLint . Linter . RuleEntry > ;
12
12
13
13
const configsDir = __dirname ;
14
14
Original file line number Diff line number Diff line change 1
1
import { ESLintUtils , TSESLint } from '@typescript-eslint/experimental-utils' ;
2
2
3
- import { getDocsUrl } from '../utils' ;
3
+ import { getDocsUrl , TestingLibraryRuleMeta } from '../utils' ;
4
4
5
5
import {
6
6
DetectionOptions ,
7
7
detectTestingLibraryUtils ,
8
8
EnhancedRuleCreate ,
9
9
} from './detect-testing-library-utils' ;
10
10
11
- // These 2 types are copied from @typescript-eslint/experimental-utils
12
- type CreateRuleMetaDocs = Omit < TSESLint . RuleMetaDataDocs , 'url' > ;
13
- type CreateRuleMeta < TMessageIds extends string > = {
14
- docs : CreateRuleMetaDocs ;
15
- } & Omit < TSESLint . RuleMetaData < TMessageIds > , 'docs' > ;
16
-
17
11
export function createTestingLibraryRule <
18
12
TOptions extends readonly unknown [ ] ,
19
13
TMessageIds extends string ,
20
14
TRuleListener extends TSESLint . RuleListener = TSESLint . RuleListener
21
15
> ( {
22
16
create,
23
17
detectionOptions = { } ,
18
+ meta,
24
19
...remainingConfig
25
20
} : Readonly < {
26
21
name : string ;
27
- meta : CreateRuleMeta < TMessageIds > ;
22
+ meta : TestingLibraryRuleMeta < TMessageIds > ;
28
23
defaultOptions : Readonly < TOptions > ;
29
24
detectionOptions ?: Partial < DetectionOptions > ;
30
25
create : EnhancedRuleCreate < TOptions , TMessageIds , TRuleListener > ;
@@ -35,5 +30,12 @@ export function createTestingLibraryRule<
35
30
create ,
36
31
detectionOptions
37
32
) ,
33
+ meta : {
34
+ ...meta ,
35
+ docs : {
36
+ ...meta . docs ,
37
+ recommended : false ,
38
+ } ,
39
+ } ,
38
40
} ) ;
39
41
}
Original file line number Diff line number Diff line change @@ -3,9 +3,11 @@ import { join, parse } from 'path';
3
3
4
4
import { TSESLint } from '@typescript-eslint/experimental-utils' ;
5
5
6
- import { importDefault } from '../utils' ;
6
+ import { importDefault , TestingLibraryRuleMeta } from '../utils' ;
7
7
8
- type RuleModule = TSESLint . RuleModule < string , unknown [ ] > ;
8
+ type RuleModule = TSESLint . RuleModule < string , unknown [ ] > & {
9
+ meta : TestingLibraryRuleMeta < string > ;
10
+ } ;
9
11
10
12
const rulesDir = __dirname ;
11
13
const excludedFiles = [ 'index' ] ;
Original file line number Diff line number Diff line change
1
+ import type { TSESLint } from '@typescript-eslint/experimental-utils' ;
2
+
3
+ // These 2 types are copied from @typescript-eslint/experimental-utils' CreateRuleMeta
4
+ // and modified to our needs
5
+ type TestingLibraryRuleMetaDocs = Omit <
6
+ TSESLint . RuleMetaDataDocs ,
7
+ 'recommended' | 'url'
8
+ > & {
9
+ recommended : Record <
10
+ SupportedTestingFramework ,
11
+ TSESLint . RuleMetaDataDocs [ 'recommended' ]
12
+ > ;
13
+ } ;
14
+ export type TestingLibraryRuleMeta < TMessageIds extends string > = {
15
+ docs : TestingLibraryRuleMetaDocs ;
16
+ } & Omit < TSESLint . RuleMetaData < TMessageIds > , 'docs' > ;
17
+
1
18
export const SUPPORTED_TESTING_FRAMEWORKS = [
2
19
'dom' ,
3
20
'angular' ,
Original file line number Diff line number Diff line change 29
29
"postbuild" : " cpy README.md ./dist && cpy package.json ./dist && cpy LICENSE ./dist" ,
30
30
"format" : " prettier --write ." ,
31
31
"format:check" : " prettier --check ." ,
32
+ "generate:configs" : " ts-node tools/generate-configs" ,
32
33
"lint" : " eslint . --max-warnings 0 --ext .js,.ts" ,
33
34
"lint:fix" : " npm run lint -- --fix" ,
34
35
"test" : " jest" ,
Original file line number Diff line number Diff line change
1
+ import type { LinterConfigRules } from '../../lib/configs' ;
2
+ import rules from '../../lib/rules' ;
3
+ import {
4
+ SUPPORTED_TESTING_FRAMEWORKS ,
5
+ SupportedTestingFramework ,
6
+ } from '../../lib/utils' ;
7
+
8
+ import { LinterConfig , writeConfig } from './utils' ;
9
+
10
+ const RULE_NAME_PREFIX = 'testing-library/' ;
11
+
12
+ const getRecommendedRulesForTestingFramework = (
13
+ framework : SupportedTestingFramework
14
+ ) : LinterConfigRules =>
15
+ Object . entries ( rules )
16
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
17
+ . filter ( ( [ _ , { meta : { docs } } ] ) => Boolean ( docs . recommended [ framework ] ) )
18
+ . reduce (
19
+ ( allRules , [ ruleName , { meta } ] ) => ( {
20
+ ...allRules ,
21
+ [ ruleName . split ( RULE_NAME_PREFIX ) [ 0 ] ] : meta . docs . recommended [ framework ] ,
22
+ } ) ,
23
+ { }
24
+ ) ;
25
+
26
+ SUPPORTED_TESTING_FRAMEWORKS . forEach ( ( framework ) => {
27
+ const specificFrameworkConfig : LinterConfig = {
28
+ plugins : [ 'testing-library' ] ,
29
+ rules : getRecommendedRulesForTestingFramework ( framework ) ,
30
+ } ;
31
+
32
+ writeConfig ( specificFrameworkConfig , framework ) ;
33
+ } ) ;
Original file line number Diff line number Diff line change
1
+ import { writeFileSync } from 'fs' ;
2
+ import { resolve } from 'path' ;
3
+
4
+ import type { TSESLint } from '@typescript-eslint/experimental-utils' ;
5
+ import { format , resolveConfig } from 'prettier' ;
6
+
7
+ const prettierConfig = resolveConfig . sync ( __dirname ) ;
8
+
9
+ export type LinterConfig = TSESLint . Linter . Config ;
10
+
11
+ const addAutoGeneratedComment = ( code : string ) =>
12
+ [
13
+ '// THIS CODE WAS AUTOMATICALLY GENERATED' ,
14
+ '// DO NOT EDIT THIS CODE BY HAND' ,
15
+ '// YOU CAN REGENERATE IT USING yarn generate:configs' ,
16
+ '' ,
17
+ code ,
18
+ ] . join ( '\n' ) ;
19
+
20
+ /**
21
+ * Helper function writes configuration.
22
+ */
23
+ export const writeConfig = ( config : LinterConfig , configName : string ) : void => {
24
+ // note: we use `export =` because ESLint will import these configs via a commonjs import
25
+ const code = `export = ${ JSON . stringify ( config ) } ;` ;
26
+ const configStr = format ( addAutoGeneratedComment ( code ) , {
27
+ parser : 'typescript' ,
28
+ ...prettierConfig ,
29
+ } ) ;
30
+ const filePath = resolve ( __dirname , `../../lib/configs/${ configName } .ts` ) ;
31
+
32
+ writeFileSync ( filePath , configStr ) ;
33
+ } ;
You can’t perform that action at this time.
0 commit comments