Skip to content

Commit e8e42c5

Browse files
committed
Fix react newline issues
1 parent 53670b8 commit e8e42c5

File tree

2 files changed

+77
-4
lines changed

2 files changed

+77
-4
lines changed

index.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
2-
* @typedef {import('unist').Node} UnistNode
3-
* @typedef {import('hast').Parent} Parent
42
* @typedef {import('hast').Root} Root
53
* @typedef {import('hast').Element} Element
64
* @typedef {import('hast').Text} Text
75
* @typedef {import('hast').Comment} Comment
86
* @typedef {import('hast').Properties} Properties
9-
* @typedef {Root['children'][number]|Root} Node
7+
* @typedef {import('hast').Content} Content
8+
* @typedef {Root|Content} Node
9+
* @typedef {Extract<Node, import('unist').Parent>} Parent
1010
* @typedef {import('estree-jsx').Node} EstreeNode
1111
* @typedef {import('estree-jsx').Program} EstreeProgram
1212
* @typedef {import('estree-jsx').JSXExpressionContainer} EstreeJsxExpressionContainer
@@ -71,6 +71,15 @@ import {zwitch} from 'zwitch'
7171
const toReact = /** @type {Record<string, string>} */ (hastToReact)
7272

7373
const own = {}.hasOwnProperty
74+
const tableElements = new Set([
75+
'table',
76+
'thead',
77+
'tbody',
78+
'tfoot',
79+
'tr',
80+
'th',
81+
'td'
82+
])
7483

7584
/**
7685
* @param {Node|MDXJsxAttributeValueExpression|MDXJsxAttribute|MDXJsxExpressionAttribute|MDXJsxFlowElement|MDXJsxTextElement|MDXFlowExpression|MDXTextExpression} tree
@@ -184,6 +193,7 @@ function element(node, context) {
184193
}
185194

186195
const children = all(node, context)
196+
187197
/** @type {Array<EstreeJsxAttribute|EstreeJsxSpreadAttribute>} */
188198
const attributes = []
189199
/** @type {string} */
@@ -552,9 +562,25 @@ function all(parent, context) {
552562
let index = -1
553563
/** @type {Array.<EstreeJsxChild>} */
554564
const results = []
565+
// Currently, a warning is triggered by react for *any* white space in
566+
// tables.
567+
// So we remove the pretty lines for now.
568+
// See: <https://github.com/facebook/react/pull/7081>.
569+
// See: <https://github.com/facebook/react/pull/7515>.
570+
// See: <https://github.com/remarkjs/remark-react/issues/64>.
571+
const ignoreLineBreak =
572+
context.schema.space === 'html' &&
573+
parent.type === 'element' &&
574+
tableElements.has(parent.tagName.toLowerCase())
555575

556576
while (++index < children.length) {
557-
const result = context.handle(children[index], context)
577+
const child = children[index]
578+
579+
if (ignoreLineBreak && child.type === 'text' && child.value === '\n') {
580+
continue
581+
}
582+
583+
const result = context.handle(child, context)
558584

559585
if (Array.isArray(result)) {
560586
results.push(...result)

test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,53 @@ test('hast-util-to-estree', (t) => {
544544
'should support custom handler that returns an array'
545545
)
546546

547+
t.deepEqual(
548+
toEstree(
549+
h('table', [
550+
{type: 'text', value: '\n'},
551+
h('tr'),
552+
{type: 'text', value: '\n'}
553+
])
554+
),
555+
{
556+
type: 'Program',
557+
body: [
558+
{
559+
type: 'ExpressionStatement',
560+
expression: {
561+
type: 'JSXElement',
562+
openingElement: {
563+
type: 'JSXOpeningElement',
564+
attributes: [],
565+
name: {type: 'JSXIdentifier', name: 'table'},
566+
selfClosing: false
567+
},
568+
closingElement: {
569+
type: 'JSXClosingElement',
570+
name: {type: 'JSXIdentifier', name: 'table'}
571+
},
572+
children: [
573+
{
574+
type: 'JSXElement',
575+
openingElement: {
576+
type: 'JSXOpeningElement',
577+
attributes: [],
578+
name: {type: 'JSXIdentifier', name: 'tr'},
579+
selfClosing: true
580+
},
581+
closingElement: null,
582+
children: []
583+
}
584+
]
585+
}
586+
}
587+
],
588+
sourceType: 'module',
589+
comments: []
590+
},
591+
'should ignore text line endings between table elements'
592+
)
593+
547594
t.end()
548595
})
549596

0 commit comments

Comments
 (0)