Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: implement TS exactOptionalPropertyType rule #90

Merged
merged 20 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
166fb4f
refactor: implement ts exactoptionalPropertyType rule
BhumikP Feb 21, 2025
fd98dca
refactor: add script module props chanegs
BhumikP Feb 24, 2025
cff239b
Merge branch 'develop' into ts-config/exact-optional-type
justlevine Feb 25, 2025
7d8fe39
refactor: update props parsing
BhumikP Feb 26, 2025
5e8794d
Merge branch 'develop' into ts-config/exact-optional-type
BhumikP Feb 27, 2025
029b9ac
refactor: WIP: fixing link default import issue
BhumikP Mar 3, 2025
db65c8d
Merge branch 'develop' into ts-config/exact-optional-type
justlevine Mar 4, 2025
7d15ba1
fix: Use generics to overcome next/link props issue
BhumikP Mar 4, 2025
a263997
refactor: Implement PR changes for type fixes
BhumikP Mar 5, 2025
c234eff
Merge branch 'develop' into ts-config/exact-optional-type
BhumikP Mar 11, 2025
fc6db14
fix: add undefined for our types
BhumikP Mar 11, 2025
a23df92
refactor: change props passing for our components
BhumikP Mar 14, 2025
90132f4
refactor: add style object changes
BhumikP Mar 17, 2025
b5be54b
Merge branch 'develop' into ts-config/exact-optional-type
BhumikP Mar 17, 2025
fcd9a3e
Merge branch 'develop' into ts-config/exact-optional-type
justlevine Mar 18, 2025
8ad82fc
chore: cleanup [first pass]
justlevine Mar 18, 2025
16e3741
dev: relocate `TemplateHeadProps` to `TemplateHead` component
justlevine Mar 18, 2025
1a9b53a
tests: update tests
justlevine Mar 18, 2025
b018ffc
chore: add changesets
BhumikP Mar 19, 2025
377f636
Update .changeset/tender-jobs-itch.md
justlevine Mar 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/tender-jobs-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@snapwp/blocks": patch
"@snapwp/query": patch
"@snapwp/types": patch
"@snapwp/core": patch
"@snapwp/next": patch
---

refactor: Enforce `exactOptionalPropertyType` TypeScript rule
2 changes: 1 addition & 1 deletion config/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"allowSyntheticDefaultImports": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"exactOptionalPropertyTypes": false, // @todo Enable this.
"exactOptionalPropertyTypes": true,
"noFallthroughCasesInSwitch": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
Expand Down
16 changes: 14 additions & 2 deletions packages/blocks/src/block-manager/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ export default class BlockManager {
* @return A tree of blocks.
*/
public static flatListToHierarchical(
blockList?: BlockData[] | null
blockList?: BlockData[] | null | undefined
): BlockTreeNode[] {
if ( ! blockList ) {
return [];
}

return flatListToHierarchical(
blockList as unknown as Record< string | number, unknown >[],
{
Expand Down Expand Up @@ -75,10 +79,18 @@ export default class BlockManager {
* @return A tree of blocks with render functions.
*/
public static parseBlockForRendering(
blockList?: BlockData[] | null
blockList?: BlockData[] | null | undefined
): BlockTreeNode[] {
if ( ! blockList ) {
return [];
}

const BlockTreeNodeArray = this.flatListToHierarchical( blockList );

if ( ! BlockTreeNodeArray ) {
return [];
}

BlockTreeNodeArray.forEach( this.attachRendererToTreeNode );

return BlockTreeNodeArray;
Expand Down
5 changes: 4 additions & 1 deletion packages/blocks/src/blocks/core-audio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ const CoreAudio: CoreAudioType = ( {
const classNames = cn( classNamesFromString );

return (
<figure className={ classNames } style={ styleObject }>
<figure
className={ classNames }
{ ...( styleObject && { style: styleObject } ) }
>
<audio
controls
src={ src }
Expand Down
7 changes: 4 additions & 3 deletions packages/blocks/src/blocks/core-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ const CoreButton: CoreButtonType = ( { attributes }: CoreButtonProps ) => {

const classNames = cn( cssClassName );
const styleObject = getStylesFromAttributes( { style } );

const { homeUrl, nextUrl } = getConfig();

const commonProps = {
className: linkClassName ?? undefined,
style: styleObject,
Expand All @@ -56,12 +56,13 @@ const CoreButton: CoreButtonType = ( { attributes }: CoreButtonProps ) => {

if ( url ) {
const href = replaceHostUrl( url, homeUrl, nextUrl );

return (
<div className={ classNames }>
<Link
href={ href }
target={ linkTarget ?? undefined }
rel={ rel ?? undefined }
target={ linkTarget }
rel={ rel }
{ ...commonProps }
>
{ !! text && <Parse html={ text } /> }
Expand Down
5 changes: 4 additions & 1 deletion packages/blocks/src/blocks/core-buttons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ const CoreButtons: CoreButtonsType = ( {
const styleObject = getStylesFromAttributes( { style } );

return (
<div className={ classNames } style={ styleObject }>
<div
className={ classNames }
{ ...( styleObject && { style: styleObject } ) }
>
{ children }
</div>
);
Expand Down
6 changes: 5 additions & 1 deletion packages/blocks/src/blocks/core-code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ import type { CoreCode as CoreCodeType, CoreCodeProps } from '@snapwp/types';
*/
const CoreCode: CoreCodeType = ( { attributes }: CoreCodeProps ) => {
const { style, cssClassName, content } = attributes || {};

const styleObject = getStylesFromAttributes( { style } );

return (
<pre className={ cssClassName || '' } style={ styleObject }>
<pre
{ ...( cssClassName && { className: cssClassName } ) }
{ ...( styleObject && { style: styleObject } ) }
>
<code>{ !! content && <Parse html={ content } /> }</code>
</pre>
);
Expand Down
3 changes: 2 additions & 1 deletion packages/blocks/src/blocks/core-column.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
CoreColumn as CoreColumnType,
CoreColumnProps,
} from '@snapwp/types';
import type { CSSProperties } from 'react';

/**
* Renders the core/column block.
Expand All @@ -22,7 +23,7 @@ const CoreColumn: CoreColumnType = ( {
const classNames = cn( cssClassName );
const styleObject = getStylesFromAttributes( { style } );

const combinedStyles: React.CSSProperties = {
const combinedStyles: CSSProperties = {
...styleObject,
flexBasis: width ?? undefined,
};
Expand Down
5 changes: 4 additions & 1 deletion packages/blocks/src/blocks/core-columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ const CoreColumns: CoreColumnsType = ( {
const classNames = cn( cssClassName );

return (
<div className={ classNames } style={ styleObject }>
<div
className={ classNames }
{ ...( styleObject && { style: styleObject } ) }
>
{ children }
</div>
);
Expand Down
4 changes: 3 additions & 1 deletion packages/blocks/src/blocks/core-cover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const DEFAULT_FOCAL_POINT = { x: 0.5, y: 0.5 };
*
* @return CSS position string or undefined if no focal point
*/
const mediaPosition = ( focalPoint?: FocalPoint | null ): string => {
const mediaPosition = (
focalPoint?: FocalPoint | null | undefined
): string => {
if ( ! focalPoint ) {
focalPoint = DEFAULT_FOCAL_POINT;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/blocks/core-details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ const CoreDetails: CoreDetailsType = ( {
return (
<details
className={ classNames }
style={ styleObject }
open={ showContent }
{ ...( styleObject && { style: styleObject } ) }
>
<summary>{ summary }</summary>
{ children }
Expand Down
9 changes: 6 additions & 3 deletions packages/blocks/src/blocks/core-file.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ const CoreFile: CoreFileType = ( {
const downloadText = downloadButtonText || FALLBACK_DOWNLOAD_BUTTON_TEXT;

return (
<div className={ classNames } style={ styleObject }>
<div
className={ classNames }
{ ...( styleObject && { style: styleObject } ) }
>
{ displayPreview && (
<object
className="wp-block-file__embed"
Expand All @@ -65,9 +68,9 @@ const CoreFile: CoreFileType = ( {
) }
{ fileName && textLinkHref && (
<Link
id={ fileId || undefined }
id={ fileId }
href={ textLinkHref }
target={ textLinkTarget || undefined }
target={ textLinkTarget }
rel={ textLinkTarget ? 'noreferrer noopener' : undefined }
>
{ !! fileName && <Parse html={ fileName } /> }
Expand Down
5 changes: 4 additions & 1 deletion packages/blocks/src/blocks/core-gallery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ const CoreGallery: CoreGalleryType = ( {
const className = cn( classNamesFromString ) || undefined;

return (
<figure className={ className } style={ styleObject }>
<figure
className={ className }
{ ...( styleObject && { style: styleObject } ) }
>
{ children }
{ caption && (
<figcaption className="wp-element-caption">
Expand Down
29 changes: 21 additions & 8 deletions packages/blocks/src/blocks/core-group.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { createElement } from 'react';
import {
createElement,
type CSSProperties,
type PropsWithChildren,
} from 'react';
import {
cn,
getClassNamesFromString,
getStylesFromAttributes,
} from '@snapwp/core';
import type {
CoreGroup as CoreGroupType,
CoreGroupProps,
TagProps,
} from '@snapwp/types';
import type { CoreGroup as CoreGroupType, CoreGroupProps } from '@snapwp/types';

/**
* Renders an HTML element with the specified tag name.
Expand All @@ -21,7 +21,16 @@ import type {
*
* @return The rendered HTML element or the children if no tag name is provided.
*/
const Tag = ( { name, className, style, children }: TagProps ) => {
const Tag = ( {
name,
className,
style,
children,
}: PropsWithChildren< {
name?: string;
className: string;
style?: CSSProperties;
} > ) => {
if ( ! name ) {
return <>{ children }</>;
}
Expand Down Expand Up @@ -59,7 +68,11 @@ const CoreGroup: CoreGroupType = ( {
const tag = tagName || 'div';

return (
<Tag name={ tag } className={ classNames } style={ styleObject }>
<Tag
name={ tag }
className={ classNames }
{ ...( !! styleObject && { style: styleObject } ) }
>
{ children }
</Tag>
);
Expand Down
4 changes: 2 additions & 2 deletions packages/blocks/src/blocks/core-heading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const CoreHeading: CoreHeadingType = ( { attributes }: CoreHeadingProps ) => {

return (
<HeadingTag
style={ styleObject }
className={ cssClassName || undefined }
className={ cssClassName }
{ ...( styleObject && { style: styleObject } ) }
>
{ !! content && <Parse html={ content } /> }
</HeadingTag>
Expand Down
8 changes: 4 additions & 4 deletions packages/blocks/src/blocks/core-image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const Figure = ( {
href={ href }
className={ linkClass }
target={ linkTarget }
{ ...( rel && { rel } ) }
rel={ rel }
>
{ children }
</Link>
Expand Down Expand Up @@ -198,9 +198,9 @@ const getImageProps = (
};

const imageProps: ComponentProps< typeof Image > = {
src: url,
alt,
title,
alt,
src: url,
};

if ( connectedMediaItem?.node ) {
Expand Down Expand Up @@ -269,7 +269,7 @@ const isLightboxEnabled = ( lightbox?: string | null ) => {
*/
const extractInteractivityAttributesForElement = (
element: string,
renderedHtml?: string | null
renderedHtml?: string | null | undefined
) => {
if ( ! renderedHtml ) {
return {};
Expand Down
5 changes: 4 additions & 1 deletion packages/blocks/src/blocks/core-list-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ const CoreListItem: CoreListItemType = ( {
const firstLineContent = content ? content.split( '\n' )[ 0 ] : undefined;

return (
<li className={ classNames || undefined } style={ styleObject }>
<li
className={ classNames || undefined }
{ ...( styleObject && { style: styleObject } ) }
>
{ firstLineContent && <Parse html={ firstLineContent } /> }
{ children }
</li>
Expand Down
3 changes: 2 additions & 1 deletion packages/blocks/src/blocks/core-list.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { cn, getStylesFromAttributes } from '@snapwp/core';
import type { CoreList as CoreListType, CoreListProps } from '@snapwp/types';
import type { CSSProperties } from 'react';

/**
* Renders the core/list block.
Expand All @@ -20,7 +21,7 @@ const CoreList: CoreListType = ( { attributes, children }: CoreListProps ) => {

const styleObject = getStylesFromAttributes( { style } );

const combinedStyles: React.CSSProperties = {
const combinedStyles: CSSProperties = {
...styleObject,
listStyleType: type ?? undefined,
};
Expand Down
6 changes: 4 additions & 2 deletions packages/blocks/src/blocks/core-paragraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const CoreParagraph: CoreParagraphType = ( {
textColor,
} = attributes || {};

const styleObject = getStylesFromAttributes( { style } );
const styleObject = getStylesFromAttributes( { style } ) || {};

/**
* Add missing styles to the style object
Expand All @@ -50,8 +50,10 @@ const CoreParagraph: CoreParagraphType = ( {
return (
<p
className={ cssClassName || undefined }
style={ styleObject }
dir={ direction || undefined }
{ ...( Object.keys( styleObject ).length > 0 && {
style: styleObject,
} ) }
>
{ !! content && <Parse html={ content } /> }
</p>
Expand Down
5 changes: 4 additions & 1 deletion packages/blocks/src/blocks/core-preformatted.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ const CorePreformatted: CorePreformattedType = ( {
const classNames = cn( classNamesFromString );

return (
<pre style={ styleObject } className={ classNames }>
<pre
className={ classNames }
{ ...( styleObject && { style: styleObject } ) }
>
{ !! content && <Parse html={ content } /> }
</pre>
);
Expand Down
5 changes: 4 additions & 1 deletion packages/blocks/src/blocks/core-pullquote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ const CorePullquote: CorePullquoteType = ( {
const classNames = cn( classNamesFromString );

return (
<figure style={ styleObject } className={ classNames }>
<figure
className={ classNames }
{ ...( styleObject && { style: styleObject } ) }
>
<blockquote>
{ pullquoteValue && (
<p>
Expand Down
5 changes: 4 additions & 1 deletion packages/blocks/src/blocks/core-quote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ const CoreQuote: CoreQuoteType = ( {
const styleObject = getStylesFromAttributes( { style } );

return (
<blockquote className={ cssClassName || '' } style={ styleObject }>
<blockquote
className={ cssClassName || undefined }
{ ...( styleObject && { style: styleObject } ) }
>
{ children }

{ !! citation && (
Expand Down
5 changes: 4 additions & 1 deletion packages/blocks/src/blocks/core-verse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ const CoreVerse: CoreVerseType = ( {
const classNames = cn( classNamesFromString );

return (
<pre className={ classNames } style={ styleObject }>
<pre
className={ classNames }
{ ...( styleObject && { style: styleObject } ) }
>
{ !! content && <Parse html={ content } /> }
</pre>
);
Expand Down
Loading