1
- import {
2
- ESLintUtils ,
3
- TSESTree ,
4
- ASTUtils ,
5
- } from '@typescript-eslint/experimental-utils' ;
6
- import { getDocsUrl , hasTestingLibraryImportModule } from '../utils' ;
7
- import {
8
- isCallExpression ,
9
- isImportSpecifier ,
10
- isMemberExpression ,
11
- isObjectPattern ,
12
- isRenderVariableDeclarator ,
13
- } from '../node-utils' ;
1
+ import { createTestingLibraryRule } from '../create-testing-library-rule' ;
2
+ import { isObjectPattern } from '../node-utils' ;
3
+ import { ASTUtils } from '@typescript-eslint/experimental-utils' ;
14
4
15
5
export const RULE_NAME = 'render-result-naming-convention' ;
16
6
export type MessageIds = 'renderResultNamingConvention' ;
17
7
18
- // TODO: remove renderFunctions option first, and then move it to ESLint settings
19
- type Options = [ { renderFunctions ?: string [ ] } ] ;
8
+ type Options = [ ] ;
20
9
21
10
const ALLOWED_VAR_NAMES = [ 'view' , 'utils' ] ;
22
11
const ALLOWED_VAR_NAMES_TEXT = ALLOWED_VAR_NAMES . map (
23
12
( name ) => `\`${ name } \``
24
13
) . join ( ', ' ) ;
25
14
26
- export default ESLintUtils . RuleCreator ( getDocsUrl ) < Options , MessageIds > ( {
15
+ export default createTestingLibraryRule < Options , MessageIds > ( {
27
16
name : RULE_NAME ,
28
17
meta : {
29
18
type : 'suggestion' ,
@@ -33,109 +22,27 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
33
22
recommended : false ,
34
23
} ,
35
24
messages : {
36
- renderResultNamingConvention : `\`{{ varName }}\` is not a recommended name for \`render\` returned value. Instead, you should destructure it, or call it using one of the valid choices : ${ ALLOWED_VAR_NAMES_TEXT } ` ,
25
+ renderResultNamingConvention : `\`{{ renderResultName }}\` is not a recommended name for \`render\` returned value. Instead, you should destructure it, or name it using one of: ${ ALLOWED_VAR_NAMES_TEXT } ` ,
37
26
} ,
38
27
fixable : null ,
39
- schema : [
40
- {
41
- type : 'object' ,
42
- properties : {
43
- renderFunctions : {
44
- type : 'array' ,
45
- } ,
46
- } ,
47
- } ,
48
- ] ,
28
+ schema : [ ] ,
49
29
} ,
50
- defaultOptions : [
51
- {
52
- renderFunctions : [ ] ,
53
- } ,
54
- ] ,
55
-
56
- create ( context , [ options ] ) {
57
- const { renderFunctions } = options ;
58
- let renderAlias : string | undefined ;
59
- let wildcardImportName : string | undefined ;
30
+ defaultOptions : [ ] ,
60
31
32
+ create ( context , _ , helpers ) {
61
33
return {
62
- // TODO: this can be removed
63
- // check named imports
64
- ImportDeclaration ( node : TSESTree . ImportDeclaration ) {
65
- if ( ! hasTestingLibraryImportModule ( node ) ) {
34
+ VariableDeclarator ( node ) {
35
+ if ( ! helpers . isRenderUtil ( node . init ) ) {
66
36
return ;
67
37
}
68
- const renderImport = node . specifiers . find (
69
- ( node ) => isImportSpecifier ( node ) && node . imported . name === 'render'
70
- ) ;
71
38
72
- if ( ! renderImport ) {
73
- return ;
74
- }
75
-
76
- renderAlias = renderImport . local . name ;
77
- } ,
78
- // TODO: this can be removed
79
- // check wildcard imports
80
- 'ImportDeclaration ImportNamespaceSpecifier' (
81
- node : TSESTree . ImportNamespaceSpecifier
82
- ) {
83
- if (
84
- ! hasTestingLibraryImportModule (
85
- node . parent as TSESTree . ImportDeclaration
86
- )
87
- ) {
88
- return ;
89
- }
90
-
91
- wildcardImportName = node . local . name ;
92
- } ,
93
- VariableDeclarator ( node : TSESTree . VariableDeclarator ) {
94
39
// check if destructuring return value from render
95
40
if ( isObjectPattern ( node . id ) ) {
96
41
return ;
97
42
}
98
43
99
- // TODO: call `helpers.isRender` with the node.init
100
- // this ini could be Identifier (render) or MemberExpression (rtl.render)
101
- const isValidRenderDeclarator = isRenderVariableDeclarator ( node , [
102
- ...renderFunctions ,
103
- renderAlias ,
104
- ] ) ;
105
-
106
- // TODO: After this point, most of the checks should be removed
107
- const isValidWildcardImport = ! ! wildcardImportName ;
108
-
109
- // check if is a Testing Library related import
110
- if ( ! isValidRenderDeclarator && ! isValidWildcardImport ) {
111
- return ;
112
- }
113
-
114
- const renderFunctionName =
115
- isCallExpression ( node . init ) &&
116
- ASTUtils . isIdentifier ( node . init . callee ) &&
117
- node . init . callee . name ;
118
-
119
- const renderFunctionObjectName =
120
- isCallExpression ( node . init ) &&
121
- isMemberExpression ( node . init . callee ) &&
122
- ASTUtils . isIdentifier ( node . init . callee . property ) &&
123
- ASTUtils . isIdentifier ( node . init . callee . object ) &&
124
- node . init . callee . property . name === 'render' &&
125
- node . init . callee . object . name ;
126
-
127
- const isRenderAlias = ! ! renderAlias ;
128
- const isCustomRender = renderFunctions . includes ( renderFunctionName ) ;
129
- const isWildCardRender =
130
- renderFunctionObjectName &&
131
- renderFunctionObjectName === wildcardImportName ;
132
-
133
- // check if is a qualified render function
134
- if ( ! isRenderAlias && ! isCustomRender && ! isWildCardRender ) {
135
- return ;
136
- }
137
-
138
44
const renderResultName = ASTUtils . isIdentifier ( node . id ) && node . id . name ;
45
+
139
46
const isAllowedRenderResultName = ALLOWED_VAR_NAMES . includes (
140
47
renderResultName
141
48
) ;
@@ -149,7 +56,7 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
149
56
node,
150
57
messageId : 'renderResultNamingConvention' ,
151
58
data : {
152
- varName : renderResultName ,
59
+ renderResultName,
153
60
} ,
154
61
} ) ;
155
62
} ,
0 commit comments