1 | # babel-plugin-transform-do-expressions
|
2 |
|
3 | > Compile `do` expressions to ES5
|
4 |
|
5 | ## Detail
|
6 |
|
7 | > The `do { .. }` expression executes a block (with one or many statements in it), and the final statement completion value inside the block becomes the completion value of the do expression.
|
8 |
|
9 | from [You Don't Know JS](https://github.com/getify/You-Dont-Know-JS/blob/master/types%20%26%20grammar/ch5.md#statement-completion-values)
|
10 |
|
11 | It can be seen as a complex version of the [ternary operator](http://mdn.io/ternary):
|
12 |
|
13 | ```js
|
14 | let a = do {
|
15 | if(x > 10) {
|
16 | 'big';
|
17 | } else {
|
18 | 'small';
|
19 | }
|
20 | };
|
21 | // is equivalent to:
|
22 | let a = x > 10 ? 'big' : 'small';
|
23 | ```
|
24 |
|
25 | [Try in REPL](http://babeljs.io/repl/#?evaluate=true&presets=es2015%2Cstage-0&code=%0Alet%20x%20%3D%20100%3B%0A%0Alet%20a%20%3D%20do%20%7B%0A%20%20if(x%20%3E%2010)%20%7B%0A%20%20%20%20'big'%3B%0A%20%20%7D%20else%20%7B%0A%20%20%20%20'small'%3B%0A%20%20%7D%0A%7D%3B%0A%0Aconsole.log(a)%3B)
|
26 |
|
27 | This example is not the best usage because it is too simple and using a ternary operator is a better option but you can have a much more complex condition in the `do { ... }` expression with several `if ... else` chains:
|
28 |
|
29 | ```js
|
30 | let x = 100;
|
31 | let y = 20;
|
32 |
|
33 | let a = do {
|
34 | if(x > 10) {
|
35 | if(y > 20) {
|
36 | 'big x, big y';
|
37 | } else {
|
38 | 'big x, small y';
|
39 | }
|
40 | } else {
|
41 | if(y > 10) {
|
42 | 'small x, big y';
|
43 | } else {
|
44 | 'small x, small y';
|
45 | }
|
46 | }
|
47 | };
|
48 | ```
|
49 |
|
50 | [Try in REPL](http://babeljs.io/repl/#?evaluate=true&presets=es2015%2Cstage-0&code=let%20x%20%3D%20100%3B%0Alet%20y%20%3D%2020%3B%0A%0Alet%20a%20%3D%20do%20%7B%0A%20%20if(x%20%3E%2010)%20%7B%0A%20%20%20%20if(y%20%3E%2020)%20%7B%0A%20%20%20%20%20%20'big%20x%2C%20big%20y'%3B%0A%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20'big%20x%2C%20small%20y'%3B%0A%20%20%20%20%7D%0A%20%20%7D%20else%20%7B%0A%20%20%20%20if(y%20%3E%2010)%20%7B%0A%20%20%20%20%20%20'small%20x%2C%20big%20y'%3B%0A%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20'small%20x%2C%20small%20y'%3B%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%3B%0A%0Aconsole.log(a)%3B)
|
51 |
|
52 | ## Example
|
53 |
|
54 | ### In JSX
|
55 | One of the most useful usage of the `do` expression is inside JSX. If we want to conditionally display a component we usually have to call another function which would implement the condition and return the correct value, for example:
|
56 |
|
57 | ```js
|
58 | const getColoredComponent = color => {
|
59 | if(color === 'blue') { return <BlueComponent/>; }
|
60 | if(color === 'red') { return <RedComponent/>; }
|
61 | if(color === 'green') { return <GreenComponent/>; }
|
62 | }
|
63 |
|
64 | const Component = props =>
|
65 | <div className='myComponent'>
|
66 | {getColoredComponent()}
|
67 | </div>
|
68 | ;
|
69 | ```
|
70 |
|
71 | Using a do expression you can add logic inside JSX:
|
72 |
|
73 | ```js
|
74 | const Component = props =>
|
75 | <div className='myComponent'>
|
76 | {do {
|
77 | if(color === 'blue') { <BlueComponent/>; }
|
78 | if(color === 'red') { <RedComponent/>; }
|
79 | if(color === 'green') { <GreenComponent/>; }
|
80 | }}
|
81 | </div>
|
82 | ;
|
83 | ```
|
84 |
|
85 | [Try in REPL](http://babeljs.io/repl/#?evaluate=true&presets=es2015%2Creact%2Cstage-0&code=const%20Component%20%3D%20props%20%3D%3E%0A%20%20%3Cdiv%20className%3D'myComponent'%3E%0A%20%20%20%20%7Bdo%20%7B%0A%20%20%20%20%20%20if(color%20%3D%3D%3D%20'blue')%20%7B%20%3CBlueComponent%2F%3E%3B%20%7D%0A%20%20%20%20%20%20if(color%20%3D%3D%3D%20'red')%20%7B%20%3CRedComponent%2F%3E%3B%20%7D%0A%20%20%20%20%20%20if(color%20%3D%3D%3D%20'green')%20%7B%20%3CGreenComponent%2F%3E%3B%20%7D%0A%20%20%20%20%7D%7D%0A%20%20%3C%2Fdiv%3E%0A%3B)
|
86 |
|
87 | ## Installation
|
88 |
|
89 | ```sh
|
90 | npm install --save-dev babel-plugin-transform-do-expressions
|
91 | ```
|
92 |
|
93 | ## Usage
|
94 |
|
95 | ### Via `.babelrc` (Recommended)
|
96 |
|
97 | **.babelrc**
|
98 |
|
99 | ```json
|
100 | {
|
101 | "plugins": ["transform-do-expressions"]
|
102 | }
|
103 | ```
|
104 |
|
105 | ### Via CLI
|
106 |
|
107 | ```sh
|
108 | babel --plugins transform-do-expressions script.js
|
109 | ```
|
110 |
|
111 | ### Via Node API
|
112 |
|
113 | ```javascript
|
114 | require("babel-core").transform("code", {
|
115 | plugins: ["transform-do-expressions"]
|
116 | });
|
117 | ```
|
118 |
|
119 | ## References
|
120 | - [Proposal](http://wiki.ecmascript.org/doku.php?id=strawman:do_expressions)
|
121 | - [You Don't Know JS](https://github.com/getify/You-Dont-Know-JS/blob/master/types%20%26%20grammar/ch5.md#statement-completion-values)
|
122 | - Very handy for conditions inside JSX: [reactjs/react-future#35](https://github.com/reactjs/react-future/issues/35#issuecomment-120009203)
|