Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
Bug/24286 create block error #24287
Bug/24286 create block error #24287
Conversation
*/ | ||
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; |
youknowriad
Jul 30, 2020
•
Contributor
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
.
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
.
ryanwelcher
Jul 30, 2020
Author
Contributor
@youknowriad thanks for the review here! I have updated the PR to throw an exception.
@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 ) { |
youknowriad
Jul 30, 2020
Contributor
Not important but yoda conditions are not a guideline in JavaScript because we enforce strict equality.
Not important but yoda conditions are not a guideline in JavaScript because we enforce strict equality.
Thanks for your PR. The error message is way clearer this way. |
90df863
into
WordPress:master
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
Types of changes
This introduces a warning() call and an early return if the blockType is not registered
Checklist: