Skip to content

Commit 602a6ca

Browse files
committed
Add strict to tsconfig.json
1 parent 0acdba3 commit 602a6ca

File tree

3 files changed

+91
-75
lines changed

3 files changed

+91
-75
lines changed

index.js

Lines changed: 71 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* @typedef {import('estree-jsx').ModuleDeclaration} EstreeModuleDeclaration
2222
* @typedef {import('estree-jsx').Expression} EstreeExpression
2323
* @typedef {import('estree-jsx').Property} EstreeProperty
24+
* @typedef {import('estree-jsx').JSXIdentifier} JSXIdentifier
25+
* @typedef {import('estree-jsx').JSXMemberExpression} JSXMemberExpression
2426
*
2527
* @typedef {EstreeJsxOpeningElement['name']} EstreeJsxElementName
2628
* @typedef {EstreeJsxAttribute['name']} EstreeJsxAttributeName
@@ -69,8 +71,9 @@ import style from 'style-to-object'
6971
import {position} from 'unist-util-position'
7072
import {zwitch} from 'zwitch'
7173

74+
const toReact = /** @type {Record<string, string>} */ (hastToReact)
75+
7276
const own = {}.hasOwnProperty
73-
const push = [].push
7477

7578
/**
7679
* @param {Node|MDXJsxAttributeValueExpression|MDXJsxAttribute|MDXJsxExpressionAttribute|MDXJsxFlowElement|MDXJsxTextElement|MDXFlowExpression|MDXTextExpression} tree
@@ -83,9 +86,12 @@ export function toEstree(tree, options = {}) {
8386
schema: options.space === 'svg' ? svg : html,
8487
comments: [],
8588
esm: [],
89+
// @ts-expect-error: hush.
8690
handle: zwitch('type', {
8791
invalid,
92+
// @ts-expect-error: hush.
8893
unknown,
94+
// @ts-expect-error: hush.
8995
handlers: Object.assign(
9096
{},
9197
{
@@ -117,7 +123,7 @@ export function toEstree(tree, options = {}) {
117123
})
118124
}
119125

120-
// @ts-ignore Types are wrong (`expression` *can* be JSX).
126+
// @ts-expect-error Types are wrong (`expression` *can* be JSX).
121127
body.push(create(tree, {type: 'ExpressionStatement', expression: result}))
122128
}
123129

@@ -206,7 +212,7 @@ function element(node, context) {
206212
}
207213

208214
prop = info.space
209-
? hastToReact[info.property] || info.property
215+
? toReact[info.property] || info.property
210216
: info.attribute
211217

212218
if (Array.isArray(value)) {
@@ -217,7 +223,7 @@ function element(node, context) {
217223

218224
if (prop === 'style') {
219225
/** @type {Object.<string, string>} */
220-
// @ts-ignore Assume `value` is then an object.
226+
// @ts-expect-error Assume `value` is then an object.
221227
const styleValue =
222228
typeof value === 'string' ? parseStyle(value, node.tagName) : value
223229

@@ -269,8 +275,8 @@ function element(node, context) {
269275
shorthand: false,
270276
computed: false,
271277
key: {type: 'Literal', value: String(prop)},
272-
// @ts-ignore No need to worry about `style` (which has a `JSXExpressionContainer`
273-
// value) because that’s a valid identifier.
278+
// @ts-expect-error No need to worry about `style` (which has a
279+
// `JSXExpressionContainer` value) because that’s a valid identifier.
274280
value: attributeValue || {type: 'Literal', value: true},
275281
kind: 'init'
276282
}
@@ -307,14 +313,14 @@ function element(node, context) {
307313
*/
308314
function mdxjsEsm(node, context) {
309315
/** @type {EstreeProgram} */
310-
// @ts-ignore Assume program.
316+
// @ts-expect-error Assume program.
311317
const estree = node.data && node.data.estree
312318
const comments = (estree && estree.comments) || []
313319

314320
if (estree) {
315-
push.apply(context.comments, comments)
321+
context.comments.push(...comments)
316322
attachComments(estree, comments)
317-
push.apply(context.esm, estree.body)
323+
context.esm.push(...estree.body)
318324
}
319325
}
320326

@@ -325,18 +331,19 @@ function mdxjsEsm(node, context) {
325331
*/
326332
function mdxExpression(node, context) {
327333
/** @type {EstreeProgram} */
328-
// @ts-ignore Assume program.
334+
// @ts-expect-error Assume program.
329335
const estree = node.data && node.data.estree
330-
/** @type {EstreeExpression} */
336+
/** @type {EstreeExpression|undefined} */
331337
let expression
332338

333339
if (estree) {
334-
push.apply(context.comments, estree.comments)
340+
context.comments.push(...(estree.comments || []))
335341
attachComments(estree, estree.comments)
336342
expression =
337-
estree.body[0] &&
338-
estree.body[0].type === 'ExpressionStatement' &&
339-
estree.body[0].expression
343+
(estree.body[0] &&
344+
estree.body[0].type === 'ExpressionStatement' &&
345+
estree.body[0].expression) ||
346+
undefined
340347
}
341348

342349
return inherit(node, {
@@ -384,18 +391,19 @@ function mdxJsxElement(node, context) {
384391
// `MDXJsxAttributeValueExpression`.
385392
else if (typeof value === 'object') {
386393
/** @type {EstreeProgram} */
387-
// @ts-ignore Assume program.
394+
// @ts-expect-error Assume program.
388395
const estree = value.data && value.data.estree
389-
/** @type {EstreeExpression} */
390-
let expression = null
396+
/** @type {EstreeExpression|undefined} */
397+
let expression
391398

392399
if (estree) {
393-
push.apply(context.comments, estree.comments)
400+
context.comments.push(...(estree.comments || []))
394401
attachComments(estree, estree.comments)
395402
expression =
396-
estree.body[0] &&
397-
estree.body[0].type === 'ExpressionStatement' &&
398-
estree.body[0].expression
403+
(estree.body[0] &&
404+
estree.body[0].type === 'ExpressionStatement' &&
405+
estree.body[0].expression) ||
406+
undefined
399407
}
400408

401409
attributeValue = inherit(value, {
@@ -419,23 +427,24 @@ function mdxJsxElement(node, context) {
419427
// MDXJsxExpressionAttribute.
420428
else {
421429
/** @type {EstreeProgram} */
422-
// @ts-ignore Assume program.
430+
// @ts-expect-error Assume program.
423431
const estree = attr.data && attr.data.estree
424-
/** @type {EstreeJsxSpreadAttribute['argument']} */
425-
let argumentValue = null
432+
/** @type {EstreeJsxSpreadAttribute['argument']|undefined} */
433+
let argumentValue
426434

427435
if (estree) {
428-
push.apply(context.comments, estree.comments)
436+
context.comments.push(...(estree.comments || []))
429437
attachComments(estree, estree.comments)
430438
argumentValue =
431-
estree.body[0] &&
432-
estree.body[0].type === 'ExpressionStatement' &&
433-
estree.body[0].expression &&
434-
estree.body[0].expression.type === 'ObjectExpression' &&
435-
estree.body[0].expression.properties &&
436-
estree.body[0].expression.properties[0] &&
437-
estree.body[0].expression.properties[0].type === 'SpreadElement' &&
438-
estree.body[0].expression.properties[0].argument
439+
(estree.body[0] &&
440+
estree.body[0].type === 'ExpressionStatement' &&
441+
estree.body[0].expression &&
442+
estree.body[0].expression.type === 'ObjectExpression' &&
443+
estree.body[0].expression.properties &&
444+
estree.body[0].expression.properties[0] &&
445+
estree.body[0].expression.properties[0].type === 'SpreadElement' &&
446+
estree.body[0].expression.properties[0].argument) ||
447+
undefined
439448
}
440449

441450
attributes.push(
@@ -486,7 +495,7 @@ function root(node, context) {
486495
/** @type {Array.<EstreeJsxChild>} */
487496
const cleanChildren = []
488497
let index = -1
489-
/** @type {Array.<EstreeJsxChild>} */
498+
/** @type {Array.<EstreeJsxChild>|undefined} */
490499
let queue
491500

492501
// Remove surrounding whitespace nodes from the fragment.
@@ -498,11 +507,10 @@ function root(node, context) {
498507
child.expression.type === 'Literal' &&
499508
whitespace(child.expression.value)
500509
) {
501-
if (queue) {
502-
queue.push(child)
503-
}
510+
if (queue) queue.push(child)
504511
} else {
505-
cleanChildren.push(...(queue || []), child)
512+
if (queue) cleanChildren.push(...queue)
513+
cleanChildren.push(child)
506514
queue = []
507515
}
508516
}
@@ -517,7 +525,7 @@ function root(node, context) {
517525

518526
/**
519527
* @param {Text} node
520-
* @returns {EstreeJsxExpressionContainer}
528+
* @returns {EstreeJsxExpressionContainer|void}
521529
*/
522530
function text(node) {
523531
const value = String(node.value || '')
@@ -564,7 +572,7 @@ function all(parent, context) {
564572
*/
565573
function inherit(hast, esnode) {
566574
const left = hast.data
567-
/** @type {Object.<string, unknown>} */
575+
/** @type {Object.<string, unknown>|undefined} */
568576
let right
569577
/** @type {string} */
570578
let key
@@ -580,7 +588,7 @@ function inherit(hast, esnode) {
580588
}
581589

582590
if (right) {
583-
// @ts-ignore `esast` extension.
591+
// @ts-expect-error `esast` extension.
584592
esnode.data = right
585593
}
586594
}
@@ -599,10 +607,14 @@ function inherit(hast, esnode) {
599607
function create(hast, esnode) {
600608
const p = position(hast)
601609

602-
if (p.start.line) {
603-
// @ts-ignore acorn-style.
610+
if (
611+
p.start.line &&
612+
p.start.offset !== undefined &&
613+
p.end.offset !== undefined
614+
) {
615+
// @ts-expect-error acorn-style.
604616
esnode.start = p.start.offset
605-
// @ts-ignore acorn-style.
617+
// @ts-expect-error acorn-style.
606618
esnode.end = p.end.offset
607619
esnode.loc = {
608620
start: {line: p.start.line, column: p.start.column - 1},
@@ -628,31 +640,34 @@ const createJsxName =
628640
* @returns {EstreeJsxElementName}
629641
*/
630642
function (name, attribute) {
631-
/** @type {EstreeJsxElementName} */
632-
let node
633-
634643
if (!attribute && name.includes('.')) {
635644
const parts = name.split('.')
636-
node = {type: 'JSXIdentifier', name: parts.shift()}
637-
while (parts.length > 0) {
645+
let part = parts.shift()
646+
/** @type {JSXIdentifier|JSXMemberExpression} */
647+
// @ts-expect-error: hush, the first is always defined.
648+
let node = {type: 'JSXIdentifier', name: part}
649+
650+
while ((part = parts.shift())) {
638651
node = {
639652
type: 'JSXMemberExpression',
640653
object: node,
641-
property: {type: 'JSXIdentifier', name: parts.shift()}
654+
property: {type: 'JSXIdentifier', name: part}
642655
}
643656
}
644-
} else if (name.includes(':')) {
657+
658+
return node
659+
}
660+
661+
if (name.includes(':')) {
645662
const parts = name.split(':')
646-
node = {
663+
return {
647664
type: 'JSXNamespacedName',
648665
namespace: {type: 'JSXIdentifier', name: parts[0]},
649666
name: {type: 'JSXIdentifier', name: parts[1]}
650667
}
651-
} else {
652-
node = {type: 'JSXIdentifier', name}
653668
}
654669

655-
return node
670+
return {type: 'JSXIdentifier', name}
656671
}
657672
)
658673

0 commit comments

Comments
 (0)