diff --git a/lib/rules/jsx-no-literals.js b/lib/rules/jsx-no-literals.js index d7f43ab05c..8280eade5b 100644 --- a/lib/rules/jsx-no-literals.js +++ b/lib/rules/jsx-no-literals.js @@ -74,8 +74,7 @@ module.exports = { const config = Object.assign({}, defaults, context.options[0] || {}); config.allowedStrings = new Set(map(iterFrom(config.allowedStrings), trimIfString)); - function defaultMessageId() { - const ancestorIsJSXElement = arguments.length >= 1 && arguments[0]; + function defaultMessageId(ancestorIsJSXElement) { if (config.noAttributeStrings && !ancestorIsJSXElement) { return 'noStringsInAttributes'; } diff --git a/lib/rules/jsx-sort-props.js b/lib/rules/jsx-sort-props.js index f811e16cf7..4c138e9f69 100644 --- a/lib/rules/jsx-sort-props.js +++ b/lib/rules/jsx-sort-props.js @@ -12,6 +12,7 @@ const toSorted = require('array.prototype.tosorted'); const docsUrl = require('../util/docsUrl'); const jsxUtil = require('../util/jsx'); const report = require('../util/report'); +const propTypesSortUtil = require('../util/propTypesSort'); const eslintUtil = require('../util/eslint'); const getText = eslintUtil.getText; @@ -21,10 +22,6 @@ const getSourceCode = eslintUtil.getSourceCode; // Rule Definition // ------------------------------------------------------------------------------ -function isCallbackPropName(name) { - return /^on[A-Z]/.test(name); -} - function isMultilineProp(node) { return node.loc.start.line !== node.loc.end.line; } @@ -85,8 +82,8 @@ function contextCompare(a, b, options) { } if (options.callbacksLast) { - const aIsCallback = isCallbackPropName(aProp); - const bIsCallback = isCallbackPropName(bProp); + const aIsCallback = propTypesSortUtil.isCallbackPropName(aProp); + const bIsCallback = propTypesSortUtil.isCallbackPropName(bProp); if (aIsCallback && !bIsCallback) { return 1; } @@ -425,8 +422,8 @@ module.exports = { let currentPropName = propName(decl); const previousValue = memo.value; const currentValue = decl.value; - const previousIsCallback = isCallbackPropName(previousPropName); - const currentIsCallback = isCallbackPropName(currentPropName); + const previousIsCallback = propTypesSortUtil.isCallbackPropName(previousPropName); + const currentIsCallback = propTypesSortUtil.isCallbackPropName(currentPropName); if (ignoreCase) { previousPropName = previousPropName.toLowerCase(); diff --git a/lib/rules/no-direct-mutation-state.js b/lib/rules/no-direct-mutation-state.js index 3df0998c6e..ef9cba77f0 100644 --- a/lib/rules/no-direct-mutation-state.js +++ b/lib/rules/no-direct-mutation-state.js @@ -41,7 +41,7 @@ module.exports = { * @returns {Boolean} True if the component is valid, false if not. */ function isValid(component) { - return Boolean(component && !component.mutateSetState); + return !!component && !component.mutateSetState; } /** diff --git a/lib/rules/no-invalid-html-attribute.js b/lib/rules/no-invalid-html-attribute.js index 6e46f3bd4c..0f018f8080 100644 --- a/lib/rules/no-invalid-html-attribute.js +++ b/lib/rules/no-invalid-html-attribute.js @@ -289,7 +289,7 @@ function checkLiteralValueNode(context, attributeName, node, parentNode, parentN } const singleAttributeParts = splitIntoRangedParts(node, /(\S+)/g); - for (const singlePart of singleAttributeParts) { + singleAttributeParts.forEach((singlePart) => { const allowedTags = VALID_VALUES.get(attributeName).get(singlePart.value); const reportingValue = singlePart.reportingValue; @@ -329,15 +329,13 @@ function checkLiteralValueNode(context, attributeName, node, parentNode, parentN suggest, }); } - } + }); const allowedPairsForAttribute = VALID_PAIR_VALUES.get(attributeName); if (allowedPairsForAttribute) { const pairAttributeParts = splitIntoRangedParts(node, /(?=(\b\S+\s*\S+))/g); - for (const pairPart of pairAttributeParts) { - for (const allowedPair of allowedPairsForAttribute) { - const pairing = allowedPair[0]; - const siblings = allowedPair[1]; + pairAttributeParts.forEach((pairPart) => { + allowedPairsForAttribute.forEach((siblings, pairing) => { const attributes = pairPart.reportingValue.split('\u0020'); const firstValue = attributes[0]; const secondValue = attributes[1]; @@ -357,12 +355,12 @@ function checkLiteralValueNode(context, attributeName, node, parentNode, parentN }); } } - } - } + }); + }); } const whitespaceParts = splitIntoRangedParts(node, /(\s+)/g); - for (const whitespacePart of whitespaceParts) { + whitespaceParts.forEach((whitespacePart) => { const data = { attributeName }; if (whitespacePart.range[0] === (node.range[0] + 1) || whitespacePart.range[1] === (node.range[1] - 1)) { @@ -386,7 +384,7 @@ function checkLiteralValueNode(context, attributeName, node, parentNode, parentN }], }); } - } + }); } const DEFAULT_ATTRIBUTES = ['rel']; @@ -579,9 +577,9 @@ function checkCreateProps(context, node, attribute) { } if (prop.value.type === 'ArrayExpression') { - for (const value of prop.value.elements) { + prop.value.elements.forEach((value) => { checkPropValidValue(context, node, value, attribute); - } + }); // eslint-disable-next-line no-continue continue; @@ -646,9 +644,9 @@ module.exports = { const attributes = new Set(context.options[0] || DEFAULT_ATTRIBUTES); - for (const attribute of attributes) { + attributes.forEach((attribute) => { checkCreateProps(context, node, attribute); - } + }); }, }; }, diff --git a/lib/rules/no-set-state.js b/lib/rules/no-set-state.js index 199e922b75..78244c38d8 100644 --- a/lib/rules/no-set-state.js +++ b/lib/rules/no-set-state.js @@ -41,7 +41,7 @@ module.exports = { * @returns {Boolean} True if the component is valid, false if not. */ function isValid(component) { - return Boolean(component && !component.useSetState); + return !!component && !component.useSetState; } /** diff --git a/lib/rules/no-unused-prop-types.js b/lib/rules/no-unused-prop-types.js index f72188201a..6da1b3d68f 100644 --- a/lib/rules/no-unused-prop-types.js +++ b/lib/rules/no-unused-prop-types.js @@ -77,10 +77,7 @@ module.exports = { * @returns {Boolean} True if the component must be validated, false if not. */ function mustBeValidated(component) { - return Boolean( - component - && !component.ignoreUnusedPropTypesValidation - ); + return !!component && !component.ignoreUnusedPropTypesValidation; } /** diff --git a/lib/rules/no-unused-state.js b/lib/rules/no-unused-state.js index 0ed480694d..e3b1d37c4b 100644 --- a/lib/rules/no-unused-state.js +++ b/lib/rules/no-unused-state.js @@ -174,7 +174,7 @@ module.exports = { // Records used state fields and new aliases for an ObjectPattern which // destructures `this.state`. function handleStateDestructuring(node) { - for (const prop of node.properties) { + node.properties.forEach((prop) => { if (prop.type === 'Property') { addUsedStateField(prop.key); } else if ( @@ -183,7 +183,7 @@ module.exports = { ) { classInfo.aliases.add(getName(prop.argument)); } - } + }); } // Used to record used state fields and new aliases for both @@ -201,7 +201,7 @@ module.exports = { if (isStateReference(unwrappedRight)) { handleStateDestructuring(left); } else if (isThisExpression(unwrappedRight) && classInfo.aliases) { - for (const prop of left.properties) { + left.properties.forEach((prop) => { if (prop.type === 'Property' && getName(prop.key) === 'state') { const name = getName(prop.value); if (name) { @@ -210,7 +210,7 @@ module.exports = { handleStateDestructuring(prop.value); } } - } + }); } break; default: @@ -220,7 +220,7 @@ module.exports = { function reportUnusedFields() { // Report all unused state fields. - for (const node of classInfo.stateFields) { + classInfo.stateFields.forEach((node) => { const name = getName(node.key); if (!classInfo.usedStateFields.has(name)) { report(context, messages.unusedStateField, 'unusedStateField', { @@ -230,7 +230,7 @@ module.exports = { }, }); } - } + }); } function handleES6ComponentEnter(node) { diff --git a/lib/rules/require-optimization.js b/lib/rules/require-optimization.js index dc0b60f73a..a58c47a191 100644 --- a/lib/rules/require-optimization.js +++ b/lib/rules/require-optimization.js @@ -104,10 +104,7 @@ module.exports = { * @returns {Boolean} True if we are declaring a shouldComponentUpdate method, false if not. */ function isSCUDeclared(node) { - return Boolean( - node - && node.name === 'shouldComponentUpdate' - ); + return !!node && node.name === 'shouldComponentUpdate'; } /** @@ -126,8 +123,8 @@ module.exports = { } } - return Boolean( - node + return ( + !!node && node.key.name === 'mixins' && hasPR ); diff --git a/lib/rules/sort-prop-types.js b/lib/rules/sort-prop-types.js index b0087e59c2..b7f3ec4ff5 100644 --- a/lib/rules/sort-prop-types.js +++ b/lib/rules/sort-prop-types.js @@ -35,28 +35,6 @@ function getKey(context, node) { return getText(context, node.key || node.argument); } -function getValueName(node) { - return node.type === 'Property' && node.value.property && node.value.property.name; -} - -function isCallbackPropName(propName) { - return /^on[A-Z]/.test(propName); -} - -function isRequiredProp(node) { - return getValueName(node) === 'isRequired'; -} - -function isShapeProp(node) { - return Boolean( - node && node.callee && node.callee.property && node.callee.property.name === 'shape' - ); -} - -function toLowerCase(item) { - return String(item).toLowerCase(); -} - /** @type {import('eslint').Rule.RuleModule} */ module.exports = { meta: { @@ -145,14 +123,14 @@ module.exports = { let prevPropName = getKey(context, prev); let currentPropName = getKey(context, curr); - const previousIsRequired = isRequiredProp(prev); - const currentIsRequired = isRequiredProp(curr); - const previousIsCallback = isCallbackPropName(prevPropName); - const currentIsCallback = isCallbackPropName(currentPropName); + const previousIsRequired = propTypesSortUtil.isRequiredProp(prev); + const currentIsRequired = propTypesSortUtil.isRequiredProp(curr); + const previousIsCallback = propTypesSortUtil.isCallbackPropName(prevPropName); + const currentIsCallback = propTypesSortUtil.isCallbackPropName(currentPropName); if (ignoreCase) { - prevPropName = toLowerCase(prevPropName); - currentPropName = toLowerCase(currentPropName); + prevPropName = String(prevPropName).toLowerCase(); + currentPropName = String(currentPropName).toLowerCase(); } if (requiredFirst) { @@ -260,7 +238,7 @@ module.exports = { return Object.assign({ CallExpression(node) { - if (!sortShapeProp || !isShapeProp(node) || !(node.arguments && node.arguments[0])) { + if (!sortShapeProp || !propTypesSortUtil.isShapeProp(node) || !(node.arguments && node.arguments[0])) { return; } diff --git a/lib/util/propTypes.js b/lib/util/propTypes.js index 9450f9222f..9525989bd8 100644 --- a/lib/util/propTypes.js +++ b/lib/util/propTypes.js @@ -172,7 +172,7 @@ module.exports = function propTypesInstructions(context, components, utils) { ObjectTypeAnnotation(annotation, parentName, seen) { let containsUnresolvedObjectTypeSpread = false; let containsSpread = false; - const containsIndexers = Boolean(annotation.indexers && annotation.indexers.length); + const containsIndexers = !!annotation.indexers && annotation.indexers.length > 0; const shapeTypeDefinition = { type: 'shape', children: {}, diff --git a/lib/util/propTypesSort.js b/lib/util/propTypesSort.js index bffcfb1073..21898a490c 100644 --- a/lib/util/propTypesSort.js +++ b/lib/util/propTypesSort.js @@ -49,8 +49,11 @@ function isCallbackPropName(propName) { * @returns {Boolean} true if the prop is PropTypes.shape. */ function isShapeProp(node) { - return Boolean( - node && node.callee && node.callee.property && node.callee.property.name === 'shape' + return !!( + node + && node.callee + && node.callee.property + && node.callee.property.name === 'shape' ); } @@ -220,4 +223,7 @@ function fixPropTypesSort( module.exports = { fixPropTypesSort, + isCallbackPropName, + isRequiredProp, + isShapeProp, }; diff --git a/tests/index.js b/tests/index.js index 258429517b..9f7a052943 100644 --- a/tests/index.js +++ b/tests/index.js @@ -34,7 +34,7 @@ describe('all rule files should be exported by the plugin', () => { describe('deprecated rules', () => { it('marks all deprecated rules as deprecated', () => { ruleFiles.forEach((ruleName) => { - const inDeprecatedRules = Boolean(plugin.deprecatedRules[ruleName]); + const inDeprecatedRules = !!plugin.deprecatedRules[ruleName]; const isDeprecated = plugin.rules[ruleName].meta.deprecated; if (inDeprecatedRules) { assert(isDeprecated, `${ruleName} metadata should mark it as deprecated`); @@ -77,7 +77,7 @@ describe('configurations', () => { }); ruleFiles.forEach((ruleName) => { - const inDeprecatedRules = Boolean(plugin.deprecatedRules[ruleName]); + const inDeprecatedRules = !!plugin.deprecatedRules[ruleName]; const inConfig = typeof plugin.configs[configName].rules[`react/${ruleName}`] !== 'undefined'; assert(inDeprecatedRules ^ inConfig); // eslint-disable-line no-bitwise }); @@ -91,7 +91,7 @@ describe('configurations', () => { assert.ok(ruleName.startsWith('react/')); assert.equal(plugin.configs[configName].rules[ruleName], 0); - const inDeprecatedRules = Boolean(plugin.deprecatedRules[ruleName]); + const inDeprecatedRules = !!plugin.deprecatedRules[ruleName]; const inConfig = typeof plugin.configs[configName].rules[ruleName] !== 'undefined'; assert(inDeprecatedRules ^ inConfig); // eslint-disable-line no-bitwise });