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 665ed82 commit 29ea36c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 56 deletions.
81 changes: 47 additions & 34 deletions lib/index.js
@@ -1,9 +1,9 @@
/**
* @typedef {import('parse5').Document} P5Document
* @typedef {import('parse5').DocumentFragment} P5Fragment
* @typedef {import('parse5').Element} P5Element
* @typedef {Omit<import('parse5').Element, 'parentNode'>} P5Element
* @typedef {import('parse5').Attribute} P5Attribute
* @typedef {import('parse5').Location} P5Location
* @typedef {Omit<import('parse5').Location, 'startOffset' | 'endOffset'> & {startOffset: number|undefined, endOffset: number|undefined}} P5Location
* @typedef {import('parse5').ParserOptions} P5ParserOptions
* @typedef {import('unist').Node} UnistNode
* @typedef {import('hast').Parent} Parent
Expand Down Expand Up @@ -34,14 +34,14 @@
* @property {string} lastStartTagName
* @property {number} consumedAfterSnapshot
* @property {boolean} active
* @property {HiddenToken} currentCharacterToken
* @property {HiddenToken} currentToken
* @property {HiddenToken|undefined} currentCharacterToken
* @property {HiddenToken|undefined} currentToken
* @property {unknown} currentAttr
*
* @typedef {Object.<string, unknown> & {location: P5Location}} HiddenToken
*
* @typedef HiddenPreprocessor
* @property {string} html
* @property {string|undefined} html
* @property {number} pos
* @property {number} lastGapPos
* @property {number} lastCharPos
Expand All @@ -51,7 +51,7 @@
* @property {boolean} endOfChunkHit
*
* @typedef HiddenLocationTracker
* @property {P5Location} currentAttrLocation
* @property {P5Location|undefined} currentAttrLocation
* @property {P5Location} ctLoc
* @property {HiddenPosTracker} posTracker
*
Expand All @@ -64,6 +64,7 @@
* @property {number} line
*/

// @ts-expect-error: untyped.
import Parser from 'parse5/lib/parser/index.js'
import {pointStart, pointEnd} from 'unist-util-position'
import {visit} from 'unist-util-visit'
Expand Down Expand Up @@ -96,18 +97,20 @@ export function raw(tree, file, options) {
let index = -1
const parser = new Parser(parseOptions)
const one = zwitch('type', {
// @ts-expect-error: hush.
handlers: {root, element, text, comment, doctype, raw: handleRaw},
// @ts-expect-error: hush.
unknown
})
/** @type {boolean} */
/** @type {boolean|undefined} */
let stitches
/** @type {HiddenTokenizer} */
/** @type {HiddenTokenizer|undefined} */
let tokenizer
/** @type {HiddenPreprocessor} */
/** @type {HiddenPreprocessor|undefined} */
let preprocessor
/** @type {HiddenPosTracker} */
/** @type {HiddenPosTracker|undefined} */
let posTracker
/** @type {HiddenLocationTracker} */
/** @type {HiddenLocationTracker|undefined} */
let locationTracker

if (isOptions(file)) {
Expand All @@ -117,6 +120,7 @@ export function raw(tree, file, options) {

if (options && options.passThrough) {
while (++index < options.passThrough.length) {
// @ts-expect-error: hush.
one.handlers[options.passThrough[index]] = stitch
}
}
Expand All @@ -143,7 +147,7 @@ export function raw(tree, file, options) {
* @type {import('unist-util-visit').Visitor<Stitch>}
*/
function mend(node, index, parent) {
if (node.value.stitch) {
if (node.value.stitch && parent !== null && index !== null) {
parent.children[index] = node.value.stitch
return index
}
Expand All @@ -159,17 +163,15 @@ export function raw(tree, file, options) {
tagName: 'template',
attrs: [],
namespaceURI: webNamespaces.html,
childNodes: [],
parentNode: undefined
childNodes: []
}
/** @type {P5Element} */
const mock = {
nodeName: 'documentmock',
tagName: 'documentmock',
attrs: [],
namespaceURI: webNamespaces.html,
childNodes: [],
parentNode: undefined
childNodes: []
}
/** @type {P5Fragment} */
const doc = {nodeName: '#document-fragment', childNodes: []}
Expand All @@ -182,6 +184,8 @@ export function raw(tree, file, options) {
parser._findFormInFragmentContext()

tokenizer = parser.tokenizer
/* c8 ignore next */
if (!tokenizer) throw new Error('Expected `tokenizer`')
preprocessor = tokenizer.preprocessor
locationTracker = tokenizer.__mixins[0]
posTracker = locationTracker.posTracker
Expand All @@ -200,8 +204,10 @@ export function raw(tree, file, options) {
/** @type {P5Document} */
const doc = parser.treeAdapter.createDocument()

parser._bootstrap(doc, null)
parser._bootstrap(doc, undefined)
tokenizer = parser.tokenizer
/* c8 ignore next */
if (!tokenizer) throw new Error('Expected `tokenizer`')
preprocessor = tokenizer.preprocessor
locationTracker = tokenizer.__mixins[0]
posTracker = locationTracker.posTracker
Expand Down Expand Up @@ -302,9 +308,15 @@ export function raw(tree, file, options) {
const column = start.column || 1
const offset = start.offset || 0

/* c8 ignore next 4 */
if (!preprocessor) throw new Error('Expected `preprocessor`')
if (!tokenizer) throw new Error('Expected `tokenizer`')
if (!posTracker) throw new Error('Expected `posTracker`')
if (!locationTracker) throw new Error('Expected `locationTracker`')

// Reset preprocessor:
// See: <https://github.com/inikulin/parse5/blob/9c683e1/packages/parse5/lib/tokenizer/preprocessor.js>.
preprocessor.html = null
preprocessor.html = undefined
preprocessor.pos = -1
preprocessor.lastGapPos = -1
preprocessor.lastCharPos = -1
Expand All @@ -324,13 +336,13 @@ export function raw(tree, file, options) {

// Reset location tracker:
// See: <https://github.com/inikulin/parse5/blob/9c683e1/packages/parse5/lib/extensions/location-info/tokenizer-mixin.js>.
locationTracker.currentAttrLocation = null
locationTracker.currentAttrLocation = undefined
locationTracker.ctLoc = createParse5Location(node)

// See the code for `parse` and `parseFragment`:
// See: <https://github.com/inikulin/parse5/blob/9c683e1/packages/parse5/lib/parser/index.js#L371>.
tokenizer.write(node.value)
parser._runParsingLoop(null)
parser._runParsingLoop(undefined)

// Process final characters if they’re still there after hibernating.
// Similar to:
Expand All @@ -356,24 +368,27 @@ export function raw(tree, file, options) {
// Recurse, because to somewhat handle `[<x>]</x>` (where `[]` denotes the
// passed through node).
if ('children' in node) {
// @ts-ignore Assume parent.
// @ts-expect-error Assume parent.
clone.children = raw(
// @ts-ignore Assume parent.
// @ts-expect-error Assume parent.
{type: 'root', children: node.children},
file,
options
// @ts-ignore Assume parent.
// @ts-expect-error Assume parent.
).children
}

// Hack: `value` is supposed to be a string, but as none of the tools
// (`parse5` or `hast-util-from-parse5`) looks at it, we can pass nodes
// through.
// @ts-ignore
// @ts-expect-error
comment({value: {stitch: clone}})
}

function resetTokenizer() {
/* c8 ignore next */
if (!tokenizer) throw new Error('Expected `tokenizer`')

// Reset tokenizer:
// See: <https://github.com/inikulin/parse5/blob/9c683e1/packages/parse5/lib/tokenizer/index.js#L218-L234>.
// Especially putting it back in the `data` state is useful: some elements,
Expand All @@ -389,9 +404,9 @@ export function raw(tree, file, options) {
tokenizer.lastStartTagName = ''
tokenizer.consumedAfterSnapshot = -1
tokenizer.active = false
tokenizer.currentCharacterToken = null
tokenizer.currentToken = null
tokenizer.currentAttr = null
tokenizer.currentCharacterToken = undefined
tokenizer.currentToken = undefined
tokenizer.currentAttr = undefined
}
}

Expand All @@ -402,7 +417,7 @@ export function raw(tree, file, options) {
function startTag(node) {
/** @type {P5Location} */
const location = Object.assign(createParse5Location(node))
// @ts-ignore extra positional info.
// @ts-expect-error extra positional info.
location.startTag = Object.assign({}, location)

// Untyped token.
Expand All @@ -411,7 +426,6 @@ function startTag(node) {
tagName: node.tagName,
selfClosing: false,
attrs: attributes(node),
// @ts-ignore extra positional info.
location
}
}
Expand All @@ -426,7 +440,7 @@ function attributes(node) {
type: 'element',
properties: node.properties,
children: []
// @ts-ignore Assume element.
// @ts-expect-error Assume element.
}).attrs
}

Expand All @@ -437,15 +451,14 @@ function attributes(node) {
function endTag(node) {
/** @type {P5Location} */
const location = Object.assign(createParse5Location(node))
// @ts-ignore extra positional info.
// @ts-expect-error extra positional info.
location.startTag = Object.assign({}, location)

// Untyped token.
return {
type: endTagToken,
tagName: node.tagName,
attrs: [],
// @ts-ignore extra positional info.
location
}
}
Expand Down Expand Up @@ -489,9 +502,9 @@ function createParse5Location(node) {
}

/**
* @param {VFile|Options} value
* @param {VFile|Options|undefined} value
* @return {value is Options}
*/
function isOptions(value) {
return value && !('contents' in value)
return Boolean(value && !('contents' in value))
}

0 comments on commit 29ea36c

Please sign in to comment.