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

Bug/24286 create block error #24287

Merged
merged 10 commits into from
Jul 30, 2020

Conversation

ryanwelcher
Copy link
Contributor

@ryanwelcher ryanwelcher commented Jul 30, 2020

closes #24286

Description

This PR implements the proposed solution indicated in #24286

How has this been tested?

Running `wp.blocks.createBlock('not/registered') in the console.

Screenshots

create-block-warning

Types of changes

This introduces a warning() call and an early return if the blockType is not registered

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

*/
export function createBlock( name, attributes = {}, innerBlocks = [] ) {
// Get the type definition associated with a registered block.
const blockType = getBlockType( name );

if ( undefined === blockType ) {
warning( `Block type '${ name }' is not registered.` );
return false;
Copy link
Contributor

@youknowriad youknowriad Jul 30, 2020

Choose a reason for hiding this comment

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

Making this non-blocking changes the expectation from someone calling this function.

We do things like that in general in Gutenberg configs.map( config => createBlock( config.name, config.attributes, config.innerBlocks )

With the current implementation, this will hide bugs and just move them down the road. IMO, we should throw an exception here instead of calling warning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@youknowriad thanks for the review here! I have updated the PR to throw an exception.

@@ -46,6 +46,10 @@ export function createBlock( name, attributes = {}, innerBlocks = [] ) {
// Get the type definition associated with a registered block.
const blockType = getBlockType( name );

if ( undefined === blockType ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not important but yoda conditions are not a guideline in JavaScript because we enforce strict equality.

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

Thanks for your PR. The error message is way clearer this way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

createBlock fires error if name doesn't match a registered block type.
2 participants