Skip to content

Commit a57a0bb

Browse files
committed
Add strict to tsconfig.json
1 parent 0977456 commit a57a0bb

File tree

3 files changed

+50
-39
lines changed

3 files changed

+50
-39
lines changed

lib/index.js

+28-18
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* @callback Handler
2929
* @param {Context} ctx
3030
* @param {P5Node} node
31-
* @param {Array.<Child>} children
31+
* @param {Array.<Child>} [children]
3232
* @returns {Node}
3333
*
3434
* @typedef Options
@@ -38,8 +38,8 @@
3838
*
3939
* @typedef Context
4040
* @property {Schema} schema
41-
* @property {VFile} file
42-
* @property {boolean} verbose
41+
* @property {VFile|undefined} file
42+
* @property {boolean|undefined} verbose
4343
* @property {boolean} location
4444
*/
4545

@@ -68,7 +68,7 @@ const map = {
6868
export function fromParse5(ast, options = {}) {
6969
/** @type {Options} */
7070
let settings
71-
/** @type {VFile} */
71+
/** @type {VFile|undefined} */
7272
let file
7373

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

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

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

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

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

@@ -186,7 +187,7 @@ function root(ctx, ast, children) {
186187
* @returns {Doctype}
187188
*/
188189
function doctype() {
189-
// @ts-ignore Types are out of date.
190+
// @ts-expect-error Types are out of date.
190191
return {type: 'doctype'}
191192
}
192193

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

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

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

246-
if ((start || end) && ctx.file) {
247-
result.content.position = {start, end}
247+
if (startTag && endTag && ctx.file) {
248+
content.position = {start: startTag.end, end: endTag.start}
248249
}
250+
251+
result.content = content
249252
}
250253

251254
return result
@@ -257,7 +260,7 @@ function element(ctx, ast, children) {
257260
* @param {Context} ctx
258261
* @param {Node} node
259262
* @param {P5ElementLocation} location
260-
* @returns {Position}
263+
* @returns {Position|null}
261264
*/
262265
function location(ctx, node, location) {
263266
const result = position(location)
@@ -267,12 +270,18 @@ function location(ctx, node, location) {
267270

268271
// Bug for unclosed with children.
269272
// See: <https://github.com./inikulin/parse5/issues/109>.
270-
if (!location.endTag && tail && tail.position && tail.position.end) {
273+
if (
274+
result &&
275+
!location.endTag &&
276+
tail &&
277+
tail.position &&
278+
tail.position.end
279+
) {
271280
result.end = Object.assign({}, tail.position.end)
272281
}
273282

274283
if (ctx.verbose) {
275-
/** @type {Object.<string, Position>} */
284+
/** @type {Object.<string, Position|null>} */
276285
const props = {}
277286
/** @type {string} */
278287
let key
@@ -311,6 +320,7 @@ function position(loc) {
311320
column: loc.endCol,
312321
offset: loc.endOffset
313322
})
323+
// @ts-expect-error `null` is fine.
314324
return start || end ? {start, end} : null
315325
}
316326

test/index.js

+20-20
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import test from 'tape'
1515
import {isHidden} from 'is-hidden'
1616
import parse5 from 'parse5'
1717
import {visit} from 'unist-util-visit'
18+
// @ts-expect-error: to do type.
1819
import vfile from 'to-vfile'
1920
import {fromParse5} from '../index.js'
2021

@@ -232,11 +233,11 @@ test('hast-util-from-parse5', (t) => {
232233
{
233234
nodeName: '#text',
234235
value: 'Hello!',
235-
// @ts-ignore runtime.
236+
// @ts-expect-error runtime.
236237
sourceCodeLocation: {}
237238
}
238239
],
239-
// @ts-ignore runtime.
240+
// @ts-expect-error runtime.
240241
sourceCodeLocation: {
241242
startLine: 1,
242243
startCol: 1,
@@ -268,7 +269,7 @@ test('hast-util-from-parse5', (t) => {
268269
attrs: [],
269270
namespaceURI: 'http://www.w3.org/1999/xhtml',
270271
childNodes: [
271-
// @ts-ignore runtime.
272+
// @ts-expect-error runtime.
272273
{
273274
nodeName: '#text',
274275
value: 'Hello!',
@@ -282,7 +283,7 @@ test('hast-util-from-parse5', (t) => {
282283
}
283284
}
284285
],
285-
// @ts-ignore runtime.
286+
// @ts-expect-error runtime.
286287
sourceCodeLocation: {
287288
startLine: 1,
288289
startCol: 1,
@@ -472,23 +473,22 @@ test('fixtures', (t) => {
472473
* @param {Node} tree
473474
*/
474475
function clean(tree) {
475-
visit(tree, cleaner)
476-
}
477-
478-
/**
479-
* @param {Node} node
480-
*/
481-
function cleaner(node) {
482-
delete node.position
483-
484-
// Remove verbose data.
485-
if (node.type === 'element') {
486-
delete node.data
487-
488-
if ('content' in node) {
489-
clean(node.content)
476+
visit(
477+
tree,
478+
// @ts-expect-error: hush.
479+
/** @type {import('unist-util-visit').Visitor<Node>} */ (node) => {
480+
delete node.position
481+
482+
// Remove verbose data.
483+
if (node.type === 'element') {
484+
delete node.data
485+
486+
if (node.content) {
487+
clean(node.content)
488+
}
489+
}
490490
}
491-
}
491+
)
492492
}
493493

494494
/**

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"declaration": true,
1111
"emitDeclarationOnly": true,
1212
"allowSyntheticDefaultImports": true,
13-
"skipLibCheck": true
13+
"skipLibCheck": true,
14+
"strict": true
1415
}
1516
}

0 commit comments

Comments
 (0)