move some babel-plugins into the main repo
This commit is contained in:
3
packages/babel-plugin-eval/.npmignore
Normal file
3
packages/babel-plugin-eval/.npmignore
Normal file
@@ -0,0 +1,3 @@
|
||||
node_modules
|
||||
*.log
|
||||
src
|
||||
49
packages/babel-plugin-eval/README.md
Normal file
49
packages/babel-plugin-eval/README.md
Normal 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"]
|
||||
});
|
||||
```
|
||||
14
packages/babel-plugin-eval/package.json
Normal file
14
packages/babel-plugin-eval/package.json
Normal 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"
|
||||
]
|
||||
}
|
||||
23
packages/babel-plugin-eval/src/index.js
Normal file
23
packages/babel-plugin-eval/src/index.js
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user