Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[parser] Fix scope handling of Flow declared functions (#12735)
- Loading branch information
1 parent
f1a3275
commit c071852
Showing
7 changed files
with
212 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// @flow | ||
|
||
import ScopeHandler, { Scope } from "../../util/scope"; | ||
import { | ||
BIND_FLAGS_FLOW_DECLARE_FN, | ||
type ScopeFlags, | ||
type BindingTypes, | ||
} from "../../util/scopeflags"; | ||
import * as N from "../../types"; | ||
|
||
// Reference implementation: https://github.com/facebook/flow/blob/23aeb2a2ef6eb4241ce178fde5d8f17c5f747fb5/src/typing/env.ml#L536-L584 | ||
class FlowScope extends Scope { | ||
// declare function foo(): type; | ||
declareFunctions: string[] = []; | ||
} | ||
|
||
export default class FlowScopeHandler extends ScopeHandler<FlowScope> { | ||
createScope(flags: ScopeFlags): FlowScope { | ||
return new FlowScope(flags); | ||
} | ||
|
||
declareName(name: string, bindingType: BindingTypes, pos: number) { | ||
const scope = this.currentScope(); | ||
if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) { | ||
this.checkRedeclarationInScope(scope, name, bindingType, pos); | ||
this.maybeExportDefined(scope, name); | ||
scope.declareFunctions.push(name); | ||
return; | ||
} | ||
|
||
super.declareName(...arguments); | ||
} | ||
|
||
isRedeclaredInScope( | ||
scope: FlowScope, | ||
name: string, | ||
bindingType: BindingTypes, | ||
): boolean { | ||
if (super.isRedeclaredInScope(...arguments)) return true; | ||
|
||
if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) { | ||
return ( | ||
!scope.declareFunctions.includes(name) && | ||
(scope.lexical.includes(name) || scope.functions.includes(name)) | ||
); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
checkLocalExport(id: N.Identifier) { | ||
if (this.scopeStack[0].declareFunctions.indexOf(id.name) === -1) { | ||
super.checkLocalExport(id); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/babel-parser/test/fixtures/flow/scope/declare-function-export/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
declare function foo(): void; | ||
export { foo }; |
61 changes: 61 additions & 0 deletions
61
packages/babel-parser/test/fixtures/flow/scope/declare-function-export/output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":15}}, | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":15}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "DeclareFunction", | ||
"start":0,"end":29,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":29}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":17,"end":28,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":28},"identifierName":"foo"}, | ||
"name": "foo", | ||
"typeAnnotation": { | ||
"type": "TypeAnnotation", | ||
"start":20,"end":28,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":28}}, | ||
"typeAnnotation": { | ||
"type": "FunctionTypeAnnotation", | ||
"start":20,"end":28,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":28}}, | ||
"typeParameters": null, | ||
"params": [], | ||
"rest": null, | ||
"returnType": { | ||
"type": "VoidTypeAnnotation", | ||
"start":24,"end":28,"loc":{"start":{"line":1,"column":24},"end":{"line":1,"column":28}} | ||
} | ||
} | ||
} | ||
}, | ||
"predicate": null | ||
}, | ||
{ | ||
"type": "ExportNamedDeclaration", | ||
"start":30,"end":45,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":15}}, | ||
"specifiers": [ | ||
{ | ||
"type": "ExportSpecifier", | ||
"start":39,"end":42,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":12}}, | ||
"local": { | ||
"type": "Identifier", | ||
"start":39,"end":42,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":12},"identifierName":"foo"}, | ||
"name": "foo" | ||
}, | ||
"exported": { | ||
"type": "Identifier", | ||
"start":39,"end":42,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":12},"identifierName":"foo"}, | ||
"name": "foo" | ||
} | ||
} | ||
], | ||
"source": null, | ||
"declaration": null, | ||
"exportKind": "value" | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/babel-parser/test/fixtures/flow/scope/dupl-decl-func-declare-func/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
function A() {} | ||
declare function A(): void; |
59 changes: 59 additions & 0 deletions
59
packages/babel-parser/test/fixtures/flow/scope/dupl-decl-func-declare-func/output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":43,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":27}}, | ||
"errors": [ | ||
"SyntaxError: Identifier 'A' has already been declared (2:17)" | ||
], | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":43,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":27}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "FunctionDeclaration", | ||
"start":0,"end":15,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":15}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":9,"end":10,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":10},"identifierName":"A"}, | ||
"name": "A" | ||
}, | ||
"generator": false, | ||
"async": false, | ||
"params": [], | ||
"body": { | ||
"type": "BlockStatement", | ||
"start":13,"end":15,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":15}}, | ||
"body": [], | ||
"directives": [] | ||
} | ||
}, | ||
{ | ||
"type": "DeclareFunction", | ||
"start":16,"end":43,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":27}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":33,"end":42,"loc":{"start":{"line":2,"column":17},"end":{"line":2,"column":26},"identifierName":"A"}, | ||
"name": "A", | ||
"typeAnnotation": { | ||
"type": "TypeAnnotation", | ||
"start":34,"end":42,"loc":{"start":{"line":2,"column":18},"end":{"line":2,"column":26}}, | ||
"typeAnnotation": { | ||
"type": "FunctionTypeAnnotation", | ||
"start":34,"end":42,"loc":{"start":{"line":2,"column":18},"end":{"line":2,"column":26}}, | ||
"typeParameters": null, | ||
"params": [], | ||
"rest": null, | ||
"returnType": { | ||
"type": "VoidTypeAnnotation", | ||
"start":38,"end":42,"loc":{"start":{"line":2,"column":22},"end":{"line":2,"column":26}} | ||
} | ||
} | ||
} | ||
}, | ||
"predicate": null | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |