1 |
3a515b92
|
cagy
|
# babel-plugin-check-es2015-constants
|
2 |
|
|
|
3 |
|
|
Validate ES2015 constants (prevents reassignment of const variables).
|
4 |
|
|
|
5 |
|
|
## Example
|
6 |
|
|
|
7 |
|
|
**In**
|
8 |
|
|
|
9 |
|
|
```js
|
10 |
|
|
const a = 1;
|
11 |
|
|
a = 2;
|
12 |
|
|
```
|
13 |
|
|
|
14 |
|
|
**Out**
|
15 |
|
|
|
16 |
|
|
```bash
|
17 |
|
|
repl: "a" is read-only
|
18 |
|
|
1 | const a = 1;
|
19 |
|
|
> 2 | a = 2;
|
20 |
|
|
| ^
|
21 |
|
|
```
|
22 |
|
|
|
23 |
|
|
[Try in REPL](http://babeljs.io/repl/#?babili=false&evaluate=true&lineWrap=false&presets=es2015&experimental=false&loose=false&spec=false&code=const%20a%20%3D%201%3B%0Aa%20%3D%202%3B&playground=true)
|
24 |
|
|
|
25 |
|
|
## Installation
|
26 |
|
|
|
27 |
|
|
```sh
|
28 |
|
|
npm install --save-dev babel-plugin-check-es2015-constants
|
29 |
|
|
```
|
30 |
|
|
|
31 |
|
|
## Usage
|
32 |
|
|
|
33 |
|
|
### Via `.babelrc` (Recommended)
|
34 |
|
|
|
35 |
|
|
**.babelrc**
|
36 |
|
|
|
37 |
|
|
```json
|
38 |
|
|
{
|
39 |
|
|
"plugins": ["check-es2015-constants"]
|
40 |
|
|
}
|
41 |
|
|
```
|
42 |
|
|
|
43 |
|
|
### Via CLI
|
44 |
|
|
|
45 |
|
|
```sh
|
46 |
|
|
babel --plugins check-es2015-constants script.js
|
47 |
|
|
```
|
48 |
|
|
|
49 |
|
|
### Via Node API
|
50 |
|
|
|
51 |
|
|
```javascript
|
52 |
|
|
require("babel-core").transform("code", {
|
53 |
|
|
plugins: ["check-es2015-constants"]
|
54 |
|
|
});
|
55 |
|
|
```
|
56 |
|
|
|
57 |
|
|
## Note
|
58 |
|
|
|
59 |
|
|
This check will only validate consts. If you need it to compile down to `var` then you must also install and enable [`transform-es2015-block-scoping`](http://babeljs.io/docs/plugins/transform-es2015-block-scoping/).
|