Merge pull request #3414 from babel/flow-binding

Warn on Flow-based bindings and don't count as a const violation
This commit is contained in:
Amjad Masad
2016-03-10 13:41:58 -08:00
5 changed files with 31 additions and 10 deletions

View File

@@ -89,14 +89,8 @@ suite("api", function () {
new Plugin({
visitor: {
Function: function(path) {
var node = path.node;
var scope = path.scope;
var alias = scope
.getProgramParent()
.getBinding(node.returnType.typeAnnotation.id.name)
.path
.node;
var alias = path.scope.getProgramParent().path.get('body')[0].node;
if (!babel.types.isTypeAlias(alias)) return;
// In case of `passPerPreset` being `false`, the
// alias node is already removed by Flow plugin.

View File

@@ -0,0 +1,2 @@
declare class foo {}
const foo = 1;

View File

@@ -0,0 +1 @@
var foo = 1;

View File

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

View File

@@ -48,6 +48,9 @@ let collectorVisitor = {
// this will be hit again once we traverse into it after this iteration
if (path.isExportDeclaration() && path.get("declaration").isDeclaration()) return;
// TODO(amasad): remove support for flow as bindings (See warning below).
//if (path.isFlow()) return;
// we've ran into a declaration!
path.scope.getFunctionParent().registerDeclaration(path);
},
@@ -484,6 +487,11 @@ export default class Scope {
this.checkBlockScopedCollisions(local, kind, name, id);
}
// It's erroneous that we currently consider flow a binding, however, we can't
// remove it because people might be depending on it. See warning section
// in `warnOnFlowBinding`.
if (local && local.path.isFlow()) local = null;
parent.references[name] = true;
this.bindings[name] = new Binding({
@@ -674,6 +682,7 @@ export default class Scope {
path.traverse(collectorVisitor, state);
this.crawling = false;
this._warnOnFlowBinding = false;
// register assignments
for (let path of state.assignments) {
// register undeclared bindings as globals
@@ -704,6 +713,7 @@ export default class Scope {
for (let path of state.constantViolations) {
path.scope.registerConstantViolation(path);
}
this._warnOnFlowBinding = true;
}
push(opts: {
@@ -834,17 +844,28 @@ export default class Scope {
return this.getBindingIdentifier(name) === node;
}
warnOnFlowBinding(binding) {
if (!this.crawling && this._warnOnFlowBinding && binding && binding.path.isFlow()) {
console.warn(`
You or one of the Babel plugins you are using are using Flow declarations as bindings.
Support for this will be removed in version 6.8. To find out the caller, grep for this
message and change it to a \`console.trace()\`.
`);
}
return binding;
}
getBinding(name: string) {
let scope = this;
do {
let binding = scope.getOwnBinding(name);
if (binding) return binding;
if (binding) return this.warnOnFlowBinding(binding);
} while (scope = scope.parent);
}
getOwnBinding(name: string) {
return this.bindings[name];
return this.warnOnFlowBinding(this.bindings[name]);
}
getBindingIdentifier(name: string) {