Skip to content

Commit 1ee98ae

Browse files
committed
Add strict to tsconfig.json
1 parent 84b8134 commit 1ee98ae

File tree

2 files changed

+52
-24
lines changed

2 files changed

+52
-24
lines changed

lib/index.js

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ const search = /\r?\n|\r/g
2424
* @param {string|Uint8Array} doc
2525
*/
2626
export function fromXml(doc) {
27-
// @ts-ignore `strictEntities` is most definitely fine.
27+
// @ts-expect-error `strictEntities` is most definitely fine.
2828
const parser = new Parser(true, {position: true, strictEntities: true})
2929
/** @type {Array.<Node>} */
3030
const stack = [{type: 'root', children: []}]
3131
let position = now()
3232

3333
parser.ondoctype = ondoctype
34-
// @ts-ignore `onsgmldeclaration` is most definitely fine.
34+
// @ts-expect-error `onsgmldeclaration` is most definitely fine.
3535
parser.onsgmldeclaration = onsgmldeclaration
3636
parser.onprocessinginstruction = onprocessinginstruction
3737
parser.ontext = ontext
@@ -43,7 +43,7 @@ export function fromXml(doc) {
4343
parser.onclosetag = exit
4444
parser.onerror = onerror
4545

46-
// @ts-ignore Buffers are most definitely fine.
46+
// @ts-expect-error Buffers are most definitely fine.
4747
parser.write(doc).close()
4848

4949
return stack[0]
@@ -74,18 +74,19 @@ export function fromXml(doc) {
7474
// eslint-disable-next-line complexity
7575
function ondoctype(value) {
7676
/** @type {Doctype} */
77+
// @ts-expect-error: `null`s are fine.
7778
const node = {type: 'doctype', name: '', public: null, system: null}
7879
let index = -1
7980
let state = 'BEGIN'
80-
/** @type {string} */
81+
/** @type {string|undefined} */
8182
let returnState
82-
/** @type {string} */
83+
/** @type {string|undefined} */
8384
let buffer
84-
/** @type {number} */
85+
/** @type {number|undefined} */
8586
let bufferIndex
86-
/** @type {number} */
87+
/** @type {number|undefined} */
8788
let start
88-
/** @type {number} */
89+
/** @type {number|undefined} */
8990
let marker
9091

9192
while (++index <= value.length) {
@@ -169,7 +170,12 @@ export function fromXml(doc) {
169170

170171
break
171172
case 'IN_EID':
172-
if (code === buffer.charCodeAt(++bufferIndex)) {
173+
if (
174+
returnState &&
175+
buffer &&
176+
bufferIndex !== undefined &&
177+
code === buffer.charCodeAt(++bufferIndex)
178+
) {
173179
if (bufferIndex === buffer.length - 1) {
174180
state = returnState
175181
}
@@ -314,12 +320,20 @@ export function fromXml(doc) {
314320
// Comment has a positional bug… 😢
315321
// They end right before the last character (`>`), so let’s add that:
316322
const actualEnd = now()
323+
324+
/* c8 ignore next 3 */
325+
if (typeof actualEnd.offset !== 'number') {
326+
throw new TypeError('Expected offset')
327+
}
328+
317329
actualEnd.column++
318330
actualEnd.offset++
319331

320332
enter(node)
321333
exit()
322334

335+
/* c8 ignore next */
336+
if (!node.position) throw new Error('Expected position')
323337
node.position.end = Object.assign({}, actualEnd)
324338
position = actualEnd
325339
}
@@ -367,11 +381,16 @@ export function fromXml(doc) {
367381
}
368382
}
369383

384+
/* c8 ignore next */
385+
if (typeof actualEnd.offset !== 'number') throw new Error('Expected offset')
386+
370387
actualEnd.offset += value.length
371388

372389
enter(node)
373390
exit()
374391

392+
/* c8 ignore next */
393+
if (!node.position) throw new Error('Expected position')
375394
node.position.end = Object.assign({}, actualEnd)
376395
position = actualEnd
377396
}
@@ -394,8 +413,9 @@ export function fromXml(doc) {
394413
* @returns {void}
395414
*/
396415
function enter(node) {
416+
// @ts-expect-error Set later.
397417
node.position = {start: Object.assign({}, position), end: undefined}
398-
// @ts-ignore Assume valid child.
418+
// @ts-expect-error Assume valid child.
399419
stack[stack.length - 1].children.push(node)
400420
stack.push(node)
401421
position = now()
@@ -405,8 +425,11 @@ export function fromXml(doc) {
405425
* @returns {void}
406426
*/
407427
function exit() {
428+
const tail = stack.pop()
429+
/* c8 ignore next */
430+
if (!tail || !tail.position) throw new Error('Expected tail')
408431
position = now()
409-
stack.pop().position.end = Object.assign({}, position)
432+
tail.position.end = Object.assign({}, position)
410433
}
411434

412435
/**
@@ -433,42 +456,46 @@ export function fromXml(doc) {
433456
/**
434457
* See: <https://www.w3.org/TR/xml/#NT-NameStartChar>
435458
*
436-
* @param {number} code
459+
* @param {number|null} code
437460
* @returns {boolean}
438461
*/
439462
function isNameStartChar(code) {
440-
// eslint-disable-next-line no-misleading-character-class
441-
return /[:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]/.test(
442-
fromCharCode(code)
463+
return (
464+
code !== null &&
465+
// eslint-disable-next-line no-misleading-character-class
466+
/[:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]/.test(
467+
fromCharCode(code)
468+
)
443469
)
444470
}
445471

446472
/**
447473
* See: <https://www.w3.org/TR/xml/#NT-NameChar>
448474
*
449-
* @param {number} code
475+
* @param {number|null} code
450476
* @returns {boolean}
451477
*/
452478
function isNameChar(code) {
453479
return (
454-
isNameStartChar(code) ||
455-
// eslint-disable-next-line no-misleading-character-class
456-
/[-.\d\u00B7\u0300-\u036F\u203F\u2040]/.test(fromCharCode(code))
480+
code !== null &&
481+
(isNameStartChar(code) ||
482+
// eslint-disable-next-line no-misleading-character-class
483+
/[-.\d\u00B7\u0300-\u036F\u203F\u2040]/.test(fromCharCode(code)))
457484
)
458485
}
459486

460487
/**
461-
* @param {number} code
488+
* @param {number|null} code
462489
* @returns {boolean}
463490
*/
464491
function isSpace(code) {
465-
return /[\t\n\r ]/.test(fromCharCode(code))
492+
return code !== null && /[\t\n\r ]/.test(fromCharCode(code))
466493
}
467494

468495
/**
469-
* @param {number} code
496+
* @param {number|null} code
470497
* @returns {boolean}
471498
*/
472499
function isPubidChar(code) {
473-
return /[\n\r !#$%'-;=?-Z_a-z]/.test(fromCharCode(code))
500+
return code !== null && /[\n\r !#$%'-;=?-Z_a-z]/.test(fromCharCode(code))
474501
}

tsconfig.json

Lines changed: 2 additions & 1 deletion
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)