Skip to content

Commit

Permalink
Merge pull request #320 from yhatt/fix-dual-fragment-hazard
Browse files Browse the repository at this point in the history
Fix `JSXSlack.Children.toArray` to make flatten children correctly even if caused dual import of `<Fragment>`
  • Loading branch information
yhatt authored Oct 31, 2024
2 parents 75b4bd2 + 3c7bcb9 commit 2d9451c
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 179 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixed

- `JSXSlackTemplateTag` type now accepts pure readonly string array ([#312](https://github.com/yhatt/jsx-slack/pull/312))
- Fix `JSXSlack.Children.toArray` to make flatten children correctly even if caused dual import of `<Fragment>` ([#319](https://github.com/yhatt/jsx-slack/issues/319), [#320](https://github.com/yhatt/jsx-slack/pull/320))

### Changed

Expand Down
2 changes: 1 addition & 1 deletion jsx-dev-runtime.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// eslint-disable-next-line import/no-unresolved
export * from './types/jsx-dev-runtime'
export * from './types/src/jsx-dev-runtime'
2 changes: 1 addition & 1 deletion jsx-runtime.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// eslint-disable-next-line import/no-unresolved
export * from './types/jsx-runtime'
export * from './types/src/jsx-runtime'
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"module": "./module/src/index.mjs",
"exports": {
".": {
"types": "./types/index.d.ts",
"types": "./types/src/index.d.ts",
"import": "./module/src/index.mjs",
"default": "./lib/index.js"
},
Expand All @@ -46,7 +46,7 @@
}
},
"sideEffects": false,
"types": "types/index.d.ts",
"types": "types/src/index.d.ts",
"files": [
"lib/",
"module/",
Expand Down Expand Up @@ -110,7 +110,7 @@
"jest-junit": "^16.0.0",
"lodash.debounce": "^4.0.8",
"mdast-util-phrasing": "^4.1.0",
"npm-run-all": "^4.1.5",
"npm-run-all2": "^7.0.1",
"pako": "^2.1.0",
"postcss": "^8.4.35",
"postcss-import": "^15.1.0",
Expand Down
25 changes: 18 additions & 7 deletions src/jsx-internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {
} = Object

const jsxSlackObjKey = '$$jsxslack' as const
const jsxSlackComponentObjKey = jsxSlackObjKey + `Component`
const jsxSlackComponentObjKey = `${jsxSlackObjKey}Component` as const

export interface BuiltInComponent<P extends {}> extends JSXSlack.FC<P> {
readonly $$jsxslackComponent: { name: string } & Record<any, any>
Expand Down Expand Up @@ -126,17 +126,28 @@ export const isValidElementInternal = (
* component.
*
* @param element - An object to verify
* @param component - The optional component to match while verifying
* @param component - The optional component or the name of built-in component
* to match while verifying
* @return `true` if the passed object was a jsx-slack element created from
* built-in component, otherwise `false`
*/
export const isValidElementFromComponent = (
obj: unknown,
component?: JSXSlack.FunctionComponent<any>,
): obj is JSXSlack.JSX.Element =>
isValidElementInternal(obj) &&
isValidComponent(obj[jsxSlackObjKey].type) &&
(!component || obj[jsxSlackObjKey].type === component)
component?: string | JSXSlack.FunctionComponent<any>,
): obj is JSXSlack.JSX.Element => {
const valid =
isValidElementInternal(obj) && isValidComponent(obj[jsxSlackObjKey].type)

if (!valid) return false

if (typeof component === 'string') {
return obj[jsxSlackObjKey].type[jsxSlackComponentObjKey].name === component
} else if (component) {
return obj[jsxSlackObjKey].type === component
}

return true
}

/**
* Clean up hidden meta value for jsx-slack from object.
Expand Down
2 changes: 1 addition & 1 deletion src/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export namespace JSXSlack {
if (child == null) return reduced

// Make flatten fragment's children
if (isValidElementFromComponent(child, Fragment))
if (isValidElementFromComponent(child, 'Fragment'))
return reduced.concat(Children.toArray([...(child as any)]))

return [...reduced, child]
Expand Down
Loading

0 comments on commit 2d9451c

Please sign in to comment.