Convert the constants transform plugin to a validation plugin

A follow up from https://github.com/babel/babel/pull/2969 were we added support for consts in the block-scoping plugin.
This commit is contained in:
Amjad Masad
2015-11-10 17:45:29 -08:00
parent 0711fa67b4
commit a17d62ca2b
39 changed files with 64 additions and 86 deletions

View File

@@ -0,0 +1,39 @@
# babel-plugin-check-es2015-constants
Validate ES2015 constants
## Installation
```sh
$ npm install babel-plugin-check-es2015-constants
```
## Usage
### Via `.babelrc` (Recommended)
**.babelrc**
```json
{
"plugins": ["check-es2015-constants"]
}
```
### Via CLI
```sh
$ babel --plugins check-es2015-constants script.js
```
### Via Node API
```javascript
require("babel-core").transform("code", {
plugins: ["check-es2015-constants"]
});
```
## Note
This check will only validate consts. If you need it to compile down to `var` then you must also install and enable [`check-es2015-block-scoping`](../babel-plugin-check-es2015-block-scoping).

View File

@@ -1,8 +1,8 @@
{
"name": "babel-plugin-transform-es2015-constants",
"name": "babel-plugin-check-es2015-constants",
"version": "6.1.4",
"description": "Compile ES2015 constants to ES5",
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-es2015-constants",
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-check-es2015-constants",
"license": "MIT",
"main": "lib/index.js",
"keywords": [

View File

@@ -0,0 +1,16 @@
export default function ({ messages }) {
return {
visitor: {
Scope({ scope }) {
for (let name in scope.bindings) {
let binding = scope.bindings[name];
if (binding.kind !== "const" && binding.kind !== "module") continue;
for (let violation of (binding.constantViolations: Array)) {
throw violation.buildCodeFrameError(messages.get("readOnly", name));
}
}
},
}
};
}

View File

@@ -0,0 +1,3 @@
{
"plugins": ["check-es2015-constants", "transform-es2015-block-scoping", "transform-es2015-destructuring"]
}

View File

@@ -1,3 +1,3 @@
{
"plugins": ["transform-es2015-constants", "transform-es2015-block-scoping", "transform-es2015-parameters", "transform-es2015-destructuring", "transform-es2015-modules-commonjs"]
"plugins": ["check-es2015-constants", "transform-es2015-block-scoping", "transform-es2015-parameters", "transform-es2015-destructuring", "transform-es2015-modules-commonjs"]
}

View File

@@ -1,39 +0,0 @@
# babel-plugin-transform-es2015-constants
Compile ES2015 constants to ES5
## Installation
```sh
$ npm install babel-plugin-transform-es2015-constants
```
## Usage
### Via `.babelrc` (Recommended)
**.babelrc**
```json
{
"plugins": ["transform-es2015-constants"]
}
```
### Via CLI
```sh
$ babel --plugins transform-es2015-constants script.js
```
### Via Node API
```javascript
require("babel-core").transform("code", {
plugins: ["transform-es2015-constants"]
});
```
## Note
This transform on its own will compile `const` to `let`. If you need it to compile down to `var` then you must also install and enable [`transform-es2015-block-scoping`](../babel-plugin-transform-es2015-block-scoping).

View File

@@ -1,38 +0,0 @@
export default function ({ messages, types: t }) {
function check(node) {
if (t.isVariableDeclaration(node, { kind: "const" })) {
node.kind = "let";
}
}
return {
visitor: {
Scope({ scope }) {
for (let name in scope.bindings) {
let binding = scope.bindings[name];
if (binding.kind !== "const" && binding.kind !== "module") continue;
for (let violation of (binding.constantViolations: Array)) {
throw violation.buildCodeFrameError(messages.get("readOnly", name));
}
}
},
VariableDeclaration({ node }) {
check(node);
},
ForXStatement({ node: { left } }) {
check(left);
},
ForStatement({ node: { init } }) {
check(init);
},
"BlockStatement|Program"({ node: { body } }) {
for (let node of body) check(node);
}
}
};
}

View File

@@ -1,3 +0,0 @@
{
"plugins": ["transform-es2015-constants", "transform-es2015-block-scoping", "transform-es2015-destructuring"]
}

View File

@@ -12,7 +12,7 @@ module.exports = {
require("babel-plugin-transform-es2015-for-of"),
require("babel-plugin-transform-es2015-sticky-regex"),
require("babel-plugin-transform-es2015-unicode-regex"),
require("babel-plugin-transform-es2015-constants"),
require("babel-plugin-check-es2015-constants"),
require("babel-plugin-transform-es2015-spread"),
require("babel-plugin-transform-es2015-parameters"),
require("babel-plugin-transform-es2015-destructuring"),

View File

@@ -20,7 +20,7 @@
"babel-plugin-transform-es2015-for-of": "^6.1.4",
"babel-plugin-transform-es2015-sticky-regex": "^6.1.4",
"babel-plugin-transform-es2015-unicode-regex": "^6.1.4",
"babel-plugin-transform-es2015-constants": "^6.1.4",
"babel-plugin-check-es2015-constants": "^6.1.4",
"babel-plugin-transform-es2015-spread": "^6.1.4",
"babel-plugin-transform-es2015-parameters": "^6.1.4",
"babel-plugin-transform-es2015-destructuring": "^6.1.4",

View File

@@ -14,7 +14,7 @@
"transform-es2015-for-of",
"transform-es2015-sticky-regex",
"transform-es2015-unicode-regex",
"transform-es2015-constants",
"check-es2015-constants",
"transform-es2015-spread",
"transform-es2015-parameters",
"transform-es2015-destructuring",