Skip to content

Commit 8dc0215

Browse files
committed
[Refactor] hoist functions to module level
1 parent e3927a3 commit 8dc0215

File tree

4 files changed

+148
-147
lines changed

4 files changed

+148
-147
lines changed

lib/rules/boolean-prop-naming.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ const eslintUtil = require('../util/eslint');
1818
const getSourceCode = eslintUtil.getSourceCode;
1919
const getText = eslintUtil.getText;
2020

21+
/**
22+
* Checks if prop is nested
23+
* @param {Object} prop Property object, single prop type declaration
24+
* @returns {boolean}
25+
*/
26+
function nestedPropTypes(prop) {
27+
return (
28+
prop.type === 'Property'
29+
&& prop.value.type === 'CallExpression'
30+
);
31+
}
32+
2133
// ------------------------------------------------------------------------------
2234
// Rule Definition
2335
// ------------------------------------------------------------------------------
@@ -162,18 +174,6 @@ module.exports = {
162174
);
163175
}
164176

165-
/**
166-
* Checks if prop is nested
167-
* @param {Object} prop Property object, single prop type declaration
168-
* @returns {boolean}
169-
*/
170-
function nestedPropTypes(prop) {
171-
return (
172-
prop.type === 'Property'
173-
&& prop.value.type === 'CallExpression'
174-
);
175-
}
176-
177177
/**
178178
* Runs recursive check on all proptypes
179179
* @param {Array} proptypes A list of Property object (for each proptype defined)

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

+102-101
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,108 @@ const OPTION_VALUES = [
3131
];
3232
const DEFAULT_CONFIG = { props: OPTION_NEVER, children: OPTION_NEVER, propElementValues: OPTION_IGNORE };
3333

34+
const HTML_ENTITY_REGEX = () => /&[A-Za-z\d#]+;/g;
35+
36+
function containsLineTerminators(rawStringValue) {
37+
return /[\n\r\u2028\u2029]/.test(rawStringValue);
38+
}
39+
40+
function containsBackslash(rawStringValue) {
41+
return arrayIncludes(rawStringValue, '\\');
42+
}
43+
44+
function containsHTMLEntity(rawStringValue) {
45+
return HTML_ENTITY_REGEX().test(rawStringValue);
46+
}
47+
48+
function containsOnlyHtmlEntities(rawStringValue) {
49+
return rawStringValue.replace(HTML_ENTITY_REGEX(), '').trim() === '';
50+
}
51+
52+
function containsDisallowedJSXTextChars(rawStringValue) {
53+
return /[{<>}]/.test(rawStringValue);
54+
}
55+
56+
function containsQuoteCharacters(value) {
57+
return /['"]/.test(value);
58+
}
59+
60+
function containsMultilineComment(value) {
61+
return /\/\*/.test(value);
62+
}
63+
64+
function escapeDoubleQuotes(rawStringValue) {
65+
return rawStringValue.replace(/\\"/g, '"').replace(/"/g, '\\"');
66+
}
67+
68+
function escapeBackslashes(rawStringValue) {
69+
return rawStringValue.replace(/\\/g, '\\\\');
70+
}
71+
72+
function needToEscapeCharacterForJSX(raw, node) {
73+
return (
74+
containsBackslash(raw)
75+
|| containsHTMLEntity(raw)
76+
|| (node.parent.type !== 'JSXAttribute' && containsDisallowedJSXTextChars(raw))
77+
);
78+
}
79+
80+
function containsWhitespaceExpression(child) {
81+
if (child.type === 'JSXExpressionContainer') {
82+
const value = child.expression.value;
83+
return value ? jsxUtil.isWhiteSpaces(value) : false;
84+
}
85+
return false;
86+
}
87+
88+
function isLineBreak(text) {
89+
return containsLineTerminators(text) && text.trim() === '';
90+
}
91+
92+
function wrapNonHTMLEntities(text) {
93+
const HTML_ENTITY = '<HTML_ENTITY>';
94+
const withCurlyBraces = text.split(HTML_ENTITY_REGEX()).map((word) => (
95+
word === '' ? '' : `{${JSON.stringify(word)}}`
96+
)).join(HTML_ENTITY);
97+
98+
const htmlEntities = text.match(HTML_ENTITY_REGEX());
99+
return htmlEntities.reduce((acc, htmlEntity) => (
100+
acc.replace(HTML_ENTITY, htmlEntity)
101+
), withCurlyBraces);
102+
}
103+
104+
function wrapWithCurlyBraces(rawText) {
105+
if (!containsLineTerminators(rawText)) {
106+
return `{${JSON.stringify(rawText)}}`;
107+
}
108+
109+
return rawText.split('\n').map((line) => {
110+
if (line.trim() === '') {
111+
return line;
112+
}
113+
const firstCharIndex = line.search(/[^\s]/);
114+
const leftWhitespace = line.slice(0, firstCharIndex);
115+
const text = line.slice(firstCharIndex);
116+
117+
if (containsHTMLEntity(line)) {
118+
return `${leftWhitespace}${wrapNonHTMLEntities(text)}`;
119+
}
120+
return `${leftWhitespace}{${JSON.stringify(text)}}`;
121+
}).join('\n');
122+
}
123+
124+
function isWhiteSpaceLiteral(node) {
125+
return node.type && node.type === 'Literal' && node.value && jsxUtil.isWhiteSpaces(node.value);
126+
}
127+
128+
function isStringWithTrailingWhiteSpaces(value) {
129+
return /^\s|\s$/.test(value);
130+
}
131+
132+
function isLiteralWithTrailingWhiteSpaces(node) {
133+
return node.type && node.type === 'Literal' && node.value && isStringWithTrailingWhiteSpaces(node.value);
134+
}
135+
34136
// ------------------------------------------------------------------------------
35137
// Rule Definition
36138
// ------------------------------------------------------------------------------
@@ -74,100 +176,11 @@ module.exports = {
74176
},
75177

76178
create(context) {
77-
const HTML_ENTITY_REGEX = () => /&[A-Za-z\d#]+;/g;
78179
const ruleOptions = context.options[0];
79180
const userConfig = typeof ruleOptions === 'string'
80181
? { props: ruleOptions, children: ruleOptions, propElementValues: OPTION_IGNORE }
81182
: Object.assign({}, DEFAULT_CONFIG, ruleOptions);
82183

83-
function containsLineTerminators(rawStringValue) {
84-
return /[\n\r\u2028\u2029]/.test(rawStringValue);
85-
}
86-
87-
function containsBackslash(rawStringValue) {
88-
return arrayIncludes(rawStringValue, '\\');
89-
}
90-
91-
function containsHTMLEntity(rawStringValue) {
92-
return HTML_ENTITY_REGEX().test(rawStringValue);
93-
}
94-
95-
function containsOnlyHtmlEntities(rawStringValue) {
96-
return rawStringValue.replace(HTML_ENTITY_REGEX(), '').trim() === '';
97-
}
98-
99-
function containsDisallowedJSXTextChars(rawStringValue) {
100-
return /[{<>}]/.test(rawStringValue);
101-
}
102-
103-
function containsQuoteCharacters(value) {
104-
return /['"]/.test(value);
105-
}
106-
107-
function containsMultilineComment(value) {
108-
return /\/\*/.test(value);
109-
}
110-
111-
function escapeDoubleQuotes(rawStringValue) {
112-
return rawStringValue.replace(/\\"/g, '"').replace(/"/g, '\\"');
113-
}
114-
115-
function escapeBackslashes(rawStringValue) {
116-
return rawStringValue.replace(/\\/g, '\\\\');
117-
}
118-
119-
function needToEscapeCharacterForJSX(raw, node) {
120-
return (
121-
containsBackslash(raw)
122-
|| containsHTMLEntity(raw)
123-
|| (node.parent.type !== 'JSXAttribute' && containsDisallowedJSXTextChars(raw))
124-
);
125-
}
126-
127-
function containsWhitespaceExpression(child) {
128-
if (child.type === 'JSXExpressionContainer') {
129-
const value = child.expression.value;
130-
return value ? jsxUtil.isWhiteSpaces(value) : false;
131-
}
132-
return false;
133-
}
134-
135-
function isLineBreak(text) {
136-
return containsLineTerminators(text) && text.trim() === '';
137-
}
138-
139-
function wrapNonHTMLEntities(text) {
140-
const HTML_ENTITY = '<HTML_ENTITY>';
141-
const withCurlyBraces = text.split(HTML_ENTITY_REGEX()).map((word) => (
142-
word === '' ? '' : `{${JSON.stringify(word)}}`
143-
)).join(HTML_ENTITY);
144-
145-
const htmlEntities = text.match(HTML_ENTITY_REGEX());
146-
return htmlEntities.reduce((acc, htmlEntity) => (
147-
acc.replace(HTML_ENTITY, htmlEntity)
148-
), withCurlyBraces);
149-
}
150-
151-
function wrapWithCurlyBraces(rawText) {
152-
if (!containsLineTerminators(rawText)) {
153-
return `{${JSON.stringify(rawText)}}`;
154-
}
155-
156-
return rawText.split('\n').map((line) => {
157-
if (line.trim() === '') {
158-
return line;
159-
}
160-
const firstCharIndex = line.search(/[^\s]/);
161-
const leftWhitespace = line.slice(0, firstCharIndex);
162-
const text = line.slice(firstCharIndex);
163-
164-
if (containsHTMLEntity(line)) {
165-
return `${leftWhitespace}${wrapNonHTMLEntities(text)}`;
166-
}
167-
return `${leftWhitespace}{${JSON.stringify(text)}}`;
168-
}).join('\n');
169-
}
170-
171184
/**
172185
* Report and fix an unnecessary curly brace violation on a node
173186
* @param {ASTNode} JSXExpressionNode - The AST node with an unnecessary JSX expression
@@ -233,18 +246,6 @@ module.exports = {
233246
});
234247
}
235248

236-
function isWhiteSpaceLiteral(node) {
237-
return node.type && node.type === 'Literal' && node.value && jsxUtil.isWhiteSpaces(node.value);
238-
}
239-
240-
function isStringWithTrailingWhiteSpaces(value) {
241-
return /^\s|\s$/.test(value);
242-
}
243-
244-
function isLiteralWithTrailingWhiteSpaces(node) {
245-
return node.type && node.type === 'Literal' && node.value && isStringWithTrailingWhiteSpaces(node.value);
246-
}
247-
248249
// Bail out if there is any character that needs to be escaped in JSX
249250
// because escaping decreases readability and the original code may be more
250251
// readable anyway or intentional for other specific reasons

lib/rules/no-unused-prop-types.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ const Components = require('../util/Components');
1414
const docsUrl = require('../util/docsUrl');
1515
const report = require('../util/report');
1616

17+
/**
18+
* Checks if the component must be validated
19+
* @param {Object} component The component to process
20+
* @returns {boolean} True if the component must be validated, false if not.
21+
*/
22+
function mustBeValidated(component) {
23+
return !!component && !component.ignoreUnusedPropTypesValidation;
24+
}
25+
1726
// ------------------------------------------------------------------------------
1827
// Rule Definition
1928
// ------------------------------------------------------------------------------
@@ -71,15 +80,6 @@ module.exports = {
7180
return configuration.ignore.indexOf(name) !== -1;
7281
}
7382

74-
/**
75-
* Checks if the component must be validated
76-
* @param {Object} component The component to process
77-
* @returns {boolean} True if the component must be validated, false if not.
78-
*/
79-
function mustBeValidated(component) {
80-
return !!component && !component.ignoreUnusedPropTypesValidation;
81-
}
82-
8383
/**
8484
* Checks if a prop is used
8585
* @param {ASTNode} node The AST node being checked.

lib/rules/self-closing-comp.js

+25-25
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,35 @@ const docsUrl = require('../util/docsUrl');
99
const jsxUtil = require('../util/jsx');
1010
const report = require('../util/report');
1111

12+
const optionDefaults = { component: true, html: true };
13+
14+
function isComponent(node) {
15+
return (
16+
node.name
17+
&& (node.name.type === 'JSXIdentifier' || node.name.type === 'JSXMemberExpression')
18+
&& !jsxUtil.isDOMComponent(node)
19+
);
20+
}
21+
22+
function childrenIsEmpty(node) {
23+
return node.parent.children.length === 0;
24+
}
25+
26+
function childrenIsMultilineSpaces(node) {
27+
const childrens = node.parent.children;
28+
29+
return (
30+
childrens.length === 1
31+
&& (childrens[0].type === 'Literal' || childrens[0].type === 'JSXText')
32+
&& childrens[0].value.indexOf('\n') !== -1
33+
&& childrens[0].value.replace(/(?!\xA0)\s/g, '') === ''
34+
);
35+
}
36+
1237
// ------------------------------------------------------------------------------
1338
// Rule Definition
1439
// ------------------------------------------------------------------------------
1540

16-
const optionDefaults = { component: true, html: true };
17-
1841
const messages = {
1942
notSelfClosing: 'Empty components are self-closing',
2043
};
@@ -49,29 +72,6 @@ module.exports = {
4972
},
5073

5174
create(context) {
52-
function isComponent(node) {
53-
return (
54-
node.name
55-
&& (node.name.type === 'JSXIdentifier' || node.name.type === 'JSXMemberExpression')
56-
&& !jsxUtil.isDOMComponent(node)
57-
);
58-
}
59-
60-
function childrenIsEmpty(node) {
61-
return node.parent.children.length === 0;
62-
}
63-
64-
function childrenIsMultilineSpaces(node) {
65-
const childrens = node.parent.children;
66-
67-
return (
68-
childrens.length === 1
69-
&& (childrens[0].type === 'Literal' || childrens[0].type === 'JSXText')
70-
&& childrens[0].value.indexOf('\n') !== -1
71-
&& childrens[0].value.replace(/(?!\xA0)\s/g, '') === ''
72-
);
73-
}
74-
7575
function isShouldBeSelfClosed(node) {
7676
const configuration = Object.assign({}, optionDefaults, context.options[0]);
7777
return (

0 commit comments

Comments
 (0)