Skip to content

Commit 45a7ae8

Browse files
committed
Refactor code-style
1 parent 7d8d2b0 commit 45a7ae8

File tree

13 files changed

+237
-224
lines changed

13 files changed

+237
-224
lines changed

lib/any.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {nest} from './nest.js'
1515
import {pseudo} from './pseudo.js'
1616
import {test} from './test.js'
1717

18-
var type = zwitch('type', {
18+
const type = zwitch('type', {
1919
unknown: unknownType,
2020
invalid: invalidType,
2121
handlers: {selectors, ruleSet, rule}
@@ -39,8 +39,8 @@ export function any(query, node, state) {
3939
* @returns {Array.<Element>}
4040
*/
4141
function selectors(query, node, state) {
42-
var collector = new Collector(state.one)
43-
var index = -1
42+
const collector = new Collector(state.one)
43+
let index = -1
4444

4545
while (++index < query.selectors.length) {
4646
collector.collectAll(ruleSet(query.selectors[index], node, state))
@@ -66,7 +66,7 @@ function ruleSet(query, node, state) {
6666
* @returns {Array.<Element>}
6767
*/
6868
function rule(query, tree, state) {
69-
var collector = new Collector(state.one)
69+
const collector = new Collector(state.one)
7070

7171
if (state.shallow && query.rule) {
7272
throw new Error('Expected selector without nesting')
@@ -94,7 +94,7 @@ function rule(query, tree, state) {
9494

9595
/** @type {SelectIterator} */
9696
function iterator(query, node, index, parent, state) {
97-
var exit = enterState(state, node)
97+
const exit = enterState(state, node)
9898

9999
if (test(query, node, index, parent, state)) {
100100
if (query.rule) {
@@ -116,8 +116,8 @@ function rule(query, tree, state) {
116116
* @returns {S}
117117
*/
118118
function configure(query, state) {
119-
var pseudos = query.pseudos || []
120-
var index = -1
119+
const pseudos = query.pseudos || []
120+
let index = -1
121121

122122
while (++index < pseudos.length) {
123123
if (pseudo.needsIndex.includes(pseudos[index].name)) {
@@ -164,7 +164,7 @@ class Collector {
164164
* @param {Array.<Element>} elements
165165
*/
166166
collectAll(elements) {
167-
var index = -1
167+
let index = -1
168168

169169
while (++index < elements.length) {
170170
this.collect(elements[index])

lib/attribute.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {find} from 'property-information'
1313
import {stringify as spaces} from 'space-separated-tokens'
1414
import {zwitch} from 'zwitch'
1515

16-
var handle = zwitch('operator', {
16+
const handle = zwitch('operator', {
1717
unknown: unknownOperator,
1818
invalid: exists,
1919
handlers: {
@@ -33,8 +33,8 @@ var handle = zwitch('operator', {
3333
* @returns {boolean}
3434
*/
3535
export function attribute(query, element, schema) {
36-
var attrs = query.attrs
37-
var index = -1
36+
const attrs = query.attrs
37+
let index = -1
3838

3939
while (++index < attrs.length) {
4040
if (!handle(attrs[index], element, find(schema, attrs[index].name))) return
@@ -79,7 +79,7 @@ function exact(query, element, info) {
7979
* @returns {boolean}
8080
*/
8181
function spaceSeparatedList(query, element, info) {
82-
var value = element.properties[info.property]
82+
const value = element.properties[info.property]
8383

8484
return (
8585
// If this is a comma-separated list, and the query is contained in it, return
@@ -104,7 +104,7 @@ function spaceSeparatedList(query, element, info) {
104104
* @returns {boolean}
105105
*/
106106
function exactOrPrefix(query, element, info) {
107-
var value = normalizeValue(element.properties[info.property], info)
107+
const value = normalizeValue(element.properties[info.property], info)
108108

109109
return (
110110
hasProperty(element, info.property) &&

lib/class-name.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
export function className(query, element) {
1212
/** @type {Array.<string>} */
1313
// @ts-ignore Assume array.
14-
var value = element.properties.className || []
15-
var index = -1
14+
const value = element.properties.className || []
15+
let index = -1
1616

1717
while (++index < query.classNames.length) {
1818
if (!value.includes(query.classNames[index])) return

lib/enter-state.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,24 @@ import {element} from './util.js'
2020
*/
2121
// eslint-disable-next-line complexity
2222
export function enterState(state, node) {
23-
var schema = state.schema
24-
var language = state.language
25-
var currentDirection = state.direction
26-
var editableOrEditingHost = state.editableOrEditingHost
23+
const schema = state.schema
24+
const language = state.language
25+
const currentDirection = state.direction
26+
const editableOrEditingHost = state.editableOrEditingHost
2727
/** @type {Direction|null} */
28-
var dirInferred
29-
/** @type {string} */
30-
var type
28+
let dirInferred
3129
/** @type {boolean} */
32-
var found
33-
/** @type {string} */
34-
var lang
35-
/** @type {Direction|null} */
36-
var dir
30+
let found
3731

3832
if (element(node)) {
3933
// @ts-ignore Assume string.
40-
lang = node.properties.xmlLang || node.properties.lang
34+
const lang = node.properties.xmlLang || node.properties.lang
4135
// @ts-ignore Assume string.
42-
type = node.properties.type || 'text'
43-
dir = dirProperty(node)
36+
const type = node.properties.type || 'text'
37+
const dir = dirProperty(node)
4438

4539
if (lang !== undefined && lang !== null) {
46-
state.language = lang
40+
state.language = String(lang)
4741
found = true
4842
}
4943

@@ -138,7 +132,7 @@ export function enterState(state, node) {
138132
* @returns {Direction}
139133
*/
140134
function dirBidi(value) {
141-
var result = direction(value)
135+
const result = direction(value)
142136
return result === 'neutral' ? null : result
143137
}
144138

@@ -147,7 +141,7 @@ function dirBidi(value) {
147141
* @returns {Direction}
148142
*/
149143
function dirProperty(node) {
150-
var value =
144+
const value =
151145
element(node) && typeof node.properties.dir === 'string'
152146
? node.properties.dir.toLowerCase()
153147
: null

lib/nest.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import {zwitch} from 'zwitch'
1212
import {enterState} from './enter-state.js'
1313
import {parent, element} from './util.js'
1414

15-
var own = {}.hasOwnProperty
15+
const own = {}.hasOwnProperty
1616

17-
var handle = zwitch('nestingOperator', {
17+
const handle = zwitch('nestingOperator', {
1818
unknown: unknownNesting,
1919
invalid: topScan, // `undefined` is the top query selector.
2020
handlers: {
@@ -53,7 +53,7 @@ function topScan(query, node, index, parent, state) {
5353

5454
/** @type {Handler} */
5555
function descendant(query, node, index, parent, state) {
56-
var previous = state.iterator
56+
const previous = state.iterator
5757

5858
state.iterator = iterator
5959
child(query, node, index, parent, state)
@@ -103,16 +103,14 @@ function generalSibling(query, _, index, parent, state) {
103103
* @param {boolean} [firstElementOnly=false]
104104
*/
105105
function indexedSearch(query, parent, state, from, firstElementOnly) {
106-
var handle = state.index ? delay : add
107-
var children = parent.children
108-
var elements = 0
109-
var index = -1
106+
const handle = state.index ? delay : add
107+
const children = parent.children
108+
let elements = 0
109+
let index = -1
110110
/** @type {Object.<string, number>} */
111-
var types = {}
111+
const types = {}
112112
/** @type {Array.<Function>} */
113-
var delayed = []
114-
/** @type {Node} */
115-
var child
113+
const delayed = []
116114

117115
// Start looking at `from`
118116
if (from === undefined || from === null) from = 0
@@ -123,15 +121,15 @@ function indexedSearch(query, parent, state, from, firstElementOnly) {
123121
// If we need to index for types, do so for all elements before `from`.
124122
if (state.index) {
125123
while (++index < from) {
126-
child = children[index]
124+
const child = children[index]
127125
if (element(child)) count(child.tagName)
128126
}
129127
}
130128

131129
index = from - 1
132130

133131
while (++index < children.length) {
134-
child = children[index]
132+
const child = children[index]
135133
// Only check elements.
136134
// Check either all elements, or only check the first sibling
137135
if (element(child)) {
@@ -157,8 +155,8 @@ function indexedSearch(query, parent, state, from, firstElementOnly) {
157155
* @param {number} childIndex
158156
*/
159157
function delay(node, childIndex) {
160-
var elementsBefore = elements
161-
var elementsByTypeBefore = own.call(types, node.tagName)
158+
const elementsBefore = elements
159+
const elementsByTypeBefore = own.call(types, node.tagName)
162160
? types[node.tagName]
163161
: 0
164162

@@ -184,7 +182,7 @@ function indexedSearch(query, parent, state, from, firstElementOnly) {
184182
* @param {number} childIndex
185183
*/
186184
function add(node, childIndex) {
187-
var exit = enterState(state, node)
185+
const exit = enterState(state, node)
188186
state.iterator(query, node, childIndex, parent, state)
189187
exit()
190188
}

lib/parse.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ import {zwitch} from 'zwitch'
1313

1414
/** @type {import('nth-check').default} */
1515
// @ts-ignore
16-
var nthCheck = fauxEsmNthCheck.default
16+
const nthCheck = fauxEsmNthCheck.default
1717

18-
var nth = new Set([
18+
const nth = new Set([
1919
'nth-child',
2020
'nth-last-child',
2121
'nth-of-type',
2222
'nth-last-of-type'
2323
])
2424

25-
var parser = new CssSelectorParser()
25+
const parser = new CssSelectorParser()
2626

27-
var compile = zwitch('type', {handlers: {selectors, ruleSet, rule}})
27+
const compile = zwitch('type', {handlers: {selectors, ruleSet, rule}})
2828

2929
parser.registerAttrEqualityMods('~', '|', '^', '$', '*')
3030
parser.registerSelectorPseudos('any', 'matches', 'not', 'has')
@@ -48,7 +48,7 @@ export function parse(selector) {
4848
* @returns {Selectors}
4949
*/
5050
function selectors(query) {
51-
var index = -1
51+
let index = -1
5252

5353
while (++index < query.selectors.length) {
5454
compile(query.selectors[index])
@@ -70,13 +70,11 @@ function ruleSet(query) {
7070
* @returns {Rule}
7171
*/
7272
function rule(query) {
73-
var pseudos = query.pseudos || []
74-
var index = -1
75-
/** @type {RulePseudo|RulePseudoNth} */
76-
var pseudo
73+
const pseudos = query.pseudos || []
74+
let index = -1
7775

7876
while (++index < pseudos.length) {
79-
pseudo = pseudos[index]
77+
const pseudo = pseudos[index]
8078

8179
if (nth.has(pseudo.name)) {
8280
// @ts-ignore Patch a non-primitive type.

lib/pseudo.js

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {whitespace} from 'hast-util-whitespace'
1919
import {zwitch} from 'zwitch'
2020
import {any} from './any.js'
2121

22-
var handle = zwitch('name', {
22+
const handle = zwitch('name', {
2323
unknown: unknownPseudo,
2424
invalid: invalidPseudo,
2525
handlers: {
@@ -76,8 +76,8 @@ pseudo.needsIndex = [
7676
* @returns {boolean}
7777
*/
7878
export function pseudo(query, element, index, parent, state) {
79-
var pseudos = query.pseudos
80-
var offset = -1
79+
const pseudos = query.pseudos
80+
let offset = -1
8181

8282
while (++offset < pseudos.length) {
8383
if (!handle(pseudos[offset], element, index, parent, state)) return
@@ -95,15 +95,13 @@ export function pseudo(query, element, index, parent, state) {
9595
* @returns {boolean}
9696
*/
9797
function matches(query, element, _1, _2, state) {
98-
var shallow = state.shallow
99-
var one = state.one
100-
/** @type {boolean} */
101-
var result
98+
const shallow = state.shallow
99+
const one = state.one
102100

103101
state.shallow = true
104102
state.one = true
105103

106-
result = any(query.value, element, state)[0] === element
104+
const result = any(query.value, element, state)[0] === element
107105

108106
state.shallow = shallow
109107
state.one = one
@@ -457,8 +455,8 @@ function onlyOfType(query, _1, _2, _3, state) {
457455
* @returns {boolean}
458456
*/
459457
function someChildren(element, check) {
460-
var children = element.children
461-
var index = -1
458+
const children = element.children
459+
let index = -1
462460

463461
while (++index < children.length) {
464462
if (check(children[index])) return true
@@ -501,18 +499,16 @@ function assertDeep(state, query) {
501499
* @returns {boolean}
502500
*/
503501
function has(query, element, _2, _3, state) {
504-
var shallow = state.shallow
505-
var one = state.one
506-
var scopeElements = state.scopeElements
507-
var value = appendScope(query.value)
508-
/** @type {boolean} */
509-
var result
502+
const shallow = state.shallow
503+
const one = state.one
504+
const scopeElements = state.scopeElements
505+
const value = appendScope(query.value)
510506

511507
state.shallow = false
512508
state.one = true
513509
state.scopeElements = [element]
514510

515-
result = any(value, element, state).length > 0
511+
const result = any(value, element, state).length > 0
516512

517513
state.shallow = shallow
518514
state.one = one
@@ -527,14 +523,12 @@ function has(query, element, _2, _3, state) {
527523
*/
528524
function appendScope(value) {
529525
/** @type {Selectors} */
530-
var selector =
526+
const selector =
531527
value.type === 'ruleSet' ? {type: 'selectors', selectors: [value]} : value
532-
var index = -1
533-
/** @type {Rule} */
534-
var rule
528+
let index = -1
535529

536530
while (++index < selector.selectors.length) {
537-
rule = selector.selectors[index].rule
531+
const rule = selector.selectors[index].rule
538532
rule.nestingOperator = null
539533

540534
if (

0 commit comments

Comments
 (0)