From 4843ff778921f76f0034961253d6a19654182f2d Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Thu, 30 Jul 2020 08:55:28 -0400 Subject: [PATCH 1/6] Add warning to createBlock. --- packages/blocks/README.md | 2 +- packages/blocks/package.json | 1 + packages/blocks/src/api/factory.js | 8 +++++++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/blocks/README.md b/packages/blocks/README.md index 27d2ea7140045..02956918791b2 100644 --- a/packages/blocks/README.md +++ b/packages/blocks/README.md @@ -203,7 +203,7 @@ _Parameters_ _Returns_ -- `Object`: Block object. +- `(Object|boolean)`: Block object or false if the block name is not a registered block. # **doBlocksMatchTemplate** diff --git a/packages/blocks/package.json b/packages/blocks/package.json index d226f5bf0e29a..a5c4122495194 100644 --- a/packages/blocks/package.json +++ b/packages/blocks/package.json @@ -39,6 +39,7 @@ "@wordpress/icons": "file:../icons", "@wordpress/is-shallow-equal": "file:../is-shallow-equal", "@wordpress/shortcode": "file:../shortcode", + "@wordpress/warning": "file:../warning", "hpq": "^1.3.0", "lodash": "^4.17.15", "rememo": "^3.0.0", diff --git a/packages/blocks/src/api/factory.js b/packages/blocks/src/api/factory.js index e72c903a3af1b..cd4c219b5cab4 100644 --- a/packages/blocks/src/api/factory.js +++ b/packages/blocks/src/api/factory.js @@ -22,6 +22,7 @@ import { * WordPress dependencies */ import { createHooks, applyFilters } from '@wordpress/hooks'; +import warning from '@wordpress/warning'; /** * Internal dependencies @@ -40,12 +41,17 @@ import { normalizeBlockType } from './utils'; * @param {Object} attributes Block attributes. * @param {?Array} innerBlocks Nested blocks. * - * @return {Object} Block object. + * @return {(Object|boolean)} Block object or false if the block name is not a registered block. */ 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; + } + // Ensure attributes contains only values defined by block type, and merge // default values for missing attributes. const sanitizedAttributes = reduce( From 937de915b47207a2b1263dece27ed7fba1bc6dfc Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Thu, 30 Jul 2020 09:03:01 -0400 Subject: [PATCH 2/6] Add tests for new change. --- packages/blocks/src/api/test/factory.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/blocks/src/api/test/factory.js b/packages/blocks/src/api/test/factory.js index 8b37d9349bd21..cadd8f0c46fc8 100644 --- a/packages/blocks/src/api/test/factory.js +++ b/packages/blocks/src/api/test/factory.js @@ -49,6 +49,11 @@ describe( 'block factory', () => { } ); describe( 'createBlock()', () => { + it( 'should trigger a warning and return false if the blockType is not registered', () => { + const registerAttempt = registerBlockType( 'not/registered' ); + expect( console ).toHaveWarnedWith( 'warning' ); + expect( registerAttempt ).toBe( false ); + } ); it( 'should create a block given its blockType, attributes, inner blocks', () => { registerBlockType( 'core/test-block', { attributes: { From b1dad82317ba06858d30fa23b2fcbc3209d8e918 Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Thu, 30 Jul 2020 09:20:16 -0400 Subject: [PATCH 3/6] Update tests to actually work. --- packages/blocks/src/api/test/factory.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/blocks/src/api/test/factory.js b/packages/blocks/src/api/test/factory.js index cadd8f0c46fc8..e81aca6a298d7 100644 --- a/packages/blocks/src/api/test/factory.js +++ b/packages/blocks/src/api/test/factory.js @@ -50,9 +50,12 @@ describe( 'block factory', () => { describe( 'createBlock()', () => { it( 'should trigger a warning and return false if the blockType is not registered', () => { - const registerAttempt = registerBlockType( 'not/registered' ); - expect( console ).toHaveWarnedWith( 'warning' ); - expect( registerAttempt ).toBe( false ); + const name = 'not/registered'; + const createAttempt = createBlock( name ); + expect( console ).toHaveWarnedWith( + `Block type '${ name }' is not registered.` + ); + expect( createAttempt ).toBe( false ); } ); it( 'should create a block given its blockType, attributes, inner blocks', () => { registerBlockType( 'core/test-block', { From 594b9c498d55d36b9f811729161217495818cb83 Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Thu, 30 Jul 2020 10:01:27 -0400 Subject: [PATCH 4/6] Add changes to package-lock.json --- package-lock.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package-lock.json b/package-lock.json index 7524389f35be7..347e104d39be4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11077,6 +11077,7 @@ "@wordpress/icons": "file:packages/icons", "@wordpress/is-shallow-equal": "file:packages/is-shallow-equal", "@wordpress/shortcode": "file:packages/shortcode", + "@wordpress/warning": "file:packages/warning", "hpq": "^1.3.0", "lodash": "^4.17.15", "rememo": "^3.0.0", From d1252513c8db75fdb94e2af2708c7df8745f542c Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Thu, 30 Jul 2020 10:32:38 -0400 Subject: [PATCH 5/6] Throw an exception instead of a warning. --- package-lock.json | 1 - packages/blocks/package.json | 1 - packages/blocks/src/api/factory.js | 4 +--- packages/blocks/src/api/test/factory.js | 8 -------- 4 files changed, 1 insertion(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index 347e104d39be4..7524389f35be7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11077,7 +11077,6 @@ "@wordpress/icons": "file:packages/icons", "@wordpress/is-shallow-equal": "file:packages/is-shallow-equal", "@wordpress/shortcode": "file:packages/shortcode", - "@wordpress/warning": "file:packages/warning", "hpq": "^1.3.0", "lodash": "^4.17.15", "rememo": "^3.0.0", diff --git a/packages/blocks/package.json b/packages/blocks/package.json index a5c4122495194..d226f5bf0e29a 100644 --- a/packages/blocks/package.json +++ b/packages/blocks/package.json @@ -39,7 +39,6 @@ "@wordpress/icons": "file:../icons", "@wordpress/is-shallow-equal": "file:../is-shallow-equal", "@wordpress/shortcode": "file:../shortcode", - "@wordpress/warning": "file:../warning", "hpq": "^1.3.0", "lodash": "^4.17.15", "rememo": "^3.0.0", diff --git a/packages/blocks/src/api/factory.js b/packages/blocks/src/api/factory.js index cd4c219b5cab4..c929dee493112 100644 --- a/packages/blocks/src/api/factory.js +++ b/packages/blocks/src/api/factory.js @@ -22,7 +22,6 @@ import { * WordPress dependencies */ import { createHooks, applyFilters } from '@wordpress/hooks'; -import warning from '@wordpress/warning'; /** * Internal dependencies @@ -48,8 +47,7 @@ export function createBlock( name, attributes = {}, innerBlocks = [] ) { const blockType = getBlockType( name ); if ( undefined === blockType ) { - warning( `Block type '${ name }' is not registered.` ); - return false; + throw new Error( `Block type '${ name }' is not registered.` ); } // Ensure attributes contains only values defined by block type, and merge diff --git a/packages/blocks/src/api/test/factory.js b/packages/blocks/src/api/test/factory.js index e81aca6a298d7..8b37d9349bd21 100644 --- a/packages/blocks/src/api/test/factory.js +++ b/packages/blocks/src/api/test/factory.js @@ -49,14 +49,6 @@ describe( 'block factory', () => { } ); describe( 'createBlock()', () => { - it( 'should trigger a warning and return false if the blockType is not registered', () => { - const name = 'not/registered'; - const createAttempt = createBlock( name ); - expect( console ).toHaveWarnedWith( - `Block type '${ name }' is not registered.` - ); - expect( createAttempt ).toBe( false ); - } ); it( 'should create a block given its blockType, attributes, inner blocks', () => { registerBlockType( 'core/test-block', { attributes: { From 1e68ce0937b9ff8168d68126b9c1cc9c3f173383 Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Thu, 30 Jul 2020 10:34:48 -0400 Subject: [PATCH 6/6] Update docs. --- packages/blocks/README.md | 2 +- packages/blocks/src/api/factory.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/blocks/README.md b/packages/blocks/README.md index 02956918791b2..27d2ea7140045 100644 --- a/packages/blocks/README.md +++ b/packages/blocks/README.md @@ -203,7 +203,7 @@ _Parameters_ _Returns_ -- `(Object|boolean)`: Block object or false if the block name is not a registered block. +- `Object`: Block object. # **doBlocksMatchTemplate** diff --git a/packages/blocks/src/api/factory.js b/packages/blocks/src/api/factory.js index c929dee493112..d87d16cafd026 100644 --- a/packages/blocks/src/api/factory.js +++ b/packages/blocks/src/api/factory.js @@ -40,7 +40,7 @@ import { normalizeBlockType } from './utils'; * @param {Object} attributes Block attributes. * @param {?Array} innerBlocks Nested blocks. * - * @return {(Object|boolean)} Block object or false if the block name is not a registered block. + * @return {Object} Block object. */ export function createBlock( name, attributes = {}, innerBlocks = [] ) { // Get the type definition associated with a registered block.