Skip to content

Commit

Permalink
Flow: interface identifier should be declared in the scope (#10220)
Browse files Browse the repository at this point in the history
* fix: typo

* declare name for flow interface

* add test case for export overload function, typescript

* test: add test

Fixes #10044

* test: update test

* test(flow): add multiple declarations regression test

* re-enable flow test case

# Conflicts:
#	packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/def-site-variance/input.js
#	packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-declare-statements/input.js
#	packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-interfaces-module-and-script/input.js
#	packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-iterator/input.js

* test: disable two flow dupl-decl test

* fix: do not declare name for declare function until we figure out a better way

* test: duplicate declare function and function would not throw
  • Loading branch information
JLHwung authored and nicolo-ribaudo committed Oct 2, 2019
1 parent 02f2d17 commit fa5057f
Show file tree
Hide file tree
Showing 78 changed files with 2,013 additions and 76 deletions.
Expand Up @@ -11,13 +11,13 @@ declare class A { static [ indexer: number]: string }
declare class A { static () : number }
declare class B { (): number }
declare class A mixins B<T>, C {}
declare type A = string
declare type A1 = string
declare type T<U> = { [k:string]: U }
declare type B = {
declare type B1 = {
fn?: (foo: string) => void,
}
declare interface I { foo: string }
declare interface I<T> { foo: T }
declare interface I1 { foo: string }
declare interface I2<T> { foo: T }
declare module.exports: { foo: string }
declare opaque type Foo<T>: Bar<T>;
declare opaque type ID;
Expand Down
Expand Up @@ -22,17 +22,17 @@ declare class B {
(): number
}
declare class A mixins B<T>, C {}
declare type A = string;
declare type A1 = string;
declare type T<U> = {
[k: string]: U
};
declare type B = {
declare type B1 = {
fn?: (foo: string) => void
};
declare interface I {
declare interface I1 {
foo: string
}
declare interface I<T> {
declare interface I2<T> {
foo: T
}
declare module.exports: {
Expand Down
Expand Up @@ -5,10 +5,10 @@ type T2 = { +p: T };
type T3 = { -p: T };
type T4 = { +[k:K]: V };
type T5 = { -[k:K]: V };
interface I { +p: T }
interface I { -p: T }
interface I { +[k:K]: V }
interface I { -[k:K]: V }
interface I1 { +p: T }
interface I2 { -p: T }
interface I3 { +[k:K]: V }
interface I4 { -[k:K]: V }
declare class I { +p: T }
declare class I { -p: T }
declare class I { +[k:K]: V }
Expand Down
Expand Up @@ -15,16 +15,16 @@ type T4 = {
type T5 = {
-[k: K]: V
};
interface I {
interface I1 {
+p: T
}
interface I {
interface I2 {
-p: T
}
interface I {
interface I3 {
+[k: K]: V
}
interface I {
interface I4 {
-[k: K]: V
}
declare class I {
Expand Down
@@ -1,7 +1,7 @@
declare class C { static [[foo]]: T }
declare class C { [[foo]]: T }
interface I { [[foo]]: X }
interface I { [[foo]](): X }
interface I1 { [[foo]]: X }
interface I2 { [[foo]](): X }
type T1 = { [[foo]]: X }
type T2 = { [[foo]](): X }
type T3 = { [[foo]]?: X }
Expand Up @@ -4,10 +4,10 @@ declare class C {
declare class C {
[[foo]]: T
}
interface I {
interface I1 {
[[foo]]: X
}
interface I {
interface I2 {
[[foo]]() => X
}
type T1 = {
Expand Down
@@ -1,7 +1,7 @@
interface A {
interface A1 {
@@iterator(): Iterator<File>;
}

interface A {
interface A2 {
@@asyncIterator(): Iterator<File>;
}
@@ -1,6 +1,6 @@
interface A {
interface A1 {
@@iterator(): Iterator<File>
}
interface A {
interface A2 {
@@asyncIterator(): Iterator<File>
}
2 changes: 1 addition & 1 deletion packages/babel-parser/src/parser/expression.js
Expand Up @@ -1975,7 +1975,7 @@ export default class ExpressionParser extends LValParser {
node.params[i],
BIND_VAR,
allowDuplicates ? null : nameHash,
"function paramter list",
"function parameter list",
);
}
}
Expand Down
9 changes: 9 additions & 0 deletions packages/babel-parser/src/plugins/flow.js
Expand Up @@ -14,6 +14,8 @@ import {
type BindingTypes,
BIND_NONE,
BIND_LEXICAL,
BIND_VAR,
BIND_FUNCTION,
SCOPE_ARROW,
SCOPE_OTHER,
} from "../util/scopeflags";
Expand Down Expand Up @@ -270,6 +272,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.id = this.flowParseTypeAnnotatableIdentifier(
/*allowPrimitiveOverride*/ true,
);
this.scope.declareName(node.id.name, BIND_VAR, node.id.start);
this.semicolon();
return this.finishNode(node, "DeclareVariable");
}
Expand Down Expand Up @@ -462,6 +465,12 @@ export default (superClass: Class<Parser>): Class<Parser> =>
): void {
node.id = this.flowParseRestrictedIdentifier(/*liberal*/ !isClass);

this.scope.declareName(
node.id.name,
isClass ? BIND_FUNCTION : BIND_LEXICAL,
node.id.start,
);

if (this.isRelational("<")) {
node.typeParameters = this.flowParseTypeParameterDeclaration();
} else {
Expand Down
@@ -0,0 +1,3 @@
interface Foo {}

export type { Foo }
@@ -0,0 +1,160 @@
{
"type": "File",
"start": 0,
"end": 37,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 19
}
},
"program": {
"type": "Program",
"start": 0,
"end": 37,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 19
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "InterfaceDeclaration",
"start": 0,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 16
}
},
"id": {
"type": "Identifier",
"start": 10,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 13
},
"identifierName": "Foo"
},
"name": "Foo"
},
"typeParameters": null,
"extends": [],
"implements": [],
"mixins": [],
"body": {
"type": "ObjectTypeAnnotation",
"start": 14,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 14
},
"end": {
"line": 1,
"column": 16
}
},
"callProperties": [],
"properties": [],
"indexers": [],
"internalSlots": [],
"exact": false
}
},
{
"type": "ExportNamedDeclaration",
"start": 18,
"end": 37,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 19
}
},
"specifiers": [
{
"type": "ExportSpecifier",
"start": 32,
"end": 35,
"loc": {
"start": {
"line": 3,
"column": 14
},
"end": {
"line": 3,
"column": 17
}
},
"local": {
"type": "Identifier",
"start": 32,
"end": 35,
"loc": {
"start": {
"line": 3,
"column": 14
},
"end": {
"line": 3,
"column": 17
},
"identifierName": "Foo"
},
"name": "Foo"
},
"exported": {
"type": "Identifier",
"start": 32,
"end": 35,
"loc": {
"start": {
"line": 3,
"column": 14
},
"end": {
"line": 3,
"column": 17
},
"identifierName": "Foo"
},
"name": "Foo"
}
}
],
"source": null,
"exportKind": "type",
"declaration": null
}
],
"directives": []
}
}
@@ -0,0 +1,2 @@
declare class C1 {}
class C1 {}
@@ -0,0 +1,8 @@
{
"sourceType": "module",
"plugins": [
"jsx",
"flow"
],
"throws": "Identifier 'C1' has already been declared (2:6)"
}
@@ -0,0 +1,6 @@
declare class C1{}
declare class C1{}

declare module M1 {
declare class C1 {}
}

0 comments on commit fa5057f

Please sign in to comment.