Skip to content

Commit af2fe6a

Browse files
committed
Take into account feedback
1 parent 2bd2d07 commit af2fe6a

File tree

2 files changed

+96
-69
lines changed

2 files changed

+96
-69
lines changed

lib/rules/jsx-curly-brace-presence.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ module.exports = {
6464
}
6565

6666
function containsHTMLEntity(rawStringValue) {
67-
return rawStringValue.match(/&([A-Za-z\d]+);/g);
67+
return /&([A-Za-z\d#]+);/.test(rawStringValue);
6868
}
6969

7070
function containsDisallowedJSXTextChars(rawStringValue) {
71-
return rawStringValue.match(/{|<|>|}/g);
71+
return /[{<>}]/.test(rawStringValue);
7272
}
7373

7474
function containsQuoteCharacters(value) {
75-
return value.match(/'|"/g);
75+
return /['"]/.test(value);
7676
}
7777

7878
function escapeDoubleQuotes(rawStringValue) {
@@ -83,19 +83,17 @@ module.exports = {
8383
return rawStringValue.replace(/\\/g, '\\\\');
8484
}
8585

86-
function needToEscapeCharacterForJSX(raw, cooked) {
86+
function needToEscapeCharacterForJSX(raw) {
8787
return (
8888
containsBackslash(raw) ||
8989
containsHTMLEntity(raw) ||
90-
containsDisallowedJSXTextChars(raw) ||
91-
containsQuoteCharacters(cooked)
90+
containsDisallowedJSXTextChars(raw)
9291
);
9392
}
9493

9594
/**
9695
* Report and fix an unnecessary curly brace violation on a node
9796
* @param {ASTNode} node - The AST node with an unnecessary JSX expression
98-
* @param {String} text - The text to replace the unnecessary JSX expression
9997
*/
10098
function reportUnnecessaryCurly(JSXExpressionNode) {
10199
context.report({
@@ -150,19 +148,24 @@ module.exports = {
150148
function lintUnnecessaryCurly(JSXExpressionNode) {
151149
const expression = JSXExpressionNode.expression;
152150
const expressionType = expression.type;
151+
const parentType = JSXExpressionNode.parent.type;
153152

154153
if (
155154
expressionType === 'Literal' &&
156155
typeof expression.value === 'string' &&
157-
!needToEscapeCharacterForJSX(expression.raw, expression.value)
156+
!needToEscapeCharacterForJSX(expression.raw) && (
157+
parentType === 'JSXElement' ||
158+
!containsQuoteCharacters(expression.value)
159+
)
158160
) {
159161
reportUnnecessaryCurly(JSXExpressionNode);
160162
} else if (
161163
expressionType === 'TemplateLiteral' &&
162164
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+
)
166169
) {
167170
reportUnnecessaryCurly(JSXExpressionNode);
168171
}

0 commit comments

Comments
 (0)