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 3 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
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
2 changes: 1 addition & 1 deletion packages/blocks/src/blocks/core-audio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const CoreAudio: CoreAudioType = ( {
return null;
}

const styleObject = getStylesFromAttributes( { style } );
const styleObject = style ? getStylesFromAttributes( { style } ) : {};

/**
* @todo replace with cssClassName once it's supported.
Expand Down
4 changes: 2 additions & 2 deletions packages/blocks/src/blocks/core-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const CoreButton: CoreButtonType = ( { attributes }: CoreButtonProps ) => {
} = attributes || {};

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

const { homeUrl, nextUrl } = getConfig();
const commonProps = {
Expand Down Expand Up @@ -60,7 +60,7 @@ const CoreButton: CoreButtonType = ( { attributes }: CoreButtonProps ) => {
<div className={ classNames }>
<Link
href={ href }
target={ linkTarget ?? undefined }
{ ...( linkTarget ? { target: linkTarget } : {} ) }
rel={ rel ?? undefined }
{ ...commonProps }
>
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/blocks/core-buttons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const CoreButtons: CoreButtonsType = ( {
const { cssClassName, style } = attributes || {};
const classNames = cn( cssClassName );

const styleObject = getStylesFromAttributes( { style } );
const styleObject = style ? getStylesFromAttributes( { style } ) : {};

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

return (
<pre className={ cssClassName || '' } style={ styleObject }>
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/blocks/core-column.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const CoreColumn: CoreColumnType = ( {
const { cssClassName, style, width } = attributes || {};

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

const combinedStyles: React.CSSProperties = {
...styleObject,
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/blocks/core-columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const CoreColumns: CoreColumnsType = ( {
}: CoreColumnsProps ) => {
const { cssClassName, style } = attributes || {};

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

return (
Expand Down
6 changes: 4 additions & 2 deletions packages/blocks/src/blocks/core-cover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const CoreCover: CoreCoverType = ( {
const isImgElement = ! ( hasParallax || isRepeated );

const styleObject = {
...getStylesFromAttributes( { style } ),
...( style ? getStylesFromAttributes( { style } ) : {} ),
minHeight: minHeight || undefined,
};

Expand Down Expand Up @@ -146,11 +146,13 @@ const CoreCover: CoreCoverType = ( {
<Image
{ ...imageProps }
className={ imgClasses }
alt={ alt }
src={ url }
style={ { objectPosition } }
data-object-fit="cover"
data-object-position={ objectPosition }
{ ...( imageProps?.alt
? { alt: imageProps.alt }
: {} ) }
/>
) : (
<div
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 @@ -25,7 +25,7 @@ const CoreDetails: CoreDetailsType = ( {
renderedHtml,
}: CoreDetailsProps ) => {
const { style, showContent, summary } = attributes ?? {};
const styleObject = getStylesFromAttributes( { style } );
const styleObject = style ? getStylesFromAttributes( { style } ) : {};

/**
* @todo replace with cssClassName once it's supported.
Expand Down
5 changes: 3 additions & 2 deletions packages/blocks/src/blocks/core-file.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const CoreFile: CoreFileType = ( {
: '';
const classNames = cn( classNamesFromString );

const styleObject = getStylesFromAttributes( { style } );
const styleObject = style ? getStylesFromAttributes( { style } ) : {};

const ariaLabel = fileName || FALLBACK_ARIA_LABEL;
const downloadText = downloadButtonText || FALLBACK_DOWNLOAD_BUTTON_TEXT;
Expand All @@ -68,7 +68,7 @@ const CoreFile: CoreFileType = ( {
<Link
id={ fileId || undefined }
href={ textLinkHref }
target={ textLinkTarget || undefined }
{ ...( textLinkTarget ? { target: textLinkTarget } : {} ) }
rel={ textLinkTarget ? 'noreferrer noopener' : undefined }
>
{ !! fileName && <Parse html={ fileName } /> }
Expand All @@ -78,6 +78,7 @@ const CoreFile: CoreFileType = ( {
<Link
href={ href }
className="wp-block-file__button wp-element-button"
//@ts-ignore allowing download prop in link
download
aria-describedby={ fileId || undefined }
>
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/blocks/core-gallery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const CoreGallery: CoreGalleryType = ( {
}: CoreGalleryProps ) => {
const { caption, style } = attributes || {};

const styleObject = getStylesFromAttributes( { style } );
const styleObject = style ? getStylesFromAttributes( { style } ) : {};

/**
* @todo replace with cssClassName once it's supported.
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/blocks/core-group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const CoreGroup: CoreGroupType = ( {
}: CoreGroupProps ) => {
const { style, tagName } = attributes ?? {};

const styleObject = getStylesFromAttributes( { style } );
const styleObject = style ? getStylesFromAttributes( { style } ) : {};

/**
* @todo replace with cssClassName once it's supported.
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/blocks/core-heading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type {
const CoreHeading: CoreHeadingType = ( { attributes }: CoreHeadingProps ) => {
const { style, cssClassName, content, level } = attributes || {};

const styleObject = getStylesFromAttributes( { style } );
const styleObject = style ? getStylesFromAttributes( { style } ) : {};

const HeadingTag = level
? ( `h${ level }` as keyof JSX.IntrinsicElements )
Expand Down
14 changes: 9 additions & 5 deletions packages/blocks/src/blocks/core-image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const Figure = ( {
<Link
href={ href }
className={ linkClass }
target={ linkTarget }
{ ...( linkTarget && { target: linkTarget } ) }
{ ...( rel && { rel } ) }
>
{ children }
Expand Down Expand Up @@ -124,7 +124,7 @@ const CoreImage: CoreImageType = ( {

return (
<Figure
renderedHtml={ renderedHtml }
renderedHtml={ renderedHtml ?? null }
classNames={ classNames }
{ ...attributes }
>
Expand Down Expand Up @@ -189,7 +189,7 @@ const getImageProps = (
sizeSlug,
} = attributes || {};

const styleObject = getStylesFromAttributes( { style } );
const styleObject = style ? getStylesFromAttributes( { style } ) : {};

const imageStyles: CSSProperties = {
...styleObject,
Expand All @@ -198,10 +198,14 @@ const getImageProps = (
};

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

if ( connectedMediaItem?.node ) {
imageProps.sizes = connectedMediaItem.node.sizes;
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/blocks/core-list-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const CoreListItem: CoreListItemType = ( {
}: CoreListItemProps ) => {
const { content, style } = attributes || {};

const styleObject = getStylesFromAttributes( { style } );
const styleObject = style ? getStylesFromAttributes( { style } ) : {};

/**
* @todo replace with cssClassName once it's supported.
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/blocks/core-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const CoreList: CoreListType = ( { attributes, children }: CoreListProps ) => {

const classNames = cn( cssClassName );

const styleObject = getStylesFromAttributes( { style } );
const styleObject = style ? getStylesFromAttributes( { style } ) : {};

const combinedStyles: React.CSSProperties = {
...styleObject,
Expand Down
6 changes: 3 additions & 3 deletions packages/blocks/src/blocks/core-media-text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const CoreMediaText: CoreMediaTextType = ( {
<Image
{ ...imageProps }
src={ mediaUrl }
alt={ mediaAlt }
{ ...( mediaAlt ? { alt: mediaAlt } : {} ) }
className={ imageClasses }
style={ positionStyles }
/>
Expand All @@ -121,7 +121,7 @@ const CoreMediaText: CoreMediaTextType = ( {
<Link
className={ linkClass }
href={ href }
target={ linkTarget }
{ ...( linkTarget ? { target: linkTarget } : {} ) }
rel={ newRel }
>
{ image }
Expand All @@ -138,7 +138,7 @@ const CoreMediaText: CoreMediaTextType = ( {
? getClassNamesFromString( renderedHtml )
: '';

const styleObject = getStylesFromAttributes( { style } );
const styleObject = style ? getStylesFromAttributes( { style } ) : {};

let gridTemplateColumns;
if ( mediaWidth !== DEFAULT_MEDIA_WIDTH ) {
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/blocks/core-paragraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const CoreParagraph: CoreParagraphType = ( {
textColor,
} = attributes || {};

const styleObject = getStylesFromAttributes( { style } );
const styleObject = style ? getStylesFromAttributes( { style } ) : {};

/**
* Add missing styles to the style object
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/blocks/core-preformatted.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const CorePreformatted: CorePreformattedType = ( {
}: CorePreformattedProps ) => {
const { content, style } = attributes || {};

const styleObject = getStylesFromAttributes( { style } );
const styleObject = style ? getStylesFromAttributes( { style } ) : {};

/**
* @todo replace with cssClassName once it's supported.
Expand Down
3 changes: 1 addition & 2 deletions packages/blocks/src/blocks/core-pullquote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ const CorePullquote: CorePullquoteType = ( {
}: CorePullquoteProps ) => {
const { style, pullquoteValue, citation } = attributes || {};

const styleObject = getStylesFromAttributes( { style } );

const styleObject = style ? getStylesFromAttributes( { style } ) : {};
/**
* @todo replace with cssClassName once it's supported.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/blocks/core-quote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const CoreQuote: CoreQuoteType = ( {
}: CoreQuoteProps ) => {
const { style, citation, cssClassName } = attributes || {};

const styleObject = getStylesFromAttributes( { style } );
const styleObject = style ? getStylesFromAttributes( { style } ) : {};

return (
<blockquote className={ cssClassName || '' } style={ styleObject }>
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/blocks/core-spacer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const CoreSpacer: CoreSpacerType = ( {
const finalHeight =
selfStretch === 'fill' || selfStretch === 'fit' ? undefined : height;

const styleObject = getStylesFromAttributes( { style } );
const styleObject = style ? getStylesFromAttributes( { style } ) : {};

const classNamesFromString = renderedHtml
? getClassNamesFromString( renderedHtml )
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/blocks/core-verse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const CoreVerse: CoreVerseType = ( {
}: CoreVerseProps ) => {
const { style, content } = attributes ?? {};

const styleObject = getStylesFromAttributes( { style } );
const styleObject = style ? getStylesFromAttributes( { style } ) : {};

/**
* @todo replace with cssClassName once it's supported.
Expand Down
4 changes: 2 additions & 2 deletions packages/blocks/src/blocks/core-video.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const CoreVideo: CoreVideoType = ( {
return null;
}

const styleObject = getStylesFromAttributes( { style } );
const styleObject = style ? getStylesFromAttributes( { style } ) : {};

/**
* @todo replace with cssClassName once it's supported.
Expand Down Expand Up @@ -110,7 +110,7 @@ const CoreVideo: CoreVideoType = ( {
src={ src }
playsInline={ playsInline || undefined }
>
<Tracks tracks={ formattedTracks } />
<Tracks tracks={ formattedTracks || [] } />
</video>

{ caption && (
Expand Down
1 change: 1 addition & 0 deletions packages/blocks/src/blocks/tests/core-audio.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ describe( 'CoreAudio', () => {
};

const { container, asFragment } = render(
// @ts-ignore to test undefined props
<CoreAudio attributes={ attributesWithoutSrc } renderedHtml="" />
);
expect( container.firstChild ).toBeNull();
Expand Down
1 change: 1 addition & 0 deletions packages/blocks/src/blocks/tests/core-button.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ describe( 'CoreButton Component', () => {

test( 'renders with undefined attributes', () => {
const { asFragment, container } = render(
//@ts-ignore to test undefined props
<CoreButton attributes={ undefined } />
);
const linkElement = container.querySelector( 'a' );
Expand Down
31 changes: 23 additions & 8 deletions packages/core/src/config/snapwp-config-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ type ConfigSchema< T > = {
};
};

/**
* Default configuration.
*/
const defaultConfig: Partial< SnapWPEnv & SnapWPConfig > = {
const DEFAULTS = {
graphqlEndpoint: 'index.php?graphql',
uploadsDirectory: '/wp-content/uploads',
restUrlPrefix: '/wp-json',
Expand All @@ -71,22 +68,40 @@ const defaultConfig: Partial< SnapWPEnv & SnapWPConfig > = {
useCorsProxy: process.env.NODE_ENV === 'development',
};

/**
* Default configuration.
*/
const defaultConfig: Partial< SnapWPEnv & SnapWPConfig > = {
graphqlEndpoint: DEFAULTS.graphqlEndpoint,
uploadsDirectory: DEFAULTS.uploadsDirectory,
restUrlPrefix: DEFAULTS.restUrlPrefix,
corsProxyPrefix: DEFAULTS.corsProxyPrefix,
useCorsProxy: DEFAULTS.useCorsProxy,
};

/**
* Get the configuration from environment variables.
*
* This is a function (instead of a constant) so we can inject the variables in Jest.
*
* @return The configuration object.
*/
//@ts-ignore - ignore check for nextUrl,homeUrl to run missing environment variable test.
const envConfig = (): Partial< SnapWPEnv > => ( {
/* eslint-disable n/no-process-env */
nextUrl: process.env.NEXT_PUBLIC_URL,
homeUrl: process.env.NEXT_PUBLIC_WORDPRESS_URL,
graphqlEndpoint: process.env.NEXT_PUBLIC_GRAPHQL_ENDPOINT,
uploadsDirectory: process.env.NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH,
restUrlPrefix: process.env.NEXT_PUBLIC_WORDPRESS_REST_URL_PREFIX,
graphqlEndpoint:
process.env.NEXT_PUBLIC_GRAPHQL_ENDPOINT || DEFAULTS.graphqlEndpoint,
uploadsDirectory:
process.env.NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH ||
DEFAULTS.uploadsDirectory,
restUrlPrefix:
process.env.NEXT_PUBLIC_WORDPRESS_REST_URL_PREFIX ||
DEFAULTS.restUrlPrefix,
useCorsProxy: process.env.NEXT_PUBLIC_USE_CORS_PROXY === 'true',
corsProxyPrefix: process.env.NEXT_PUBLIC_CORS_PROXY_PREFIX,
corsProxyPrefix:
process.env.NEXT_PUBLIC_CORS_PROXY_PREFIX || DEFAULTS.corsProxyPrefix,
/* eslint-enable n/no-process-env */
} );

Expand Down
Loading