@@ -25,7 +25,28 @@ const messages = {
25
25
invalidPropValue : 'Invalid prop value: "{{text}}"' ,
26
26
noStringsInAttributes : 'Strings not allowed in attributes: "{{text}}"' ,
27
27
noStringsInJSX : 'Strings not allowed in JSX files: "{{text}}"' ,
28
- literalNotInJSXExpression : 'Missing JSX expression container around literal string: "{{text}}"' ,
28
+ literalNotInJSXExpression :
29
+ 'Missing JSX expression container around literal string: "{{text}}"' ,
30
+ } ;
31
+
32
+ /** @type {import('json-schema').JSONSchema4['properties'] } */
33
+ const elementSchemaProperties = {
34
+ noStrings : {
35
+ type : 'boolean' ,
36
+ } ,
37
+ allowedStrings : {
38
+ type : 'array' ,
39
+ uniqueItems : true ,
40
+ items : {
41
+ type : 'string' ,
42
+ } ,
43
+ } ,
44
+ ignoreProps : {
45
+ type : 'boolean' ,
46
+ } ,
47
+ noAttributeStrings : {
48
+ type : 'boolean' ,
49
+ } ,
29
50
} ;
30
51
31
52
/** @type {import('eslint').Rule.RuleModule } */
@@ -40,28 +61,21 @@ module.exports = {
40
61
41
62
messages,
42
63
43
- schema : [ {
44
- type : 'object' ,
45
- properties : {
46
- noStrings : {
47
- type : 'boolean' ,
48
- } ,
49
- allowedStrings : {
50
- type : 'array' ,
51
- uniqueItems : true ,
52
- items : {
53
- type : 'string' ,
64
+ schema : [
65
+ {
66
+ type : 'object' ,
67
+ properties : Object . assign (
68
+ {
69
+ elementOverrides : {
70
+ type : 'object' ,
71
+ properties : elementSchemaProperties ,
72
+ } ,
54
73
} ,
55
- } ,
56
- ignoreProps : {
57
- type : 'boolean' ,
58
- } ,
59
- noAttributeStrings : {
60
- type : 'boolean' ,
61
- } ,
74
+ elementSchemaProperties
75
+ ) ,
76
+ additionalProperties : false ,
62
77
} ,
63
- additionalProperties : false ,
64
- } ] ,
78
+ ] ,
65
79
} ,
66
80
67
81
create ( context ) {
@@ -72,7 +86,9 @@ module.exports = {
72
86
noAttributeStrings : false ,
73
87
} ;
74
88
const config = Object . assign ( { } , defaults , context . options [ 0 ] || { } ) ;
75
- config . allowedStrings = new Set ( map ( iterFrom ( config . allowedStrings ) , trimIfString ) ) ;
89
+ config . allowedStrings = new Set (
90
+ map ( iterFrom ( config . allowedStrings ) , trimIfString )
91
+ ) ;
76
92
77
93
function defaultMessageId ( ancestorIsJSXElement ) {
78
94
if ( config . noAttributeStrings && ! ancestorIsJSXElement ) {
@@ -101,9 +117,15 @@ module.exports = {
101
117
const parent = getParentIgnoringBinaryExpressions ( node ) ;
102
118
103
119
function isParentNodeStandard ( ) {
104
- if ( ! / ^ [ \s ] + $ / . test ( node . value ) && typeof node . value === 'string' && parent . type . includes ( 'JSX' ) ) {
120
+ if (
121
+ ! / ^ [ \s ] + $ / . test ( node . value ) &&
122
+ typeof node . value === 'string' &&
123
+ parent . type . includes ( 'JSX' )
124
+ ) {
105
125
if ( config . noAttributeStrings ) {
106
- return parent . type === 'JSXAttribute' || parent . type === 'JSXElement' ;
126
+ return (
127
+ parent . type === 'JSXAttribute' || parent . type === 'JSXElement'
128
+ ) ;
107
129
}
108
130
if ( ! config . noAttributeStrings ) {
109
131
return parent . type !== 'JSXAttribute' ;
@@ -139,7 +161,11 @@ module.exports = {
139
161
const parentType = parents . parentType ;
140
162
const grandParentType = parents . grandParentType ;
141
163
142
- return parentType === 'JSXFragment' || parentType === 'JSXElement' || grandParentType === 'JSXElement' ;
164
+ return (
165
+ parentType === 'JSXFragment' ||
166
+ parentType === 'JSXElement' ||
167
+ grandParentType === 'JSXElement'
168
+ ) ;
143
169
}
144
170
145
171
function reportLiteralNode ( node , messageId ) {
@@ -160,13 +186,21 @@ module.exports = {
160
186
161
187
return {
162
188
Literal ( node ) {
163
- if ( getValidation ( node ) && ( hasJSXElementParentOrGrandParent ( node ) || ! config . ignoreProps ) ) {
189
+ if (
190
+ getValidation ( node ) &&
191
+ ( hasJSXElementParentOrGrandParent ( node ) || ! config . ignoreProps )
192
+ ) {
164
193
reportLiteralNode ( node ) ;
165
194
}
166
195
} ,
167
196
168
197
JSXAttribute ( node ) {
169
- const isNodeValueString = node && node . value && node . value . type === 'Literal' && typeof node . value . value === 'string' && ! config . allowedStrings . has ( node . value . value ) ;
198
+ const isNodeValueString =
199
+ node &&
200
+ node . value &&
201
+ node . value . type === 'Literal' &&
202
+ typeof node . value . value === 'string' &&
203
+ ! config . allowedStrings . has ( node . value . value ) ;
170
204
171
205
if ( config . noStrings && ! config . ignoreProps && isNodeValueString ) {
172
206
const messageId = 'invalidPropValue' ;
@@ -184,10 +218,16 @@ module.exports = {
184
218
const parents = getParentAndGrandParentType ( node ) ;
185
219
const parentType = parents . parentType ;
186
220
const grandParentType = parents . grandParentType ;
187
- const isParentJSXExpressionCont = parentType === 'JSXExpressionContainer' ;
188
- const isParentJSXElement = parentType === 'JSXElement' || grandParentType === 'JSXElement' ;
189
-
190
- if ( isParentJSXExpressionCont && config . noStrings && ( isParentJSXElement || ! config . ignoreProps ) ) {
221
+ const isParentJSXExpressionCont =
222
+ parentType === 'JSXExpressionContainer' ;
223
+ const isParentJSXElement =
224
+ parentType === 'JSXElement' || grandParentType === 'JSXElement' ;
225
+
226
+ if (
227
+ isParentJSXExpressionCont &&
228
+ config . noStrings &&
229
+ ( isParentJSXElement || ! config . ignoreProps )
230
+ ) {
191
231
reportLiteralNode ( node ) ;
192
232
}
193
233
} ,
0 commit comments