From 8590295993bd90802b73ba8a4ed1e5edf4986318 Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Tue, 25 Jul 2023 10:48:42 +0300 Subject: [PATCH] Enable unicorn/prefer-spread rule --- .eslintrc.json | 1 - js/src/dom/data.js | 2 +- js/src/dom/selector-engine.js | 4 ++-- js/src/dropdown.js | 6 ++++-- js/src/tooltip.js | 6 ++++-- js/src/util/sanitizer.js | 6 +++--- js/tests/integration/bundle-modularity.js | 2 +- js/tests/integration/bundle.js | 2 +- js/tests/unit/collapse.spec.js | 2 +- js/tests/unit/dom/selector-engine.spec.js | 10 +++++----- site/content/docs/5.3/examples/checkout/checkout.js | 4 ++-- site/content/docs/5.3/examples/sidebars/sidebars.js | 2 +- site/static/docs/5.3/assets/js/validate-forms.js | 4 ++-- 13 files changed, 27 insertions(+), 24 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 2efe773b6f..bd6f452814 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -96,7 +96,6 @@ "unicorn/prefer-dom-node-dataset": "off", "unicorn/prefer-module": "off", "unicorn/prefer-query-selector": "off", - "unicorn/prefer-spread": "off", "unicorn/prefer-string-replace-all": "off", "unicorn/prevent-abbreviations": "off" }, diff --git a/js/src/dom/data.js b/js/src/dom/data.js index 407f67e392..45a3c6a7e3 100644 --- a/js/src/dom/data.js +++ b/js/src/dom/data.js @@ -23,7 +23,7 @@ export default { // can be removed later when multiple key/instances are fine to be used if (!instanceMap.has(key) && instanceMap.size !== 0) { // eslint-disable-next-line no-console - console.error(`Bootstrap doesn't allow more than one instance per element. Bound instance: ${Array.from(instanceMap.keys())[0]}.`) + console.error(`Bootstrap doesn't allow more than one instance per element. Bound instance: ${[...instanceMap.keys()][0]}.`) return } diff --git a/js/src/dom/selector-engine.js b/js/src/dom/selector-engine.js index a4d81f3b91..edfa8abde7 100644 --- a/js/src/dom/selector-engine.js +++ b/js/src/dom/selector-engine.js @@ -34,7 +34,7 @@ const getSelector = element => { const SelectorEngine = { find(selector, element = document.documentElement) { - return [].concat(...Element.prototype.querySelectorAll.call(element, selector)) + return [...Element.prototype.querySelectorAll.call(element, selector)] }, findOne(selector, element = document.documentElement) { @@ -42,7 +42,7 @@ const SelectorEngine = { }, children(element, selector) { - return [].concat(...element.children).filter(child => child.matches(selector)) + return [...element.children].filter(child => child.matches(selector)) }, parents(element, selector) { diff --git a/js/src/dropdown.js b/js/src/dropdown.js index 9190b3ed57..59265ea70f 100644 --- a/js/src/dropdown.js +++ b/js/src/dropdown.js @@ -143,7 +143,8 @@ class Dropdown extends BaseComponent { // only needed because of broken event delegation on iOS // https://www.quirksmode.org/blog/archives/2014/02/mouse_event_bub.html if ('ontouchstart' in document.documentElement && !this._parent.closest(SELECTOR_NAVBAR_NAV)) { - for (const element of [].concat(...document.body.children)) { + const children = [...document.body.children] + for (const element of children) { EventHandler.on(element, 'mouseover', noop) } } @@ -193,7 +194,8 @@ class Dropdown extends BaseComponent { // If this is a touch-enabled device we remove the extra // empty mouseover listeners we added for iOS support if ('ontouchstart' in document.documentElement) { - for (const element of [].concat(...document.body.children)) { + const children = [...document.body.children] + for (const element of children) { EventHandler.off(element, 'mouseover', noop) } } diff --git a/js/src/tooltip.js b/js/src/tooltip.js index 6a6cfeff21..4b3f3800a7 100644 --- a/js/src/tooltip.js +++ b/js/src/tooltip.js @@ -222,7 +222,8 @@ class Tooltip extends BaseComponent { // only needed because of broken event delegation on iOS // https://www.quirksmode.org/blog/archives/2014/02/mouse_event_bub.html if ('ontouchstart' in document.documentElement) { - for (const element of [].concat(...document.body.children)) { + const children = [...document.body.children] + for (const element of children) { EventHandler.on(element, 'mouseover', noop) } } @@ -256,7 +257,8 @@ class Tooltip extends BaseComponent { // If this is a touch-enabled device we remove the extra // empty mouseover listeners we added for iOS support if ('ontouchstart' in document.documentElement) { - for (const element of [].concat(...document.body.children)) { + const children = [...document.body.children] + for (const element of children) { EventHandler.off(element, 'mouseover', noop) } } diff --git a/js/src/util/sanitizer.js b/js/src/util/sanitizer.js index 3d2883aff3..0be81d6f27 100644 --- a/js/src/util/sanitizer.js +++ b/js/src/util/sanitizer.js @@ -93,7 +93,7 @@ export function sanitizeHtml(unsafeHtml, allowList, sanitizeFunction) { const domParser = new window.DOMParser() const createdDocument = domParser.parseFromString(unsafeHtml, 'text/html') - const elements = [].concat(...createdDocument.body.querySelectorAll('*')) + const elements = [...createdDocument.body.querySelectorAll('*')] for (const element of elements) { const elementName = element.nodeName.toLowerCase() @@ -103,8 +103,8 @@ export function sanitizeHtml(unsafeHtml, allowList, sanitizeFunction) { continue } - const attributeList = [].concat(...element.attributes) - const allowedAttributes = [].concat(allowList['*'] || [], allowList[elementName] || []) + const attributeList = [...element.attributes] + const allowedAttributes = [...allowList['*'] || [], ...allowList[elementName] || []] for (const attribute of attributeList) { if (!allowedAttribute(attribute, allowedAttributes)) { diff --git a/js/tests/integration/bundle-modularity.js b/js/tests/integration/bundle-modularity.js index 3c1eec9440..13498ba9f6 100644 --- a/js/tests/integration/bundle-modularity.js +++ b/js/tests/integration/bundle-modularity.js @@ -4,6 +4,6 @@ import Tooltip from '../../dist/tooltip' import '../../dist/carousel' window.addEventListener('load', () => { - [].concat(...document.querySelectorAll('[data-bs-toggle="tooltip"]')) + [...document.querySelectorAll('[data-bs-toggle="tooltip"]')] .map(tooltipNode => new Tooltip(tooltipNode)) }) diff --git a/js/tests/integration/bundle.js b/js/tests/integration/bundle.js index 452088a7d8..9887fc9f31 100644 --- a/js/tests/integration/bundle.js +++ b/js/tests/integration/bundle.js @@ -1,6 +1,6 @@ import { Tooltip } from '../../../dist/js/bootstrap.esm.js' window.addEventListener('load', () => { - [].concat(...document.querySelectorAll('[data-bs-toggle="tooltip"]')) + [...document.querySelectorAll('[data-bs-toggle="tooltip"]')] .map(tooltipNode => new Tooltip(tooltipNode)) }) diff --git a/js/tests/unit/collapse.spec.js b/js/tests/unit/collapse.spec.js index 58c5367526..9c920af5ff 100644 --- a/js/tests/unit/collapse.spec.js +++ b/js/tests/unit/collapse.spec.js @@ -153,7 +153,7 @@ describe('Collapse', () => { const collapseEl1 = fixtureEl.querySelector('#collapse1') const collapseEl2 = fixtureEl.querySelector('#collapse2') - const collapseList = [].concat(...fixtureEl.querySelectorAll('.collapse')) + const collapseList = [...fixtureEl.querySelectorAll('.collapse')] .map(el => new Collapse(el, { parent, toggle: false diff --git a/js/tests/unit/dom/selector-engine.spec.js b/js/tests/unit/dom/selector-engine.spec.js index 95d9bf8ec9..f0ec5faf9a 100644 --- a/js/tests/unit/dom/selector-engine.spec.js +++ b/js/tests/unit/dom/selector-engine.spec.js @@ -68,7 +68,7 @@ describe('SelectorEngine', () => { ].join('') const list = fixtureEl.querySelector('ul') - const liList = [].concat(...fixtureEl.querySelectorAll('li')) + const liList = [...fixtureEl.querySelectorAll('li')] const result = SelectorEngine.children(list, 'li') expect(result).toEqual(liList) @@ -356,7 +356,7 @@ describe('SelectorEngine', () => { const testEl = fixtureEl.querySelector('#test') - expect(SelectorEngine.getMultipleElementsFromSelector(testEl)).toEqual(Array.from(fixtureEl.querySelectorAll('.target'))) + expect(SelectorEngine.getMultipleElementsFromSelector(testEl)).toEqual([...fixtureEl.querySelectorAll('.target')]) }) it('should get elements if several ids are given', () => { @@ -368,7 +368,7 @@ describe('SelectorEngine', () => { const testEl = fixtureEl.querySelector('#test') - expect(SelectorEngine.getMultipleElementsFromSelector(testEl)).toEqual(Array.from(fixtureEl.querySelectorAll('.target'))) + expect(SelectorEngine.getMultipleElementsFromSelector(testEl)).toEqual([...fixtureEl.querySelectorAll('.target')]) }) it('should get elements if several ids with special chars are given', () => { @@ -380,7 +380,7 @@ describe('SelectorEngine', () => { const testEl = fixtureEl.querySelector('#test') - expect(SelectorEngine.getMultipleElementsFromSelector(testEl)).toEqual(Array.from(fixtureEl.querySelectorAll('.target'))) + expect(SelectorEngine.getMultipleElementsFromSelector(testEl)).toEqual([...fixtureEl.querySelectorAll('.target')]) }) it('should get elements in array, from href if no data-bs-target set', () => { @@ -392,7 +392,7 @@ describe('SelectorEngine', () => { const testEl = fixtureEl.querySelector('#test') - expect(SelectorEngine.getMultipleElementsFromSelector(testEl)).toEqual(Array.from(fixtureEl.querySelectorAll('.target'))) + expect(SelectorEngine.getMultipleElementsFromSelector(testEl)).toEqual([...fixtureEl.querySelectorAll('.target')]) }) it('should return empty array if elements not found', () => { diff --git a/site/content/docs/5.3/examples/checkout/checkout.js b/site/content/docs/5.3/examples/checkout/checkout.js index 30ea0aa6b1..d6ab60d474 100644 --- a/site/content/docs/5.3/examples/checkout/checkout.js +++ b/site/content/docs/5.3/examples/checkout/checkout.js @@ -3,10 +3,10 @@ 'use strict' // Fetch all the forms we want to apply custom Bootstrap validation styles to - const forms = document.querySelectorAll('.needs-validation') + const forms = document.querySelectorAll('.needs-validation'); // Loop over them and prevent submission - Array.from(forms).forEach(form => { + [...forms].forEach(form => { form.addEventListener('submit', event => { if (!form.checkValidity()) { event.preventDefault() diff --git a/site/content/docs/5.3/examples/sidebars/sidebars.js b/site/content/docs/5.3/examples/sidebars/sidebars.js index 4075f1f155..616ebef1d9 100644 --- a/site/content/docs/5.3/examples/sidebars/sidebars.js +++ b/site/content/docs/5.3/examples/sidebars/sidebars.js @@ -1,7 +1,7 @@ /* global bootstrap: false */ (() => { 'use strict' - const tooltipTriggerList = Array.from(document.querySelectorAll('[data-bs-toggle="tooltip"]')) + const tooltipTriggerList = [...document.querySelectorAll('[data-bs-toggle="tooltip"]')] tooltipTriggerList.forEach(tooltipTriggerEl => { new bootstrap.Tooltip(tooltipTriggerEl) }) diff --git a/site/static/docs/5.3/assets/js/validate-forms.js b/site/static/docs/5.3/assets/js/validate-forms.js index 30ea0aa6b1..d6ab60d474 100644 --- a/site/static/docs/5.3/assets/js/validate-forms.js +++ b/site/static/docs/5.3/assets/js/validate-forms.js @@ -3,10 +3,10 @@ 'use strict' // Fetch all the forms we want to apply custom Bootstrap validation styles to - const forms = document.querySelectorAll('.needs-validation') + const forms = document.querySelectorAll('.needs-validation'); // Loop over them and prevent submission - Array.from(forms).forEach(form => { + [...forms].forEach(form => { form.addEventListener('submit', event => { if (!form.checkValidity()) { event.preventDefault()