Skip to content

Commit

Permalink
feat: remove dependencies for Node built-ins
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDeBoey committed Oct 30, 2024
1 parent c297d92 commit 8fab926
Show file tree
Hide file tree
Showing 52 changed files with 127 additions and 269 deletions.
7 changes: 2 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
'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])));
}

/**
* @param {object} rules - rules object mapping rule name to rule module
* @returns {Record<string, 2 | 'error'>}
*/
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<typeof allRules>} */
Expand Down
12 changes: 3 additions & 9 deletions lib/rules/boolean-prop-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -384,7 +378,7 @@ module.exports = {
return;
}

values(components.list()).forEach((component) => {
Object.values(components.list()).forEach((component) => {
const annotation = getComponentTypeAnnotation(component);

if (annotation) {
Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions lib/rules/checked-requires-onchange-or-readonly.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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]
Expand Down
4 changes: 1 addition & 3 deletions lib/rules/default-props-match-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 1 addition & 2 deletions lib/rules/display-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions lib/rules/forbid-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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(
Expand Down
7 changes: 3 additions & 4 deletions lib/rules/function-component-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -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: {
Expand All @@ -243,7 +242,7 @@ module.exports = {
},
});
}
if (!hasName(node) && !arrayIncludes(unnamedConfig, functionType)) {
if (!hasName(node) && !unnamedConfig.includes(functionType)) {
report(node, {
messageId: unnamedConfig[0],
fixerOptions: {
Expand Down
13 changes: 5 additions & 8 deletions lib/rules/jsx-closing-bracket-location.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
Expand Down
10 changes: 3 additions & 7 deletions lib/rules/jsx-closing-tag-location.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -117,8 +114,7 @@ module.exports = {
node,
loc: node.loc,
fix(fixer) {
const indent = repeat(
' ',
const indent = ' '.repeat(
getIndentation(openingStartOfLine, opening),
);

Expand Down
11 changes: 3 additions & 8 deletions lib/rules/jsx-curly-brace-presence.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,7 +33,7 @@ function containsLineTerminators(rawStringValue) {
}

function containsBackslash(rawStringValue) {
return arrayIncludes(rawStringValue, '\\');
return rawStringValue.includes('\\');
}

function containsHTMLEntity(rawStringValue) {
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 4 additions & 5 deletions lib/rules/jsx-curly-spacing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

// --------------------------------------------------------------------------
Expand Down
6 changes: 2 additions & 4 deletions lib/rules/jsx-indent-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -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),
);
},
});
Expand Down
12 changes: 3 additions & 9 deletions lib/rules/jsx-indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions lib/rules/jsx-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 8fab926

Please sign in to comment.