Skip to content

Commit

Permalink
[Refactor] reduce egregious use of array spread, in favor of `[].conc…
Browse files Browse the repository at this point in the history
…at` idiom
  • Loading branch information
ljharb committed Mar 16, 2022
1 parent 0f1615a commit 1826628
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 41 deletions.
24 changes: 12 additions & 12 deletions __tests__/src/rules/no-static-element-interactions-test.js
Expand Up @@ -348,8 +348,8 @@ const neverValid = [

const recommendedOptions = configs.recommended.rules[`jsx-a11y/${ruleName}`][1] || {};
ruleTester.run(`${ruleName}:recommended`, rule, {
valid: [
...alwaysValid,
valid: [].concat(
alwaysValid,
// All the possible handlers
{ code: '<div onCopy={() => {}} />;' },
{ code: '<div onCut={() => {}} />;' },
Expand Down Expand Up @@ -417,22 +417,22 @@ ruleTester.run(`${ruleName}:recommended`, rule, {
// Expressions should pass in recommended mode
{ code: '<div role={ROLE_BUTTON} onClick={() => {}} />;' },
{ code: '<div {...this.props} role={this.props.role} onKeyPress={e => this.handleKeyPress(e)}>{this.props.children}</div>' },
]
)
.map(ruleOptionsMapperFactory(recommendedOptions))
.map(parserOptionsMapper),
invalid: [
...neverValid,
]
invalid: [].concat(
neverValid,
)
.map(ruleOptionsMapperFactory(recommendedOptions))
.map(parserOptionsMapper),
});

ruleTester.run(`${ruleName}:strict`, rule, {
valid: [
...alwaysValid,
].map(parserOptionsMapper),
invalid: [
...neverValid,
valid: [].concat(
alwaysValid,
).map(parserOptionsMapper),
invalid: [].concat(
neverValid,
// All the possible handlers
{ code: '<div onContextMenu={() => {}} />;', errors: [expectedError] },
{ code: '<div onDblClick={() => {}} />;', errors: [expectedError] },
Expand All @@ -453,5 +453,5 @@ ruleTester.run(`${ruleName}:strict`, rule, {
// Expressions should fail in strict mode
{ code: '<div role={ROLE_BUTTON} onClick={() => {}} />;', errors: [expectedError] },
{ code: '<div {...this.props} role={this.props.role} onKeyPress={e => this.handleKeyPress(e)}>{this.props.children}</div>', errors: [expectedError] },
].map(parserOptionsMapper),
).map(parserOptionsMapper),
});
14 changes: 7 additions & 7 deletions src/rules/alt-text.js
Expand Up @@ -212,15 +212,15 @@ export default {
(components, customComponentsForElement) => components.concat(customComponentsForElement || []),
[],
);
const typesToValidate = new Set([]
.concat(customComponents, ...elementOptions)
.map((type) => {
if (type === 'input[type="image"]') { return 'input'; }
return type;
}));
const typesToValidate = new Set(
[].concat(
customComponents,
elementOptions,
).map((type) => (type === 'input[type="image"]' ? 'input' : type)),
);

return {
JSXOpeningElement: (node) => {
JSXOpeningElement(node) {
const nodeType = elementType(node);
if (!typesToValidate.has(nodeType)) { return; }

Expand Down
2 changes: 1 addition & 1 deletion src/rules/aria-unsupported-elements.js
Expand Up @@ -44,7 +44,7 @@ export default {
return;
}

const invalidAttributes = [...aria.keys()].concat('role');
const invalidAttributes = [...aria.keys(), 'role'];

node.attributes.forEach((prop) => {
if (prop.type === 'JSXSpreadAttribute') {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/autocomplete-valid.js
Expand Up @@ -27,7 +27,7 @@ export default {
JSXOpeningElement: (node) => {
const options = context.options[0] || {};
const { inputComponents = [] } = options;
const inputTypes = ['input', ...inputComponents];
const inputTypes = ['input'].concat(inputComponents);

const elType = elementType(node);
const autocomplete = getLiteralPropValue(getProp(node.attributes, 'autocomplete'));
Expand Down
2 changes: 1 addition & 1 deletion src/rules/control-has-associated-label.js
Expand Up @@ -51,7 +51,7 @@ export default ({
ignoreRoles = [],
} = options;

const newIgnoreElements = new Set([...ignoreElements, ...ignoreList]);
const newIgnoreElements = new Set([].concat(ignoreElements, ignoreList));

const rule = (node: JSXElement): void => {
const tag = elementType(node.openingElement);
Expand Down
8 changes: 4 additions & 4 deletions src/rules/interactive-supports-focus.js
Expand Up @@ -42,10 +42,10 @@ const schema = generateObjSchema({
});
const domElements = [...dom.keys()];

const interactiveProps = [
...eventHandlersByType.mouse,
...eventHandlersByType.keyboard,
];
const interactiveProps = [].concat(
eventHandlersByType.mouse,
eventHandlersByType.keyboard,
);

export default ({
meta: {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/label-has-for.js
Expand Up @@ -28,7 +28,7 @@ const schema = {
};
// Breadth-first search, assuming that HTML for forms is shallow.
function validateNesting(node) {
let queue = [...node.parent.children];
let queue = node.parent.children.slice();
let child;
let opener;
while (queue.length) {
Expand Down
12 changes: 6 additions & 6 deletions src/rules/no-noninteractive-element-interactions.js
Expand Up @@ -32,12 +32,12 @@ import isPresentationRole from '../util/isPresentationRole';
const errorMessage = 'Non-interactive elements should not be assigned mouse or keyboard event listeners.';

const domElements = [...dom.keys()];
const defaultInteractiveProps = [
...eventHandlersByType.focus,
...eventHandlersByType.image,
...eventHandlersByType.keyboard,
...eventHandlersByType.mouse,
];
const defaultInteractiveProps = [].concat(
eventHandlersByType.focus,
eventHandlersByType.image,
eventHandlersByType.keyboard,
eventHandlersByType.mouse,
);
const schema = generateObjSchema({
handlers: arraySchema,
});
Expand Down
10 changes: 5 additions & 5 deletions src/rules/no-static-element-interactions.js
Expand Up @@ -32,11 +32,11 @@ import isPresentationRole from '../util/isPresentationRole';
const errorMessage = 'Avoid non-native interactive elements. If using native HTML is not possible, add an appropriate role and support for tabbing, mouse, keyboard, and touch inputs to an interactive content element.';

const domElements = [...dom.keys()];
const defaultInteractiveProps = [
...eventHandlersByType.focus,
...eventHandlersByType.keyboard,
...eventHandlersByType.mouse,
];
const defaultInteractiveProps = [].concat(
eventHandlersByType.focus,
eventHandlersByType.keyboard,
eventHandlersByType.mouse,
);
const schema = generateObjSchema({
handlers: arraySchema,
});
Expand Down
6 changes: 3 additions & 3 deletions src/util/mayHaveAccessibleLabel.js
Expand Up @@ -16,12 +16,12 @@ function hasLabellingProp(
openingElement: JSXOpeningElement,
additionalLabellingProps?: Array<string> = [],
) {
const labellingProps = [
const labellingProps = [].concat(
'alt', // Assume alt is used correctly on an image
'aria-label',
'aria-labelledby',
...additionalLabellingProps,
];
additionalLabellingProps,
);
return openingElement.attributes.some((attribute): boolean => {
// We must assume that a spread value contains a labelling prop.
if (attribute.type !== 'JSXAttribute') {
Expand Down

0 comments on commit 1826628

Please sign in to comment.