Skip to content

Commit

Permalink
[refactor]`jsx-curly-braces-presence, jsx-one-expression-per-line, no…
Browse files Browse the repository at this point in the history
…-danger-with-children`: isWhiteSpace check function to util dir
  • Loading branch information
vedadeepta committed Sep 13, 2019
1 parent 4377ed1 commit bf4bb8d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/rules/jsx-curly-brace-presence.js
Expand Up @@ -104,7 +104,7 @@ module.exports = {
function containsWhitespaceExpression(child) {
if (child.type === 'JSXExpressionContainer') {
const value = child.expression.value;
return value ? !(/\S/.test(value)) : false;
return value ? jsxUtil.isWhiteSpaces(value) : false;
}
return false;
}
Expand Down Expand Up @@ -206,7 +206,7 @@ module.exports = {
}

function isWhiteSpaceLiteral(node) {
return node.type && node.type === 'Literal' && node.value && !(/\S/.test(node.value));
return node.type && node.type === 'Literal' && node.value && jsxUtil.isWhiteSpaces(node.value);
}

function getAdjacentSiblings(node, children) {
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/jsx-one-expression-per-line.js
Expand Up @@ -6,6 +6,7 @@
'use strict';

const docsUrl = require('../util/docsUrl');
const jsxUtil = require('../util/jsx');

// ------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -89,7 +90,7 @@ module.exports = {
let countNewLinesAfterContent = 0;

if (child.type === 'Literal' || child.type === 'JSXText') {
if (/^\s*$/.test(child.raw)) {
if (jsxUtil.isWhiteSpaces(child.raw)) {
return;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/rules/no-danger-with-children.js
Expand Up @@ -6,6 +6,7 @@
'use strict';

const variableUtil = require('../util/variable');
const jsxUtil = require('../util/jsx');
const docsUrl = require('../util/docsUrl');

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

return isLiteral && isMultiline && isWhiteSpaces;
}
Expand Down
12 changes: 11 additions & 1 deletion lib/util/jsx.js
Expand Up @@ -76,9 +76,19 @@ function isJSXAttributeKey(node) {
node.name.name === 'key';
}

/**
* Check if value has only whitespaces
* @param {string} value
* @returns {boolean}
*/
function isWhiteSpaces(value) {
return typeof value === 'string' ? /^\s*$/.test(value) : false;
}

module.exports = {
isDOMComponent,
isFragment,
isJSX,
isJSXAttributeKey
isJSXAttributeKey,
isWhiteSpaces
};

0 comments on commit bf4bb8d

Please sign in to comment.