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

Add warning package #19317

Merged
merged 21 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
234e5b6
Add new warning package
diegohaz Dec 24, 2019
6649664
Add babel-plugin-dev-expression
diegohaz Dec 24, 2019
e3ba9d7
Remove babel-plugin-dev-expression
diegohaz Dec 26, 2019
a2732ec
Add babel-plugin to the warning pacakge
diegohaz Dec 26, 2019
93eddbb
Include @wordpress/warning/babel-plugin into @wordpress/babel-preset-…
diegohaz Dec 26, 2019
e357a17
Add warning to webpack.config.js
diegohaz Dec 26, 2019
ff25618
Merge branch 'master' into add/warning
diegohaz Jan 7, 2020
d42843e
Merge branch 'master' into add/warning
diegohaz Jan 9, 2020
be1f921
Check the presence of process.env
diegohaz Jan 9, 2020
0765740
Check presence of process.env in the code transformed by the babel pl…
diegohaz Jan 9, 2020
aa2d200
Move babel-plugin.js and its test file to the directory top-level
diegohaz Jan 9, 2020
b045842
Improve README with information about dead code removal
diegohaz Jan 9, 2020
47cce9f
Turn sideEffects into true in package.json
diegohaz Jan 9, 2020
bb8209a
Revert sideEffects change
diegohaz Jan 9, 2020
d046ef6
npm run docs:build
diegohaz Jan 9, 2020
25dfd7f
Update packages/warning/babel-plugin.js
diegohaz Jan 13, 2020
44cdafe
warning: Add types checking for warning package
aduth Jan 14, 2020
ac20da3
Merge branch 'master' into add/warning
diegohaz Jan 15, 2020
f0bf765
Avoid indentation on the warning function with an early return
diegohaz Jan 15, 2020
b389b34
Fix TS error on babel-plugin.js
diegohaz Jan 15, 2020
1724346
Accept a single string as message instead of multiple messages
diegohaz Jan 15, 2020
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 babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = function( api ) {
plugins: [
'babel-plugin-emotion',
'babel-plugin-inline-json-import',
'babel-plugin-dev-expression',
diegohaz marked this conversation as resolved.
Show resolved Hide resolved
],
};
};
1 change: 1 addition & 0 deletions bin/api-docs/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const packages = [
'shortcode',
'url',
'viewport',
'warning',
'wordcount',
];

Expand Down
6 changes: 6 additions & 0 deletions docs/manifest-devhub.json
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,12 @@
"markdown_source": "../packages/viewport/README.md",
"parent": "packages"
},
{
"title": "@wordpress/warning",
"slug": "packages-warning",
"markdown_source": "../packages/warning/README.md",
"parent": "packages"
},
{
"title": "@wordpress/wordcount",
"slug": "packages-wordcount",
Expand Down
13 changes: 11 additions & 2 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"@wordpress/token-list": "file:packages/token-list",
"@wordpress/url": "file:packages/url",
"@wordpress/viewport": "file:packages/viewport",
"@wordpress/warning": "file:packages/warning",
"@wordpress/wordcount": "file:packages/wordcount"
},
"devDependencies": {
Expand Down Expand Up @@ -102,6 +103,7 @@
"@wordpress/postcss-themes": "file:packages/postcss-themes",
"@wordpress/scripts": "file:packages/scripts",
"babel-loader": "8.0.6",
"babel-plugin-dev-expression": "0.2.2",
"babel-plugin-emotion": "10.0.23",
"babel-plugin-inline-json-import": "0.3.2",
"babel-plugin-react-native-classname-to-style": "1.2.2",
Expand Down
1 change: 1 addition & 0 deletions packages/warning/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
Empty file added packages/warning/CHANGELOG.md
Empty file.
44 changes: 44 additions & 0 deletions packages/warning/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Warning

Utility for warning messages to the console based on a passed condition.

## Installation

Install the module

```bash
npm install @wordpress/warning --save
```

This is recommended to use in conjunction with [`babel-plugin-dev-expression`](https://github.com/4Catalyzer/babel-plugin-dev-expression) so your warning messages won't end up in your production bundle.

_This package assumes that your code will run in an **ES2015+** environment. If you're using an environment that has limited or no support for ES2015+ such as lower versions of IE then using [core-js](https://github.com/zloirock/core-js) or [@babel/polyfill](https://babeljs.io/docs/en/next/babel-polyfill) will add support for these methods. Learn more about it in [Babel docs](https://babeljs.io/docs/en/next/caveats)._

## API

<!-- START TOKEN(Autogenerated API docs) -->

<a name="default" href="#default">#</a> **default**

Shows a warning with `messages` if `condition` passes and environment is not `production`.

_Usage_

```js
import warning from '@wordpress/warning';

function MyComponent( props ) {
warning( ! props.title, '`props.title` was not passed' );
...
}
```

_Parameters_

- _condition_ `boolean`: Whether the warning will be triggered or not.
- _messages_ `Array<string>`: Message(s) to show in the warning.


<!-- END TOKEN(Autogenerated API docs) -->

<br/><br/><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p>
27 changes: 27 additions & 0 deletions packages/warning/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@wordpress/warning",
"version": "0.0.1",
"description": "Warning utility for WordPress.",
"author": "The WordPress Contributors",
"license": "GPL-2.0-or-later",
"keywords": [
"wordpress",
"warning"
],
"homepage": "https://github.com/WordPress/gutenberg/tree/master/packages/warning/README.md",
"repository": {
"type": "git",
"url": "https://github.com/WordPress/gutenberg.git",
"directory": "packages/warning"
},
"bugs": {
"url": "https://github.com/WordPress/gutenberg/issues"
},
"main": "build/index.js",
"module": "build-module/index.js",
"react-native": "src/index",
"sideEffects": false,
"publishConfig": {
"access": "public"
}
}
37 changes: 37 additions & 0 deletions packages/warning/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Shows a warning with `messages` if `condition` passes and environment is not `production`.
*
* @param {boolean} condition Whether the warning will be triggered or not.
* @param {string[]} messages Message(s) to show in the warning.
*
* @example
* ```js
* import warning from '@wordpress/warning';
*
* function MyComponent( props ) {
* warning( ! props.title, '`props.title` was not passed' );
* ...
* }
* ```
*/
export default function warning( condition, ...messages ) {
if ( process.env.NODE_ENV !== 'production' ) {
if ( ! condition ) {
return;
}

const text = messages.join( '\n' );
aduth marked this conversation as resolved.
Show resolved Hide resolved

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

// Throwing an error and catching it immediately to improve debugging
// A consumer can use 'pause on caught exceptions'
// https://github.com/facebook/react/issues/4216
try {
throw Error( text );
} catch ( x ) {
// do nothing
}
}
}
30 changes: 30 additions & 0 deletions packages/warning/src/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Internal dependencies
*/
import warning from '..';

const initialNodeEnv = process.env.NODE_ENV;

describe( 'warning', () => {
afterEach( () => {
process.env.NODE_ENV = initialNodeEnv;
} );

it( 'logs to console.warn when NODE_ENV is not "production"', () => {
process.env.NODE_ENV = 'development';
warning( true, 'warn', 'ing' );
expect( console ).toHaveWarnedWith( 'warn\ning' );
} );

it( 'does not log to console.warn if NODE_ENV is "production"', () => {
process.env.NODE_ENV = 'production';
warning( true, 'warn', 'ing' );
expect( console ).not.toHaveWarned();
} );

it( 'does not log to console.warn if condition is falsy', () => {
process.env.NODE_ENV = 'development';
warning( false, 'warn', 'ing' );
expect( console ).not.toHaveWarned();
} );
} );