Skip to content

Commit

Permalink
Add strict to tsconfig.json
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 22, 2021
1 parent 658764a commit 6f7b407
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 31 deletions.
11 changes: 7 additions & 4 deletions lib/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@ import {test} from './test.js'
import {root} from './util.js'

var type = zwitch('type', {
// @ts-expect-error: hush.
unknown: unknownType,
invalid: invalidType,
// @ts-expect-error: hush.
handlers: {selectors, ruleSet, rule}
})

/**
* @param {Selectors|RuleSet|Rule} query
* @param {Node} node
* @param {Node|undefined} node
* @param {SelectState} state
* @returns {Array.<Node>}
*/
export function any(query, node, state) {
// @ts-ignore zwitch types are off.
// @ts-expect-error: fine.
return query && node ? type(query, node, state) : []
}

Expand Down Expand Up @@ -90,7 +93,7 @@ function rule(query, tree, state) {
/** @type {SelectIterator} */
function iterator(query, node, index, parent, state) {
if (test(query, node, index, parent, state)) {
if ('rule' in query) {
if (query.rule) {
nest(query.rule, node, index, parent, configure(query.rule, state))
} else {
collect(node)
Expand Down Expand Up @@ -136,7 +139,7 @@ function invalidType() {
}

/**
* @param {boolean} one
* @param {boolean|undefined} one
*/
function collector(one) {
/** @type {Array.<Node>} */
Expand Down
21 changes: 20 additions & 1 deletion lib/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
import {zwitch} from 'zwitch'

var handle = zwitch('operator', {
// @ts-expect-error: hush.
unknown: unknownOperator,
// @ts-expect-error: hush.
invalid: exists,
handlers: {
// @ts-expect-error: hush.
'=': exact,
// @ts-expect-error: hush.
'^=': begins,
// @ts-expect-error: hush.
'$=': ends,
// @ts-expect-error: hush.
'*=': containsString,
// @ts-expect-error: hush.
'~=': containsArray
}
})
Expand All @@ -39,6 +46,7 @@ export function attribute(query, node) {
* @param {Node} node
*/
function exists(query, node) {
// @ts-expect-error: Looks like a record.
return node[query.name] !== null && node[query.name] !== undefined
}

Expand All @@ -49,6 +57,7 @@ function exists(query, node) {
* @param {Node} node
*/
function exact(query, node) {
// @ts-expect-error: Looks like a record.
return exists(query, node) && String(node[query.name]) === query.value
}

Expand All @@ -59,6 +68,8 @@ function exact(query, node) {
* @param {Node} node
*/
function containsArray(query, node) {
/** @type {unknown} */
// @ts-expect-error: Looks like a record.
var value = node[query.name]

if (value === null || value === undefined) return false
Expand All @@ -82,9 +93,12 @@ function containsArray(query, node) {
* @param {Node} node
*/
function begins(query, node) {
/** @type {unknown} */
// @ts-expect-error: Looks like a record.
var value = node[query.name]

return (
query.value &&
typeof value === 'string' &&
value.slice(0, query.value.length) === query.value
)
Expand All @@ -97,9 +111,12 @@ function begins(query, node) {
* @param {Node} node
*/
function ends(query, node) {
/** @type {unknown} */
// @ts-expect-error: Looks like a record.
var value = node[query.name]

return (
query.value &&
typeof value === 'string' &&
value.slice(-query.value.length) === query.value
)
Expand All @@ -112,8 +129,10 @@ function ends(query, node) {
* @param {Node} node
*/
function containsString(query, node) {
/** @type {unknown} */
// @ts-expect-error: Looks like a record.
var value = node[query.name]
return typeof value === 'string' && value.includes(query.value)
return query.value && typeof value === 'string' && value.includes(query.value)
}

// Shouldn’t be invoked, Parser throws an error instead.
Expand Down
58 changes: 54 additions & 4 deletions lib/nest.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@ import {parent} from './util.js'
var own = {}.hasOwnProperty

var handle = zwitch('nestingOperator', {
// @ts-expect-error: hush.
unknown: unknownNesting,
// @ts-expect-error: hush.
invalid: topScan, // `undefined` is the top query selector.
handlers: {
// @ts-expect-error: hush.
null: descendant, // `null` is the descendant combinator.
// @ts-expect-error: hush.
'>': child,
// @ts-expect-error: hush.
'+': adjacentSibling,
// @ts-expect-error: hush.
'~': generalSibling
}
})

/** @type {Handler} */
export function nest(query, node, index, parent, state) {
return handle(query, node, index, parent, state)
}
export const nest = handle

// Shouldn’t be invoked, parser gives correct data.
/* c8 ignore next 6 */
Expand All @@ -41,17 +45,33 @@ function unknownNesting(query) {
/** @type {Handler} */
function topScan(query, node, index, parent, state) {
// Shouldn’t happen.
/* c8 ignore next 3 */
/* c8 ignore next 7 */
if (parent) {
throw new Error('topScan is supposed to be called from the root node')
}

if (!state.iterator) {
throw new Error('Expected `iterator` to be defined')
}

// Shouldn’t happen.
/* c8 ignore next 3 */
if (typeof index !== 'number') {
throw new TypeError('Expected `index` to be defined')
}

state.iterator(query, node, index, parent, state)
if (!state.shallow) descendant(query, node, index, parent, state)
}

/** @type {Handler} */
function descendant(query, node, index, parent, state) {
// Shouldn’t happen.
/* c8 ignore next 3 */
if (!state.iterator) {
throw new Error('Expected `iterator` to be defined')
}

var previous = state.iterator

state.iterator = iterator
Expand Down Expand Up @@ -79,6 +99,12 @@ function child(query, node, _1, _2, state) {

/** @type {Handler} */
function adjacentSibling(query, _, index, parent, state) {
// Shouldn’t happen.
/* c8 ignore next 3 */
if (typeof index !== 'number') {
throw new TypeError('Expected `index` to be defined')
}

// Shouldn’t happen.
/* c8 ignore next */
if (!parent) return
Expand All @@ -92,6 +118,12 @@ function adjacentSibling(query, _, index, parent, state) {

/** @type {Handler} */
function generalSibling(query, _, index, parent, state) {
// Shouldn’t happen.
/* c8 ignore next 3 */
if (typeof index !== 'number') {
throw new TypeError('Expected `index` to be defined')
}

// Shouldn’t happen.
/* c8 ignore next */
if (!parent) return
Expand Down Expand Up @@ -161,6 +193,12 @@ class WalkIterator {
index = this.typeIndex.index(child)
this.delayed.push(delay)
} else {
// Shouldn’t happen.
/* c8 ignore next 3 */
if (!this.state.iterator) {
throw new Error('Expected `iterator` to be defined')
}

this.state.iterator(this.query, child, start, this.parent, this.state)
}

Expand All @@ -173,6 +211,18 @@ class WalkIterator {
* @this {WalkIterator}
*/
function delay() {
// Shouldn’t happen.
/* c8 ignore next 3 */
if (!this.typeIndex) {
throw new TypeError('Expected `typeIndex` to be defined')
}

// Shouldn’t happen.
/* c8 ignore next 3 */
if (!this.state.iterator) {
throw new Error('Expected `iterator` to be defined')
}

this.state.typeIndex = index
this.state.nodeIndex = nodeIndex
this.state.typeCount = this.typeIndex.count(child)
Expand Down
9 changes: 5 additions & 4 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import fauxEsmNthCheck from 'nth-check'
import {zwitch} from 'zwitch'

/** @type {import('nth-check').default} */
// @ts-ignore
// @ts-expect-error
var nthCheck = fauxEsmNthCheck.default

var nth = new Set([
Expand All @@ -28,6 +28,7 @@ parser.registerAttrEqualityMods('~', '^', '$', '*')
parser.registerSelectorPseudos('any', 'matches', 'not', 'has')
parser.registerNestingOperators('>', '+', '~')

// @ts-expect-error: hush.
var compile = zwitch('type', {handlers: {selectors, ruleSet, rule}})

/**
Expand All @@ -39,7 +40,7 @@ export function parse(selector) {
throw new TypeError('Expected `string` as selector, not `' + selector + '`')
}

// @ts-ignore types are wrong.
// @ts-expect-error types are wrong.
return compile(parser.parse(selector))
}

Expand Down Expand Up @@ -77,9 +78,9 @@ function rule(query) {
pseudo = pseudos[index]

if (nth.has(pseudo.name)) {
// @ts-ignore Patch a non-primitive type.
// @ts-expect-error Patch a non-primitive type.
pseudo.value = nthCheck(pseudo.value)
// @ts-ignore Patch a non-primitive type.
// @ts-expect-error Patch a non-primitive type.
pseudo.valueType = 'function'
}
}
Expand Down
Loading

0 comments on commit 6f7b407

Please sign in to comment.