move some babel-plugins into the main repo

This commit is contained in:
Sebastian McKenzie
2015-09-01 06:58:53 +01:00
parent f33c96c276
commit 9f9d9cd84b
61 changed files with 1779 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
node_modules
*.log
src

View File

@@ -0,0 +1,49 @@
# babel-plugin-eval
Compile eval calls with string literals
## Example
**In**
```javascript
eval("(() => 'foo')");
```
**Out**
```javascript
eval("(function () { return 'foo'; })");
```
## Installation
```sh
$ npm install babel-plugin-eval
```
## Usage
### Via `.babelrc` (Recommended)
**.babelrc**
```json
{
"plugins": ["eval"]
}
```
### Via CLI
```sh
$ babel --plugins eval script.js
```
### Via Node API
```javascript
require("babel-core").transform("code", {
plugins: ["eval"]
});
```

View File

@@ -0,0 +1,14 @@
{
"name": "babel-plugin-eval",
"version": "1.0.1",
"description": "Compile eval calls with string literals",
"repository": "babel-plugins/babel-plugin-eval",
"license": "MIT",
"main": "lib/index.js",
"devDependencies": {
"babel": "^5.6.0"
},
"keywords": [
"babel-plugin"
]
}

View File

@@ -0,0 +1,23 @@
export default function ({ Plugin, parse, traverse }) {
return new Plugin("eval", {
metadata: {
group: "builtin-pre",
},
visitor: {
CallExpression(node) {
if (this.get("callee").isIdentifier({ name: "eval" }) && node.arguments.length === 1) {
var evaluate = this.get("arguments")[0].evaluate();
if (!evaluate.confident) return;
var code = evaluate.value;
if (typeof code !== "string") return;
var ast = parse(code);
traverse.removeProperties(ast);
return ast.program;
}
}
}
});
}