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 23, 2021
1 parent 45a7ae8 commit bd62f7d
Show file tree
Hide file tree
Showing 14 changed files with 202 additions and 88 deletions.
16 changes: 9 additions & 7 deletions lib/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ import {pseudo} from './pseudo.js'
import {test} from './test.js'

const 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 {HastNode} node
* @param {HastNode|undefined} node
* @param {SelectState} state
* @returns {Array.<Element>}
*/
export function any(query, node, state) {
// @ts-ignore zwitch types are off.
// @ts-expect-error zwitch types are off.
return query && node ? type(query, node, state) : []
}

Expand Down Expand Up @@ -79,10 +81,10 @@ function rule(query, tree, state) {
null,
configure(query, {
schema: state.space === 'svg' ? svg : html,
language: null,
language: undefined,
direction: 'ltr',
editableOrEditingHost: false,
// @ts-ignore assume elements.
// @ts-expect-error assume elements.
scopeElements: tree.type === 'root' ? tree.children : [tree],
iterator,
one: state.one,
Expand All @@ -100,7 +102,7 @@ function rule(query, tree, state) {
if (query.rule) {
nest(query.rule, node, index, parent, configure(query.rule, state))
} else {
// @ts-ignore `test` also asserts `node is Element`
// @ts-expect-error `test` also asserts `node is Element`
collector.collect(node)
state.found = true
}
Expand Down Expand Up @@ -147,12 +149,12 @@ function invalidType() {

class Collector {
/**
* @param {boolean} one
* @param {boolean|undefined} [one]
*/
constructor(one) {
/** @type {Array.<Element>} */
this.result = []
/** @type {boolean} */
/** @type {boolean|undefined} */
this.one = one
/** @type {boolean} */
this.found = false
Expand Down
66 changes: 44 additions & 22 deletions lib/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,22 @@ import {stringify as spaces} from 'space-separated-tokens'
import {zwitch} from 'zwitch'

const 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.
'~=': spaceSeparatedList,
// @ts-expect-error: hush.
'|=': exactOrPrefix,
// @ts-expect-error: hush.
'^=': begins,
// @ts-expect-error: hush.
'$=': ends,
// @ts-expect-error: hush.
'*=': contains
}
})
Expand All @@ -37,7 +45,9 @@ export function attribute(query, element, schema) {
let index = -1

while (++index < attrs.length) {
if (!handle(attrs[index], element, find(schema, attrs[index].name))) return
if (!handle(attrs[index], element, find(schema, attrs[index].name))) {
return false
}
}

return true
Expand All @@ -64,9 +74,10 @@ function exists(_, element, info) {
* @returns {boolean}
*/
function exact(query, element, info) {
return (
return Boolean(
hasProperty(element, info.property) &&
normalizeValue(element.properties[info.property], info) === query.value
element.properties &&
normalizeValue(element.properties[info.property], info) === query.value
)
}

Expand All @@ -79,14 +90,15 @@ function exact(query, element, info) {
* @returns {boolean}
*/
function spaceSeparatedList(query, element, info) {
const value = element.properties[info.property]
const value = element.properties && element.properties[info.property]

return (
// If this is a comma-separated list, and the query is contained in it, return
// true.
(!info.commaSeparated &&
value &&
typeof value === 'object' &&
query.value &&
value.includes(query.value)) ||
// For all other values (including comma-separated lists), return whether this
// is an exact match.
Expand All @@ -104,13 +116,17 @@ function spaceSeparatedList(query, element, info) {
* @returns {boolean}
*/
function exactOrPrefix(query, element, info) {
const value = normalizeValue(element.properties[info.property], info)
const value = normalizeValue(
element.properties && element.properties[info.property],
info
)

return (
return Boolean(
hasProperty(element, info.property) &&
(value === query.value ||
(value.slice(0, query.value.length) === query.value &&
value.charAt(query.value.length) === '-'))
query.value &&
(value === query.value ||
(value.slice(0, query.value.length) === query.value &&
value.charAt(query.value.length) === '-'))
)
}

Expand All @@ -123,12 +139,14 @@ function exactOrPrefix(query, element, info) {
* @returns {boolean}
*/
function begins(query, element, info) {
return (
return Boolean(
hasProperty(element, info.property) &&
normalizeValue(element.properties[info.property], info).slice(
0,
query.value.length
) === query.value
element.properties &&
query.value &&
normalizeValue(element.properties[info.property], info).slice(
0,
query.value.length
) === query.value
)
}

Expand All @@ -141,11 +159,13 @@ function begins(query, element, info) {
* @returns {boolean}
*/
function ends(query, element, info) {
return (
return Boolean(
hasProperty(element, info.property) &&
normalizeValue(element.properties[info.property], info).slice(
-query.value.length
) === query.value
element.properties &&
query.value &&
normalizeValue(element.properties[info.property], info).slice(
-query.value.length
) === query.value
)
}

Expand All @@ -158,11 +178,13 @@ function ends(query, element, info) {
* @returns {boolean}
*/
function contains(query, element, info) {
return (
return Boolean(
hasProperty(element, info.property) &&
normalizeValue(element.properties[info.property], info).includes(
query.value
)
element.properties &&
query.value &&
normalizeValue(element.properties[info.property], info).includes(
query.value
)
)
}

Expand Down
8 changes: 5 additions & 3 deletions lib/class-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
*/
export function className(query, element) {
/** @type {Array.<string>} */
// @ts-ignore Assume array.
// @ts-expect-error Assume array.
const value = element.properties.className || []
let index = -1

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

return true
Expand Down
31 changes: 17 additions & 14 deletions lib/enter-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import {direction} from 'direction'
import {isElement} from 'hast-util-is-element'
// @ts-expect-error: to do type.
import toString from 'hast-util-to-string'
import {svg} from 'property-information'
import {visit, EXIT, SKIP} from 'unist-util-visit'
Expand All @@ -24,15 +25,13 @@ export function enterState(state, node) {
const language = state.language
const currentDirection = state.direction
const editableOrEditingHost = state.editableOrEditingHost
/** @type {Direction|null} */
/** @type {Direction|undefined} */
let dirInferred
/** @type {boolean} */
/** @type {boolean|undefined} */
let found

if (element(node)) {
// @ts-ignore Assume string.
if (element(node) && node.properties) {
const lang = node.properties.xmlLang || node.properties.lang
// @ts-ignore Assume string.
const type = node.properties.type || 'text'
const dir = dirProperty(node)

Expand All @@ -41,7 +40,7 @@ export function enterState(state, node) {
found = true
}

if (schema.space === 'html') {
if (schema && schema.space === 'html') {
if (node.properties.contentEditable === 'true') {
state.editableOrEditingHost = true
found = true
Expand Down Expand Up @@ -78,13 +77,14 @@ export function enterState(state, node) {
type === 'text')
) {
// Check value of `<input>`.
// @ts-ignore something is `never` in types but this is needed.
// @ts-expect-error something is `never` in types but this is needed.
dirInferred = node.properties.value
? // @ts-ignore Assume string
? // @ts-expect-error Assume string
dirBidi(node.properties.value)
: 'ltr'
} else {
// Check text nodes in `node`.
// @ts-expect-error: fine.
visit(node, inferDirectionality)
}
}
Expand Down Expand Up @@ -129,23 +129,26 @@ export function enterState(state, node) {

/**
* @param {string} value
* @returns {Direction}
* @returns {Direction|undefined}
*/
function dirBidi(value) {
const result = direction(value)
return result === 'neutral' ? null : result
return result === 'neutral' ? undefined : result
}

/**
* @param {ElementChild} node
* @returns {Direction}
* @returns {Direction|undefined}
*/
function dirProperty(node) {
const value =
element(node) && typeof node.properties.dir === 'string'
element(node) && node.properties && typeof node.properties.dir === 'string'
? node.properties.dir.toLowerCase()
: null
return value === 'auto' || value === 'ltr' || value === 'rtl' ? value : null
: undefined

return value === 'auto' || value === 'ltr' || value === 'rtl'
? value
: undefined
}

function noop() {}
2 changes: 1 addition & 1 deletion lib/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
* @returns {boolean}
*/
export function id(query, element) {
return element.properties.id === query.id
return Boolean(element.properties && element.properties.id === query.id)
}
Loading

0 comments on commit bd62f7d

Please sign in to comment.