Skip to content

Commit 5cdc3cc

Browse files
authored
Add tests for optional chaining and null coalescing (#7952)
1 parent d12b4b6 commit 5cdc3cc

File tree

6 files changed

+187
-0
lines changed

6 files changed

+187
-0
lines changed

CHANGELOG.md

+43
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,46 @@
1+
## 3.3.0 (2019-xx-xx)
2+
3+
DRAFT
4+
5+
### Custom Templates
6+
7+
DRAFT
8+
9+
### Optional Chaining and Nullish Coalescing Operators
10+
11+
We now support the [optional chaining](https://github.com/TC39/proposal-optional-chaining) and [nullish coalescing](https://github.com/tc39/proposal-nullish-coalescing) operators!
12+
13+
```js
14+
// Optional chaining
15+
a?.(); // undefined if `a` is null/undefined
16+
b?.c; // undefined if `b` is null/undefined
17+
18+
// Nullish coalescing
19+
undefined ?? 'some other default'; // result: 'some other default'
20+
null ?? 'some other default'; // result: 'some other default'
21+
'' ?? 'some other default'; // result: ''
22+
0 ?? 300; // result: 0
23+
false ?? true; // result: false
24+
```
25+
26+
**If your're using TypeScript, you will need to upgrade your `typescript` dependency to `3.7.0` or later if you wish to use the new operators.**
27+
28+
**If you're using Visual Studio Code 1.40 (the latest as of this release) or earlier, you will need to configure your editor if you want it to understand the new operators.**
29+
30+
If you're using TypeScript in your project and have already upgrade its version as described above, then you can [configure VS Code to `Use Workspace Version` of TypeScript](https://code.visualstudio.com/docs/typescript/typescript-compiling#_using-newer-typescript-versions). If your project isn't using TypeScript, you can use the [JavaScript and TypeScript Nightly extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.vscode-typescript-next) until VS Code releases a newer version including TypeScript `3.7.0` or newer.
31+
32+
### Numeric Separators
33+
34+
We've added support for [numeric separators](https://github.com/tc39/proposal-numeric-separator) to improve readability of numeric literals.
35+
36+
```js
37+
1000000000; // Is this a billion? a hundred millions? Ten millions?
38+
101475938.38; // what scale is this? what power of 10?
39+
40+
1_000_000_000; // Ah, so a billion
41+
101_475_938.38; // And this is hundreds of millions
42+
```
43+
144
## 3.2.0 (2019-10-03)
245

346
v3.2.0 is a minor release that adds support for production profiling and ignoring TypeScript type errors to make migrating JavaScript projects to TypeScript easier. It also includes other minor bug fixes and documentation updates.

packages/react-scripts/fixtures/kitchensink/template/src/App.js

+10
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ class App extends Component {
171171
this.setFeature(f.default)
172172
);
173173
break;
174+
case 'nullish-coalescing':
175+
import('./features/syntax/NullishCoalescing').then(f =>
176+
this.setFeature(f.default)
177+
);
178+
break;
174179
case 'object-destructuring':
175180
import('./features/syntax/ObjectDestructuring').then(f =>
176181
this.setFeature(f.default)
@@ -181,6 +186,11 @@ class App extends Component {
181186
this.setFeature(f.default)
182187
);
183188
break;
189+
case 'optional-chaining':
190+
import('./features/syntax/OptionalChaining').then(f =>
191+
this.setFeature(f.default)
192+
);
193+
break;
184194
case 'promises':
185195
import('./features/syntax/Promises').then(f =>
186196
this.setFeature(f.default)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import React, { Component } from 'react';
9+
import PropTypes from 'prop-types';
10+
11+
function load() {
12+
return [
13+
{ id: 1, name: '1' },
14+
{ id: 2, name: '2' },
15+
{ id: 3, name: '3' },
16+
{ id: 4, name: '4' },
17+
];
18+
}
19+
20+
export default class extends Component {
21+
static propTypes = {
22+
onReady: PropTypes.func.isRequired,
23+
};
24+
25+
constructor(props) {
26+
super(props);
27+
this.state = { users: [] };
28+
}
29+
30+
async componentDidMount() {
31+
const users = load();
32+
this.setState({ users });
33+
}
34+
35+
componentDidUpdate() {
36+
this.props.onReady();
37+
}
38+
39+
render() {
40+
return (
41+
<div id="feature-nullish-coalescing">
42+
{this.state.users.map(user => (
43+
<div key={user.id}>{user.name ?? 'John Doe'}</div>
44+
))}
45+
</div>
46+
);
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import React from 'react';
9+
import ReactDOM from 'react-dom';
10+
import NullishCoalescing from './NullishCoalescing';
11+
12+
describe('nullish coalescing', () => {
13+
it('renders without crashing', () => {
14+
const div = document.createElement('div');
15+
return new Promise(resolve => {
16+
ReactDOM.render(<NullishCoalescing onReady={resolve} />, div);
17+
});
18+
});
19+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import React, { Component } from 'react';
9+
import PropTypes from 'prop-types';
10+
11+
function load() {
12+
return [
13+
{ id: 1, name: '1' },
14+
{ id: 2, name: '2' },
15+
{ id: 3, name: '3' },
16+
{ id: 4, name: '4' },
17+
];
18+
}
19+
20+
export default class extends Component {
21+
static propTypes = {
22+
onReady: PropTypes.func.isRequired,
23+
};
24+
25+
constructor(props) {
26+
super(props);
27+
this.state = { users: [] };
28+
}
29+
30+
async componentDidMount() {
31+
const users = load?.();
32+
this.setState({ users });
33+
}
34+
35+
componentDidUpdate() {
36+
this.props.onReady();
37+
}
38+
39+
render() {
40+
return (
41+
<div id="feature-optional-chaining">
42+
{this.state.users.map(user => (
43+
<div key={user.id}>{user?.name}</div>
44+
))}
45+
</div>
46+
);
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import React from 'react';
9+
import ReactDOM from 'react-dom';
10+
import OptionalChaining from './OptionalChaining';
11+
12+
describe('optional chaining', () => {
13+
it('renders without crashing', () => {
14+
const div = document.createElement('div');
15+
return new Promise(resolve => {
16+
ReactDOM.render(<OptionalChaining onReady={resolve} />, div);
17+
});
18+
});
19+
});

0 commit comments

Comments
 (0)