Skip to content

Commit 9e766b1

Browse files
authored
refactor: enable more ESLint rules, fix related errors (#5442)
1 parent 831c852 commit 9e766b1

30 files changed

Lines changed: 452 additions & 463 deletions

‎.eslintrc.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"@typescript-eslint/no-invalid-void-type": "off",
2727
"@typescript-eslint/no-shadow": "off",
2828
"@typescript-eslint/no-unused-expressions": "off",
29-
"@typescript-eslint/no-use-before-define": "off",
3029
"@typescript-eslint/prefer-optional-chain": "off",
3130
"no-only-tests/no-only-tests": "error",
3231
"simple-import-sort/imports": [
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
declare module '@vaadin/vaadin-usage-statistics/vaadin-usage-statistics.js' {
2-
export { usageStatistics };
3-
42
function usageStatistics(): void;
3+
4+
export { usageStatistics };
55
}

‎packages/component-base/src/async.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ const microtaskCallbacks = [];
2727
let microtaskNodeContent = 0;
2828
let microtaskScheduled = false;
2929
const microtaskNode = document.createTextNode('');
30-
new window.MutationObserver(microtaskFlush).observe(microtaskNode, { characterData: true });
3130

3231
function microtaskFlush() {
3332
microtaskScheduled = false;
@@ -48,6 +47,8 @@ function microtaskFlush() {
4847
microtaskLastHandle += len;
4948
}
5049

50+
new window.MutationObserver(microtaskFlush).observe(microtaskNode, { characterData: true });
51+
5152
/**
5253
* Async interface wrapper around `setTimeout`.
5354
*

‎packages/component-base/src/debounce.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Code distributed by Google as part of the polymer project is also
88
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
99
*/
1010

11+
const debouncerQueue = new Set();
12+
1113
/**
1214
* @summary Collapse multiple callbacks into one invocation after a timer.
1315
*/
@@ -133,8 +135,6 @@ export class Debouncer {
133135
}
134136
}
135137

136-
let debouncerQueue = new Set();
137-
138138
/**
139139
* Adds a `Debouncer` to a list of globally flushable tasks.
140140
*

‎packages/component-base/src/dir-mixin.js‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,6 @@
99
*/
1010
const directionSubscribers = [];
1111

12-
function directionUpdater() {
13-
const documentDir = getDocumentDir();
14-
directionSubscribers.forEach((element) => {
15-
alignDirs(element, documentDir);
16-
});
17-
}
18-
19-
const directionObserver = new MutationObserver(directionUpdater);
20-
directionObserver.observe(document.documentElement, { attributes: true, attributeFilter: ['dir'] });
21-
2212
function alignDirs(element, documentDir, elementDir = element.getAttribute('dir')) {
2313
if (documentDir) {
2414
element.setAttribute('dir', documentDir);
@@ -31,6 +21,16 @@ function getDocumentDir() {
3121
return document.documentElement.getAttribute('dir');
3222
}
3323

24+
function directionUpdater() {
25+
const documentDir = getDocumentDir();
26+
directionSubscribers.forEach((element) => {
27+
alignDirs(element, documentDir);
28+
});
29+
}
30+
31+
const directionObserver = new MutationObserver(directionUpdater);
32+
directionObserver.observe(document.documentElement, { attributes: true, attributeFilter: ['dir'] });
33+
3434
/**
3535
* A mixin to handle `dir` attribute based on the one set on the `<html>` element.
3636
*

‎packages/component-base/src/focus-utils.js‎

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,6 @@ function isElementHiddenDirectly(element) {
6161
return false;
6262
}
6363

64-
/**
65-
* Returns the normalized element tabindex. If not focusable, returns -1.
66-
* It checks for the attribute "tabindex" instead of the element property
67-
* `tabIndex` since browsers assign different values to it.
68-
* e.g. in Firefox `<div contenteditable>` has `tabIndex = -1`
69-
*
70-
* @param {HTMLElement} element
71-
* @return {number}
72-
*/
73-
function normalizeTabIndex(element) {
74-
if (!isElementFocusable(element)) {
75-
return -1;
76-
}
77-
78-
const tabIndex = element.getAttribute('tabindex') || 0;
79-
return Number(tabIndex);
80-
}
81-
8264
/**
8365
* Returns if element `a` has lower tab order compared to element `b`
8466
* (both elements are assumed to be focusable and tabbable).
@@ -138,42 +120,6 @@ function sortElementsByTabIndex(elements) {
138120
return mergeSortByTabIndex(left, right);
139121
}
140122

141-
/**
142-
* Searches for nodes that are tabbable and adds them to the `result` array.
143-
* Returns if the `result` array needs to be sorted by tabindex.
144-
*
145-
* @param {Node} node The starting point for the search; added to `result` if tabbable.
146-
* @param {HTMLElement[]} result
147-
* @return {boolean}
148-
* @private
149-
*/
150-
function collectFocusableNodes(node, result) {
151-
if (node.nodeType !== Node.ELEMENT_NODE || isElementHiddenDirectly(node)) {
152-
// Don't traverse children if the node is not an HTML element or not visible.
153-
return false;
154-
}
155-
156-
const element = /** @type {HTMLElement} */ (node);
157-
const tabIndex = normalizeTabIndex(element);
158-
let needsSort = tabIndex > 0;
159-
if (tabIndex >= 0) {
160-
result.push(element);
161-
}
162-
163-
let children = [];
164-
if (element.localName === 'slot') {
165-
children = element.assignedNodes({ flatten: true });
166-
} else {
167-
// Use shadow root if possible, will check for distributed nodes.
168-
children = (element.shadowRoot || element).children;
169-
}
170-
[...children].forEach((child) => {
171-
// Ensure method is always invoked to collect focusable children.
172-
needsSort = collectFocusableNodes(child, result) || needsSort;
173-
});
174-
return needsSort;
175-
}
176-
177123
/**
178124
* Returns true if the element is hidden, false otherwise.
179125
*
@@ -239,6 +185,60 @@ export function isElementFocused(element) {
239185
return element.getRootNode().activeElement === element;
240186
}
241187

188+
/**
189+
* Returns the normalized element tabindex. If not focusable, returns -1.
190+
* It checks for the attribute "tabindex" instead of the element property
191+
* `tabIndex` since browsers assign different values to it.
192+
* e.g. in Firefox `<div contenteditable>` has `tabIndex = -1`
193+
*
194+
* @param {HTMLElement} element
195+
* @return {number}
196+
*/
197+
function normalizeTabIndex(element) {
198+
if (!isElementFocusable(element)) {
199+
return -1;
200+
}
201+
202+
const tabIndex = element.getAttribute('tabindex') || 0;
203+
return Number(tabIndex);
204+
}
205+
206+
/**
207+
* Searches for nodes that are tabbable and adds them to the `result` array.
208+
* Returns if the `result` array needs to be sorted by tabindex.
209+
*
210+
* @param {Node} node The starting point for the search; added to `result` if tabbable.
211+
* @param {HTMLElement[]} result
212+
* @return {boolean}
213+
* @private
214+
*/
215+
function collectFocusableNodes(node, result) {
216+
if (node.nodeType !== Node.ELEMENT_NODE || isElementHiddenDirectly(node)) {
217+
// Don't traverse children if the node is not an HTML element or not visible.
218+
return false;
219+
}
220+
221+
const element = /** @type {HTMLElement} */ (node);
222+
const tabIndex = normalizeTabIndex(element);
223+
let needsSort = tabIndex > 0;
224+
if (tabIndex >= 0) {
225+
result.push(element);
226+
}
227+
228+
let children = [];
229+
if (element.localName === 'slot') {
230+
children = element.assignedNodes({ flatten: true });
231+
} else {
232+
// Use shadow root if possible, will check for distributed nodes.
233+
children = (element.shadowRoot || element).children;
234+
}
235+
[...children].forEach((child) => {
236+
// Ensure method is always invoked to collect focusable children.
237+
needsSort = collectFocusableNodes(child, result) || needsSort;
238+
});
239+
return needsSort;
240+
}
241+
242242
/**
243243
* Returns a tab-ordered array of focusable elements for a root element.
244244
* The resulting array will include the root element if it is focusable.

‎packages/component-base/src/gestures.d.ts‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ Code distributed by Google as part of the polymer project is also
88
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
99
*/
1010

11-
export { deepTargetFind };
12-
1311
/**
1412
* Finds the element rendered on the screen at the provided coordinates.
1513
*
@@ -20,34 +18,30 @@ export { deepTargetFind };
2018
* found at the screen position given.
2119
*/
2220
declare function deepTargetFind(x: number, y: number): Element | null;
23-
24-
export { addListener };
21+
export { deepTargetFind };
2522

2623
/**
2724
* Adds an event listener to a node for the given gesture type.
2825
*
2926
* @returns Returns true if a gesture event listener was added.
3027
*/
3128
declare function addListener(node: EventTarget, evType: string, handler: (p0: Event) => void): boolean;
32-
33-
export { removeListener };
29+
export { addListener };
3430

3531
/**
3632
* Removes an event listener from a node for the given gesture type.
3733
*
3834
* @returns Returns true if a gesture event listener was removed.
3935
*/
4036
declare function removeListener(node: EventTarget, evType: string, handler: (p0: Event) => void): boolean;
41-
42-
export { register };
37+
export { removeListener };
4338

4439
/**
4540
* Registers a new gesture event recognizer for adding new custom
4641
* gesture event types.
4742
*/
4843
declare function register(recog: GestureRecognizer): void;
49-
50-
export { setTouchAction };
44+
export { register };
5145

5246
/**
5347
* Sets scrolling direction on node.
@@ -56,13 +50,13 @@ export { setTouchAction };
5650
* adding event listeners.
5751
*/
5852
declare function setTouchAction(node: EventTarget, value: string): void;
59-
60-
export { prevent };
53+
export { setTouchAction };
6154

6255
/**
6356
* Prevents the dispatch and default action of the given event name.
6457
*/
6558
declare function prevent(evName: string): void;
59+
export { prevent };
6660

6761
export interface GestureRecognizer {
6862
reset(): void;

‎packages/component-base/src/gestures.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Code distributed by Google as part of the polymer project is also
88
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
99
*/
1010

11+
/* eslint-disable @typescript-eslint/no-use-before-define */
12+
1113
/**
1214
* @fileoverview
1315
*

‎packages/component-base/test/dir-mixin.test.js‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ const runTests = (baseClass) => {
108108
await nextFrame();
109109
});
110110

111+
// Toggle document dir attribute value
112+
function setDir(direction) {
113+
if (direction) {
114+
document.documentElement.setAttribute('dir', direction);
115+
} else {
116+
document.documentElement.removeAttribute('dir');
117+
}
118+
}
119+
111120
// Check that for each `documentDirections` value set, `element` dir
112121
// attribute value equals to the corresponding `elementDirections` value.
113122
async function expectDirections(documentDirections, elementDirections) {
@@ -118,15 +127,6 @@ const runTests = (baseClass) => {
118127
}
119128
}
120129

121-
// Toggle document dir attribute value
122-
function setDir(direction) {
123-
if (direction) {
124-
document.documentElement.setAttribute('dir', direction);
125-
} else {
126-
document.documentElement.removeAttribute('dir');
127-
}
128-
}
129-
130130
it('should propagate direction as dir attribute to the element from the documentElement', async () => {
131131
await expectDirections(['rtl', 'ltr', '', 'rtl', null], ['rtl', 'ltr', null, 'rtl', null]);
132132
});
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
import { fire, nextFrame, oneEvent } from '@vaadin/testing-helpers';
22
import { isTouch } from '@vaadin/component-base/src/browser-utils.js';
33

4-
export async function openSubMenus(menu) {
5-
await oneEvent(menu.$.overlay, 'vaadin-overlay-open');
6-
const itemElement = menu.$.overlay.querySelector('[aria-haspopup="true"]');
7-
if (itemElement) {
8-
itemElement.dispatchEvent(new CustomEvent('mouseover', { bubbles: true, composed: true }));
9-
const subMenu = getSubMenu(menu);
10-
await openSubMenus(subMenu);
11-
}
12-
}
13-
144
export async function openMenu(target, event = isTouch ? 'click' : 'mouseover') {
155
const menu = target.closest('vaadin-context-menu');
166
if (menu) {
@@ -28,3 +18,13 @@ export function getMenuItems(menu) {
2818
export function getSubMenu(menu) {
2919
return menu.$.overlay.querySelector('vaadin-context-menu');
3020
}
21+
22+
export async function openSubMenus(menu) {
23+
await oneEvent(menu.$.overlay, 'vaadin-overlay-open');
24+
const itemElement = menu.$.overlay.querySelector('[aria-haspopup="true"]');
25+
if (itemElement) {
26+
itemElement.dispatchEvent(new CustomEvent('mouseover', { bubbles: true, composed: true }));
27+
const subMenu = getSubMenu(menu);
28+
await openSubMenus(subMenu);
29+
}
30+
}

0 commit comments

Comments
 (0)