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

Block Editor: Warn when useBlockProps hook used with a wrong Block API version #33274

Merged
merged 2 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/block-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@wordpress/shortcode": "file:../shortcode",
"@wordpress/token-list": "file:../token-list",
"@wordpress/url": "file:../url",
"@wordpress/warning": "file:../warning",
"@wordpress/wordcount": "file:../wordcount",
"classnames": "^2.2.5",
"css-mediaquery": "^0.1.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '@wordpress/blocks';
import { useMergeRefs } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';
import warning from '@wordpress/warning';

/**
* Internal dependencies
Expand Down Expand Up @@ -65,6 +66,7 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
index,
mode,
name,
blockAPIVersion,
blockTitle,
isPartOfSelection,
adjustScrolling,
Expand All @@ -89,11 +91,13 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
isAncestorMultiSelected( clientId );
const blockName = getBlockName( clientId );
const rootClientId = getBlockRootClientId( clientId );
const blockType = getBlockType( blockName );
return {
index: getBlockIndex( clientId, rootClientId ),
mode: getBlockMode( clientId ),
name: blockName,
blockTitle: getBlockType( blockName ).title,
blockAPIVersion: blockType.apiVersion,
blockTitle: blockType.title,
isPartOfSelection: isSelected || isPartOfMultiSelection,
adjustScrolling:
isSelected || isFirstMultiSelectedBlock( clientId ),
Expand Down Expand Up @@ -128,6 +132,12 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
} ),
] );

if ( blockAPIVersion < 2 ) {
warning(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we error or warn?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning is our custom helper that works nearly the same as React warnings that get applied only for development builds.

`Block type "${ name }" must support API version 2 or higher to work correctly with "useBlockProps" method.`
);
}

return {
...wrapperProps,
...props,
Expand Down
4 changes: 4 additions & 0 deletions packages/warning/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Enhancement

- Ensure that the warning for a given message is logged only once.

## 2.1.0 (2021-05-20)

## 2.0.0 (2021-05-14)
Expand Down
12 changes: 12 additions & 0 deletions packages/warning/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* Internal dependencies
*/
import { logged } from './utils';

function isDev() {
return (
typeof process !== 'undefined' &&
Expand Down Expand Up @@ -28,6 +33,11 @@ export default function warning( message ) {
return;
}

// Skip if already logged.
if ( message in logged ) {
return;
}

// eslint-disable-next-line no-console
console.warn( message );

Expand All @@ -39,4 +49,6 @@ export default function warning( message ) {
} catch ( x ) {
// do nothing
}

logged[ message ] = true;
}
13 changes: 13 additions & 0 deletions packages/warning/src/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
* Internal dependencies
*/
import warning from '..';
import { logged } from '../utils';

const initialNodeEnv = process.env.NODE_ENV;

describe( 'warning', () => {
afterEach( () => {
process.env.NODE_ENV = initialNodeEnv;
for ( const key in logged ) {
delete logged[ key ];
}
} );

it( 'logs to console.warn when NODE_ENV is not "production"', () => {
Expand All @@ -21,4 +25,13 @@ describe( 'warning', () => {
warning( 'warning' );
expect( console ).not.toHaveWarned();
} );

it( 'should show a message once', () => {
warning( 'warning' );
warning( 'warning' );

expect( console ).toHaveWarned();
// eslint-disable-next-line no-console
expect( console.warn ).toHaveBeenCalledTimes( 1 );
} );
} );
7 changes: 7 additions & 0 deletions packages/warning/src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Object map tracking messages which have been logged, for use in ensuring a
* message is only logged once.
*
* @type {Record<string, true | undefined>}
*/
export const logged = Object.create( null );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it Object.create( null ) instead of just {} to avoid prototype pollution? Very clever! 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I copied it over from @wordpress/deprecated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we be better off with Set?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, we could use Set 👍🏻

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#33380 should address it.