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 0977456 commit a57a0bb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 39 deletions.
46 changes: 28 additions & 18 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @callback Handler
* @param {Context} ctx
* @param {P5Node} node
* @param {Array.<Child>} children
* @param {Array.<Child>} [children]
* @returns {Node}
*
* @typedef Options
Expand All @@ -38,8 +38,8 @@
*
* @typedef Context
* @property {Schema} schema
* @property {VFile} file
* @property {boolean} verbose
* @property {VFile|undefined} file
* @property {boolean|undefined} verbose
* @property {boolean} location
*/

Expand Down Expand Up @@ -68,7 +68,7 @@ const map = {
export function fromParse5(ast, options = {}) {
/** @type {Options} */
let settings
/** @type {VFile} */
/** @type {VFile|undefined} */
let file

if (isFile(options)) {
Expand Down Expand Up @@ -100,8 +100,9 @@ export function fromParse5(ast, options = {}) {
function transform(ctx, ast) {
const schema = ctx.schema
/** @type {Handler} */
// @ts-expect-error: index is fine.
const fn = own.call(map, ast.nodeName) ? map[ast.nodeName] : element
/** @type {Array.<Child>} */
/** @type {Array.<Child>|undefined} */
let children

// Element.
Expand All @@ -116,7 +117,7 @@ function transform(ctx, ast) {
const result = fn(ctx, ast, children)

if ('sourceCodeLocation' in ast && ast.sourceCodeLocation && ctx.file) {
// @ts-ignore It’s fine.
// @ts-expect-error It’s fine.
const position = location(ctx, result, ast.sourceCodeLocation)

if (position) {
Expand All @@ -143,7 +144,7 @@ function nodes(ctx, children) {
const result = []

while (++index < children.length) {
// @ts-ignore Assume no roots in children.
// @ts-expect-error Assume no roots in children.
result[index] = transform(ctx, children[index])
}

Expand Down Expand Up @@ -186,7 +187,7 @@ function root(ctx, ast, children) {
* @returns {Doctype}
*/
function doctype() {
// @ts-ignore Types are out of date.
// @ts-expect-error Types are out of date.
return {type: 'doctype'}
}

Expand Down Expand Up @@ -235,17 +236,19 @@ function element(ctx, ast, children) {
const result = fn(ast.tagName, props, children)

if (result.tagName === 'template' && 'content' in ast) {
// @ts-ignore Types are wrong.
const pos = ast.sourceCodeLocation
const start = pos && pos.startTag && position(pos.startTag).end
const end = pos && pos.endTag && position(pos.endTag).start
const startTag = pos && pos.startTag && position(pos.startTag)
const endTag = pos && pos.endTag && position(pos.endTag)

// @ts-ignore Types are wrong.
result.content = transform(ctx, ast.content)
/** @type {Root} */
// @ts-expect-error Types are wrong.
const content = transform(ctx, ast.content)

if ((start || end) && ctx.file) {
result.content.position = {start, end}
if (startTag && endTag && ctx.file) {
content.position = {start: startTag.end, end: endTag.start}
}

result.content = content
}

return result
Expand All @@ -257,7 +260,7 @@ function element(ctx, ast, children) {
* @param {Context} ctx
* @param {Node} node
* @param {P5ElementLocation} location
* @returns {Position}
* @returns {Position|null}
*/
function location(ctx, node, location) {
const result = position(location)
Expand All @@ -267,12 +270,18 @@ function location(ctx, node, location) {

// Bug for unclosed with children.
// See: <https://github.com/inikulin/parse5/issues/109>.
if (!location.endTag && tail && tail.position && tail.position.end) {
if (
result &&
!location.endTag &&
tail &&
tail.position &&
tail.position.end
) {
result.end = Object.assign({}, tail.position.end)
}

if (ctx.verbose) {
/** @type {Object.<string, Position>} */
/** @type {Object.<string, Position|null>} */
const props = {}
/** @type {string} */
let key
Expand Down Expand Up @@ -311,6 +320,7 @@ function position(loc) {
column: loc.endCol,
offset: loc.endOffset
})
// @ts-expect-error `null` is fine.
return start || end ? {start, end} : null
}

Expand Down
40 changes: 20 additions & 20 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import test from 'tape'
import {isHidden} from 'is-hidden'
import parse5 from 'parse5'
import {visit} from 'unist-util-visit'
// @ts-expect-error: to do type.
import vfile from 'to-vfile'
import {fromParse5} from '../index.js'

Expand Down Expand Up @@ -232,11 +233,11 @@ test('hast-util-from-parse5', (t) => {
{
nodeName: '#text',
value: 'Hello!',
// @ts-ignore runtime.
// @ts-expect-error runtime.
sourceCodeLocation: {}
}
],
// @ts-ignore runtime.
// @ts-expect-error runtime.
sourceCodeLocation: {
startLine: 1,
startCol: 1,
Expand Down Expand Up @@ -268,7 +269,7 @@ test('hast-util-from-parse5', (t) => {
attrs: [],
namespaceURI: 'http://www.w3.org/1999/xhtml',
childNodes: [
// @ts-ignore runtime.
// @ts-expect-error runtime.
{
nodeName: '#text',
value: 'Hello!',
Expand All @@ -282,7 +283,7 @@ test('hast-util-from-parse5', (t) => {
}
}
],
// @ts-ignore runtime.
// @ts-expect-error runtime.
sourceCodeLocation: {
startLine: 1,
startCol: 1,
Expand Down Expand Up @@ -472,23 +473,22 @@ test('fixtures', (t) => {
* @param {Node} tree
*/
function clean(tree) {
visit(tree, cleaner)
}

/**
* @param {Node} node
*/
function cleaner(node) {
delete node.position

// Remove verbose data.
if (node.type === 'element') {
delete node.data

if ('content' in node) {
clean(node.content)
visit(
tree,
// @ts-expect-error: hush.
/** @type {import('unist-util-visit').Visitor<Node>} */ (node) => {
delete node.position

// Remove verbose data.
if (node.type === 'element') {
delete node.data

if (node.content) {
clean(node.content)
}
}
}
}
)
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"declaration": true,
"emitDeclarationOnly": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true
"skipLibCheck": true,
"strict": true
}
}

0 comments on commit a57a0bb

Please sign in to comment.