6.0.0
I'm extremely stupid and didn't commit as I go. To anyone reading this I'm extremely sorry. A lot of these changes are very broad and I plan on releasing Babel 6.0.0 today live on stage at Ember Camp London so I'm afraid I couldn't wait. If you're ever in London I'll buy you a beer (or assorted beverage!) to make up for it, also I'll kiss your feet and give you a back massage, maybe.
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
node_modules
|
||||
*.log
|
||||
src
|
||||
35
packages/babel-plugin-undeclared-variables-check/README.md
Normal file
35
packages/babel-plugin-undeclared-variables-check/README.md
Normal file
@@ -0,0 +1,35 @@
|
||||
# babel-plugin-undeclared-variables-check
|
||||
|
||||
Throw a compile-time error on references to undeclared variables
|
||||
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
$ npm install babel-plugin-undeclared-variables-check
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Via `.babelrc` (Recommended)
|
||||
|
||||
**.babelrc**
|
||||
|
||||
```json
|
||||
{
|
||||
"plugins": ["undeclared-variables-check"]
|
||||
}
|
||||
```
|
||||
|
||||
### Via CLI
|
||||
|
||||
```sh
|
||||
$ babel --plugins undeclared-variables-check script.js
|
||||
```
|
||||
|
||||
### Via Node API
|
||||
|
||||
```javascript
|
||||
require("babel-core").transform("code", {
|
||||
plugins: ["undeclared-variables-check"]
|
||||
});
|
||||
```
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "babel-plugin-undeclared-variables-check",
|
||||
"version": "6.0.6",
|
||||
"description": "Throw a compile-time error on references to undeclared variables",
|
||||
"repository": "babel/babel",
|
||||
"license": "MIT",
|
||||
"main": "lib/index.js",
|
||||
"keywords": [
|
||||
"babel-plugin"
|
||||
],
|
||||
"dependencies": {
|
||||
"babel-runtime": "^6.0.6",
|
||||
"leven": "^1.0.2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import leven from "leven";
|
||||
|
||||
export default function ({ messages }) {
|
||||
return {
|
||||
visitor: {
|
||||
ReferencedIdentifier(path) {
|
||||
let { node, scope } = path;
|
||||
|
||||
let binding = scope.getBinding(node.name);
|
||||
if (binding && binding.kind === "type" && !path.parentPath.isFlow()) {
|
||||
throw path.buildCodeFrameError(messages.get("undeclaredVariableType", node.name), ReferenceError);
|
||||
}
|
||||
|
||||
if (scope.hasBinding(node.name)) return;
|
||||
|
||||
// get the closest declaration to offer as a suggestion
|
||||
// the variable name may have just been mistyped
|
||||
|
||||
let bindings = scope.getAllBindings();
|
||||
|
||||
let closest;
|
||||
let shortest = -1;
|
||||
|
||||
for (let name in bindings) {
|
||||
let distance = leven(node.name, name);
|
||||
if (distance <= 0 || distance > 3) continue;
|
||||
if (distance <= shortest) continue;
|
||||
|
||||
closest = name;
|
||||
shortest = distance;
|
||||
}
|
||||
|
||||
let msg;
|
||||
if (closest) {
|
||||
msg = messages.get("undeclaredVariableSuggestion", node.name, closest);
|
||||
} else {
|
||||
msg = messages.get("undeclaredVariable", node.name);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
throw path.buildCodeFrameError(msg, ReferenceError);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user