@@ -64,15 +64,15 @@ module.exports = {
64
64
}
65
65
66
66
function containsHTMLEntity ( rawStringValue ) {
67
- return rawStringValue . match ( / & ( [ A - Z a - z \d ] + ) ; / g ) ;
67
+ return / & ( [ A - Z a - z \d # ] + ) ; / . test ( rawStringValue ) ;
68
68
}
69
69
70
70
function containsDisallowedJSXTextChars ( rawStringValue ) {
71
- return rawStringValue . match ( / { | < | > | } / g ) ;
71
+ return / [ { < > } ] / . test ( rawStringValue ) ;
72
72
}
73
73
74
74
function containsQuoteCharacters ( value ) {
75
- return value . match ( / ' | " / g ) ;
75
+ return / [ ' " ] / . test ( value ) ;
76
76
}
77
77
78
78
function escapeDoubleQuotes ( rawStringValue ) {
@@ -83,19 +83,17 @@ module.exports = {
83
83
return rawStringValue . replace ( / \\ / g, '\\\\' ) ;
84
84
}
85
85
86
- function needToEscapeCharacterForJSX ( raw , cooked ) {
86
+ function needToEscapeCharacterForJSX ( raw ) {
87
87
return (
88
88
containsBackslash ( raw ) ||
89
89
containsHTMLEntity ( raw ) ||
90
- containsDisallowedJSXTextChars ( raw ) ||
91
- containsQuoteCharacters ( cooked )
90
+ containsDisallowedJSXTextChars ( raw )
92
91
) ;
93
92
}
94
93
95
94
/**
96
95
* Report and fix an unnecessary curly brace violation on a node
97
96
* @param {ASTNode } node - The AST node with an unnecessary JSX expression
98
- * @param {String } text - The text to replace the unnecessary JSX expression
99
97
*/
100
98
function reportUnnecessaryCurly ( JSXExpressionNode ) {
101
99
context . report ( {
@@ -150,19 +148,24 @@ module.exports = {
150
148
function lintUnnecessaryCurly ( JSXExpressionNode ) {
151
149
const expression = JSXExpressionNode . expression ;
152
150
const expressionType = expression . type ;
151
+ const parentType = JSXExpressionNode . parent . type ;
153
152
154
153
if (
155
154
expressionType === 'Literal' &&
156
155
typeof expression . value === 'string' &&
157
- ! needToEscapeCharacterForJSX ( expression . raw , expression . value )
156
+ ! needToEscapeCharacterForJSX ( expression . raw ) && (
157
+ parentType === 'JSXElement' ||
158
+ ! containsQuoteCharacters ( expression . value )
159
+ )
158
160
) {
159
161
reportUnnecessaryCurly ( JSXExpressionNode ) ;
160
162
} else if (
161
163
expressionType === 'TemplateLiteral' &&
162
164
expression . expressions . length === 0 &&
163
- ! needToEscapeCharacterForJSX (
164
- expression . quasis [ 0 ] . value . raw , expression . quasis [ 0 ] . value . cooked
165
- )
165
+ ! needToEscapeCharacterForJSX ( expression . quasis [ 0 ] . value . raw ) && (
166
+ parentType === 'JSXElement' ||
167
+ ! containsQuoteCharacters ( expression . quasis [ 0 ] . value . cooked )
168
+ )
166
169
) {
167
170
reportUnnecessaryCurly ( JSXExpressionNode ) ;
168
171
}
0 commit comments