Skip to content

Commit

Permalink
Match numbers and string to element-valued properties (#6028)
Browse files Browse the repository at this point in the history
## Problem
If there's no annotation for a component property, we only treat JSX
elements as matching values for render props, even though strings and
numbers would be valid elements children as well.

## Fix
If we detect a render prop, the matching code treats strings and numbers
as matching as well.

#5979
  • Loading branch information
bkrmendy authored Jul 1, 2024
1 parent ba2b1e2 commit 6a26bbc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import React from 'react'
import { stripNulls } from '../../../../core/shared/array-utils'
import * as EP from '../../../../core/shared/element-path'
import { maybeToArray } from '../../../../core/shared/optional-utils'
import { emptySet } from '../../../../core/shared/set-utils'
Expand Down Expand Up @@ -176,4 +178,53 @@ describe('matchForPropertyValue', () => {
]
`)
})

it('matches strings and numbers too if the value of the property is a jsx element', () => {
const variableNamesInScope: Array<VariableInfo> = stripNulls([
variableInfoFromValue(
'data',
'data',
{
size: 300,
description: 'relative',
open: false,
label: React.createElement('div'),
likes: ['Alice', 'Bob'],
quote: {
author: 'E.Poe',
text: 'I became insane, with long intervals of horrible sanity.',
},
},
EP.fromString('aaa'),
emptySet(),
),
])

const currentPropertyValue: PropertyValue = {
type: 'existing',
value: React.createElement('div'),
}
const targetPropertyName = 'label'
const actualResult = matchForPropertyValue(
null,
currentPropertyValue,
targetPropertyName,
)(variableNamesInScope)

const matching = actualResult
.flatMap((r) => [
r,
...(r.type === 'array' ? r.elements : r.type === 'object' ? r.props : []),
])
.filter((r) => r.matches === 'matches')
.map((r) => r.expression)

expect(matching).toMatchInlineSnapshot(`
Array [
"data.size",
"data.description",
"data.label",
]
`)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ function objectShapesMatch(l: object, r: object): boolean {

function variableShapesMatch(current: unknown, other: unknown): boolean {
if (React.isValidElement(current)) {
return isValidReactNode(other)
return isValidReactNode(other) || typeof other === 'string' || typeof other === 'number'
}

if (Array.isArray(current) && Array.isArray(other)) {
Expand Down

0 comments on commit 6a26bbc

Please sign in to comment.