Skip to content

Commit 489ced3

Browse files
vedadeeptaljharb
authored andcommitted
[refactor] jsx-curly-braces-presence, jsx-one-expression-per-line, no-danger-with-children: add isWhiteSpaces to lib/util/jsx
1 parent 9b2db61 commit 489ced3

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ module.exports = {
104104
function containsWhitespaceExpression(child) {
105105
if (child.type === 'JSXExpressionContainer') {
106106
const value = child.expression.value;
107-
return value ? !(/\S/.test(value)) : false;
107+
return value ? jsxUtil.isWhiteSpaces(value) : false;
108108
}
109109
return false;
110110
}
@@ -206,7 +206,7 @@ module.exports = {
206206
}
207207

208208
function isWhiteSpaceLiteral(node) {
209-
return node.type && node.type === 'Literal' && node.value && !(/\S/.test(node.value));
209+
return node.type && node.type === 'Literal' && node.value && jsxUtil.isWhiteSpaces(node.value);
210210
}
211211

212212
function getAdjacentSiblings(node, children) {

lib/rules/jsx-one-expression-per-line.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
'use strict';
77

88
const docsUrl = require('../util/docsUrl');
9+
const jsxUtil = require('../util/jsx');
910

1011
// ------------------------------------------------------------------------------
1112
// Rule Definition
@@ -89,7 +90,7 @@ module.exports = {
8990
let countNewLinesAfterContent = 0;
9091

9192
if (child.type === 'Literal' || child.type === 'JSXText') {
92-
if (/^\s*$/.test(child.raw)) {
93+
if (jsxUtil.isWhiteSpaces(child.raw)) {
9394
return;
9495
}
9596

lib/rules/no-danger-with-children.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
'use strict';
77

88
const variableUtil = require('../util/variable');
9+
const jsxUtil = require('../util/jsx');
910
const docsUrl = require('../util/docsUrl');
1011

1112
// ------------------------------------------------------------------------------
@@ -81,7 +82,7 @@ module.exports = {
8182
function isLineBreak(node) {
8283
const isLiteral = node.type === 'Literal' || node.type === 'JSXText';
8384
const isMultiline = node.loc.start.line !== node.loc.end.line;
84-
const isWhiteSpaces = /^\s*$/.test(node.value);
85+
const isWhiteSpaces = jsxUtil.isWhiteSpaces(node.value);
8586

8687
return isLiteral && isMultiline && isWhiteSpaces;
8788
}

lib/util/jsx.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,19 @@ function isJSXAttributeKey(node) {
7676
node.name.name === 'key';
7777
}
7878

79+
/**
80+
* Check if value has only whitespaces
81+
* @param {string} value
82+
* @returns {boolean}
83+
*/
84+
function isWhiteSpaces(value) {
85+
return typeof value === 'string' ? /^\s*$/.test(value) : false;
86+
}
87+
7988
module.exports = {
8089
isDOMComponent,
8190
isFragment,
8291
isJSX,
83-
isJSXAttributeKey
92+
isJSXAttributeKey,
93+
isWhiteSpaces
8494
};

0 commit comments

Comments
 (0)