remove undeclared plugin [skip ci] (#5407)
This commit is contained in:
parent
0847ae055d
commit
b4b03d48fa
@ -1,4 +0,0 @@
|
||||
node_modules
|
||||
*.log
|
||||
src
|
||||
test
|
||||
@ -1,56 +0,0 @@
|
||||
# babel-plugin-undeclared-variables-check
|
||||
|
||||
> This plugin throws a compile-time error on references to undeclared variables.
|
||||
|
||||
## Example
|
||||
|
||||
**In**
|
||||
|
||||
```javascript
|
||||
function foo() {}
|
||||
foo();
|
||||
bar();
|
||||
```
|
||||
|
||||
**Out**
|
||||
|
||||
```
|
||||
ReferenceError: stdin: Line 3: Reference to undeclared variable "bar" - did you mean "foo"?
|
||||
1 | function foo() {}
|
||||
2 | foo();
|
||||
> 3 | bar();
|
||||
| ^
|
||||
4 |
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
npm install --save-dev 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"]
|
||||
});
|
||||
```
|
||||
@ -1,17 +0,0 @@
|
||||
{
|
||||
"name": "babel-plugin-undeclared-variables-check",
|
||||
"version": "6.22.0",
|
||||
"description": "Throw a compile-time error on references to undeclared variables",
|
||||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-undeclared-variables-check",
|
||||
"license": "MIT",
|
||||
"main": "lib/index.js",
|
||||
"keywords": [
|
||||
"babel-plugin"
|
||||
],
|
||||
"dependencies": {
|
||||
"leven": "^1.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-helper-plugin-test-runner": "^6.22.0"
|
||||
}
|
||||
}
|
||||
@ -1,46 +0,0 @@
|
||||
import leven from "leven";
|
||||
|
||||
export default function ({ messages }) {
|
||||
return {
|
||||
visitor: {
|
||||
ReferencedIdentifier(path) {
|
||||
const { node, scope } = path;
|
||||
|
||||
const 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
|
||||
|
||||
const bindings = scope.getAllBindings();
|
||||
|
||||
let closest;
|
||||
let shortest = -1;
|
||||
|
||||
for (const name in bindings) {
|
||||
const 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);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -1,5 +0,0 @@
|
||||
function foo() {
|
||||
|
||||
}
|
||||
|
||||
foo();
|
||||
@ -1,3 +0,0 @@
|
||||
{
|
||||
"plugins": ["undeclared-variables-check"]
|
||||
}
|
||||
@ -1 +0,0 @@
|
||||
({foo})
|
||||
@ -1,3 +0,0 @@
|
||||
{
|
||||
"throws": "Reference to undeclared variable"
|
||||
}
|
||||
@ -1 +0,0 @@
|
||||
foo();
|
||||
@ -1,3 +0,0 @@
|
||||
{
|
||||
"throws": "Reference to undeclared variable"
|
||||
}
|
||||
@ -1,3 +0,0 @@
|
||||
import runner from "babel-helper-plugin-test-runner";
|
||||
|
||||
runner(__dirname);
|
||||
Loading…
x
Reference in New Issue
Block a user