Skip to content

Commit

Permalink
Use @wordpress/warning during block registration instead of `consol…
Browse files Browse the repository at this point in the history
…e.error` and `console.warn` (WordPress#63610)

* Use `@wordpress/warning` in block registration

* Clear logged set

Co-authored-by: SantosGuillamot <[email protected]>
Co-authored-by: gziolo <[email protected]>
  • Loading branch information
3 people authored and carstingaxion committed Jul 18, 2024
1 parent 7f5741c commit 775bd79
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 69 deletions.
2 changes: 2 additions & 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/blocks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@wordpress/private-apis": "file:../private-apis",
"@wordpress/rich-text": "file:../rich-text",
"@wordpress/shortcode": "file:../shortcode",
"@wordpress/warning": "file:../warning",
"change-case": "^4.1.2",
"colord": "^2.7.0",
"fast-deep-equal": "^3.1.3",
Expand Down
47 changes: 20 additions & 27 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint no-console: [ 'error', { allow: [ 'error', 'warn' ] } ] */

/**
* WordPress dependencies
*/
import { select, dispatch } from '@wordpress/data';
import { _x } from '@wordpress/i18n';
import warning from '@wordpress/warning';

/**
* Internal dependencies
Expand Down Expand Up @@ -225,18 +224,18 @@ export function registerBlockType( blockNameOrMetadata, settings ) {
: blockNameOrMetadata;

if ( typeof name !== 'string' ) {
console.error( 'Block names must be strings.' );
warning( 'Block names must be strings.' );
return;
}

if ( ! /^[a-z][a-z0-9-]*\/[a-z][a-z0-9-]*$/.test( name ) ) {
console.error(
warning(
'Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-block'
);
return;
}
if ( select( blocksStore ).getBlockType( name ) ) {
console.error( 'Block "' + name + '" is already registered.' );
warning( 'Block "' + name + '" is already registered.' );
return;
}

Expand Down Expand Up @@ -381,7 +380,7 @@ export function unregisterBlockCollection( namespace ) {
export function unregisterBlockType( name ) {
const oldBlock = select( blocksStore ).getBlockType( name );
if ( ! oldBlock ) {
console.error( 'Block "' + name + '" is not registered.' );
warning( 'Block "' + name + '" is not registered.' );
return;
}
dispatch( blocksStore ).removeBlockTypes( name );
Expand Down Expand Up @@ -724,7 +723,7 @@ export const getBlockVariations = ( blockName, scope ) => {
*/
export const registerBlockVariation = ( blockName, variation ) => {
if ( typeof variation.name !== 'string' ) {
console.warn( 'Variation names must be unique strings.' );
warning( 'Variation names must be unique strings.' );
}

dispatch( blocksStore ).addBlockVariations( blockName, variation );
Expand Down Expand Up @@ -805,86 +804,82 @@ export const registerBlockBindingsSource = ( source ) => {
select( blocksStore )
).getBlockBindingsSource( name );
if ( existingSource ) {
console.error(
warning(
'Block bindings source "' + name + '" is already registered.'
);
return;
}

// Check the `name` property is correct.
if ( ! name ) {
console.error( 'Block bindings source must contain a name.' );
warning( 'Block bindings source must contain a name.' );
return;
}

if ( typeof name !== 'string' ) {
console.error( 'Block bindings source name must be a string.' );
warning( 'Block bindings source name must be a string.' );
return;
}

if ( /[A-Z]+/.test( name ) ) {
console.error(
warning(
'Block bindings source name must not contain uppercase characters.'
);
return;
}

if ( ! /^[a-z0-9/-]+$/.test( name ) ) {
console.error(
warning(
'Block bindings source name must contain only valid characters: lowercase characters, hyphens, or digits. Example: my-plugin/my-custom-source.'
);
return;
}

if ( ! /^[a-z0-9-]+\/[a-z0-9-]+$/.test( name ) ) {
console.error(
warning(
'Block bindings source name must contain a namespace and valid characters. Example: my-plugin/my-custom-source.'
);
return;
}

// Check the `label` property is correct.
if ( ! label ) {
console.error( 'Block bindings source must contain a label.' );
warning( 'Block bindings source must contain a label.' );
return;
}

if ( typeof label !== 'string' ) {
console.error( 'Block bindings source label must be a string.' );
warning( 'Block bindings source label must be a string.' );
return;
}

// Check the `getValue` property is correct.
if ( getValue && typeof getValue !== 'function' ) {
console.error( 'Block bindings source getValue must be a function.' );
warning( 'Block bindings source getValue must be a function.' );
return;
}

// Check the `setValue` property is correct.
if ( setValue && typeof setValue !== 'function' ) {
console.error( 'Block bindings source setValue must be a function.' );
warning( 'Block bindings source setValue must be a function.' );
return;
}

// Check the `setValues` property is correct.
if ( setValues && typeof setValues !== 'function' ) {
console.error( 'Block bindings source setValues must be a function.' );
warning( 'Block bindings source setValues must be a function.' );
return;
}

// Check the `getPlaceholder` property is correct.
if ( getPlaceholder && typeof getPlaceholder !== 'function' ) {
console.error(
'Block bindings source getPlaceholder must be a function.'
);
warning( 'Block bindings source getPlaceholder must be a function.' );
return;
}

// Check the `getPlaceholder` property is correct.
if ( canUserEditValue && typeof canUserEditValue !== 'function' ) {
console.error(
'Block bindings source canUserEditValue must be a function.'
);
warning( 'Block bindings source canUserEditValue must be a function.' );
return;
}

Expand All @@ -906,9 +901,7 @@ export const registerBlockBindingsSource = ( source ) => {
export function unregisterBlockBindingsSource( name ) {
const oldSource = getBlockBindingsSource( name );
if ( ! oldSource ) {
console.error(
'Block bindings source "' + name + '" is not registered.'
);
warning( 'Block bindings source "' + name + '" is not registered.' );
return;
}
unlock( dispatch( blocksStore ) ).removeBlockBindingsSource( name );
Expand Down
Loading

0 comments on commit 775bd79

Please sign in to comment.