From 952181ed4c03b5f8abdeb361551bcdd34c1499be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20De=20Boey?= Date: Wed, 30 Oct 2024 01:06:40 +0100 Subject: [PATCH] feat: remove dependencies for Node built-ins --- index.js | 7 ++--- lib/rules/boolean-prop-naming.js | 12 +++------ .../checked-requires-onchange-or-readonly.js | 4 +-- lib/rules/default-props-match-prop-types.js | 4 +-- lib/rules/display-name.js | 3 +-- lib/rules/forbid-elements.js | 5 ++-- lib/rules/function-component-definition.js | 7 +++-- lib/rules/jsx-closing-bracket-location.js | 13 ++++----- lib/rules/jsx-closing-tag-location.js | 10 +++---- lib/rules/jsx-curly-brace-presence.js | 11 +++----- lib/rules/jsx-curly-spacing.js | 9 +++---- lib/rules/jsx-indent-props.js | 6 ++--- lib/rules/jsx-indent.js | 12 +++------ lib/rules/jsx-key.js | 5 ++-- lib/rules/jsx-max-depth.js | 6 ++--- lib/rules/jsx-no-duplicate-props.js | 3 +-- lib/rules/jsx-no-literals.js | 8 +++--- lib/rules/jsx-no-script-url.js | 3 +-- lib/rules/jsx-no-target-blank.js | 5 ++-- lib/rules/jsx-no-useless-fragment.js | 10 +++---- lib/rules/jsx-sort-props.js | 11 +++----- lib/rules/jsx-wrap-multilines.js | 10 +++---- lib/rules/no-array-index-key.js | 3 +-- lib/rules/no-arrow-function-lifecycle.js | 9 ++----- lib/rules/no-danger.js | 6 ++--- lib/rules/no-deprecated.js | 7 +++-- lib/rules/no-direct-mutation-state.js | 4 +-- lib/rules/no-invalid-html-attribute.js | 3 +-- lib/rules/no-multi-comp.js | 4 +-- lib/rules/no-object-type-as-default-prop.js | 4 +-- lib/rules/no-set-state.js | 4 +-- lib/rules/no-unknown-property.js | 11 ++++---- lib/rules/no-unused-prop-types.js | 4 +-- lib/rules/prefer-read-only-props.js | 6 +---- lib/rules/prefer-stateless-function.js | 11 +++----- lib/rules/prop-types.js | 6 ++--- lib/rules/require-default-props.js | 8 +++--- lib/rules/require-optimization.js | 6 ++--- lib/rules/require-render-return.js | 6 ++--- lib/rules/sort-comp.js | 17 +++++------- lib/rules/static-property-placement.js | 7 +++-- lib/rules/void-dom-elements-no-children.js | 4 +-- lib/util/Components.js | 27 +++++++------------ lib/util/defaultProps.js | 5 ++-- lib/util/makeNoMethodSetStateRule.js | 8 +++--- lib/util/propTypes.js | 18 +++++-------- lib/util/propTypesSort.js | 10 ++----- lib/util/usedPropTypes.js | 9 ++----- package.json | 12 +-------- tests/helpers/parsers.js | 8 +++--- tests/util/Components.js | 11 +++----- types/string.prototype.repeat/index.d.ts | 3 --- 52 files changed, 129 insertions(+), 276 deletions(-) delete mode 100644 types/string.prototype.repeat/index.d.ts diff --git a/index.js b/index.js index 8426993e5b..32a883f424 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,9 @@ 'use strict'; -const fromEntries = require('object.fromentries'); -const entries = require('object.entries'); - const allRules = require('./lib/rules'); function filterRules(rules, predicate) { - return fromEntries(entries(rules).filter((entry) => predicate(entry[1]))); + return Object.fromEntries(Object.entries(rules).filter((entry) => predicate(entry[1]))); } /** @@ -14,7 +11,7 @@ function filterRules(rules, predicate) { * @returns {Record} */ function configureAsError(rules) { - return fromEntries(Object.keys(rules).map((key) => [`react/${key}`, 2])); + return Object.fromEntries(Object.keys(rules).map((key) => [`react/${key}`, 2])); } /** @type {Partial} */ diff --git a/lib/rules/boolean-prop-naming.js b/lib/rules/boolean-prop-naming.js index c867e622b5..42274462a8 100644 --- a/lib/rules/boolean-prop-naming.js +++ b/lib/rules/boolean-prop-naming.js @@ -5,19 +5,13 @@ 'use strict'; -const flatMap = require('array.prototype.flatmap'); -const values = require('object.values'); - const Components = require('../util/Components'); const propsUtil = require('../util/props'); const astUtil = require('../util/ast'); const docsUrl = require('../util/docsUrl'); const propWrapperUtil = require('../util/propWrapper'); const report = require('../util/report'); -const eslintUtil = require('../util/eslint'); - -const getSourceCode = eslintUtil.getSourceCode; -const getText = eslintUtil.getText; +const { getSourceCode, getText } = require('../util/eslint'); /** * Checks if prop is nested @@ -384,7 +378,7 @@ module.exports = { return; } - values(components.list()).forEach((component) => { + Object.values(components.list()).forEach((component) => { const annotation = getComponentTypeAnnotation(component); if (annotation) { @@ -396,7 +390,7 @@ module.exports = { } else if (annotation.type === 'TSTypeReference') { propType = objectTypeAnnotations.get(annotation.typeName.name); } else if (annotation.type === 'TSIntersectionType') { - propType = flatMap(annotation.types, (type) => ( + propType = annotation.types.flatMap((type) => ( type.type === 'TSTypeReference' ? objectTypeAnnotations.get(type.typeName.name) : type diff --git a/lib/rules/checked-requires-onchange-or-readonly.js b/lib/rules/checked-requires-onchange-or-readonly.js index 7358426af9..c7a9ba757d 100644 --- a/lib/rules/checked-requires-onchange-or-readonly.js +++ b/lib/rules/checked-requires-onchange-or-readonly.js @@ -6,7 +6,6 @@ 'use strict'; const ASTUtils = require('jsx-ast-utils'); -const flatMap = require('array.prototype.flatmap'); const isCreateElement = require('../util/isCreateElement'); const report = require('../util/report'); const docsUrl = require('../util/docsUrl'); @@ -30,8 +29,7 @@ const defaultOptions = { */ function extractTargetProps(properties, keyName) { return new Set( - flatMap( - properties, + properties.flatMap( (prop) => ( prop[keyName] && targetPropSet.has(prop[keyName].name) ? [prop[keyName].name] diff --git a/lib/rules/default-props-match-prop-types.js b/lib/rules/default-props-match-prop-types.js index de30f0d6ac..d93b247c36 100644 --- a/lib/rules/default-props-match-prop-types.js +++ b/lib/rules/default-props-match-prop-types.js @@ -6,8 +6,6 @@ 'use strict'; -const values = require('object.values'); - const Components = require('../util/Components'); const docsUrl = require('../util/docsUrl'); const report = require('../util/report'); @@ -95,7 +93,7 @@ module.exports = { return { 'Program:exit'() { // If no defaultProps could be found, we don't report anything. - values(components.list()) + Object.values(components.list()) .filter((component) => component.defaultProps) .forEach((component) => { reportInvalidDefaultProps( diff --git a/lib/rules/display-name.js b/lib/rules/display-name.js index 4b07f74de7..aab2cdb02b 100644 --- a/lib/rules/display-name.js +++ b/lib/rules/display-name.js @@ -5,7 +5,6 @@ 'use strict'; -const values = require('object.values'); const filter = require('es-iterator-helpers/Iterator.prototype.filter'); const forEach = require('es-iterator-helpers/Iterator.prototype.forEach'); @@ -269,7 +268,7 @@ module.exports = { 'Program:exit'() { const list = components.list(); // Report missing display name for all components - values(list).filter((component) => !component.hasDisplayName).forEach((component) => { + Object.values(list).filter((component) => !component.hasDisplayName).forEach((component) => { reportMissingDisplayName(component); }); if (checkContextObjects) { diff --git a/lib/rules/forbid-elements.js b/lib/rules/forbid-elements.js index 299ee3b849..e819c65c29 100644 --- a/lib/rules/forbid-elements.js +++ b/lib/rules/forbid-elements.js @@ -5,9 +5,8 @@ 'use strict'; -const has = require('hasown'); const docsUrl = require('../util/docsUrl'); -const getText = require('../util/eslint').getText; +const { getText } = require('../util/eslint'); const isCreateElement = require('../util/isCreateElement'); const report = require('../util/report'); @@ -73,7 +72,7 @@ module.exports = { }); function reportIfForbidden(element, node) { - if (has(indexedForbidConfigs, element)) { + if (Object.hasOwn(indexedForbidConfigs, element)) { const message = indexedForbidConfigs[element].message; report( diff --git a/lib/rules/function-component-definition.js b/lib/rules/function-component-definition.js index 4801463b10..33ee573ff8 100644 --- a/lib/rules/function-component-definition.js +++ b/lib/rules/function-component-definition.js @@ -5,11 +5,10 @@ 'use strict'; -const arrayIncludes = require('array-includes'); const Components = require('../util/Components'); const docsUrl = require('../util/docsUrl'); const reportC = require('../util/report'); -const getText = require('../util/eslint').getText; +const { getText } = require('../util/eslint'); const propsUtil = require('../util/props'); // ------------------------------------------------------------------------------ @@ -230,7 +229,7 @@ module.exports = { if (node.parent && node.parent.type === 'Property') return; - if (hasName(node) && !arrayIncludes(namedConfig, functionType)) { + if (hasName(node) && !namedConfig.includes(functionType)) { report(node, { messageId: namedConfig[0], fixerOptions: { @@ -243,7 +242,7 @@ module.exports = { }, }); } - if (!hasName(node) && !arrayIncludes(unnamedConfig, functionType)) { + if (!hasName(node) && !unnamedConfig.includes(functionType)) { report(node, { messageId: unnamedConfig[0], fixerOptions: { diff --git a/lib/rules/jsx-closing-bracket-location.js b/lib/rules/jsx-closing-bracket-location.js index eed661e944..dfe1b222f5 100644 --- a/lib/rules/jsx-closing-bracket-location.js +++ b/lib/rules/jsx-closing-bracket-location.js @@ -5,11 +5,8 @@ 'use strict'; -const has = require('hasown'); -const repeat = require('string.prototype.repeat'); - const docsUrl = require('../util/docsUrl'); -const getSourceCode = require('../util/eslint').getSourceCode; +const { getSourceCode } = require('../util/eslint'); const report = require('../util/report'); // ------------------------------------------------------------------------------ @@ -84,16 +81,16 @@ module.exports = { options.selfClosing = config; } else if (typeof config === 'object') { // [1, {location: 'something'}] (back-compat) - if (has(config, 'location')) { + if (Object.hasOwn(config, 'location')) { options.nonEmpty = config.location; options.selfClosing = config.location; } // [1, {nonEmpty: 'something'}] - if (has(config, 'nonEmpty')) { + if (Object.hasOwn(config, 'nonEmpty')) { options.nonEmpty = config.nonEmpty; } // [1, {selfClosing: 'something'}] - if (has(config, 'selfClosing')) { + if (Object.hasOwn(config, 'selfClosing')) { options.selfClosing = config.selfClosing; } } @@ -185,7 +182,7 @@ module.exports = { } if (indentation.length + 1 < newColumn) { // Non-whitespace characters were included in the column offset - spaces = repeat(' ', +correctColumn - indentation.length); + spaces = ' '.repeat(+correctColumn - indentation.length); } return indentation + spaces; } diff --git a/lib/rules/jsx-closing-tag-location.js b/lib/rules/jsx-closing-tag-location.js index f55570f3ca..f73df901ca 100644 --- a/lib/rules/jsx-closing-tag-location.js +++ b/lib/rules/jsx-closing-tag-location.js @@ -5,12 +5,9 @@ 'use strict'; -const repeat = require('string.prototype.repeat'); -const has = require('hasown'); - const astUtil = require('../util/ast'); const docsUrl = require('../util/docsUrl'); -const getSourceCode = require('../util/eslint').getSourceCode; +const { getSourceCode } = require('../util/eslint'); const report = require('../util/report'); // ------------------------------------------------------------------------------ @@ -66,7 +63,7 @@ module.exports = { if (typeof config === 'string') { option = config; } else if (typeof config === 'object') { - if (has(config, 'location')) { + if (Object.hasOwn(config, 'location')) { option = config.location; } } @@ -117,8 +114,7 @@ module.exports = { node, loc: node.loc, fix(fixer) { - const indent = repeat( - ' ', + const indent = ' '.repeat( getIndentation(openingStartOfLine, opening), ); diff --git a/lib/rules/jsx-curly-brace-presence.js b/lib/rules/jsx-curly-brace-presence.js index 695e08a075..57dcc23481 100755 --- a/lib/rules/jsx-curly-brace-presence.js +++ b/lib/rules/jsx-curly-brace-presence.js @@ -6,15 +6,10 @@ 'use strict'; -const arrayIncludes = require('array-includes'); - const docsUrl = require('../util/docsUrl'); const jsxUtil = require('../util/jsx'); const report = require('../util/report'); -const eslintUtil = require('../util/eslint'); - -const getSourceCode = eslintUtil.getSourceCode; -const getText = eslintUtil.getText; +const { getSourceCode, getText } = require('../util/eslint'); // ------------------------------------------------------------------------------ // Constants @@ -38,7 +33,7 @@ function containsLineTerminators(rawStringValue) { } function containsBackslash(rawStringValue) { - return arrayIncludes(rawStringValue, '\\'); + return rawStringValue.includes('\\'); } function containsHTMLEntity(rawStringValue) { @@ -335,7 +330,7 @@ module.exports = { const childrenExcludingWhitespaceLiteral = children.filter((child) => !isWhiteSpaceLiteral(child)); const adjSiblings = getAdjacentSiblings(node, childrenExcludingWhitespaceLiteral); - return adjSiblings.some((x) => x.type && arrayIncludes(['JSXExpressionContainer', 'JSXElement'], x.type)); + return adjSiblings.some((x) => x.type && ['JSXExpressionContainer', 'JSXElement'].includes(x.type)); } function shouldCheckForUnnecessaryCurly(node, config) { const parent = node.parent; diff --git a/lib/rules/jsx-curly-spacing.js b/lib/rules/jsx-curly-spacing.js index 277297403b..bd75eacd7c 100644 --- a/lib/rules/jsx-curly-spacing.js +++ b/lib/rules/jsx-curly-spacing.js @@ -11,9 +11,8 @@ 'use strict'; -const has = require('hasown'); const docsUrl = require('../util/docsUrl'); -const getSourceCode = require('../util/eslint').getSourceCode; +const { getSourceCode } = require('../util/eslint'); const report = require('../util/report'); // ------------------------------------------------------------------------------ @@ -120,7 +119,7 @@ module.exports = { function normalizeConfig(configOrTrue, defaults, lastPass) { const config = configOrTrue === true ? {} : configOrTrue; const when = config.when || defaults.when; - const allowMultiline = has(config, 'allowMultiline') ? config.allowMultiline : defaults.allowMultiline; + const allowMultiline = Object.hasOwn(config, 'allowMultiline') ? config.allowMultiline : defaults.allowMultiline; const spacing = config.spacing || {}; let objectLiteralSpaces = spacing.objectLiterals || defaults.objectLiteralSpaces; if (lastPass) { @@ -148,9 +147,9 @@ module.exports = { when: DEFAULT_WHEN, allowMultiline: DEFAULT_ALLOW_MULTILINE, }); - const attributes = has(originalConfig, 'attributes') ? originalConfig.attributes : DEFAULT_ATTRIBUTES; + const attributes = Object.hasOwn(originalConfig, 'attributes') ? originalConfig.attributes : DEFAULT_ATTRIBUTES; const attributesConfig = attributes ? normalizeConfig(attributes, defaultConfig, true) : null; - const children = has(originalConfig, 'children') ? originalConfig.children : DEFAULT_CHILDREN; + const children = Object.hasOwn(originalConfig, 'children') ? originalConfig.children : DEFAULT_CHILDREN; const childrenConfig = children ? normalizeConfig(children, defaultConfig, true) : null; // -------------------------------------------------------------------------- diff --git a/lib/rules/jsx-indent-props.js b/lib/rules/jsx-indent-props.js index 9badb7f4cc..a788c3112a 100644 --- a/lib/rules/jsx-indent-props.js +++ b/lib/rules/jsx-indent-props.js @@ -30,11 +30,9 @@ 'use strict'; -const repeat = require('string.prototype.repeat'); - const astUtil = require('../util/ast'); const docsUrl = require('../util/docsUrl'); -const getText = require('../util/eslint').getText; +const { getText } = require('../util/eslint'); const reportC = require('../util/report'); // ------------------------------------------------------------------------------ @@ -133,7 +131,7 @@ module.exports = { data: msgContext, fix(fixer) { return fixer.replaceTextRange([node.range[0] - node.loc.start.column, node.range[0]], - repeat(indentType === 'space' ? ' ' : '\t', needed), + (indentType === 'space' ? ' ' : '\t').repeat(needed), ); }, }); diff --git a/lib/rules/jsx-indent.js b/lib/rules/jsx-indent.js index 9d5e0c034a..0269aa1aae 100644 --- a/lib/rules/jsx-indent.js +++ b/lib/rules/jsx-indent.js @@ -30,17 +30,11 @@ 'use strict'; -const matchAll = require('string.prototype.matchall'); -const repeat = require('string.prototype.repeat'); - const astUtil = require('../util/ast'); const docsUrl = require('../util/docsUrl'); const reportC = require('../util/report'); const jsxUtil = require('../util/jsx'); -const eslintUtil = require('../util/eslint'); - -const getSourceCode = eslintUtil.getSourceCode; -const getText = eslintUtil.getText; +const { getSourceCode, getText } = require('../util/eslint'); // ------------------------------------------------------------------------------ // Rule Definition @@ -111,7 +105,7 @@ module.exports = { * @private */ function getFixerFunction(node, needed) { - const indent = repeat(indentChar, needed); + const indent = indentChar.repeat(needed); if (node.type === 'JSXText' || node.type === 'Literal') { return function fix(fixer) { @@ -326,7 +320,7 @@ module.exports = { const value = node.value; const regExp = indentType === 'space' ? /\n( *)[\t ]*\S/g : /\n(\t*)[\t ]*\S/g; const nodeIndentsPerLine = Array.from( - matchAll(String(value), regExp), + String(value).matchAll(regExp), (match) => (match[1] ? match[1].length : 0), ); const hasFirstInLineNode = nodeIndentsPerLine.length > 0; diff --git a/lib/rules/jsx-key.js b/lib/rules/jsx-key.js index ab15bdf2b4..6f42123d19 100644 --- a/lib/rules/jsx-key.js +++ b/lib/rules/jsx-key.js @@ -7,12 +7,11 @@ const hasProp = require('jsx-ast-utils/hasProp'); const propName = require('jsx-ast-utils/propName'); -const values = require('object.values'); const docsUrl = require('../util/docsUrl'); const pragmaUtil = require('../util/pragma'); const report = require('../util/report'); const astUtil = require('../util/ast'); -const getText = require('../util/eslint').getText; +const { getText } = require('../util/eslint'); // ------------------------------------------------------------------------------ // Rule Definition @@ -234,7 +233,7 @@ module.exports = { }); if (warnOnDuplicates) { - values(map).filter((v) => v.length > 1).forEach((v) => { + Object.values(map).filter((v) => v.length > 1).forEach((v) => { v.forEach((n) => { if (!seen.has(n)) { seen.add(n); diff --git a/lib/rules/jsx-max-depth.js b/lib/rules/jsx-max-depth.js index 12625fa704..a9d9bfb8ca 100644 --- a/lib/rules/jsx-max-depth.js +++ b/lib/rules/jsx-max-depth.js @@ -5,8 +5,6 @@ 'use strict'; -const has = require('hasown'); -const includes = require('array-includes'); const variableUtil = require('../util/variable'); const jsxUtil = require('../util/jsx'); const docsUrl = require('../util/docsUrl'); @@ -49,7 +47,7 @@ module.exports = { const DEFAULT_DEPTH = 2; const option = context.options[0] || {}; - const maxDepth = has(option, 'max') ? option.max : DEFAULT_DEPTH; + const maxDepth = Object.hasOwn(option, 'max') ? option.max : DEFAULT_DEPTH; function isExpression(node) { return node.type === 'JSXExpressionContainer'; @@ -106,7 +104,7 @@ module.exports = { const variable = variableUtil.getVariableFromContext(context, startNode, name); if (variable && variable.references) { - const containDuplicates = previousReferences.some((ref) => includes(variable.references, ref)); + const containDuplicates = previousReferences.some((ref) => variable.references.includes(ref)); // Prevent getting stuck in circular references if (containDuplicates) { diff --git a/lib/rules/jsx-no-duplicate-props.js b/lib/rules/jsx-no-duplicate-props.js index 77de5bde51..0e40879094 100644 --- a/lib/rules/jsx-no-duplicate-props.js +++ b/lib/rules/jsx-no-duplicate-props.js @@ -5,7 +5,6 @@ 'use strict'; -const has = require('hasown'); const docsUrl = require('../util/docsUrl'); const report = require('../util/report'); @@ -63,7 +62,7 @@ module.exports = { name = name.toLowerCase(); } - if (has(props, name)) { + if (Object.hasOwn(props, name)) { report(context, messages.noDuplicateProps, 'noDuplicateProps', { node: decl, }); diff --git a/lib/rules/jsx-no-literals.js b/lib/rules/jsx-no-literals.js index 34ff95b194..59be186cab 100644 --- a/lib/rules/jsx-no-literals.js +++ b/lib/rules/jsx-no-literals.js @@ -10,12 +10,10 @@ const iterFrom = require('es-iterator-helpers/Iterator.from'); const map = require('es-iterator-helpers/Iterator.prototype.map'); const some = require('es-iterator-helpers/Iterator.prototype.some'); const flatMap = require('es-iterator-helpers/Iterator.prototype.flatMap'); -const fromEntries = require('object.fromentries'); -const entries = require('object.entries'); const docsUrl = require('../util/docsUrl'); const report = require('../util/report'); -const getText = require('../util/eslint').getText; +const { getText } = require('../util/eslint'); // ------------------------------------------------------------------------------ // Rule Definition @@ -140,9 +138,9 @@ function normalizeConfig(config) { }); if (config.elementOverrides) { - normalizedConfig.elementOverrides = fromEntries( + normalizedConfig.elementOverrides = Object.fromEntries( flatMap( - iterFrom(entries(config.elementOverrides)), + iterFrom(Object.entries(config.elementOverrides)), (entry) => { const elementName = entry[0]; const rawElementConfig = entry[1]; diff --git a/lib/rules/jsx-no-script-url.js b/lib/rules/jsx-no-script-url.js index b18ce2ccb3..d180630df0 100644 --- a/lib/rules/jsx-no-script-url.js +++ b/lib/rules/jsx-no-script-url.js @@ -5,7 +5,6 @@ 'use strict'; -const includes = require('array-includes'); const docsUrl = require('../util/docsUrl'); const linkComponentsUtil = require('../util/linkComponents'); const report = require('../util/report'); @@ -30,7 +29,7 @@ function shouldVerifyProp(node, config) { if (!name || !parentName || !config.has(parentName)) return false; const attributes = config.get(parentName); - return includes(attributes, name); + return attributes.includes(name); } function parseLegacyOption(config, option) { diff --git a/lib/rules/jsx-no-target-blank.js b/lib/rules/jsx-no-target-blank.js index 779346e268..c1cacf8743 100644 --- a/lib/rules/jsx-no-target-blank.js +++ b/lib/rules/jsx-no-target-blank.js @@ -5,7 +5,6 @@ 'use strict'; -const includes = require('array-includes'); const docsUrl = require('../util/docsUrl'); const linkComponentsUtil = require('../util/linkComponents'); const report = require('../util/report'); @@ -50,7 +49,7 @@ function attributeValuePossiblyBlank(attribute) { } function hasExternalLink(node, linkAttributes, warnOnSpreadAttributes, spreadAttributeIndex) { - const linkIndex = findLastIndex(node.attributes, (attr) => attr.name && includes(linkAttributes, attr.name.name)); + const linkIndex = findLastIndex(node.attributes, (attr) => attr.name && linkAttributes.includes(attr.name.name)); const foundExternalLink = linkIndex !== -1 && ((attr) => attr.value && attr.value.type === 'Literal' && /^(?:\w+:|\/\/)/.test(attr.value.value))( node.attributes[linkIndex]); return foundExternalLink || (warnOnSpreadAttributes && linkIndex < spreadAttributeIndex); @@ -58,7 +57,7 @@ function hasExternalLink(node, linkAttributes, warnOnSpreadAttributes, spreadAtt function hasDynamicLink(node, linkAttributes) { const dynamicLinkIndex = findLastIndex(node.attributes, (attr) => attr.name - && includes(linkAttributes, attr.name.name) + && linkAttributes.includes(attr.name.name) && attr.value && attr.value.type === 'JSXExpressionContainer'); if (dynamicLinkIndex !== -1) { diff --git a/lib/rules/jsx-no-useless-fragment.js b/lib/rules/jsx-no-useless-fragment.js index 979b805787..858534b379 100644 --- a/lib/rules/jsx-no-useless-fragment.js +++ b/lib/rules/jsx-no-useless-fragment.js @@ -4,14 +4,12 @@ 'use strict'; -const arrayIncludes = require('array-includes'); - const pragmaUtil = require('../util/pragma'); const astUtil = require('../util/ast'); const jsxUtil = require('../util/jsx'); const docsUrl = require('../util/docsUrl'); const report = require('../util/report'); -const getText = require('../util/eslint').getText; +const { getText } = require('../util/eslint'); function isJSXText(node) { return !!node && (node.type === 'JSXText' || node.type === 'Literal'); @@ -52,8 +50,8 @@ function trimLikeReact(text) { const leadingSpaces = /^\s*/.exec(text)[0]; const trailingSpaces = /\s*$/.exec(text)[0]; - const start = arrayIncludes(leadingSpaces, '\n') ? leadingSpaces.length : 0; - const end = arrayIncludes(trailingSpaces, '\n') ? text.length - trailingSpaces.length : text.length; + const start = leadingSpaces.includes('\n') ? leadingSpaces.length : 0; + const end = trailingSpaces.includes('\n') ? text.length - trailingSpaces.length : text.length; return text.slice(start, end); } @@ -121,7 +119,7 @@ module.exports = { function isPaddingSpaces(node) { return isJSXText(node) && isOnlyWhitespace(node.raw) - && arrayIncludes(node.raw, '\n'); + && node.raw.includes('\n'); } function isFragmentWithSingleExpression(node) { diff --git a/lib/rules/jsx-sort-props.js b/lib/rules/jsx-sort-props.js index 09ff604a37..7ed95221ba 100644 --- a/lib/rules/jsx-sort-props.js +++ b/lib/rules/jsx-sort-props.js @@ -6,17 +6,12 @@ 'use strict'; const propName = require('jsx-ast-utils/propName'); -const includes = require('array-includes'); -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; -const getSourceCode = eslintUtil.getSourceCode; +const { getSourceCode, getText } = require('../util/eslint'); // ------------------------------------------------------------------------------ // Rule Definition @@ -241,7 +236,7 @@ function generateFixerFunction(node, context, reservedList) { const sortableAttributeGroups = getGroupsOfSortableAttributes(attributes, context); const sortedAttributeGroups = sortableAttributeGroups .slice(0) - .map((group) => toSorted(group, (a, b) => contextCompare(a, b, options))); + .map((group) => group.toSorted((a, b) => contextCompare(a, b, options))); return function fixFunction(fixer) { const fixers = []; @@ -324,7 +319,7 @@ const reportedNodeAttributes = new WeakMap(); function reportNodeAttribute(nodeAttribute, errorType, node, context, reservedList) { const errors = reportedNodeAttributes.get(nodeAttribute) || []; - if (includes(errors, errorType)) { + if (errors.includes(errorType)) { return; } diff --git a/lib/rules/jsx-wrap-multilines.js b/lib/rules/jsx-wrap-multilines.js index 05600cef58..b940540eaf 100644 --- a/lib/rules/jsx-wrap-multilines.js +++ b/lib/rules/jsx-wrap-multilines.js @@ -5,15 +5,11 @@ 'use strict'; -const has = require('hasown'); const docsUrl = require('../util/docsUrl'); -const eslintUtil = require('../util/eslint'); +const { getSourceCode, getText } = require('../util/eslint'); const jsxUtil = require('../util/jsx'); const reportC = require('../util/report'); -const isParenthesized = require('../util/ast').isParenthesized; - -const getSourceCode = eslintUtil.getSourceCode; -const getText = eslintUtil.getText; +const { isParenthesized } = require('../util/ast'); // ------------------------------------------------------------------------------ // Constants @@ -85,7 +81,7 @@ module.exports = { create(context) { function getOption(type) { const userOptions = context.options[0] || {}; - if (has(userOptions, type)) { + if (Object.hasOwn(userOptions, type)) { return userOptions[type]; } return DEFAULTS[type]; diff --git a/lib/rules/no-array-index-key.js b/lib/rules/no-array-index-key.js index 90381b3a08..c4a3d54d0f 100644 --- a/lib/rules/no-array-index-key.js +++ b/lib/rules/no-array-index-key.js @@ -5,7 +5,6 @@ 'use strict'; -const has = require('hasown'); const astUtil = require('../util/ast'); const docsUrl = require('../util/docsUrl'); const pragma = require('../util/pragma'); @@ -113,7 +112,7 @@ module.exports = { if (callee.property.type !== 'Identifier') { return null; } - if (!has(iteratorFunctionsToIndexParamPosition, callee.property.name)) { + if (!Object.hasOwn(iteratorFunctionsToIndexParamPosition, callee.property.name)) { return null; } diff --git a/lib/rules/no-arrow-function-lifecycle.js b/lib/rules/no-arrow-function-lifecycle.js index 26596d91ea..374f26ee39 100644 --- a/lib/rules/no-arrow-function-lifecycle.js +++ b/lib/rules/no-arrow-function-lifecycle.js @@ -5,18 +5,13 @@ 'use strict'; -const values = require('object.values'); - const Components = require('../util/Components'); const astUtil = require('../util/ast'); const componentUtil = require('../util/componentUtil'); const docsUrl = require('../util/docsUrl'); const lifecycleMethods = require('../util/lifecycleMethods'); const report = require('../util/report'); -const eslintUtil = require('../util/eslint'); - -const getSourceCode = eslintUtil.getSourceCode; -const getText = eslintUtil.getText; +const { getSourceCode, getText } = require('../util/eslint'); function getRuleText(node) { const params = node.value.params.map((p) => p.name); @@ -139,7 +134,7 @@ module.exports = { return { 'Program:exit'() { - values(components.list()).forEach((component) => { + Object.values(components.list()).forEach((component) => { const properties = astUtil.getComponentProperties(component.node); reportNoArrowFunctionLifecycle(properties); }); diff --git a/lib/rules/no-danger.js b/lib/rules/no-danger.js index 54dbed49cf..c1b494b200 100644 --- a/lib/rules/no-danger.js +++ b/lib/rules/no-danger.js @@ -5,8 +5,6 @@ 'use strict'; -const has = require('hasown'); -const fromEntries = require('object.fromentries/polyfill')(); const minimatch = require('minimatch'); const docsUrl = require('../util/docsUrl'); @@ -21,7 +19,7 @@ const DANGEROUS_PROPERTY_NAMES = [ 'dangerouslySetInnerHTML', ]; -const DANGEROUS_PROPERTIES = fromEntries(DANGEROUS_PROPERTY_NAMES.map((prop) => [prop, prop])); +const DANGEROUS_PROPERTIES = Object.fromEntries(DANGEROUS_PROPERTY_NAMES.map((prop) => [prop, prop])); // ------------------------------------------------------------------------------ // Helpers @@ -33,7 +31,7 @@ const DANGEROUS_PROPERTIES = fromEntries(DANGEROUS_PROPERTY_NAMES.map((prop) => * @returns {boolean} Whether or not the attribute is dangerous. */ function isDangerous(name) { - return has(DANGEROUS_PROPERTIES, name); + return Object.hasOwn(DANGEROUS_PROPERTIES, name); } // ------------------------------------------------------------------------------ diff --git a/lib/rules/no-deprecated.js b/lib/rules/no-deprecated.js index a8254005d1..8b19090751 100644 --- a/lib/rules/no-deprecated.js +++ b/lib/rules/no-deprecated.js @@ -7,14 +7,13 @@ 'use strict'; -const entries = require('object.entries'); const astUtil = require('../util/ast'); const componentUtil = require('../util/componentUtil'); const docsUrl = require('../util/docsUrl'); const pragmaUtil = require('../util/pragma'); -const testReactVersion = require('../util/version').testReactVersion; +const { testReactVersion } = require('../util/version'); const report = require('../util/report'); -const getText = require('../util/eslint').getText; +const { getText } = require('../util/eslint'); // ------------------------------------------------------------------------------ // Constants @@ -167,7 +166,7 @@ module.exports = { return false; } - entries(MODULES).some((entry) => { + Object.entries(MODULES).some((entry) => { const key = entry[0]; const moduleNames = entry[1]; if ( diff --git a/lib/rules/no-direct-mutation-state.js b/lib/rules/no-direct-mutation-state.js index 761151fbb8..c7539cc5f5 100644 --- a/lib/rules/no-direct-mutation-state.js +++ b/lib/rules/no-direct-mutation-state.js @@ -6,8 +6,6 @@ 'use strict'; -const values = require('object.values'); - const Components = require('../util/Components'); const componentUtil = require('../util/componentUtil'); const docsUrl = require('../util/docsUrl'); @@ -144,7 +142,7 @@ module.exports = { }, 'Program:exit'() { - values(components.list()) + Object.values(components.list()) .filter((component) => !isValid(component)) .forEach((component) => { reportMutations(component); diff --git a/lib/rules/no-invalid-html-attribute.js b/lib/rules/no-invalid-html-attribute.js index 3fd69db8ca..b18e263b7f 100644 --- a/lib/rules/no-invalid-html-attribute.js +++ b/lib/rules/no-invalid-html-attribute.js @@ -5,7 +5,6 @@ 'use strict'; -const matchAll = require('string.prototype.matchall'); const docsUrl = require('../util/docsUrl'); const report = require('../util/report'); @@ -245,7 +244,7 @@ const messages = { function splitIntoRangedParts(node, regex) { const valueRangeStart = node.range[0] + 1; // the plus one is for the initial quote - return Array.from(matchAll(node.value, regex), (match) => { + return Array.from(node.value.matchAll(regex), (match) => { const start = match.index + valueRangeStart; const end = start + match[0].length; diff --git a/lib/rules/no-multi-comp.js b/lib/rules/no-multi-comp.js index 8cf73c90bc..d3e826cd79 100644 --- a/lib/rules/no-multi-comp.js +++ b/lib/rules/no-multi-comp.js @@ -5,8 +5,6 @@ 'use strict'; -const values = require('object.values'); - const Components = require('../util/Components'); const docsUrl = require('../util/docsUrl'); const report = require('../util/report'); @@ -67,7 +65,7 @@ module.exports = { return; } - values(components.list()) + Object.values(components.list()) .filter((component) => !isIgnored(component)) .slice(1) .forEach((component) => { diff --git a/lib/rules/no-object-type-as-default-prop.js b/lib/rules/no-object-type-as-default-prop.js index 2683bfea4c..0bad2dd5f6 100644 --- a/lib/rules/no-object-type-as-default-prop.js +++ b/lib/rules/no-object-type-as-default-prop.js @@ -5,8 +5,6 @@ 'use strict'; -const values = require('object.values'); - const Components = require('../util/Components'); const docsUrl = require('../util/docsUrl'); const astUtil = require('../util/ast'); @@ -93,7 +91,7 @@ module.exports = { create: Components.detect((context, components) => ({ 'Program:exit'() { const list = components.list(); - values(list) + Object.values(list) .filter((component) => hasUsedObjectDestructuringSyntax(component.node.params)) .forEach((component) => { const node = component.node; diff --git a/lib/rules/no-set-state.js b/lib/rules/no-set-state.js index 44967bf03c..e3db687b46 100644 --- a/lib/rules/no-set-state.js +++ b/lib/rules/no-set-state.js @@ -5,8 +5,6 @@ 'use strict'; -const values = require('object.values'); - const Components = require('../util/Components'); const docsUrl = require('../util/docsUrl'); const report = require('../util/report'); @@ -77,7 +75,7 @@ module.exports = { }, 'Program:exit'() { - values(components.list()) + Object.values(components.list()) .filter((component) => !isValid(component)) .forEach((component) => { reportSetStateUsages(component); diff --git a/lib/rules/no-unknown-property.js b/lib/rules/no-unknown-property.js index dc5018007a..d323633829 100644 --- a/lib/rules/no-unknown-property.js +++ b/lib/rules/no-unknown-property.js @@ -5,10 +5,9 @@ 'use strict'; -const has = require('hasown'); const docsUrl = require('../util/docsUrl'); -const getText = require('../util/eslint').getText; -const testReactVersion = require('../util/version').testReactVersion; +const { getText } = require('../util/eslint'); +const { testReactVersion } = require('../util/version'); const report = require('../util/report'); // ------------------------------------------------------------------------------ @@ -494,10 +493,10 @@ function tagNameHasDot(node) { * @returns {string | undefined} The standard name of the attribute, or undefined if no standard name was found. */ function getStandardName(name, context) { - if (has(DOM_ATTRIBUTE_NAMES, name)) { + if (Object.hasOwn(DOM_ATTRIBUTE_NAMES, name)) { return DOM_ATTRIBUTE_NAMES[/** @type {keyof DOM_ATTRIBUTE_NAMES} */ (name)]; } - if (has(SVGDOM_ATTRIBUTE_NAMES, name)) { + if (Object.hasOwn(SVGDOM_ATTRIBUTE_NAMES, name)) { return SVGDOM_ATTRIBUTE_NAMES[/** @type {keyof SVGDOM_ATTRIBUTE_NAMES} */ (name)]; } const names = getDOMPropertyNames(context); @@ -597,7 +596,7 @@ module.exports = { // Let's dive deeper into tags that are HTML/DOM elements (`