From 16e9f1c8e56d8a2b99f0f5769adf9da41ee9aa96 Mon Sep 17 00:00:00 2001 From: Daniel Sainati Date: Sun, 21 Feb 2021 11:44:27 -0500 Subject: [PATCH] Support Flow `this` parameter annotations (#12234) --- .../babel-generator/src/generators/flow.ts | 12 + .../test/fixtures/flow/this-param/input.js | 45 ++++ .../test/fixtures/flow/this-param/output.js | 56 +++++ .../babel-parser/src/plugins/flow/index.js | 145 ++++++++++- .../object-method/invalid-setter/output.json | 10 +- .../good_10/output.json | 1 + .../good_14/output.json | 1 + .../good_15/output.json | 1 + .../good_01/output.json | 1 + .../good_03/output.json | 1 + .../good_04/output.json | 1 + .../good_05/output.json | 1 + .../good_06/output.json | 1 + .../good_07/output.json | 1 + .../good_08/output.json | 1 + .../good_09/output.json | 1 + .../good_10/output.json | 1 + .../good_13/output.json | 1 + .../fixtures/flow/array-types/4/output.json | 1 + .../fixtures/flow/array-types/5/output.json | 1 + .../flow/call-properties/1/output.json | 1 + .../flow/call-properties/2/output.json | 1 + .../flow/call-properties/3/output.json | 2 + .../flow/call-properties/4/output.json | 1 + .../flow/call-properties/5/output.json | 1 + .../getter-setter/output.json | 6 + .../output.json | 1 + .../declare-export/export-class/output.json | 1 + .../export-default-arrow/output.json | 1 + .../export-default-function/output.json | 1 + .../export-function/output.json | 1 + .../flow/declare-module/10/output.json | 1 + .../flow/declare-module/4/output.json | 1 + .../flow/declare-module/5/output.json | 1 + .../flow/declare-module/6/output.json | 1 + .../flow/declare-statements/10/output.json | 1 + .../flow/declare-statements/12/output.json | 1 + .../flow/declare-statements/18/output.json | 2 + .../flow/declare-statements/3/output.json | 1 + .../flow/declare-statements/4/output.json | 1 + .../flow/declare-statements/5/output.json | 1 + .../flow/declare-statements/6/output.json | 1 + .../flow/declare-statements/7/output.json | 1 + .../output.json | 1 + .../10/output.json | 1 + .../4/output.json | 1 + .../output.json | 1 + .../interface-method/output.json | 1 + .../internal-slot/object-method/output.json | 1 + .../fixtures/flow/iterator/01/output.json | 1 + .../fixtures/flow/iterator/02/output.json | 1 + .../fixtures/flow/iterator/12/output.json | 9 +- .../fixtures/flow/iterator/13/output.json | 9 +- .../fixtures/flow/iterator/14/output.json | 1 + .../fixtures/flow/iterator/15/output.json | 1 + .../declare-function/output.json | 2 + .../complex-param-types/output.json | 1 + .../output.json | 1 + .../invalid-getter-param-count/output.json | 1 + .../invalid-setter-param-count/output.json | 1 + .../invalid-setter-param-type/output.json | 1 + .../fixtures/flow/predicates/1/output.json | 1 + .../fixtures/flow/predicates/6/output.json | 1 + .../flow/regression/issue-321/output.json | 2 + .../scope/declare-function-export/output.json | 1 + .../output.json | 2 + .../output.json | 1 + .../dupl-decl-declare-func-func/output.json | 1 + .../dupl-decl-func-declare-func/output.json | 1 + .../this-annotation/declare-function/input.js | 7 + .../declare-function/output.json | 212 ++++++++++++++++ .../this-annotation/declare-method/input.js | 6 + .../declare-method/output.json | 235 ++++++++++++++++++ .../flow/this-annotation/default/input.js | 1 + .../flow/this-annotation/default/output.json | 73 ++++++ .../function-declaration/input.js | 7 + .../function-declaration/output.json | 186 ++++++++++++++ .../this-annotation/function-type/input.js | 7 + .../this-annotation/function-type/output.json | 196 +++++++++++++++ .../flow/this-annotation/method/input.js | 6 + .../flow/this-annotation/method/output.json | 226 +++++++++++++++++ .../object-constructor/input.js | 5 + .../object-constructor/output.json | 77 ++++++ .../input.js | 3 + .../output.json | 92 +++++++ .../object-method-type/input.js | 3 + .../object-method-type/output.json | 69 +++++ .../object-type-constructor/input.js | 5 + .../object-type-constructor/output.json | 83 +++++++ .../this-arrow-function/input.js | 1 + .../this-arrow-function/output.json | 62 +++++ .../this-constructor-declare/input.js | 3 + .../this-constructor-declare/output.json | 80 ++++++ .../this-annotation/this-constructor/input.js | 3 + .../this-constructor/output.json | 68 +++++ .../this-annotation/this-getter-type/input.js | 3 + .../this-getter-type/output.json | 72 ++++++ .../flow/this-annotation/this-getter/input.js | 3 + .../this-annotation/this-getter/output.json | 69 +++++ .../this-annotation/this-no-annot/input.js | 1 + .../this-annotation/this-no-annot/output.json | 40 +++ .../this-not-first-type/input.js | 1 + .../this-not-first-type/output.json | 67 +++++ .../this-annotation/this-not-first/input.js | 1 + .../this-not-first/output.json | 61 +++++ .../this-optional-type/input.js | 1 + .../this-optional-type/output.json | 47 ++++ .../this-annotation/this-optional/input.js | 1 + .../this-annotation/this-optional/output.json | 49 ++++ .../this-annotation/this-setter-type/input.js | 3 + .../this-setter-type/output.json | 73 ++++++ .../flow/this-annotation/this-setter/input.js | 3 + .../this-annotation/this-setter/output.json | 68 +++++ .../this-string-literal-constructor/input.js | 3 + .../output.json | 72 ++++++ .../flow/this-annotation/this-type/input.js | 5 + .../this-annotation/this-type/output.json | 125 ++++++++++ .../fixtures/flow/type-alias/4/output.json | 2 + .../flow/type-annotations/10/output.json | 1 + .../flow/type-annotations/11/output.json | 1 + .../flow/type-annotations/13/output.json | 1 + .../flow/type-annotations/14/output.json | 1 + .../flow/type-annotations/15/output.json | 1 + .../flow/type-annotations/42/output.json | 1 + .../flow/type-annotations/43/output.json | 1 + .../flow/type-annotations/57/output.json | 2 + .../flow/type-annotations/7/output.json | 1 + .../flow/type-annotations/8/output.json | 1 + .../flow/type-annotations/86/output.json | 1 + .../flow/type-annotations/87/output.json | 1 + .../flow/type-annotations/88/output.json | 1 + .../flow/type-annotations/9/output.json | 1 + .../object-type-method/output.json | 10 + .../async-arrow-like/output.json | 65 ++--- .../type-generics/async-arrow/output.json | 1 + .../fixtures/flow/type-grouping/2/output.json | 2 + .../output.json | 8 + .../output.json | 4 + .../default/output.json | 51 ++-- .../interface-reserved-word/output.json | 4 + .../type-object-reserved-word/output.json | 4 + .../fixtures/flow/typecasts/3/output.json | 9 +- .../src/index.js | 7 + .../strip-types/strip-this-param/input.js | 26 ++ .../strip-types/strip-this-param/output.js | 35 +++ .../parameters/flow-this-param/input.js | 3 + .../parameters/flow-this-param/output.js | 3 + .../src/ast-types/generated/index.ts | 1 + packages/babel-types/src/definitions/flow.ts | 1 + 149 files changed, 3010 insertions(+), 83 deletions(-) create mode 100644 packages/babel-generator/test/fixtures/flow/this-param/input.js create mode 100644 packages/babel-generator/test/fixtures/flow/this-param/output.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/declare-function/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/declare-function/output.json create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/declare-method/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/declare-method/output.json create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/default/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/default/output.json create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/function-declaration/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/function-declaration/output.json create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/function-type/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/function-type/output.json create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/method/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/method/output.json create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/object-constructor/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/object-constructor/output.json create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/object-method-type-this-not-first/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/object-method-type-this-not-first/output.json create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/object-method-type/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/object-method-type/output.json create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/object-type-constructor/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/object-type-constructor/output.json create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-arrow-function/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-arrow-function/output.json create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-constructor-declare/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-constructor-declare/output.json create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-constructor/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-constructor/output.json create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-getter-type/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-getter-type/output.json create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-getter/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-getter/output.json create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-no-annot/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-no-annot/output.json create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-not-first-type/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-not-first-type/output.json create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-not-first/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-not-first/output.json create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-optional-type/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-optional-type/output.json create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-optional/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-optional/output.json create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-setter-type/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-setter-type/output.json create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-setter/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-setter/output.json create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-string-literal-constructor/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-string-literal-constructor/output.json create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-type/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/this-annotation/this-type/output.json create mode 100644 packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-this-param/input.js create mode 100644 packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-this-param/output.js create mode 100644 packages/babel-plugin-transform-parameters/test/fixtures/parameters/flow-this-param/input.js create mode 100644 packages/babel-plugin-transform-parameters/test/fixtures/parameters/flow-this-param/output.js diff --git a/packages/babel-generator/src/generators/flow.ts b/packages/babel-generator/src/generators/flow.ts index 587d3fcb85..25a5c33184 100644 --- a/packages/babel-generator/src/generators/flow.ts +++ b/packages/babel-generator/src/generators/flow.ts @@ -289,6 +289,18 @@ export function FunctionTypeAnnotation( ) { this.print(node.typeParameters, node); this.token("("); + + if (node.this) { + this.word("this"); + this.token(":"); + this.space(); + this.print(node.this.typeAnnotation, node); + if (node.params.length || node.rest) { + this.token(","); + this.space(); + } + } + this.printList(node.params, node); if (node.rest) { diff --git a/packages/babel-generator/test/fixtures/flow/this-param/input.js b/packages/babel-generator/test/fixtures/flow/this-param/input.js new file mode 100644 index 0000000000..deac524b71 --- /dev/null +++ b/packages/babel-generator/test/fixtures/flow/this-param/input.js @@ -0,0 +1,45 @@ +function one (this : number) {} +function two (this : number, a) {} +function three (this : number, ...a) {} +function four (this : number, a, b, ...c) {} +function five (this : T) {} + +type six = (this : number) => void +type seven = (this : number, a : number) => void +type eight = (this : number, ...a : any) => void +type nine = (this : T) => void + +type ten = { + m1(this : string) : void, + m2(this : string, a : number) : void, + m3(this : string, ...a : any) : void, + m4(this : T) : void +} + +declare function eleven (this : number) : void +declare function twelve (this : string, a : number) : void +declare function thirteen (this : string, ...a : any) : void +declare function fourteen(this : T) : void + +declare class Fifteen { + m1(this : string) : void, + m2(this : string, a : number) : void, + m3(this : string, ...a : any) : void, + m4(this : T) : void +} + +class Sixteen { + m1 (this : number) {} + m2 (this : number, a) {} + m3 (this : number, ...a) {} + m4 (this : number, a, b, ...c) {} + m5 (this : T) {} +} + +let seventeen = { + m1 (this : number) {}, + m2 (this : number, a) {}, + m3 (this : number, ...a) {}, + m4 (this : number, a, b, ...c) {}, + m5 (this : T) {} +} diff --git a/packages/babel-generator/test/fixtures/flow/this-param/output.js b/packages/babel-generator/test/fixtures/flow/this-param/output.js new file mode 100644 index 0000000000..6f10af8826 --- /dev/null +++ b/packages/babel-generator/test/fixtures/flow/this-param/output.js @@ -0,0 +1,56 @@ +function one(this: number) {} + +function two(this: number, a) {} + +function three(this: number, ...a) {} + +function four(this: number, a, b, ...c) {} + +function five(this: T) {} + +type six = (this: number) => void; +type seven = (this: number, a: number) => void; +type eight = (this: number, ...a: any) => void; +type nine = (this: T) => void; +type ten = { + m1(this: string): void, + m2(this: string, a: number): void, + m3(this: string, ...a: any): void, + m4(this: T): void, +}; +declare function eleven(this: number): void; +declare function twelve(this: string, a: number): void; +declare function thirteen(this: string, ...a: any): void; +declare function fourteen(this: T): void; +declare class Fifteen { + m1(this: string): void, + m2(this: string, a: number): void, + m3(this: string, ...a: any): void, + m4(this: T): void, +} + +class Sixteen { + m1(this: number) {} + + m2(this: number, a) {} + + m3(this: number, ...a) {} + + m4(this: number, a, b, ...c) {} + + m5(this: T) {} + +} + +let seventeen = { + m1(this: number) {}, + + m2(this: number, a) {}, + + m3(this: number, ...a) {}, + + m4(this: number, a, b, ...c) {}, + + m5(this: T) {} + +}; \ No newline at end of file diff --git a/packages/babel-parser/src/plugins/flow/index.js b/packages/babel-parser/src/plugins/flow/index.js index 0f606cfd35..2df5a06a9a 100644 --- a/packages/babel-parser/src/plugins/flow/index.js +++ b/packages/babel-parser/src/plugins/flow/index.js @@ -81,6 +81,7 @@ const FlowErrors = Object.freeze({ "Number enum members need to be initialized, e.g. `%1 = 1` in enum `%0`.", EnumStringMemberInconsistentlyInitailized: "String enum members need to consistently either all use initializers, or use no initializers, in enum `%0`.", + GetterMayNotHaveThisParam: "A getter cannot have a `this` parameter.", ImportTypeShorthandOnlyInPureImport: "The `type` and `typeof` keywords on named imports can only be used on regular `import` statements. It cannot be used with `import type` or `import typeof` statements", InexactInsideExact: @@ -97,7 +98,16 @@ const FlowErrors = Object.freeze({ NestedFlowComment: "Cannot have a flow comment inside another flow comment", OptionalBindingPattern: "A binding pattern parameter cannot be optional in an implementation signature.", + SetterMayNotHaveThisParam: "A setter cannot have a `this` parameter.", SpreadVariance: "Spread properties cannot have variance", + ThisParamAnnotationRequired: + "A type annotation is required for the `this` parameter.", + ThisParamBannedInConstructor: + "Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions.", + ThisParamMayNotBeOptional: "The `this` parameter cannot be optional.", + ThisParamMustBeFirst: + "The `this` parameter must be the first function parameter.", + ThisParamNoDefault: "The `this` parameter may not have a default value.", TypeBeforeInitializer: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`", TypeCastInPattern: @@ -308,6 +318,7 @@ export default (superClass: Class): Class => const tmp = this.flowParseFunctionTypeParams(); typeNode.params = tmp.params; typeNode.rest = tmp.rest; + typeNode.this = tmp._this; this.expect(tt.parenR); [ @@ -905,21 +916,30 @@ export default (superClass: Class): Class => node.params = []; node.rest = null; node.typeParameters = null; + node.this = null; if (this.isRelational("<")) { node.typeParameters = this.flowParseTypeParameterDeclaration(); } this.expect(tt.parenL); + if (this.match(tt._this)) { + node.this = this.flowParseFunctionTypeParam(/* first */ true); + // match Flow parser behavior + node.this.name = null; + if (!this.match(tt.parenR)) { + this.expect(tt.comma); + } + } while (!this.match(tt.parenR) && !this.match(tt.ellipsis)) { - node.params.push(this.flowParseFunctionTypeParam()); + node.params.push(this.flowParseFunctionTypeParam(false)); if (!this.match(tt.parenR)) { this.expect(tt.comma); } } if (this.eat(tt.ellipsis)) { - node.rest = this.flowParseFunctionTypeParam(); + node.rest = this.flowParseFunctionTypeParam(false); } this.expect(tt.parenR); node.returnType = this.flowParseTypeInitialiser(); @@ -1162,6 +1182,17 @@ export default (superClass: Class): Class => if (kind === "get" || kind === "set") { this.flowCheckGetterSetterParams(node); } + /** Declared classes/interfaces do not allow spread */ + if ( + !allowSpread && + node.key.name === "constructor" && + node.value.this + ) { + this.raise( + node.value.this.start, + FlowErrors.ThisParamBannedInConstructor, + ); + } } else { if (kind !== "init") this.unexpected(); @@ -1189,6 +1220,16 @@ export default (superClass: Class): Class => const start = property.start; const length = property.value.params.length + (property.value.rest ? 1 : 0); + + if (property.value.this) { + this.raise( + property.value.this.start, + property.kind === "get" + ? FlowErrors.GetterMayNotHaveThisParam + : FlowErrors.SetterMayNotHaveThisParam, + ); + } + if (length !== paramCount) { if (property.kind === "get") { this.raise(start, Errors.BadGetterArity); @@ -1270,16 +1311,24 @@ export default (superClass: Class): Class => return this.finishNode(node, "TupleTypeAnnotation"); } - flowParseFunctionTypeParam(): N.FlowFunctionTypeParam { + flowParseFunctionTypeParam(first: boolean): N.FlowFunctionTypeParam { let name = null; let optional = false; let typeAnnotation = null; const node = this.startNode(); const lh = this.lookahead(); + const isThis = this.state.type === tt._this; + if (lh.type === tt.colon || lh.type === tt.question) { - name = this.parseIdentifier(); + if (isThis && !first) { + this.raise(node.start, FlowErrors.ThisParamMustBeFirst); + } + name = this.parseIdentifier(isThis); if (this.eat(tt.question)) { optional = true; + if (isThis) { + this.raise(node.start, FlowErrors.ThisParamMayNotBeOptional); + } } typeAnnotation = this.flowParseTypeInitialiser(); } else { @@ -1303,18 +1352,31 @@ export default (superClass: Class): Class => flowParseFunctionTypeParams( params: N.FlowFunctionTypeParam[] = [], - ): { params: N.FlowFunctionTypeParam[], rest: ?N.FlowFunctionTypeParam } { + ): { + params: N.FlowFunctionTypeParam[], + rest: ?N.FlowFunctionTypeParam, + _this: ?N.FlowFunctionTypeParam, + } { let rest: ?N.FlowFunctionTypeParam = null; + let _this: ?N.FlowFunctionTypeParam = null; + if (this.match(tt._this)) { + _this = this.flowParseFunctionTypeParam(/* first */ true); + // match Flow parser behavior + _this.name = null; + if (!this.match(tt.parenR)) { + this.expect(tt.comma); + } + } while (!this.match(tt.parenR) && !this.match(tt.ellipsis)) { - params.push(this.flowParseFunctionTypeParam()); + params.push(this.flowParseFunctionTypeParam(false)); if (!this.match(tt.parenR)) { this.expect(tt.comma); } } if (this.eat(tt.ellipsis)) { - rest = this.flowParseFunctionTypeParam(); + rest = this.flowParseFunctionTypeParam(false); } - return { params, rest }; + return { params, rest, _this }; } flowIdentToTypeAnnotation( @@ -1408,6 +1470,7 @@ export default (superClass: Class): Class => tmp = this.flowParseFunctionTypeParams(); node.params = tmp.params; node.rest = tmp.rest; + node.this = tmp._this; this.expect(tt.parenR); this.expect(tt.arrow); @@ -1423,7 +1486,7 @@ export default (superClass: Class): Class => // Check to see if this is actually a grouped type if (!this.match(tt.parenR) && !this.match(tt.ellipsis)) { - if (this.match(tt.name)) { + if (this.match(tt.name) || this.match(tt._this)) { const token = this.lookahead().type; isGroupedType = token !== tt.question && token !== tt.colon; } else { @@ -1462,6 +1525,7 @@ export default (superClass: Class): Class => node.params = tmp.params; node.rest = tmp.rest; + node.this = tmp._this; this.expect(tt.parenR); @@ -2311,6 +2375,11 @@ export default (superClass: Class): Class => return !this.match(tt.colon) && super.isNonstaticConstructor(method); } + // determine whether a parameter is a this param + isThisParam(param) { + return param.type === "Identifier" && param.name === "this"; + } + // parse type parameters for class methods pushClassMethod( classBody: N.ClassBody, @@ -2336,6 +2405,24 @@ export default (superClass: Class): Class => isConstructor, allowsDirectSuper, ); + + if (method.params && isConstructor) { + const params = method.params; + if (params.length > 0 && this.isThisParam(params[0])) { + this.raise(method.start, FlowErrors.ThisParamBannedInConstructor); + } + // estree support + } else if ( + // $FlowFixMe flow does not know about the face that estree can replace ClassMethod with MethodDefinition + method.type === "MethodDefinition" && + isConstructor && + method.value.params + ) { + const params = method.value.params; + if (params.length > 0 && this.isThisParam(params[0])) { + this.raise(method.start, FlowErrors.ThisParamBannedInConstructor); + } + } } pushClassPrivateMethod( @@ -2377,6 +2464,19 @@ export default (superClass: Class): Class => } } + checkGetterSetterParams(method: N.ObjectMethod | N.ClassMethod): void { + super.checkGetterSetterParams(method); + const params = this.getObjectOrClassMethodParams(method); + if (params.length > 0) { + const param = params[0]; + if (this.isThisParam(param) && method.kind === "get") { + this.raise(param.start, FlowErrors.GetterMayNotHaveThisParam); + } else if (this.isThisParam(param)) { + this.raise(param.start, FlowErrors.SetterMayNotHaveThisParam); + } + } + } + parsePropertyName( node: N.ObjectOrClassMember | N.ClassMember | N.TsNamedTypeElementBase, isPrivateNameAllowed: boolean, @@ -2434,12 +2534,22 @@ export default (superClass: Class): Class => if (param.type !== "Identifier") { this.raise(param.start, FlowErrors.OptionalBindingPattern); } + if (this.isThisParam(param)) { + this.raise(param.start, FlowErrors.ThisParamMayNotBeOptional); + } ((param: any): N.Identifier).optional = true; } if (this.match(tt.colon)) { param.typeAnnotation = this.flowParseTypeAnnotation(); + } else if (this.isThisParam(param)) { + this.raise(param.start, FlowErrors.ThisParamAnnotationRequired); } + + if (this.match(tt.eq) && this.isThisParam(param)) { + this.raise(param.start, FlowErrors.ThisParamNoDefault); + } + this.resetEndLocation(param); return param; } @@ -2609,6 +2719,16 @@ export default (superClass: Class): Class => node.specifiers.push(this.finishNode(specifier, "ImportSpecifier")); } + parseBindingAtom(): N.Pattern { + switch (this.state.type) { + case tt._this: + // "this" may be the name of a parameter, so allow it. + return this.parseIdentifier(/* liberal */ true); + default: + return super.parseBindingAtom(); + } + } + // parse function type parameters - function foo() {} parseFunctionParams(node: N.Function, allowModifiers?: boolean): void { // $FlowFixMe @@ -2866,6 +2986,13 @@ export default (superClass: Class): Class => return; } + // ensure the `this` param is first, if it exists + for (let i = 0; i < node.params.length; i++) { + if (this.isThisParam(node.params[i]) && i > 0) { + this.raise(node.params[i].start, FlowErrors.ThisParamMustBeFirst); + } + } + return super.checkParams(...arguments); } diff --git a/packages/babel-parser/test/fixtures/estree/object-method/invalid-setter/output.json b/packages/babel-parser/test/fixtures/estree/object-method/invalid-setter/output.json index bba388a434..98c7c647bf 100644 --- a/packages/babel-parser/test/fixtures/estree/object-method/invalid-setter/output.json +++ b/packages/babel-parser/test/fixtures/estree/object-method/invalid-setter/output.json @@ -16,6 +16,10 @@ "expression": { "type": "ObjectExpression", "start":1,"end":14,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":14}}, + "extra": { + "parenthesized": true, + "parenStart": 0 + }, "properties": [ { "type": "Property", @@ -44,11 +48,7 @@ }, "shorthand": false } - ], - "extra": { - "parenthesized": true, - "parenStart": 0 - } + ] } } ] diff --git a/packages/babel-parser/test/fixtures/flow/anonymous-function-no-parens-types/good_10/output.json b/packages/babel-parser/test/fixtures/flow/anonymous-function-no-parens-types/good_10/output.json index 33e95403e7..5e4a04fcf8 100644 --- a/packages/babel-parser/test/fixtures/flow/anonymous-function-no-parens-types/good_10/output.json +++ b/packages/babel-parser/test/fixtures/flow/anonymous-function-no-parens-types/good_10/output.json @@ -50,6 +50,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":32,"end":38,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":38}} diff --git a/packages/babel-parser/test/fixtures/flow/anonymous-function-no-parens-types/good_14/output.json b/packages/babel-parser/test/fixtures/flow/anonymous-function-no-parens-types/good_14/output.json index eadc2d9853..67e57a7f76 100644 --- a/packages/babel-parser/test/fixtures/flow/anonymous-function-no-parens-types/good_14/output.json +++ b/packages/babel-parser/test/fixtures/flow/anonymous-function-no-parens-types/good_14/output.json @@ -39,6 +39,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":27,"end":33,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":33}} diff --git a/packages/babel-parser/test/fixtures/flow/anonymous-function-no-parens-types/good_15/output.json b/packages/babel-parser/test/fixtures/flow/anonymous-function-no-parens-types/good_15/output.json index 64a149197c..cb3951a318 100644 --- a/packages/babel-parser/test/fixtures/flow/anonymous-function-no-parens-types/good_15/output.json +++ b/packages/babel-parser/test/fixtures/flow/anonymous-function-no-parens-types/good_15/output.json @@ -49,6 +49,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":30,"end":36,"loc":{"start":{"line":1,"column":30},"end":{"line":1,"column":36}} diff --git a/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_01/output.json b/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_01/output.json index 00814ac016..7e1547e599 100644 --- a/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_01/output.json +++ b/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_01/output.json @@ -48,6 +48,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":41,"end":45,"loc":{"start":{"line":1,"column":41},"end":{"line":1,"column":45}} diff --git a/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_03/output.json b/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_03/output.json index 8dcd1b0be6..908fb3b426 100644 --- a/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_03/output.json +++ b/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_03/output.json @@ -32,6 +32,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":21,"end":25,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":25}} diff --git a/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_04/output.json b/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_04/output.json index 5dc10956fa..ed6189670e 100644 --- a/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_04/output.json +++ b/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_04/output.json @@ -32,6 +32,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":22,"end":26,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":26}} diff --git a/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_05/output.json b/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_05/output.json index 0125e6307f..3ec32b3977 100644 --- a/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_05/output.json +++ b/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_05/output.json @@ -47,6 +47,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":28,"end":32,"loc":{"start":{"line":1,"column":28},"end":{"line":1,"column":32}} diff --git a/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_06/output.json b/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_06/output.json index c8e969c818..76bacf0758 100644 --- a/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_06/output.json +++ b/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_06/output.json @@ -47,6 +47,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":29,"end":33,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":33}} diff --git a/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_07/output.json b/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_07/output.json index 9441e11a5d..951c19736c 100644 --- a/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_07/output.json +++ b/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_07/output.json @@ -46,6 +46,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":32,"end":36,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":36}} diff --git a/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_08/output.json b/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_08/output.json index c39333b886..68b0835fdf 100644 --- a/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_08/output.json +++ b/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_08/output.json @@ -45,6 +45,7 @@ } } }, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":31,"end":35,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":35}} diff --git a/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_09/output.json b/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_09/output.json index 7e3230844e..2c15f4a41e 100644 --- a/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_09/output.json +++ b/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_09/output.json @@ -71,6 +71,7 @@ } } }, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":46,"end":50,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":50}} diff --git a/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_10/output.json b/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_10/output.json index 316359169e..5f7e4b4827 100644 --- a/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_10/output.json +++ b/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_10/output.json @@ -46,6 +46,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "NumberLiteralTypeAnnotation", "start":28,"end":31,"loc":{"start":{"line":1,"column":28},"end":{"line":1,"column":31}}, diff --git a/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_13/output.json b/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_13/output.json index 0aba1cc06f..536676cd37 100644 --- a/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_13/output.json +++ b/packages/babel-parser/test/fixtures/flow/anonymous-function-types/good_13/output.json @@ -42,6 +42,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "NumberLiteralTypeAnnotation", "start":26,"end":29,"loc":{"start":{"line":1,"column":26},"end":{"line":1,"column":29}}, diff --git a/packages/babel-parser/test/fixtures/flow/array-types/4/output.json b/packages/babel-parser/test/fixtures/flow/array-types/4/output.json index 7d395e9205..909ca9cce0 100644 --- a/packages/babel-parser/test/fixtures/flow/array-types/4/output.json +++ b/packages/babel-parser/test/fixtures/flow/array-types/4/output.json @@ -26,6 +26,7 @@ "start":7,"end":21,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":21}}, "params": [], "rest": null, + "this": null, "returnType": { "type": "ArrayTypeAnnotation", "start":13,"end":21,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":21}}, diff --git a/packages/babel-parser/test/fixtures/flow/array-types/5/output.json b/packages/babel-parser/test/fixtures/flow/array-types/5/output.json index f6fbb17357..b38fb7bf05 100644 --- a/packages/babel-parser/test/fixtures/flow/array-types/5/output.json +++ b/packages/babel-parser/test/fixtures/flow/array-types/5/output.json @@ -29,6 +29,7 @@ "start":8,"end":20,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":20}}, "params": [], "rest": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":14,"end":20,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":20}} diff --git a/packages/babel-parser/test/fixtures/flow/call-properties/1/output.json b/packages/babel-parser/test/fixtures/flow/call-properties/1/output.json index 225c82bca9..812bbf31ad 100644 --- a/packages/babel-parser/test/fixtures/flow/call-properties/1/output.json +++ b/packages/babel-parser/test/fixtures/flow/call-properties/1/output.json @@ -35,6 +35,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":14,"end":20,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":20}} diff --git a/packages/babel-parser/test/fixtures/flow/call-properties/2/output.json b/packages/babel-parser/test/fixtures/flow/call-properties/2/output.json index 7c3d10f344..9fc09769be 100644 --- a/packages/babel-parser/test/fixtures/flow/call-properties/2/output.json +++ b/packages/babel-parser/test/fixtures/flow/call-properties/2/output.json @@ -35,6 +35,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":14,"end":20,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":20}} diff --git a/packages/babel-parser/test/fixtures/flow/call-properties/3/output.json b/packages/babel-parser/test/fixtures/flow/call-properties/3/output.json index 84334ba4a9..4b1644494a 100644 --- a/packages/babel-parser/test/fixtures/flow/call-properties/3/output.json +++ b/packages/babel-parser/test/fixtures/flow/call-properties/3/output.json @@ -35,6 +35,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":14,"end":20,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":20}} @@ -66,6 +67,7 @@ ], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "StringTypeAnnotation", "start":46,"end":52,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":52}} diff --git a/packages/babel-parser/test/fixtures/flow/call-properties/4/output.json b/packages/babel-parser/test/fixtures/flow/call-properties/4/output.json index 3f664920aa..086f5b45d7 100644 --- a/packages/babel-parser/test/fixtures/flow/call-properties/4/output.json +++ b/packages/babel-parser/test/fixtures/flow/call-properties/4/output.json @@ -67,6 +67,7 @@ } ] }, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":21,"end":27,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":27}} diff --git a/packages/babel-parser/test/fixtures/flow/call-properties/5/output.json b/packages/babel-parser/test/fixtures/flow/call-properties/5/output.json index 891892e2f7..118b6d1a08 100644 --- a/packages/babel-parser/test/fixtures/flow/call-properties/5/output.json +++ b/packages/babel-parser/test/fixtures/flow/call-properties/5/output.json @@ -33,6 +33,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":18,"end":24,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":24}} diff --git a/packages/babel-parser/test/fixtures/flow/class-properties/getter-setter/output.json b/packages/babel-parser/test/fixtures/flow/class-properties/getter-setter/output.json index f35d695d38..46c34ef531 100644 --- a/packages/babel-parser/test/fixtures/flow/class-properties/getter-setter/output.json +++ b/packages/babel-parser/test/fixtures/flow/class-properties/getter-setter/output.json @@ -42,6 +42,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":29,"end":35,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":17}} @@ -82,6 +83,7 @@ ], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":57,"end":61,"loc":{"start":{"line":3,"column":20},"end":{"line":3,"column":24}} @@ -111,6 +113,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":76,"end":82,"loc":{"start":{"line":4,"column":13},"end":{"line":4,"column":19}} @@ -155,6 +158,7 @@ ], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":106,"end":110,"loc":{"start":{"line":5,"column":22},"end":{"line":5,"column":26}} @@ -184,6 +188,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":123,"end":129,"loc":{"start":{"line":6,"column":11},"end":{"line":6,"column":17}} @@ -228,6 +233,7 @@ ], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":151,"end":155,"loc":{"start":{"line":7,"column":20},"end":{"line":7,"column":24}} diff --git a/packages/babel-parser/test/fixtures/flow/classes/constructor-override-with-class-prop-plugin/output.json b/packages/babel-parser/test/fixtures/flow/classes/constructor-override-with-class-prop-plugin/output.json index e7dba971db..078602d8ac 100644 --- a/packages/babel-parser/test/fixtures/flow/classes/constructor-override-with-class-prop-plugin/output.json +++ b/packages/babel-parser/test/fixtures/flow/classes/constructor-override-with-class-prop-plugin/output.json @@ -42,6 +42,7 @@ "start":27,"end":37,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":25}}, "params": [], "rest": null, + "this": null, "returnType": { "type": "ThisTypeAnnotation", "start":33,"end":37,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":25}} diff --git a/packages/babel-parser/test/fixtures/flow/declare-export/export-class/output.json b/packages/babel-parser/test/fixtures/flow/declare-export/export-class/output.json index 7e96f7035a..b2ef70379a 100644 --- a/packages/babel-parser/test/fixtures/flow/declare-export/export-class/output.json +++ b/packages/babel-parser/test/fixtures/flow/declare-export/export-class/output.json @@ -76,6 +76,7 @@ ], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":68,"end":72,"loc":{"start":{"line":1,"column":68},"end":{"line":1,"column":72}} diff --git a/packages/babel-parser/test/fixtures/flow/declare-export/export-default-arrow/output.json b/packages/babel-parser/test/fixtures/flow/declare-export/export-default-arrow/output.json index f479dcf7f1..1b6d2b6529 100644 --- a/packages/babel-parser/test/fixtures/flow/declare-export/export-default-arrow/output.json +++ b/packages/babel-parser/test/fixtures/flow/declare-export/export-default-arrow/output.json @@ -30,6 +30,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":37,"end":43,"loc":{"start":{"line":1,"column":37},"end":{"line":1,"column":43}} diff --git a/packages/babel-parser/test/fixtures/flow/declare-export/export-default-function/output.json b/packages/babel-parser/test/fixtures/flow/declare-export/export-default-function/output.json index 670fd3f48b..9f6fe31dd5 100644 --- a/packages/babel-parser/test/fixtures/flow/declare-export/export-default-function/output.json +++ b/packages/babel-parser/test/fixtures/flow/declare-export/export-default-function/output.json @@ -41,6 +41,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "StringTypeAnnotation", "start":49,"end":55,"loc":{"start":{"line":1,"column":49},"end":{"line":1,"column":55}} diff --git a/packages/babel-parser/test/fixtures/flow/declare-export/export-function/output.json b/packages/babel-parser/test/fixtures/flow/declare-export/export-function/output.json index db29c7c10d..b9bdc4a556 100644 --- a/packages/babel-parser/test/fixtures/flow/declare-export/export-function/output.json +++ b/packages/babel-parser/test/fixtures/flow/declare-export/export-function/output.json @@ -57,6 +57,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "StringTypeAnnotation", "start":64,"end":70,"loc":{"start":{"line":1,"column":64},"end":{"line":1,"column":70}} diff --git a/packages/babel-parser/test/fixtures/flow/declare-module/10/output.json b/packages/babel-parser/test/fixtures/flow/declare-module/10/output.json index ffea09887f..e360b80f3b 100644 --- a/packages/babel-parser/test/fixtures/flow/declare-module/10/output.json +++ b/packages/babel-parser/test/fixtures/flow/declare-module/10/output.json @@ -36,6 +36,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":33,"end":39,"loc":{"start":{"line":1,"column":33},"end":{"line":1,"column":39}} diff --git a/packages/babel-parser/test/fixtures/flow/declare-module/4/output.json b/packages/babel-parser/test/fixtures/flow/declare-module/4/output.json index 4815b5f5dc..31d82465a5 100644 --- a/packages/babel-parser/test/fixtures/flow/declare-module/4/output.json +++ b/packages/babel-parser/test/fixtures/flow/declare-module/4/output.json @@ -35,6 +35,7 @@ "typeParameters": null, "params": [], "rest": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":43,"end":49,"loc":{"start":{"line":1,"column":43},"end":{"line":1,"column":49}} diff --git a/packages/babel-parser/test/fixtures/flow/declare-module/5/output.json b/packages/babel-parser/test/fixtures/flow/declare-module/5/output.json index 20e0428453..b4a8c7abe6 100644 --- a/packages/babel-parser/test/fixtures/flow/declare-module/5/output.json +++ b/packages/babel-parser/test/fixtures/flow/declare-module/5/output.json @@ -54,6 +54,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":44,"end":50,"loc":{"start":{"line":1,"column":44},"end":{"line":1,"column":50}} diff --git a/packages/babel-parser/test/fixtures/flow/declare-module/6/output.json b/packages/babel-parser/test/fixtures/flow/declare-module/6/output.json index dcc3dbe026..b1e3e5d06a 100644 --- a/packages/babel-parser/test/fixtures/flow/declare-module/6/output.json +++ b/packages/babel-parser/test/fixtures/flow/declare-module/6/output.json @@ -48,6 +48,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":52,"end":58,"loc":{"start":{"line":1,"column":52},"end":{"line":1,"column":58}} diff --git a/packages/babel-parser/test/fixtures/flow/declare-statements/10/output.json b/packages/babel-parser/test/fixtures/flow/declare-statements/10/output.json index 06a5d571ff..921856f927 100644 --- a/packages/babel-parser/test/fixtures/flow/declare-statements/10/output.json +++ b/packages/babel-parser/test/fixtures/flow/declare-statements/10/output.json @@ -42,6 +42,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":32,"end":38,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":38}} diff --git a/packages/babel-parser/test/fixtures/flow/declare-statements/12/output.json b/packages/babel-parser/test/fixtures/flow/declare-statements/12/output.json index 63f60d28b1..beff50dce4 100644 --- a/packages/babel-parser/test/fixtures/flow/declare-statements/12/output.json +++ b/packages/babel-parser/test/fixtures/flow/declare-statements/12/output.json @@ -33,6 +33,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":30,"end":36,"loc":{"start":{"line":1,"column":30},"end":{"line":1,"column":36}} diff --git a/packages/babel-parser/test/fixtures/flow/declare-statements/18/output.json b/packages/babel-parser/test/fixtures/flow/declare-statements/18/output.json index faab69fb62..bae862069d 100644 --- a/packages/babel-parser/test/fixtures/flow/declare-statements/18/output.json +++ b/packages/babel-parser/test/fixtures/flow/declare-statements/18/output.json @@ -33,6 +33,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":30,"end":36,"loc":{"start":{"line":1,"column":30},"end":{"line":1,"column":36}} @@ -72,6 +73,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":62,"end":68,"loc":{"start":{"line":2,"column":23},"end":{"line":2,"column":29}} diff --git a/packages/babel-parser/test/fixtures/flow/declare-statements/3/output.json b/packages/babel-parser/test/fixtures/flow/declare-statements/3/output.json index 64089206d0..bd3076f29b 100644 --- a/packages/babel-parser/test/fixtures/flow/declare-statements/3/output.json +++ b/packages/babel-parser/test/fixtures/flow/declare-statements/3/output.json @@ -23,6 +23,7 @@ "typeParameters": null, "params": [], "rest": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":24,"end":28,"loc":{"start":{"line":1,"column":24},"end":{"line":1,"column":28}} diff --git a/packages/babel-parser/test/fixtures/flow/declare-statements/4/output.json b/packages/babel-parser/test/fixtures/flow/declare-statements/4/output.json index f377b44c81..d3af897197 100644 --- a/packages/babel-parser/test/fixtures/flow/declare-statements/4/output.json +++ b/packages/babel-parser/test/fixtures/flow/declare-statements/4/output.json @@ -23,6 +23,7 @@ "typeParameters": null, "params": [], "rest": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":24,"end":28,"loc":{"start":{"line":1,"column":24},"end":{"line":1,"column":28}} diff --git a/packages/babel-parser/test/fixtures/flow/declare-statements/5/output.json b/packages/babel-parser/test/fixtures/flow/declare-statements/5/output.json index 033bfd0bfc..8fb724f7ed 100644 --- a/packages/babel-parser/test/fixtures/flow/declare-statements/5/output.json +++ b/packages/babel-parser/test/fixtures/flow/declare-statements/5/output.json @@ -34,6 +34,7 @@ }, "params": [], "rest": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":27,"end":31,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":31}} diff --git a/packages/babel-parser/test/fixtures/flow/declare-statements/6/output.json b/packages/babel-parser/test/fixtures/flow/declare-statements/6/output.json index 7a8d401120..e7a071e819 100644 --- a/packages/babel-parser/test/fixtures/flow/declare-statements/6/output.json +++ b/packages/babel-parser/test/fixtures/flow/declare-statements/6/output.json @@ -52,6 +52,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":44,"end":48,"loc":{"start":{"line":1,"column":44},"end":{"line":1,"column":48}} diff --git a/packages/babel-parser/test/fixtures/flow/declare-statements/7/output.json b/packages/babel-parser/test/fixtures/flow/declare-statements/7/output.json index e5ae7dd2d3..8f4a842da2 100644 --- a/packages/babel-parser/test/fixtures/flow/declare-statements/7/output.json +++ b/packages/babel-parser/test/fixtures/flow/declare-statements/7/output.json @@ -77,6 +77,7 @@ ], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":67,"end":71,"loc":{"start":{"line":1,"column":67},"end":{"line":1,"column":71}} diff --git a/packages/babel-parser/test/fixtures/flow/interface-types/identifier-named-static-method/output.json b/packages/babel-parser/test/fixtures/flow/interface-types/identifier-named-static-method/output.json index db1accdec4..e6b9035c5b 100644 --- a/packages/babel-parser/test/fixtures/flow/interface-types/identifier-named-static-method/output.json +++ b/packages/babel-parser/test/fixtures/flow/interface-types/identifier-named-static-method/output.json @@ -43,6 +43,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":31,"end":37,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":37}} diff --git a/packages/babel-parser/test/fixtures/flow/interfaces-module-and-script/10/output.json b/packages/babel-parser/test/fixtures/flow/interfaces-module-and-script/10/output.json index 08a97862a7..85dc1ab0ac 100644 --- a/packages/babel-parser/test/fixtures/flow/interfaces-module-and-script/10/output.json +++ b/packages/babel-parser/test/fixtures/flow/interfaces-module-and-script/10/output.json @@ -61,6 +61,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":44,"end":48,"loc":{"start":{"line":3,"column":13},"end":{"line":3,"column":17}} diff --git a/packages/babel-parser/test/fixtures/flow/interfaces-module-and-script/4/output.json b/packages/babel-parser/test/fixtures/flow/interfaces-module-and-script/4/output.json index a4261a11c1..c779878f81 100644 --- a/packages/babel-parser/test/fixtures/flow/interfaces-module-and-script/4/output.json +++ b/packages/babel-parser/test/fixtures/flow/interfaces-module-and-script/4/output.json @@ -41,6 +41,7 @@ "start":19,"end":31,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":31}}, "params": [], "rest": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":25,"end":31,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":31}} diff --git a/packages/babel-parser/test/fixtures/flow/interfaces-module-and-script/identifier-named-static-method/output.json b/packages/babel-parser/test/fixtures/flow/interfaces-module-and-script/identifier-named-static-method/output.json index b083454821..eac134286c 100644 --- a/packages/babel-parser/test/fixtures/flow/interfaces-module-and-script/identifier-named-static-method/output.json +++ b/packages/babel-parser/test/fixtures/flow/interfaces-module-and-script/identifier-named-static-method/output.json @@ -42,6 +42,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":24,"end":30,"loc":{"start":{"line":1,"column":24},"end":{"line":1,"column":30}} diff --git a/packages/babel-parser/test/fixtures/flow/internal-slot/interface-method/output.json b/packages/babel-parser/test/fixtures/flow/internal-slot/interface-method/output.json index 285585f5d6..e61dc7bd65 100644 --- a/packages/babel-parser/test/fixtures/flow/internal-slot/interface-method/output.json +++ b/packages/babel-parser/test/fixtures/flow/internal-slot/interface-method/output.json @@ -43,6 +43,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "GenericTypeAnnotation", "start":25,"end":26,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":26}}, diff --git a/packages/babel-parser/test/fixtures/flow/internal-slot/object-method/output.json b/packages/babel-parser/test/fixtures/flow/internal-slot/object-method/output.json index 16491604be..51061a05eb 100644 --- a/packages/babel-parser/test/fixtures/flow/internal-slot/object-method/output.json +++ b/packages/babel-parser/test/fixtures/flow/internal-slot/object-method/output.json @@ -40,6 +40,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "GenericTypeAnnotation", "start":22,"end":23,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":23}}, diff --git a/packages/babel-parser/test/fixtures/flow/iterator/01/output.json b/packages/babel-parser/test/fixtures/flow/iterator/01/output.json index 9cf79269df..f606edd835 100644 --- a/packages/babel-parser/test/fixtures/flow/iterator/01/output.json +++ b/packages/babel-parser/test/fixtures/flow/iterator/01/output.json @@ -42,6 +42,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "GenericTypeAnnotation", "start":34,"end":48,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":30}}, diff --git a/packages/babel-parser/test/fixtures/flow/iterator/02/output.json b/packages/babel-parser/test/fixtures/flow/iterator/02/output.json index 91a02b2c73..764bc16513 100644 --- a/packages/babel-parser/test/fixtures/flow/iterator/02/output.json +++ b/packages/babel-parser/test/fixtures/flow/iterator/02/output.json @@ -42,6 +42,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "GenericTypeAnnotation", "start":39,"end":53,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":35}}, diff --git a/packages/babel-parser/test/fixtures/flow/iterator/12/output.json b/packages/babel-parser/test/fixtures/flow/iterator/12/output.json index c8866d6974..75fcbeb9c0 100644 --- a/packages/babel-parser/test/fixtures/flow/iterator/12/output.json +++ b/packages/babel-parser/test/fixtures/flow/iterator/12/output.json @@ -44,6 +44,7 @@ "start":30,"end":42,"loc":{"start":{"line":1,"column":30},"end":{"line":1,"column":42}}, "params": [], "rest": null, + "this": null, "returnType": { "type": "StringTypeAnnotation", "start":36,"end":42,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":42}} @@ -70,6 +71,10 @@ "argument": { "type": "TypeCastExpression", "start":57,"end":63,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":16}}, + "extra": { + "parenthesized": true, + "parenStart": 56 + }, "expression": { "type": "NumericLiteral", "start":57,"end":58,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":11}}, @@ -86,10 +91,6 @@ "type": "AnyTypeAnnotation", "start":60,"end":63,"loc":{"start":{"line":2,"column":13},"end":{"line":2,"column":16}} } - }, - "extra": { - "parenthesized": true, - "parenStart": 56 } } } diff --git a/packages/babel-parser/test/fixtures/flow/iterator/13/output.json b/packages/babel-parser/test/fixtures/flow/iterator/13/output.json index 3d995d3398..a7d70847b5 100644 --- a/packages/babel-parser/test/fixtures/flow/iterator/13/output.json +++ b/packages/babel-parser/test/fixtures/flow/iterator/13/output.json @@ -44,6 +44,7 @@ "start":35,"end":47,"loc":{"start":{"line":1,"column":35},"end":{"line":1,"column":47}}, "params": [], "rest": null, + "this": null, "returnType": { "type": "StringTypeAnnotation", "start":41,"end":47,"loc":{"start":{"line":1,"column":41},"end":{"line":1,"column":47}} @@ -70,6 +71,10 @@ "argument": { "type": "TypeCastExpression", "start":62,"end":68,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":16}}, + "extra": { + "parenthesized": true, + "parenStart": 61 + }, "expression": { "type": "NumericLiteral", "start":62,"end":63,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":11}}, @@ -86,10 +91,6 @@ "type": "AnyTypeAnnotation", "start":65,"end":68,"loc":{"start":{"line":2,"column":13},"end":{"line":2,"column":16}} } - }, - "extra": { - "parenthesized": true, - "parenStart": 61 } } } diff --git a/packages/babel-parser/test/fixtures/flow/iterator/14/output.json b/packages/babel-parser/test/fixtures/flow/iterator/14/output.json index 3e8644cc75..a511d794d2 100644 --- a/packages/babel-parser/test/fixtures/flow/iterator/14/output.json +++ b/packages/babel-parser/test/fixtures/flow/iterator/14/output.json @@ -42,6 +42,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "GenericTypeAnnotation", "start":30,"end":44,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":30}}, diff --git a/packages/babel-parser/test/fixtures/flow/iterator/15/output.json b/packages/babel-parser/test/fixtures/flow/iterator/15/output.json index d88ede13e9..d9ce5b8903 100644 --- a/packages/babel-parser/test/fixtures/flow/iterator/15/output.json +++ b/packages/babel-parser/test/fixtures/flow/iterator/15/output.json @@ -42,6 +42,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "GenericTypeAnnotation", "start":35,"end":49,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":35}}, diff --git a/packages/babel-parser/test/fixtures/flow/multiple-declarations/declare-function/output.json b/packages/babel-parser/test/fixtures/flow/multiple-declarations/declare-function/output.json index f9915cba13..b1265cceb6 100644 --- a/packages/babel-parser/test/fixtures/flow/multiple-declarations/declare-function/output.json +++ b/packages/babel-parser/test/fixtures/flow/multiple-declarations/declare-function/output.json @@ -23,6 +23,7 @@ "typeParameters": null, "params": [], "rest": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":23,"end":27,"loc":{"start":{"line":1,"column":23},"end":{"line":1,"column":27}} @@ -48,6 +49,7 @@ "typeParameters": null, "params": [], "rest": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":51,"end":55,"loc":{"start":{"line":2,"column":23},"end":{"line":2,"column":27}} diff --git a/packages/babel-parser/test/fixtures/flow/object-types/complex-param-types/output.json b/packages/babel-parser/test/fixtures/flow/object-types/complex-param-types/output.json index 948d1edb1a..7d0f9faf2d 100644 --- a/packages/babel-parser/test/fixtures/flow/object-types/complex-param-types/output.json +++ b/packages/babel-parser/test/fixtures/flow/object-types/complex-param-types/output.json @@ -66,6 +66,7 @@ ], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":25,"end":29,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":29}} diff --git a/packages/babel-parser/test/fixtures/flow/object-types/invalid-getter-param-count-rest/output.json b/packages/babel-parser/test/fixtures/flow/object-types/invalid-getter-param-count-rest/output.json index e100d1aafc..200019bb51 100644 --- a/packages/babel-parser/test/fixtures/flow/object-types/invalid-getter-param-count-rest/output.json +++ b/packages/babel-parser/test/fixtures/flow/object-types/invalid-getter-param-count-rest/output.json @@ -57,6 +57,7 @@ } }, "typeParameters": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":28,"end":34,"loc":{"start":{"line":2,"column":17},"end":{"line":2,"column":23}} diff --git a/packages/babel-parser/test/fixtures/flow/object-types/invalid-getter-param-count/output.json b/packages/babel-parser/test/fixtures/flow/object-types/invalid-getter-param-count/output.json index 0c62673bf1..d98c7d2c41 100644 --- a/packages/babel-parser/test/fixtures/flow/object-types/invalid-getter-param-count/output.json +++ b/packages/babel-parser/test/fixtures/flow/object-types/invalid-getter-param-count/output.json @@ -57,6 +57,7 @@ ], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":32,"end":38,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":27}} diff --git a/packages/babel-parser/test/fixtures/flow/object-types/invalid-setter-param-count/output.json b/packages/babel-parser/test/fixtures/flow/object-types/invalid-setter-param-count/output.json index 345f3c0c34..fa18c3c235 100644 --- a/packages/babel-parser/test/fixtures/flow/object-types/invalid-setter-param-count/output.json +++ b/packages/babel-parser/test/fixtures/flow/object-types/invalid-setter-param-count/output.json @@ -42,6 +42,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":22,"end":26,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":15}} diff --git a/packages/babel-parser/test/fixtures/flow/object-types/invalid-setter-param-type/output.json b/packages/babel-parser/test/fixtures/flow/object-types/invalid-setter-param-type/output.json index 07259bb336..b2207b3a6f 100644 --- a/packages/babel-parser/test/fixtures/flow/object-types/invalid-setter-param-type/output.json +++ b/packages/babel-parser/test/fixtures/flow/object-types/invalid-setter-param-type/output.json @@ -57,6 +57,7 @@ } }, "typeParameters": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":26,"end":30,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":19}} diff --git a/packages/babel-parser/test/fixtures/flow/predicates/1/output.json b/packages/babel-parser/test/fixtures/flow/predicates/1/output.json index bd9ce62767..6ff5da59cb 100644 --- a/packages/babel-parser/test/fixtures/flow/predicates/1/output.json +++ b/packages/babel-parser/test/fixtures/flow/predicates/1/output.json @@ -38,6 +38,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "BooleanTypeAnnotation", "start":32,"end":39,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":39}} diff --git a/packages/babel-parser/test/fixtures/flow/predicates/6/output.json b/packages/babel-parser/test/fixtures/flow/predicates/6/output.json index e3602f7e23..2300a1b299 100644 --- a/packages/babel-parser/test/fixtures/flow/predicates/6/output.json +++ b/packages/babel-parser/test/fixtures/flow/predicates/6/output.json @@ -124,6 +124,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "GenericTypeAnnotation", "start":64,"end":85,"loc":{"start":{"line":1,"column":64},"end":{"line":1,"column":85}}, diff --git a/packages/babel-parser/test/fixtures/flow/regression/issue-321/output.json b/packages/babel-parser/test/fixtures/flow/regression/issue-321/output.json index 0c22b938a4..f2be47a37d 100644 --- a/packages/babel-parser/test/fixtures/flow/regression/issue-321/output.json +++ b/packages/babel-parser/test/fixtures/flow/regression/issue-321/output.json @@ -63,6 +63,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":33,"end":37,"loc":{"start":{"line":1,"column":33},"end":{"line":1,"column":37}} @@ -155,6 +156,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":93,"end":97,"loc":{"start":{"line":2,"column":35},"end":{"line":2,"column":39}} diff --git a/packages/babel-parser/test/fixtures/flow/scope/declare-function-export/output.json b/packages/babel-parser/test/fixtures/flow/scope/declare-function-export/output.json index 2e29413949..905af01b17 100644 --- a/packages/babel-parser/test/fixtures/flow/scope/declare-function-export/output.json +++ b/packages/babel-parser/test/fixtures/flow/scope/declare-function-export/output.json @@ -23,6 +23,7 @@ "typeParameters": null, "params": [], "rest": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":24,"end":28,"loc":{"start":{"line":1,"column":24},"end":{"line":1,"column":28}} diff --git a/packages/babel-parser/test/fixtures/flow/scope/dupl-decl-declare-func-declare-func/output.json b/packages/babel-parser/test/fixtures/flow/scope/dupl-decl-declare-func-declare-func/output.json index de9cd47bb3..6656daffab 100644 --- a/packages/babel-parser/test/fixtures/flow/scope/dupl-decl-declare-func-declare-func/output.json +++ b/packages/babel-parser/test/fixtures/flow/scope/dupl-decl-declare-func-declare-func/output.json @@ -23,6 +23,7 @@ "typeParameters": null, "params": [], "rest": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":22,"end":26,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":26}} @@ -48,6 +49,7 @@ "typeParameters": null, "params": [], "rest": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":50,"end":54,"loc":{"start":{"line":2,"column":22},"end":{"line":2,"column":26}} diff --git a/packages/babel-parser/test/fixtures/flow/scope/dupl-decl-declare-func-declare-var/output.json b/packages/babel-parser/test/fixtures/flow/scope/dupl-decl-declare-func-declare-var/output.json index 27ab9b95ad..3d0bf7fb38 100644 --- a/packages/babel-parser/test/fixtures/flow/scope/dupl-decl-declare-func-declare-var/output.json +++ b/packages/babel-parser/test/fixtures/flow/scope/dupl-decl-declare-func-declare-var/output.json @@ -23,6 +23,7 @@ "typeParameters": null, "params": [], "rest": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":22,"end":26,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":26}} diff --git a/packages/babel-parser/test/fixtures/flow/scope/dupl-decl-declare-func-func/output.json b/packages/babel-parser/test/fixtures/flow/scope/dupl-decl-declare-func-func/output.json index 6895d0b4ef..583414100a 100644 --- a/packages/babel-parser/test/fixtures/flow/scope/dupl-decl-declare-func-func/output.json +++ b/packages/babel-parser/test/fixtures/flow/scope/dupl-decl-declare-func-func/output.json @@ -23,6 +23,7 @@ "typeParameters": null, "params": [], "rest": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":22,"end":26,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":26}} diff --git a/packages/babel-parser/test/fixtures/flow/scope/dupl-decl-func-declare-func/output.json b/packages/babel-parser/test/fixtures/flow/scope/dupl-decl-func-declare-func/output.json index e088b316d6..ac0fe1790e 100644 --- a/packages/babel-parser/test/fixtures/flow/scope/dupl-decl-func-declare-func/output.json +++ b/packages/babel-parser/test/fixtures/flow/scope/dupl-decl-func-declare-func/output.json @@ -44,6 +44,7 @@ "typeParameters": null, "params": [], "rest": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":38,"end":42,"loc":{"start":{"line":2,"column":22},"end":{"line":2,"column":26}} diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/declare-function/input.js b/packages/babel-parser/test/fixtures/flow/this-annotation/declare-function/input.js new file mode 100644 index 0000000000..a11916d5e7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/declare-function/input.js @@ -0,0 +1,7 @@ +declare function foo (this : number, a : string, b : number) : void + +declare function bar (this : number): void + +declare function baz (this : number, ...a : any): void + +declare function qux (this : T) : void diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/declare-function/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/declare-function/output.json new file mode 100644 index 0000000000..925b7febf3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/declare-function/output.json @@ -0,0 +1,212 @@ +{ + "type": "File", + "start":0,"end":210,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":41}}, + "program": { + "type": "Program", + "start":0,"end":210,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":41}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "DeclareFunction", + "start":0,"end":67,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":67}}, + "id": { + "type": "Identifier", + "start":17,"end":67,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":67},"identifierName":"foo"}, + "name": "foo", + "typeAnnotation": { + "type": "TypeAnnotation", + "start":21,"end":67,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":67}}, + "typeAnnotation": { + "type": "FunctionTypeAnnotation", + "start":21,"end":67,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":67}}, + "typeParameters": null, + "params": [ + { + "type": "FunctionTypeParam", + "start":37,"end":47,"loc":{"start":{"line":1,"column":37},"end":{"line":1,"column":47}}, + "name": { + "type": "Identifier", + "start":37,"end":38,"loc":{"start":{"line":1,"column":37},"end":{"line":1,"column":38},"identifierName":"a"}, + "name": "a" + }, + "optional": false, + "typeAnnotation": { + "type": "StringTypeAnnotation", + "start":41,"end":47,"loc":{"start":{"line":1,"column":41},"end":{"line":1,"column":47}} + } + }, + { + "type": "FunctionTypeParam", + "start":49,"end":59,"loc":{"start":{"line":1,"column":49},"end":{"line":1,"column":59}}, + "name": { + "type": "Identifier", + "start":49,"end":50,"loc":{"start":{"line":1,"column":49},"end":{"line":1,"column":50},"identifierName":"b"}, + "name": "b" + }, + "optional": false, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":53,"end":59,"loc":{"start":{"line":1,"column":53},"end":{"line":1,"column":59}} + } + } + ], + "rest": null, + "this": { + "type": "FunctionTypeParam", + "start":22,"end":35,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":35}}, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":29,"end":35,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":35}} + } + }, + "returnType": { + "type": "VoidTypeAnnotation", + "start":63,"end":67,"loc":{"start":{"line":1,"column":63},"end":{"line":1,"column":67}} + } + } + } + }, + "predicate": null + }, + { + "type": "DeclareFunction", + "start":69,"end":111,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":42}}, + "id": { + "type": "Identifier", + "start":86,"end":111,"loc":{"start":{"line":3,"column":17},"end":{"line":3,"column":42},"identifierName":"bar"}, + "name": "bar", + "typeAnnotation": { + "type": "TypeAnnotation", + "start":90,"end":111,"loc":{"start":{"line":3,"column":21},"end":{"line":3,"column":42}}, + "typeAnnotation": { + "type": "FunctionTypeAnnotation", + "start":90,"end":111,"loc":{"start":{"line":3,"column":21},"end":{"line":3,"column":42}}, + "typeParameters": null, + "params": [], + "rest": null, + "this": { + "type": "FunctionTypeParam", + "start":91,"end":104,"loc":{"start":{"line":3,"column":22},"end":{"line":3,"column":35}}, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":98,"end":104,"loc":{"start":{"line":3,"column":29},"end":{"line":3,"column":35}} + } + }, + "returnType": { + "type": "VoidTypeAnnotation", + "start":107,"end":111,"loc":{"start":{"line":3,"column":38},"end":{"line":3,"column":42}} + } + } + } + }, + "predicate": null + }, + { + "type": "DeclareFunction", + "start":113,"end":167,"loc":{"start":{"line":5,"column":0},"end":{"line":5,"column":54}}, + "id": { + "type": "Identifier", + "start":130,"end":167,"loc":{"start":{"line":5,"column":17},"end":{"line":5,"column":54},"identifierName":"baz"}, + "name": "baz", + "typeAnnotation": { + "type": "TypeAnnotation", + "start":134,"end":167,"loc":{"start":{"line":5,"column":21},"end":{"line":5,"column":54}}, + "typeAnnotation": { + "type": "FunctionTypeAnnotation", + "start":134,"end":167,"loc":{"start":{"line":5,"column":21},"end":{"line":5,"column":54}}, + "typeParameters": null, + "params": [], + "rest": { + "type": "FunctionTypeParam", + "start":153,"end":160,"loc":{"start":{"line":5,"column":40},"end":{"line":5,"column":47}}, + "name": { + "type": "Identifier", + "start":153,"end":154,"loc":{"start":{"line":5,"column":40},"end":{"line":5,"column":41},"identifierName":"a"}, + "name": "a" + }, + "optional": false, + "typeAnnotation": { + "type": "AnyTypeAnnotation", + "start":157,"end":160,"loc":{"start":{"line":5,"column":44},"end":{"line":5,"column":47}} + } + }, + "this": { + "type": "FunctionTypeParam", + "start":135,"end":148,"loc":{"start":{"line":5,"column":22},"end":{"line":5,"column":35}}, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":142,"end":148,"loc":{"start":{"line":5,"column":29},"end":{"line":5,"column":35}} + } + }, + "returnType": { + "type": "VoidTypeAnnotation", + "start":163,"end":167,"loc":{"start":{"line":5,"column":50},"end":{"line":5,"column":54}} + } + } + } + }, + "predicate": null + }, + { + "type": "DeclareFunction", + "start":169,"end":210,"loc":{"start":{"line":7,"column":0},"end":{"line":7,"column":41}}, + "id": { + "type": "Identifier", + "start":186,"end":210,"loc":{"start":{"line":7,"column":17},"end":{"line":7,"column":41},"identifierName":"qux"}, + "name": "qux", + "typeAnnotation": { + "type": "TypeAnnotation", + "start":189,"end":210,"loc":{"start":{"line":7,"column":20},"end":{"line":7,"column":41}}, + "typeAnnotation": { + "type": "FunctionTypeAnnotation", + "start":189,"end":210,"loc":{"start":{"line":7,"column":20},"end":{"line":7,"column":41}}, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start":189,"end":192,"loc":{"start":{"line":7,"column":20},"end":{"line":7,"column":23}}, + "params": [ + { + "type": "TypeParameter", + "start":190,"end":191,"loc":{"start":{"line":7,"column":21},"end":{"line":7,"column":22}}, + "name": "T", + "variance": null + } + ] + }, + "params": [], + "rest": null, + "this": { + "type": "FunctionTypeParam", + "start":194,"end":202,"loc":{"start":{"line":7,"column":25},"end":{"line":7,"column":33}}, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "GenericTypeAnnotation", + "start":201,"end":202,"loc":{"start":{"line":7,"column":32},"end":{"line":7,"column":33}}, + "typeParameters": null, + "id": { + "type": "Identifier", + "start":201,"end":202,"loc":{"start":{"line":7,"column":32},"end":{"line":7,"column":33},"identifierName":"T"}, + "name": "T" + } + } + }, + "returnType": { + "type": "VoidTypeAnnotation", + "start":206,"end":210,"loc":{"start":{"line":7,"column":37},"end":{"line":7,"column":41}} + } + } + } + }, + "predicate": null + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/declare-method/input.js b/packages/babel-parser/test/fixtures/flow/this-annotation/declare-method/input.js new file mode 100644 index 0000000000..69755c4c5f --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/declare-method/input.js @@ -0,0 +1,6 @@ +declare class A { + m(this : number, a : number, b : string) : void, + n(this : number, ...c : any) : void, + o(this : number) : void, + p(this : T) : void +} diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/declare-method/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/declare-method/output.json new file mode 100644 index 0000000000..b089a3cf4d --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/declare-method/output.json @@ -0,0 +1,235 @@ +{ + "type": "File", + "start":0,"end":168,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}}, + "program": { + "type": "Program", + "start":0,"end":168,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "DeclareClass", + "start":0,"end":168,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}}, + "id": { + "type": "Identifier", + "start":14,"end":15,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":15},"identifierName":"A"}, + "name": "A" + }, + "typeParameters": null, + "extends": [], + "implements": [], + "mixins": [], + "body": { + "type": "ObjectTypeAnnotation", + "start":16,"end":168,"loc":{"start":{"line":1,"column":16},"end":{"line":6,"column":1}}, + "callProperties": [], + "properties": [ + { + "type": "ObjectTypeProperty", + "start":22,"end":69,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":51}}, + "key": { + "type": "Identifier", + "start":22,"end":23,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":5},"identifierName":"m"}, + "name": "m" + }, + "static": false, + "proto": false, + "kind": "init", + "method": true, + "value": { + "type": "FunctionTypeAnnotation", + "start":22,"end":69,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":51}}, + "params": [ + { + "type": "FunctionTypeParam", + "start":39,"end":49,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":31}}, + "name": { + "type": "Identifier", + "start":39,"end":40,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":22},"identifierName":"a"}, + "name": "a" + }, + "optional": false, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":43,"end":49,"loc":{"start":{"line":2,"column":25},"end":{"line":2,"column":31}} + } + }, + { + "type": "FunctionTypeParam", + "start":51,"end":61,"loc":{"start":{"line":2,"column":33},"end":{"line":2,"column":43}}, + "name": { + "type": "Identifier", + "start":51,"end":52,"loc":{"start":{"line":2,"column":33},"end":{"line":2,"column":34},"identifierName":"b"}, + "name": "b" + }, + "optional": false, + "typeAnnotation": { + "type": "StringTypeAnnotation", + "start":55,"end":61,"loc":{"start":{"line":2,"column":37},"end":{"line":2,"column":43}} + } + } + ], + "rest": null, + "typeParameters": null, + "this": { + "type": "FunctionTypeParam", + "start":24,"end":37,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":19}}, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":31,"end":37,"loc":{"start":{"line":2,"column":13},"end":{"line":2,"column":19}} + } + }, + "returnType": { + "type": "VoidTypeAnnotation", + "start":65,"end":69,"loc":{"start":{"line":2,"column":47},"end":{"line":2,"column":51}} + } + }, + "optional": false + }, + { + "type": "ObjectTypeProperty", + "start":75,"end":110,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":39}}, + "key": { + "type": "Identifier", + "start":75,"end":76,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":5},"identifierName":"n"}, + "name": "n" + }, + "static": false, + "proto": false, + "kind": "init", + "method": true, + "value": { + "type": "FunctionTypeAnnotation", + "start":75,"end":110,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":39}}, + "params": [], + "rest": { + "type": "FunctionTypeParam", + "start":95,"end":102,"loc":{"start":{"line":3,"column":24},"end":{"line":3,"column":31}}, + "name": { + "type": "Identifier", + "start":95,"end":96,"loc":{"start":{"line":3,"column":24},"end":{"line":3,"column":25},"identifierName":"c"}, + "name": "c" + }, + "optional": false, + "typeAnnotation": { + "type": "AnyTypeAnnotation", + "start":99,"end":102,"loc":{"start":{"line":3,"column":28},"end":{"line":3,"column":31}} + } + }, + "typeParameters": null, + "this": { + "type": "FunctionTypeParam", + "start":77,"end":90,"loc":{"start":{"line":3,"column":6},"end":{"line":3,"column":19}}, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":84,"end":90,"loc":{"start":{"line":3,"column":13},"end":{"line":3,"column":19}} + } + }, + "returnType": { + "type": "VoidTypeAnnotation", + "start":106,"end":110,"loc":{"start":{"line":3,"column":35},"end":{"line":3,"column":39}} + } + }, + "optional": false + }, + { + "type": "ObjectTypeProperty", + "start":116,"end":139,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":27}}, + "key": { + "type": "Identifier", + "start":116,"end":117,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":5},"identifierName":"o"}, + "name": "o" + }, + "static": false, + "proto": false, + "kind": "init", + "method": true, + "value": { + "type": "FunctionTypeAnnotation", + "start":116,"end":139,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":27}}, + "params": [], + "rest": null, + "typeParameters": null, + "this": { + "type": "FunctionTypeParam", + "start":118,"end":131,"loc":{"start":{"line":4,"column":6},"end":{"line":4,"column":19}}, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":125,"end":131,"loc":{"start":{"line":4,"column":13},"end":{"line":4,"column":19}} + } + }, + "returnType": { + "type": "VoidTypeAnnotation", + "start":135,"end":139,"loc":{"start":{"line":4,"column":23},"end":{"line":4,"column":27}} + } + }, + "optional": false + }, + { + "type": "ObjectTypeProperty", + "start":145,"end":166,"loc":{"start":{"line":5,"column":4},"end":{"line":5,"column":25}}, + "key": { + "type": "Identifier", + "start":145,"end":146,"loc":{"start":{"line":5,"column":4},"end":{"line":5,"column":5},"identifierName":"p"}, + "name": "p" + }, + "static": false, + "proto": false, + "kind": "init", + "method": true, + "value": { + "type": "FunctionTypeAnnotation", + "start":145,"end":166,"loc":{"start":{"line":5,"column":4},"end":{"line":5,"column":25}}, + "params": [], + "rest": null, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start":146,"end":149,"loc":{"start":{"line":5,"column":5},"end":{"line":5,"column":8}}, + "params": [ + { + "type": "TypeParameter", + "start":147,"end":148,"loc":{"start":{"line":5,"column":6},"end":{"line":5,"column":7}}, + "name": "T", + "variance": null + } + ] + }, + "this": { + "type": "FunctionTypeParam", + "start":150,"end":158,"loc":{"start":{"line":5,"column":9},"end":{"line":5,"column":17}}, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "GenericTypeAnnotation", + "start":157,"end":158,"loc":{"start":{"line":5,"column":16},"end":{"line":5,"column":17}}, + "typeParameters": null, + "id": { + "type": "Identifier", + "start":157,"end":158,"loc":{"start":{"line":5,"column":16},"end":{"line":5,"column":17},"identifierName":"T"}, + "name": "T" + } + } + }, + "returnType": { + "type": "VoidTypeAnnotation", + "start":162,"end":166,"loc":{"start":{"line":5,"column":21},"end":{"line":5,"column":25}} + } + }, + "optional": false + } + ], + "indexers": [], + "internalSlots": [], + "exact": false + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/default/input.js b/packages/babel-parser/test/fixtures/flow/this-annotation/default/input.js new file mode 100644 index 0000000000..5c62115ccd --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/default/input.js @@ -0,0 +1 @@ +function foo (this : number = 2) {} diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/default/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/default/output.json new file mode 100644 index 0000000000..594580b04d --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/default/output.json @@ -0,0 +1,73 @@ +{ + "type": "File", + "start":0,"end":38,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":38}}, + "errors": [ + "SyntaxError: The `this` parameter may not have a default value. (1:17)" + ], + "program": { + "type": "Program", + "start":0,"end":38,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":38}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "FunctionDeclaration", + "start":0,"end":38,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":38}}, + "id": { + "type": "Identifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12},"identifierName":"foo"}, + "name": "foo" + }, + "generator": false, + "async": false, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start":12,"end":15,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":15}}, + "params": [ + { + "type": "TypeParameter", + "start":13,"end":14,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":14}}, + "name": "T", + "variance": null + } + ] + }, + "params": [ + { + "type": "AssignmentPattern", + "start":17,"end":34,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":34}}, + "left": { + "type": "Identifier", + "start":17,"end":30,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":30},"identifierName":"this"}, + "name": "this", + "typeAnnotation": { + "type": "TypeAnnotation", + "start":22,"end":30,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":30}}, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":24,"end":30,"loc":{"start":{"line":1,"column":24},"end":{"line":1,"column":30}} + } + } + }, + "right": { + "type": "NumericLiteral", + "start":33,"end":34,"loc":{"start":{"line":1,"column":33},"end":{"line":1,"column":34}}, + "extra": { + "rawValue": 2, + "raw": "2" + }, + "value": 2 + } + } + ], + "body": { + "type": "BlockStatement", + "start":36,"end":38,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":38}}, + "body": [], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/function-declaration/input.js b/packages/babel-parser/test/fixtures/flow/this-annotation/function-declaration/input.js new file mode 100644 index 0000000000..f2d69ae4f8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/function-declaration/input.js @@ -0,0 +1,7 @@ +function foo (this : number, a : string, b) {} + +function bar (this : number) {} + +function baz (this : number, ...a) {} + +function qux (this : T) {} diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/function-declaration/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/function-declaration/output.json new file mode 100644 index 0000000000..2c042f9f63 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/function-declaration/output.json @@ -0,0 +1,186 @@ +{ + "type": "File", + "start":0,"end":149,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":29}}, + "program": { + "type": "Program", + "start":0,"end":149,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":29}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "FunctionDeclaration", + "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}}, + "id": { + "type": "Identifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12},"identifierName":"foo"}, + "name": "foo" + }, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":14,"end":27,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":27},"identifierName":"this"}, + "name": "this", + "typeAnnotation": { + "type": "TypeAnnotation", + "start":19,"end":27,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":27}}, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":21,"end":27,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":27}} + } + } + }, + { + "type": "Identifier", + "start":29,"end":39,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":39},"identifierName":"a"}, + "name": "a", + "typeAnnotation": { + "type": "TypeAnnotation", + "start":31,"end":39,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":39}}, + "typeAnnotation": { + "type": "StringTypeAnnotation", + "start":33,"end":39,"loc":{"start":{"line":1,"column":33},"end":{"line":1,"column":39}} + } + } + }, + { + "type": "Identifier", + "start":41,"end":42,"loc":{"start":{"line":1,"column":41},"end":{"line":1,"column":42},"identifierName":"b"}, + "name": "b" + } + ], + "body": { + "type": "BlockStatement", + "start":44,"end":46,"loc":{"start":{"line":1,"column":44},"end":{"line":1,"column":46}}, + "body": [], + "directives": [] + } + }, + { + "type": "FunctionDeclaration", + "start":48,"end":79,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":31}}, + "id": { + "type": "Identifier", + "start":57,"end":60,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":12},"identifierName":"bar"}, + "name": "bar" + }, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":62,"end":75,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":27},"identifierName":"this"}, + "name": "this", + "typeAnnotation": { + "type": "TypeAnnotation", + "start":67,"end":75,"loc":{"start":{"line":3,"column":19},"end":{"line":3,"column":27}}, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":69,"end":75,"loc":{"start":{"line":3,"column":21},"end":{"line":3,"column":27}} + } + } + } + ], + "body": { + "type": "BlockStatement", + "start":77,"end":79,"loc":{"start":{"line":3,"column":29},"end":{"line":3,"column":31}}, + "body": [], + "directives": [] + } + }, + { + "type": "FunctionDeclaration", + "start":81,"end":118,"loc":{"start":{"line":5,"column":0},"end":{"line":5,"column":37}}, + "id": { + "type": "Identifier", + "start":90,"end":93,"loc":{"start":{"line":5,"column":9},"end":{"line":5,"column":12},"identifierName":"baz"}, + "name": "baz" + }, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":95,"end":108,"loc":{"start":{"line":5,"column":14},"end":{"line":5,"column":27},"identifierName":"this"}, + "name": "this", + "typeAnnotation": { + "type": "TypeAnnotation", + "start":100,"end":108,"loc":{"start":{"line":5,"column":19},"end":{"line":5,"column":27}}, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":102,"end":108,"loc":{"start":{"line":5,"column":21},"end":{"line":5,"column":27}} + } + } + }, + { + "type": "RestElement", + "start":110,"end":114,"loc":{"start":{"line":5,"column":29},"end":{"line":5,"column":33}}, + "argument": { + "type": "Identifier", + "start":113,"end":114,"loc":{"start":{"line":5,"column":32},"end":{"line":5,"column":33},"identifierName":"a"}, + "name": "a" + } + } + ], + "body": { + "type": "BlockStatement", + "start":116,"end":118,"loc":{"start":{"line":5,"column":35},"end":{"line":5,"column":37}}, + "body": [], + "directives": [] + } + }, + { + "type": "FunctionDeclaration", + "start":120,"end":149,"loc":{"start":{"line":7,"column":0},"end":{"line":7,"column":29}}, + "id": { + "type": "Identifier", + "start":129,"end":132,"loc":{"start":{"line":7,"column":9},"end":{"line":7,"column":12},"identifierName":"qux"}, + "name": "qux" + }, + "generator": false, + "async": false, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start":132,"end":135,"loc":{"start":{"line":7,"column":12},"end":{"line":7,"column":15}}, + "params": [ + { + "type": "TypeParameter", + "start":133,"end":134,"loc":{"start":{"line":7,"column":13},"end":{"line":7,"column":14}}, + "name": "T", + "variance": null + } + ] + }, + "params": [ + { + "type": "Identifier", + "start":137,"end":145,"loc":{"start":{"line":7,"column":17},"end":{"line":7,"column":25},"identifierName":"this"}, + "name": "this", + "typeAnnotation": { + "type": "TypeAnnotation", + "start":142,"end":145,"loc":{"start":{"line":7,"column":22},"end":{"line":7,"column":25}}, + "typeAnnotation": { + "type": "GenericTypeAnnotation", + "start":144,"end":145,"loc":{"start":{"line":7,"column":24},"end":{"line":7,"column":25}}, + "typeParameters": null, + "id": { + "type": "Identifier", + "start":144,"end":145,"loc":{"start":{"line":7,"column":24},"end":{"line":7,"column":25},"identifierName":"T"}, + "name": "T" + } + } + } + } + ], + "body": { + "type": "BlockStatement", + "start":147,"end":149,"loc":{"start":{"line":7,"column":27},"end":{"line":7,"column":29}}, + "body": [], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/function-type/input.js b/packages/babel-parser/test/fixtures/flow/this-annotation/function-type/input.js new file mode 100644 index 0000000000..0ae4768556 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/function-type/input.js @@ -0,0 +1,7 @@ +type T = (this : number, a : string, b : number) => void + +type U = (this : number, ...c : any) => void + +type V = (this : number) => void + +type Q = (this : T) => void diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/function-type/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/function-type/output.json new file mode 100644 index 0000000000..b3a6efdfd0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/function-type/output.json @@ -0,0 +1,196 @@ +{ + "type": "File", + "start":0,"end":168,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":30}}, + "program": { + "type": "Program", + "start":0,"end":168,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":30}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TypeAlias", + "start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":56}}, + "id": { + "type": "Identifier", + "start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6},"identifierName":"T"}, + "name": "T" + }, + "typeParameters": null, + "right": { + "type": "FunctionTypeAnnotation", + "start":9,"end":56,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":56}}, + "params": [ + { + "type": "FunctionTypeParam", + "start":25,"end":35,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":35}}, + "name": { + "type": "Identifier", + "start":25,"end":26,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":26},"identifierName":"a"}, + "name": "a" + }, + "optional": false, + "typeAnnotation": { + "type": "StringTypeAnnotation", + "start":29,"end":35,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":35}} + } + }, + { + "type": "FunctionTypeParam", + "start":37,"end":47,"loc":{"start":{"line":1,"column":37},"end":{"line":1,"column":47}}, + "name": { + "type": "Identifier", + "start":37,"end":38,"loc":{"start":{"line":1,"column":37},"end":{"line":1,"column":38},"identifierName":"b"}, + "name": "b" + }, + "optional": false, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":41,"end":47,"loc":{"start":{"line":1,"column":41},"end":{"line":1,"column":47}} + } + } + ], + "rest": null, + "this": { + "type": "FunctionTypeParam", + "start":10,"end":23,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":23}}, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":17,"end":23,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":23}} + } + }, + "returnType": { + "type": "VoidTypeAnnotation", + "start":52,"end":56,"loc":{"start":{"line":1,"column":52},"end":{"line":1,"column":56}} + }, + "typeParameters": null + } + }, + { + "type": "TypeAlias", + "start":58,"end":102,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":44}}, + "id": { + "type": "Identifier", + "start":63,"end":64,"loc":{"start":{"line":3,"column":5},"end":{"line":3,"column":6},"identifierName":"U"}, + "name": "U" + }, + "typeParameters": null, + "right": { + "type": "FunctionTypeAnnotation", + "start":67,"end":102,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":44}}, + "params": [], + "rest": { + "type": "FunctionTypeParam", + "start":86,"end":93,"loc":{"start":{"line":3,"column":28},"end":{"line":3,"column":35}}, + "name": { + "type": "Identifier", + "start":86,"end":87,"loc":{"start":{"line":3,"column":28},"end":{"line":3,"column":29},"identifierName":"c"}, + "name": "c" + }, + "optional": false, + "typeAnnotation": { + "type": "AnyTypeAnnotation", + "start":90,"end":93,"loc":{"start":{"line":3,"column":32},"end":{"line":3,"column":35}} + } + }, + "this": { + "type": "FunctionTypeParam", + "start":68,"end":81,"loc":{"start":{"line":3,"column":10},"end":{"line":3,"column":23}}, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":75,"end":81,"loc":{"start":{"line":3,"column":17},"end":{"line":3,"column":23}} + } + }, + "returnType": { + "type": "VoidTypeAnnotation", + "start":98,"end":102,"loc":{"start":{"line":3,"column":40},"end":{"line":3,"column":44}} + }, + "typeParameters": null + } + }, + { + "type": "TypeAlias", + "start":104,"end":136,"loc":{"start":{"line":5,"column":0},"end":{"line":5,"column":32}}, + "id": { + "type": "Identifier", + "start":109,"end":110,"loc":{"start":{"line":5,"column":5},"end":{"line":5,"column":6},"identifierName":"V"}, + "name": "V" + }, + "typeParameters": null, + "right": { + "type": "FunctionTypeAnnotation", + "start":113,"end":136,"loc":{"start":{"line":5,"column":9},"end":{"line":5,"column":32}}, + "params": [], + "rest": null, + "this": { + "type": "FunctionTypeParam", + "start":114,"end":127,"loc":{"start":{"line":5,"column":10},"end":{"line":5,"column":23}}, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":121,"end":127,"loc":{"start":{"line":5,"column":17},"end":{"line":5,"column":23}} + } + }, + "returnType": { + "type": "VoidTypeAnnotation", + "start":132,"end":136,"loc":{"start":{"line":5,"column":28},"end":{"line":5,"column":32}} + }, + "typeParameters": null + } + }, + { + "type": "TypeAlias", + "start":138,"end":168,"loc":{"start":{"line":7,"column":0},"end":{"line":7,"column":30}}, + "id": { + "type": "Identifier", + "start":143,"end":144,"loc":{"start":{"line":7,"column":5},"end":{"line":7,"column":6},"identifierName":"Q"}, + "name": "Q" + }, + "typeParameters": null, + "right": { + "type": "FunctionTypeAnnotation", + "start":147,"end":168,"loc":{"start":{"line":7,"column":9},"end":{"line":7,"column":30}}, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start":147,"end":150,"loc":{"start":{"line":7,"column":9},"end":{"line":7,"column":12}}, + "params": [ + { + "type": "TypeParameter", + "start":148,"end":149,"loc":{"start":{"line":7,"column":10},"end":{"line":7,"column":11}}, + "name": "T", + "variance": null + } + ] + }, + "params": [], + "rest": null, + "this": { + "type": "FunctionTypeParam", + "start":151,"end":159,"loc":{"start":{"line":7,"column":13},"end":{"line":7,"column":21}}, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "GenericTypeAnnotation", + "start":158,"end":159,"loc":{"start":{"line":7,"column":20},"end":{"line":7,"column":21}}, + "typeParameters": null, + "id": { + "type": "Identifier", + "start":158,"end":159,"loc":{"start":{"line":7,"column":20},"end":{"line":7,"column":21},"identifierName":"T"}, + "name": "T" + } + } + }, + "returnType": { + "type": "VoidTypeAnnotation", + "start":164,"end":168,"loc":{"start":{"line":7,"column":26},"end":{"line":7,"column":30}} + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/method/input.js b/packages/babel-parser/test/fixtures/flow/this-annotation/method/input.js new file mode 100644 index 0000000000..9b145069ab --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/method/input.js @@ -0,0 +1,6 @@ +class A { + m(this : number, a : number, b : string) {} + n(this : number, ...c) {} + o(this : number) {} + p(this : T) {} +} diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/method/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/method/output.json new file mode 100644 index 0000000000..b972757ded --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/method/output.json @@ -0,0 +1,226 @@ +{ + "type": "File", + "start":0,"end":135,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}}, + "program": { + "type": "Program", + "start":0,"end":135,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":0,"end":135,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}}, + "id": { + "type": "Identifier", + "start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"A"}, + "name": "A" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":8,"end":135,"loc":{"start":{"line":1,"column":8},"end":{"line":6,"column":1}}, + "body": [ + { + "type": "ClassMethod", + "start":14,"end":57,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":47}}, + "static": false, + "key": { + "type": "Identifier", + "start":14,"end":15,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":5},"identifierName":"m"}, + "name": "m" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":16,"end":29,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":19},"identifierName":"this"}, + "name": "this", + "typeAnnotation": { + "type": "TypeAnnotation", + "start":21,"end":29,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":19}}, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":23,"end":29,"loc":{"start":{"line":2,"column":13},"end":{"line":2,"column":19}} + } + } + }, + { + "type": "Identifier", + "start":31,"end":41,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":31},"identifierName":"a"}, + "name": "a", + "typeAnnotation": { + "type": "TypeAnnotation", + "start":33,"end":41,"loc":{"start":{"line":2,"column":23},"end":{"line":2,"column":31}}, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":35,"end":41,"loc":{"start":{"line":2,"column":25},"end":{"line":2,"column":31}} + } + } + }, + { + "type": "Identifier", + "start":43,"end":53,"loc":{"start":{"line":2,"column":33},"end":{"line":2,"column":43},"identifierName":"b"}, + "name": "b", + "typeAnnotation": { + "type": "TypeAnnotation", + "start":45,"end":53,"loc":{"start":{"line":2,"column":35},"end":{"line":2,"column":43}}, + "typeAnnotation": { + "type": "StringTypeAnnotation", + "start":47,"end":53,"loc":{"start":{"line":2,"column":37},"end":{"line":2,"column":43}} + } + } + } + ], + "body": { + "type": "BlockStatement", + "start":55,"end":57,"loc":{"start":{"line":2,"column":45},"end":{"line":2,"column":47}}, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start":62,"end":87,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":29}}, + "static": false, + "key": { + "type": "Identifier", + "start":62,"end":63,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":5},"identifierName":"n"}, + "name": "n" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":64,"end":77,"loc":{"start":{"line":3,"column":6},"end":{"line":3,"column":19},"identifierName":"this"}, + "name": "this", + "typeAnnotation": { + "type": "TypeAnnotation", + "start":69,"end":77,"loc":{"start":{"line":3,"column":11},"end":{"line":3,"column":19}}, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":71,"end":77,"loc":{"start":{"line":3,"column":13},"end":{"line":3,"column":19}} + } + } + }, + { + "type": "RestElement", + "start":79,"end":83,"loc":{"start":{"line":3,"column":21},"end":{"line":3,"column":25}}, + "argument": { + "type": "Identifier", + "start":82,"end":83,"loc":{"start":{"line":3,"column":24},"end":{"line":3,"column":25},"identifierName":"c"}, + "name": "c" + } + } + ], + "body": { + "type": "BlockStatement", + "start":85,"end":87,"loc":{"start":{"line":3,"column":27},"end":{"line":3,"column":29}}, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start":92,"end":111,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":23}}, + "static": false, + "key": { + "type": "Identifier", + "start":92,"end":93,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":5},"identifierName":"o"}, + "name": "o" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":94,"end":107,"loc":{"start":{"line":4,"column":6},"end":{"line":4,"column":19},"identifierName":"this"}, + "name": "this", + "typeAnnotation": { + "type": "TypeAnnotation", + "start":99,"end":107,"loc":{"start":{"line":4,"column":11},"end":{"line":4,"column":19}}, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":101,"end":107,"loc":{"start":{"line":4,"column":13},"end":{"line":4,"column":19}} + } + } + } + ], + "body": { + "type": "BlockStatement", + "start":109,"end":111,"loc":{"start":{"line":4,"column":21},"end":{"line":4,"column":23}}, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start":116,"end":133,"loc":{"start":{"line":5,"column":4},"end":{"line":5,"column":21}}, + "static": false, + "key": { + "type": "Identifier", + "start":116,"end":117,"loc":{"start":{"line":5,"column":4},"end":{"line":5,"column":5},"identifierName":"p"}, + "name": "p" + }, + "computed": false, + "kind": "method", + "typeParameters": { + "type": "TypeParameterDeclaration", + "start":117,"end":120,"loc":{"start":{"line":5,"column":5},"end":{"line":5,"column":8}}, + "params": [ + { + "type": "TypeParameter", + "start":118,"end":119,"loc":{"start":{"line":5,"column":6},"end":{"line":5,"column":7}}, + "name": "T", + "variance": null + } + ] + }, + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":121,"end":129,"loc":{"start":{"line":5,"column":9},"end":{"line":5,"column":17},"identifierName":"this"}, + "name": "this", + "typeAnnotation": { + "type": "TypeAnnotation", + "start":126,"end":129,"loc":{"start":{"line":5,"column":14},"end":{"line":5,"column":17}}, + "typeAnnotation": { + "type": "GenericTypeAnnotation", + "start":128,"end":129,"loc":{"start":{"line":5,"column":16},"end":{"line":5,"column":17}}, + "typeParameters": null, + "id": { + "type": "Identifier", + "start":128,"end":129,"loc":{"start":{"line":5,"column":16},"end":{"line":5,"column":17},"identifierName":"T"}, + "name": "T" + } + } + } + } + ], + "body": { + "type": "BlockStatement", + "start":131,"end":133,"loc":{"start":{"line":5,"column":19},"end":{"line":5,"column":21}}, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/object-constructor/input.js b/packages/babel-parser/test/fixtures/flow/this-annotation/object-constructor/input.js new file mode 100644 index 0000000000..903a43ad1c --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/object-constructor/input.js @@ -0,0 +1,5 @@ +// @flow + +({ + constructor(this: number){} +}) diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/object-constructor/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/object-constructor/output.json new file mode 100644 index 0000000000..7736cab5a7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/object-constructor/output.json @@ -0,0 +1,77 @@ +{ + "type": "File", + "start":0,"end":47,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":2}}, + "program": { + "type": "Program", + "start":0,"end":47,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":2}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":10,"end":47,"loc":{"start":{"line":3,"column":0},"end":{"line":5,"column":2}}, + "leadingComments": [ + { + "type": "CommentLine", + "value": " @flow", + "start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}} + } + ], + "expression": { + "type": "ObjectExpression", + "start":11,"end":46,"loc":{"start":{"line":3,"column":1},"end":{"line":5,"column":1}}, + "extra": { + "parenthesized": true, + "parenStart": 10 + }, + "properties": [ + { + "type": "ObjectMethod", + "start":17,"end":44,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":31}}, + "method": true, + "key": { + "type": "Identifier", + "start":17,"end":28,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":15},"identifierName":"constructor"}, + "name": "constructor" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":29,"end":41,"loc":{"start":{"line":4,"column":16},"end":{"line":4,"column":28},"identifierName":"this"}, + "name": "this", + "typeAnnotation": { + "type": "TypeAnnotation", + "start":33,"end":41,"loc":{"start":{"line":4,"column":20},"end":{"line":4,"column":28}}, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":35,"end":41,"loc":{"start":{"line":4,"column":22},"end":{"line":4,"column":28}} + } + } + } + ], + "body": { + "type": "BlockStatement", + "start":42,"end":44,"loc":{"start":{"line":4,"column":29},"end":{"line":4,"column":31}}, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " @flow", + "start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}} + } + ] +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/object-method-type-this-not-first/input.js b/packages/babel-parser/test/fixtures/flow/this-annotation/object-method-type-this-not-first/input.js new file mode 100644 index 0000000000..5970255410 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/object-method-type-this-not-first/input.js @@ -0,0 +1,3 @@ +type T = { + foo(a : number, this : number) : void +} diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/object-method-type-this-not-first/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/object-method-type-this-not-first/output.json new file mode 100644 index 0000000000..8a93ef5748 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/object-method-type-this-not-first/output.json @@ -0,0 +1,92 @@ +{ + "type": "File", + "start":0,"end":54,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "errors": [ + "SyntaxError: The `this` parameter must be the first function parameter. (2:20)" + ], + "program": { + "type": "Program", + "start":0,"end":54,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TypeAlias", + "start":0,"end":54,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "id": { + "type": "Identifier", + "start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6},"identifierName":"T"}, + "name": "T" + }, + "typeParameters": null, + "right": { + "type": "ObjectTypeAnnotation", + "start":9,"end":54,"loc":{"start":{"line":1,"column":9},"end":{"line":3,"column":1}}, + "callProperties": [], + "properties": [ + { + "type": "ObjectTypeProperty", + "start":15,"end":52,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":41}}, + "key": { + "type": "Identifier", + "start":15,"end":18,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":7},"identifierName":"foo"}, + "name": "foo" + }, + "static": false, + "proto": false, + "kind": "init", + "method": true, + "value": { + "type": "FunctionTypeAnnotation", + "start":15,"end":52,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":41}}, + "params": [ + { + "type": "FunctionTypeParam", + "start":19,"end":29,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":18}}, + "name": { + "type": "Identifier", + "start":19,"end":20,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":9},"identifierName":"a"}, + "name": "a" + }, + "optional": false, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":23,"end":29,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":18}} + } + }, + { + "type": "FunctionTypeParam", + "start":31,"end":44,"loc":{"start":{"line":2,"column":20},"end":{"line":2,"column":33}}, + "name": { + "type": "Identifier", + "start":31,"end":35,"loc":{"start":{"line":2,"column":20},"end":{"line":2,"column":24},"identifierName":"this"}, + "name": "this" + }, + "optional": false, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":38,"end":44,"loc":{"start":{"line":2,"column":27},"end":{"line":2,"column":33}} + } + } + ], + "rest": null, + "typeParameters": null, + "this": null, + "returnType": { + "type": "VoidTypeAnnotation", + "start":48,"end":52,"loc":{"start":{"line":2,"column":37},"end":{"line":2,"column":41}} + } + }, + "optional": false + } + ], + "indexers": [], + "internalSlots": [], + "exact": false, + "inexact": false + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/object-method-type/input.js b/packages/babel-parser/test/fixtures/flow/this-annotation/object-method-type/input.js new file mode 100644 index 0000000000..46171c399e --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/object-method-type/input.js @@ -0,0 +1,3 @@ +type T = { + foo(this : number) : void +} diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/object-method-type/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/object-method-type/output.json new file mode 100644 index 0000000000..f71f3acee7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/object-method-type/output.json @@ -0,0 +1,69 @@ +{ + "type": "File", + "start":0,"end":42,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "program": { + "type": "Program", + "start":0,"end":42,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TypeAlias", + "start":0,"end":42,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "id": { + "type": "Identifier", + "start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6},"identifierName":"T"}, + "name": "T" + }, + "typeParameters": null, + "right": { + "type": "ObjectTypeAnnotation", + "start":9,"end":42,"loc":{"start":{"line":1,"column":9},"end":{"line":3,"column":1}}, + "callProperties": [], + "properties": [ + { + "type": "ObjectTypeProperty", + "start":15,"end":40,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":29}}, + "key": { + "type": "Identifier", + "start":15,"end":18,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":7},"identifierName":"foo"}, + "name": "foo" + }, + "static": false, + "proto": false, + "kind": "init", + "method": true, + "value": { + "type": "FunctionTypeAnnotation", + "start":15,"end":40,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":29}}, + "params": [], + "rest": null, + "typeParameters": null, + "this": { + "type": "FunctionTypeParam", + "start":19,"end":32,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":21}}, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":26,"end":32,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":21}} + } + }, + "returnType": { + "type": "VoidTypeAnnotation", + "start":36,"end":40,"loc":{"start":{"line":2,"column":25},"end":{"line":2,"column":29}} + } + }, + "optional": false + } + ], + "indexers": [], + "internalSlots": [], + "exact": false, + "inexact": false + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/object-type-constructor/input.js b/packages/babel-parser/test/fixtures/flow/this-annotation/object-type-constructor/input.js new file mode 100644 index 0000000000..7993687854 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/object-type-constructor/input.js @@ -0,0 +1,5 @@ +// @flow + +type T = { + constructor(this : number) : void +} diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/object-type-constructor/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/object-type-constructor/output.json new file mode 100644 index 0000000000..8555aed34e --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/object-type-constructor/output.json @@ -0,0 +1,83 @@ +{ + "type": "File", + "start":0,"end":60,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}}, + "program": { + "type": "Program", + "start":0,"end":60,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TypeAlias", + "start":10,"end":60,"loc":{"start":{"line":3,"column":0},"end":{"line":5,"column":1}}, + "leadingComments": [ + { + "type": "CommentLine", + "value": " @flow", + "start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}} + } + ], + "id": { + "type": "Identifier", + "start":15,"end":16,"loc":{"start":{"line":3,"column":5},"end":{"line":3,"column":6},"identifierName":"T"}, + "name": "T" + }, + "typeParameters": null, + "right": { + "type": "ObjectTypeAnnotation", + "start":19,"end":60,"loc":{"start":{"line":3,"column":9},"end":{"line":5,"column":1}}, + "callProperties": [], + "properties": [ + { + "type": "ObjectTypeProperty", + "start":25,"end":58,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":37}}, + "key": { + "type": "Identifier", + "start":25,"end":36,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":15},"identifierName":"constructor"}, + "name": "constructor" + }, + "static": false, + "proto": false, + "kind": "init", + "method": true, + "value": { + "type": "FunctionTypeAnnotation", + "start":25,"end":58,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":37}}, + "params": [], + "rest": null, + "typeParameters": null, + "this": { + "type": "FunctionTypeParam", + "start":37,"end":50,"loc":{"start":{"line":4,"column":16},"end":{"line":4,"column":29}}, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":44,"end":50,"loc":{"start":{"line":4,"column":23},"end":{"line":4,"column":29}} + } + }, + "returnType": { + "type": "VoidTypeAnnotation", + "start":54,"end":58,"loc":{"start":{"line":4,"column":33},"end":{"line":4,"column":37}} + } + }, + "optional": false + } + ], + "indexers": [], + "internalSlots": [], + "exact": false, + "inexact": false + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " @flow", + "start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}} + } + ] +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-arrow-function/input.js b/packages/babel-parser/test/fixtures/flow/this-annotation/this-arrow-function/input.js new file mode 100644 index 0000000000..cb37e78f70 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-arrow-function/input.js @@ -0,0 +1 @@ +let x = (this : number) => 0 diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-arrow-function/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/this-arrow-function/output.json new file mode 100644 index 0000000000..954fd71984 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-arrow-function/output.json @@ -0,0 +1,62 @@ +{ + "type": "File", + "start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}}, + "errors": [ + "SyntaxError: Binding invalid left-hand side in function parameter list (1:9)" + ], + "program": { + "type": "Program", + "start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "VariableDeclaration", + "start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":4,"end":28,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":28}}, + "id": { + "type": "Identifier", + "start":4,"end":5,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":5},"identifierName":"x"}, + "name": "x" + }, + "init": { + "type": "ArrowFunctionExpression", + "start":8,"end":28,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":28}}, + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "ThisExpression", + "start":9,"end":22,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":22}}, + "typeAnnotation": { + "type": "TypeAnnotation", + "start":14,"end":22,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":22}}, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":16,"end":22,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":22}} + } + } + } + ], + "body": { + "type": "NumericLiteral", + "start":27,"end":28,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":28}}, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-constructor-declare/input.js b/packages/babel-parser/test/fixtures/flow/this-annotation/this-constructor-declare/input.js new file mode 100644 index 0000000000..6e9a7ca40c --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-constructor-declare/input.js @@ -0,0 +1,3 @@ +declare class A { + constructor(this : number) : A; +} diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-constructor-declare/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/this-constructor-declare/output.json new file mode 100644 index 0000000000..d3461e69af --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-constructor-declare/output.json @@ -0,0 +1,80 @@ +{ + "type": "File", + "start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "errors": [ + "SyntaxError: Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions. (2:16)" + ], + "program": { + "type": "Program", + "start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "DeclareClass", + "start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "id": { + "type": "Identifier", + "start":14,"end":15,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":15},"identifierName":"A"}, + "name": "A" + }, + "typeParameters": null, + "extends": [], + "implements": [], + "mixins": [], + "body": { + "type": "ObjectTypeAnnotation", + "start":16,"end":55,"loc":{"start":{"line":1,"column":16},"end":{"line":3,"column":1}}, + "callProperties": [], + "properties": [ + { + "type": "ObjectTypeProperty", + "start":22,"end":52,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":34}}, + "key": { + "type": "Identifier", + "start":22,"end":33,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":15},"identifierName":"constructor"}, + "name": "constructor" + }, + "static": false, + "proto": false, + "kind": "init", + "method": true, + "value": { + "type": "FunctionTypeAnnotation", + "start":22,"end":52,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":34}}, + "params": [], + "rest": null, + "typeParameters": null, + "this": { + "type": "FunctionTypeParam", + "start":34,"end":47,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":29}}, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":41,"end":47,"loc":{"start":{"line":2,"column":23},"end":{"line":2,"column":29}} + } + }, + "returnType": { + "type": "GenericTypeAnnotation", + "start":51,"end":52,"loc":{"start":{"line":2,"column":33},"end":{"line":2,"column":34}}, + "typeParameters": null, + "id": { + "type": "Identifier", + "start":51,"end":52,"loc":{"start":{"line":2,"column":33},"end":{"line":2,"column":34},"identifierName":"A"}, + "name": "A" + } + } + }, + "optional": false + } + ], + "indexers": [], + "internalSlots": [], + "exact": false + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-constructor/input.js b/packages/babel-parser/test/fixtures/flow/this-annotation/this-constructor/input.js new file mode 100644 index 0000000000..69517defe8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-constructor/input.js @@ -0,0 +1,3 @@ +class A { + constructor(this: string) {} +} diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-constructor/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/this-constructor/output.json new file mode 100644 index 0000000000..3ed70ed007 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-constructor/output.json @@ -0,0 +1,68 @@ +{ + "type": "File", + "start":0,"end":42,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "errors": [ + "SyntaxError: Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions. (2:2)" + ], + "program": { + "type": "Program", + "start":0,"end":42,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":0,"end":42,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "id": { + "type": "Identifier", + "start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"A"}, + "name": "A" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":8,"end":42,"loc":{"start":{"line":1,"column":8},"end":{"line":3,"column":1}}, + "body": [ + { + "type": "ClassMethod", + "start":12,"end":40,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":30}}, + "static": false, + "key": { + "type": "Identifier", + "start":12,"end":23,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":13},"identifierName":"constructor"}, + "name": "constructor" + }, + "computed": false, + "kind": "constructor", + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":24,"end":36,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":26},"identifierName":"this"}, + "name": "this", + "typeAnnotation": { + "type": "TypeAnnotation", + "start":28,"end":36,"loc":{"start":{"line":2,"column":18},"end":{"line":2,"column":26}}, + "typeAnnotation": { + "type": "StringTypeAnnotation", + "start":30,"end":36,"loc":{"start":{"line":2,"column":20},"end":{"line":2,"column":26}} + } + } + } + ], + "body": { + "type": "BlockStatement", + "start":38,"end":40,"loc":{"start":{"line":2,"column":28},"end":{"line":2,"column":30}}, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-getter-type/input.js b/packages/babel-parser/test/fixtures/flow/this-annotation/this-getter-type/input.js new file mode 100644 index 0000000000..da0c98c60f --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-getter-type/input.js @@ -0,0 +1,3 @@ +type T = { + get foo(this: string) : void +} diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-getter-type/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/this-getter-type/output.json new file mode 100644 index 0000000000..1d448d6fd6 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-getter-type/output.json @@ -0,0 +1,72 @@ +{ + "type": "File", + "start":0,"end":43,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "errors": [ + "SyntaxError: A getter cannot have a `this` parameter. (2:10)" + ], + "program": { + "type": "Program", + "start":0,"end":43,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TypeAlias", + "start":0,"end":43,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "id": { + "type": "Identifier", + "start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6},"identifierName":"T"}, + "name": "T" + }, + "typeParameters": null, + "right": { + "type": "ObjectTypeAnnotation", + "start":9,"end":43,"loc":{"start":{"line":1,"column":9},"end":{"line":3,"column":1}}, + "callProperties": [], + "properties": [ + { + "type": "ObjectTypeProperty", + "start":13,"end":41,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":30}}, + "key": { + "type": "Identifier", + "start":17,"end":20,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":9},"identifierName":"foo"}, + "name": "foo" + }, + "static": false, + "proto": false, + "kind": "get", + "method": true, + "value": { + "type": "FunctionTypeAnnotation", + "start":13,"end":41,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":30}}, + "params": [], + "rest": null, + "typeParameters": null, + "this": { + "type": "FunctionTypeParam", + "start":21,"end":33,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":22}}, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "StringTypeAnnotation", + "start":27,"end":33,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":22}} + } + }, + "returnType": { + "type": "VoidTypeAnnotation", + "start":37,"end":41,"loc":{"start":{"line":2,"column":26},"end":{"line":2,"column":30}} + } + }, + "optional": false + } + ], + "indexers": [], + "internalSlots": [], + "exact": false, + "inexact": false + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-getter/input.js b/packages/babel-parser/test/fixtures/flow/this-annotation/this-getter/input.js new file mode 100644 index 0000000000..b822a1766a --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-getter/input.js @@ -0,0 +1,3 @@ +class Foo { + get foo(this: string) {} +} diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-getter/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/this-getter/output.json new file mode 100644 index 0000000000..320f898c90 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-getter/output.json @@ -0,0 +1,69 @@ +{ + "type": "File", + "start":0,"end":40,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "errors": [ + "SyntaxError: getter must not have any formal parameters (2:2)", + "SyntaxError: A getter cannot have a `this` parameter. (2:10)" + ], + "program": { + "type": "Program", + "start":0,"end":40,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":0,"end":40,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "id": { + "type": "Identifier", + "start":6,"end":9,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":9},"identifierName":"Foo"}, + "name": "Foo" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":10,"end":40,"loc":{"start":{"line":1,"column":10},"end":{"line":3,"column":1}}, + "body": [ + { + "type": "ClassMethod", + "start":14,"end":38,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":26}}, + "static": false, + "key": { + "type": "Identifier", + "start":18,"end":21,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":9},"identifierName":"foo"}, + "name": "foo" + }, + "computed": false, + "kind": "get", + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":22,"end":34,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":22},"identifierName":"this"}, + "name": "this", + "typeAnnotation": { + "type": "TypeAnnotation", + "start":26,"end":34,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":22}}, + "typeAnnotation": { + "type": "StringTypeAnnotation", + "start":28,"end":34,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":22}} + } + } + } + ], + "body": { + "type": "BlockStatement", + "start":36,"end":38,"loc":{"start":{"line":2,"column":24},"end":{"line":2,"column":26}}, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-no-annot/input.js b/packages/babel-parser/test/fixtures/flow/this-annotation/this-no-annot/input.js new file mode 100644 index 0000000000..270cd80bc8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-no-annot/input.js @@ -0,0 +1 @@ +function foo (this) {} diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-no-annot/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/this-no-annot/output.json new file mode 100644 index 0000000000..dbbbe1ef2f --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-no-annot/output.json @@ -0,0 +1,40 @@ +{ + "type": "File", + "start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}}, + "errors": [ + "SyntaxError: A type annotation is required for the `this` parameter. (1:14)" + ], + "program": { + "type": "Program", + "start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "FunctionDeclaration", + "start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}}, + "id": { + "type": "Identifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12},"identifierName":"foo"}, + "name": "foo" + }, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":14,"end":18,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":18},"identifierName":"this"}, + "name": "this" + } + ], + "body": { + "type": "BlockStatement", + "start":20,"end":22,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":22}}, + "body": [], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-not-first-type/input.js b/packages/babel-parser/test/fixtures/flow/this-annotation/this-not-first-type/input.js new file mode 100644 index 0000000000..09ab5a69ee --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-not-first-type/input.js @@ -0,0 +1 @@ +type T = (a : string, this : number) => void diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-not-first-type/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/this-not-first-type/output.json new file mode 100644 index 0000000000..e903a00111 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-not-first-type/output.json @@ -0,0 +1,67 @@ +{ + "type": "File", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":44}}, + "errors": [ + "SyntaxError: The `this` parameter must be the first function parameter. (1:22)" + ], + "program": { + "type": "Program", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":44}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TypeAlias", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":44}}, + "id": { + "type": "Identifier", + "start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6},"identifierName":"T"}, + "name": "T" + }, + "typeParameters": null, + "right": { + "type": "FunctionTypeAnnotation", + "start":9,"end":44,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":44}}, + "params": [ + { + "type": "FunctionTypeParam", + "start":10,"end":20,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":20}}, + "name": { + "type": "Identifier", + "start":10,"end":11,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":11},"identifierName":"a"}, + "name": "a" + }, + "optional": false, + "typeAnnotation": { + "type": "StringTypeAnnotation", + "start":14,"end":20,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":20}} + } + }, + { + "type": "FunctionTypeParam", + "start":22,"end":35,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":35}}, + "name": { + "type": "Identifier", + "start":22,"end":26,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":26},"identifierName":"this"}, + "name": "this" + }, + "optional": false, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":29,"end":35,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":35}} + } + } + ], + "rest": null, + "this": null, + "returnType": { + "type": "VoidTypeAnnotation", + "start":40,"end":44,"loc":{"start":{"line":1,"column":40},"end":{"line":1,"column":44}} + }, + "typeParameters": null + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-not-first/input.js b/packages/babel-parser/test/fixtures/flow/this-annotation/this-not-first/input.js new file mode 100644 index 0000000000..69e929f34f --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-not-first/input.js @@ -0,0 +1 @@ +function foo (a : string, this : number) {} diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-not-first/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/this-not-first/output.json new file mode 100644 index 0000000000..02b00d7837 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-not-first/output.json @@ -0,0 +1,61 @@ +{ + "type": "File", + "start":0,"end":43,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":43}}, + "errors": [ + "SyntaxError: The `this` parameter must be the first function parameter. (1:26)" + ], + "program": { + "type": "Program", + "start":0,"end":43,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":43}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "FunctionDeclaration", + "start":0,"end":43,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":43}}, + "id": { + "type": "Identifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12},"identifierName":"foo"}, + "name": "foo" + }, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":14,"end":24,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":24},"identifierName":"a"}, + "name": "a", + "typeAnnotation": { + "type": "TypeAnnotation", + "start":16,"end":24,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":24}}, + "typeAnnotation": { + "type": "StringTypeAnnotation", + "start":18,"end":24,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":24}} + } + } + }, + { + "type": "Identifier", + "start":26,"end":39,"loc":{"start":{"line":1,"column":26},"end":{"line":1,"column":39},"identifierName":"this"}, + "name": "this", + "typeAnnotation": { + "type": "TypeAnnotation", + "start":31,"end":39,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":39}}, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":33,"end":39,"loc":{"start":{"line":1,"column":33},"end":{"line":1,"column":39}} + } + } + } + ], + "body": { + "type": "BlockStatement", + "start":41,"end":43,"loc":{"start":{"line":1,"column":41},"end":{"line":1,"column":43}}, + "body": [], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-optional-type/input.js b/packages/babel-parser/test/fixtures/flow/this-annotation/this-optional-type/input.js new file mode 100644 index 0000000000..0c28bd181d --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-optional-type/input.js @@ -0,0 +1 @@ +type T = (this? : number) => void diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-optional-type/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/this-optional-type/output.json new file mode 100644 index 0000000000..6e19b60a59 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-optional-type/output.json @@ -0,0 +1,47 @@ +{ + "type": "File", + "start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":33}}, + "errors": [ + "SyntaxError: The `this` parameter cannot be optional. (1:10)" + ], + "program": { + "type": "Program", + "start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":33}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TypeAlias", + "start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":33}}, + "id": { + "type": "Identifier", + "start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6},"identifierName":"T"}, + "name": "T" + }, + "typeParameters": null, + "right": { + "type": "FunctionTypeAnnotation", + "start":9,"end":33,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":33}}, + "params": [], + "rest": null, + "this": { + "type": "FunctionTypeParam", + "start":10,"end":24,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":24}}, + "name": null, + "optional": true, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":18,"end":24,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":24}} + } + }, + "returnType": { + "type": "VoidTypeAnnotation", + "start":29,"end":33,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":33}} + }, + "typeParameters": null + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-optional/input.js b/packages/babel-parser/test/fixtures/flow/this-annotation/this-optional/input.js new file mode 100644 index 0000000000..4b5c4782df --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-optional/input.js @@ -0,0 +1 @@ +function foo(this? : number) {} diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-optional/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/this-optional/output.json new file mode 100644 index 0000000000..33bd48751f --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-optional/output.json @@ -0,0 +1,49 @@ +{ + "type": "File", + "start":0,"end":31,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":31}}, + "errors": [ + "SyntaxError: The `this` parameter cannot be optional. (1:13)" + ], + "program": { + "type": "Program", + "start":0,"end":31,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":31}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "FunctionDeclaration", + "start":0,"end":31,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":31}}, + "id": { + "type": "Identifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12},"identifierName":"foo"}, + "name": "foo" + }, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":13,"end":27,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":27},"identifierName":"this"}, + "name": "this", + "optional": true, + "typeAnnotation": { + "type": "TypeAnnotation", + "start":19,"end":27,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":27}}, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":21,"end":27,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":27}} + } + } + } + ], + "body": { + "type": "BlockStatement", + "start":29,"end":31,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":31}}, + "body": [], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-setter-type/input.js b/packages/babel-parser/test/fixtures/flow/this-annotation/this-setter-type/input.js new file mode 100644 index 0000000000..a1f5eeb702 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-setter-type/input.js @@ -0,0 +1,3 @@ +type T = { + set foo(this: string) : void +} diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-setter-type/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/this-setter-type/output.json new file mode 100644 index 0000000000..06408c4054 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-setter-type/output.json @@ -0,0 +1,73 @@ +{ + "type": "File", + "start":0,"end":43,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "errors": [ + "SyntaxError: A setter cannot have a `this` parameter. (2:10)", + "SyntaxError: setter must have exactly one formal parameter (2:2)" + ], + "program": { + "type": "Program", + "start":0,"end":43,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TypeAlias", + "start":0,"end":43,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "id": { + "type": "Identifier", + "start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6},"identifierName":"T"}, + "name": "T" + }, + "typeParameters": null, + "right": { + "type": "ObjectTypeAnnotation", + "start":9,"end":43,"loc":{"start":{"line":1,"column":9},"end":{"line":3,"column":1}}, + "callProperties": [], + "properties": [ + { + "type": "ObjectTypeProperty", + "start":13,"end":41,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":30}}, + "key": { + "type": "Identifier", + "start":17,"end":20,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":9},"identifierName":"foo"}, + "name": "foo" + }, + "static": false, + "proto": false, + "kind": "set", + "method": true, + "value": { + "type": "FunctionTypeAnnotation", + "start":13,"end":41,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":30}}, + "params": [], + "rest": null, + "typeParameters": null, + "this": { + "type": "FunctionTypeParam", + "start":21,"end":33,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":22}}, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "StringTypeAnnotation", + "start":27,"end":33,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":22}} + } + }, + "returnType": { + "type": "VoidTypeAnnotation", + "start":37,"end":41,"loc":{"start":{"line":2,"column":26},"end":{"line":2,"column":30}} + } + }, + "optional": false + } + ], + "indexers": [], + "internalSlots": [], + "exact": false, + "inexact": false + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-setter/input.js b/packages/babel-parser/test/fixtures/flow/this-annotation/this-setter/input.js new file mode 100644 index 0000000000..4bada6fc79 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-setter/input.js @@ -0,0 +1,3 @@ +class Foo { + set foo(this: string) {} +} diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-setter/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/this-setter/output.json new file mode 100644 index 0000000000..b71169d3ea --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-setter/output.json @@ -0,0 +1,68 @@ +{ + "type": "File", + "start":0,"end":40,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "errors": [ + "SyntaxError: A setter cannot have a `this` parameter. (2:10)" + ], + "program": { + "type": "Program", + "start":0,"end":40,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":0,"end":40,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "id": { + "type": "Identifier", + "start":6,"end":9,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":9},"identifierName":"Foo"}, + "name": "Foo" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":10,"end":40,"loc":{"start":{"line":1,"column":10},"end":{"line":3,"column":1}}, + "body": [ + { + "type": "ClassMethod", + "start":14,"end":38,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":26}}, + "static": false, + "key": { + "type": "Identifier", + "start":18,"end":21,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":9},"identifierName":"foo"}, + "name": "foo" + }, + "computed": false, + "kind": "set", + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":22,"end":34,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":22},"identifierName":"this"}, + "name": "this", + "typeAnnotation": { + "type": "TypeAnnotation", + "start":26,"end":34,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":22}}, + "typeAnnotation": { + "type": "StringTypeAnnotation", + "start":28,"end":34,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":22}} + } + } + } + ], + "body": { + "type": "BlockStatement", + "start":36,"end":38,"loc":{"start":{"line":2,"column":24},"end":{"line":2,"column":26}}, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-string-literal-constructor/input.js b/packages/babel-parser/test/fixtures/flow/this-annotation/this-string-literal-constructor/input.js new file mode 100644 index 0000000000..5d2c393671 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-string-literal-constructor/input.js @@ -0,0 +1,3 @@ +class A { + "constructor"(this: string) {} +} diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-string-literal-constructor/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/this-string-literal-constructor/output.json new file mode 100644 index 0000000000..c9c0891bde --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-string-literal-constructor/output.json @@ -0,0 +1,72 @@ +{ + "type": "File", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "errors": [ + "SyntaxError: Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions. (2:2)" + ], + "program": { + "type": "Program", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "id": { + "type": "Identifier", + "start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"A"}, + "name": "A" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":8,"end":44,"loc":{"start":{"line":1,"column":8},"end":{"line":3,"column":1}}, + "body": [ + { + "type": "ClassMethod", + "start":12,"end":42,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":32}}, + "static": false, + "key": { + "type": "StringLiteral", + "start":12,"end":25,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":15}}, + "extra": { + "rawValue": "constructor", + "raw": "\"constructor\"" + }, + "value": "constructor" + }, + "computed": false, + "kind": "constructor", + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":26,"end":38,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":28},"identifierName":"this"}, + "name": "this", + "typeAnnotation": { + "type": "TypeAnnotation", + "start":30,"end":38,"loc":{"start":{"line":2,"column":20},"end":{"line":2,"column":28}}, + "typeAnnotation": { + "type": "StringTypeAnnotation", + "start":32,"end":38,"loc":{"start":{"line":2,"column":22},"end":{"line":2,"column":28}} + } + } + } + ], + "body": { + "type": "BlockStatement", + "start":40,"end":42,"loc":{"start":{"line":2,"column":30},"end":{"line":2,"column":32}}, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-type/input.js b/packages/babel-parser/test/fixtures/flow/this-annotation/this-type/input.js new file mode 100644 index 0000000000..f472d1c018 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-type/input.js @@ -0,0 +1,5 @@ +type T = (this, number) => void; + +declare class A { + fn(this, number): void; +} diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-type/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/this-type/output.json new file mode 100644 index 0000000000..6e7b440816 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-type/output.json @@ -0,0 +1,125 @@ +{ + "type": "File", + "start":0,"end":79,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}}, + "program": { + "type": "Program", + "start":0,"end":79,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TypeAlias", + "start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":32}}, + "id": { + "type": "Identifier", + "start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6},"identifierName":"T"}, + "name": "T" + }, + "typeParameters": null, + "right": { + "type": "FunctionTypeAnnotation", + "start":9,"end":31,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":31}}, + "params": [ + { + "type": "FunctionTypeParam", + "start":10,"end":15,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":15}}, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "ThisTypeAnnotation", + "start":10,"end":14,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":14}} + } + }, + { + "type": "FunctionTypeParam", + "start":16,"end":22,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":22}}, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":16,"end":22,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":22}} + } + } + ], + "rest": null, + "this": null, + "returnType": { + "type": "VoidTypeAnnotation", + "start":27,"end":31,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":31}} + }, + "typeParameters": null + } + }, + { + "type": "DeclareClass", + "start":34,"end":79,"loc":{"start":{"line":3,"column":0},"end":{"line":5,"column":1}}, + "id": { + "type": "Identifier", + "start":48,"end":49,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":15},"identifierName":"A"}, + "name": "A" + }, + "typeParameters": null, + "extends": [], + "implements": [], + "mixins": [], + "body": { + "type": "ObjectTypeAnnotation", + "start":50,"end":79,"loc":{"start":{"line":3,"column":16},"end":{"line":5,"column":1}}, + "callProperties": [], + "properties": [ + { + "type": "ObjectTypeProperty", + "start":54,"end":76,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":24}}, + "key": { + "type": "Identifier", + "start":54,"end":56,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":4},"identifierName":"fn"}, + "name": "fn" + }, + "static": false, + "proto": false, + "kind": "init", + "method": true, + "value": { + "type": "FunctionTypeAnnotation", + "start":54,"end":76,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":24}}, + "params": [ + { + "type": "FunctionTypeParam", + "start":63,"end":69,"loc":{"start":{"line":4,"column":11},"end":{"line":4,"column":17}}, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start":63,"end":69,"loc":{"start":{"line":4,"column":11},"end":{"line":4,"column":17}} + } + } + ], + "rest": null, + "typeParameters": null, + "this": { + "type": "FunctionTypeParam", + "start":57,"end":61,"loc":{"start":{"line":4,"column":5},"end":{"line":4,"column":9}}, + "name": null, + "optional": false, + "typeAnnotation": { + "type": "ThisTypeAnnotation", + "start":57,"end":61,"loc":{"start":{"line":4,"column":5},"end":{"line":4,"column":9}} + } + }, + "returnType": { + "type": "VoidTypeAnnotation", + "start":72,"end":76,"loc":{"start":{"line":4,"column":20},"end":{"line":4,"column":24}} + } + }, + "optional": false + } + ], + "indexers": [], + "internalSlots": [], + "exact": false + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/type-alias/4/output.json b/packages/babel-parser/test/fixtures/flow/type-alias/4/output.json index 8592d1737f..228f9b9abf 100644 --- a/packages/babel-parser/test/fixtures/flow/type-alias/4/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-alias/4/output.json @@ -126,6 +126,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":83,"end":89,"loc":{"start":{"line":7,"column":20},"end":{"line":7,"column":26}} @@ -152,6 +153,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "StringTypeAnnotation", "start":111,"end":117,"loc":{"start":{"line":8,"column":20},"end":{"line":8,"column":26}} diff --git a/packages/babel-parser/test/fixtures/flow/type-annotations/10/output.json b/packages/babel-parser/test/fixtures/flow/type-annotations/10/output.json index 7a545d1e65..44873b5d54 100644 --- a/packages/babel-parser/test/fixtures/flow/type-annotations/10/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-annotations/10/output.json @@ -59,6 +59,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":47,"end":53,"loc":{"start":{"line":1,"column":47},"end":{"line":1,"column":53}} diff --git a/packages/babel-parser/test/fixtures/flow/type-annotations/11/output.json b/packages/babel-parser/test/fixtures/flow/type-annotations/11/output.json index eff1cf78a2..68c4fdcd36 100644 --- a/packages/babel-parser/test/fixtures/flow/type-annotations/11/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-annotations/11/output.json @@ -73,6 +73,7 @@ } } }, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":58,"end":64,"loc":{"start":{"line":1,"column":58},"end":{"line":1,"column":64}} diff --git a/packages/babel-parser/test/fixtures/flow/type-annotations/13/output.json b/packages/babel-parser/test/fixtures/flow/type-annotations/13/output.json index fe45c3c0e5..d2601c75d8 100644 --- a/packages/babel-parser/test/fixtures/flow/type-annotations/13/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-annotations/13/output.json @@ -27,6 +27,7 @@ "start":15,"end":25,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":25}}, "params": [], "rest": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":21,"end":25,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":25}} diff --git a/packages/babel-parser/test/fixtures/flow/type-annotations/14/output.json b/packages/babel-parser/test/fixtures/flow/type-annotations/14/output.json index 9360adf64e..eadf167279 100644 --- a/packages/babel-parser/test/fixtures/flow/type-annotations/14/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-annotations/14/output.json @@ -42,6 +42,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":27,"end":33,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":33}} diff --git a/packages/babel-parser/test/fixtures/flow/type-annotations/15/output.json b/packages/babel-parser/test/fixtures/flow/type-annotations/15/output.json index f744349140..dcdb5c2b3e 100644 --- a/packages/babel-parser/test/fixtures/flow/type-annotations/15/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-annotations/15/output.json @@ -42,6 +42,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":28,"end":34,"loc":{"start":{"line":1,"column":28},"end":{"line":1,"column":34}} diff --git a/packages/babel-parser/test/fixtures/flow/type-annotations/42/output.json b/packages/babel-parser/test/fixtures/flow/type-annotations/42/output.json index 3e4e5b0900..d75964c311 100644 --- a/packages/babel-parser/test/fixtures/flow/type-annotations/42/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-annotations/42/output.json @@ -87,6 +87,7 @@ } }, "typeParameters": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":43,"end":47,"loc":{"start":{"line":1,"column":43},"end":{"line":1,"column":47}} diff --git a/packages/babel-parser/test/fixtures/flow/type-annotations/43/output.json b/packages/babel-parser/test/fixtures/flow/type-annotations/43/output.json index 049308dd2a..89f3442c57 100644 --- a/packages/babel-parser/test/fixtures/flow/type-annotations/43/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-annotations/43/output.json @@ -76,6 +76,7 @@ } ] }, + "this": null, "returnType": { "type": "GenericTypeAnnotation", "start":22,"end":23,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":23}}, diff --git a/packages/babel-parser/test/fixtures/flow/type-annotations/57/output.json b/packages/babel-parser/test/fixtures/flow/type-annotations/57/output.json index b19b04b972..817e35c3bb 100644 --- a/packages/babel-parser/test/fixtures/flow/type-annotations/57/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-annotations/57/output.json @@ -26,6 +26,7 @@ "start":8,"end":35,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":35}}, "params": [], "rest": null, + "this": null, "returnType": { "type": "UnionTypeAnnotation", "start":14,"end":35,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":35}}, @@ -39,6 +40,7 @@ "start":23,"end":35,"loc":{"start":{"line":1,"column":23},"end":{"line":1,"column":35}}, "params": [], "rest": null, + "this": null, "returnType": { "type": "StringTypeAnnotation", "start":29,"end":35,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":35}} diff --git a/packages/babel-parser/test/fixtures/flow/type-annotations/7/output.json b/packages/babel-parser/test/fixtures/flow/type-annotations/7/output.json index b49204c7f0..9cbace9de4 100644 --- a/packages/babel-parser/test/fixtures/flow/type-annotations/7/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-annotations/7/output.json @@ -30,6 +30,7 @@ "start":23,"end":33,"loc":{"start":{"line":1,"column":23},"end":{"line":1,"column":33}}, "params": [], "rest": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":29,"end":33,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":33}} diff --git a/packages/babel-parser/test/fixtures/flow/type-annotations/8/output.json b/packages/babel-parser/test/fixtures/flow/type-annotations/8/output.json index cbfbdd5166..0527292f0e 100644 --- a/packages/babel-parser/test/fixtures/flow/type-annotations/8/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-annotations/8/output.json @@ -30,6 +30,7 @@ "start":23,"end":35,"loc":{"start":{"line":1,"column":23},"end":{"line":1,"column":35}}, "params": [], "rest": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":29,"end":35,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":35}} diff --git a/packages/babel-parser/test/fixtures/flow/type-annotations/86/output.json b/packages/babel-parser/test/fixtures/flow/type-annotations/86/output.json index 3f525d3603..52ae1d2ec9 100644 --- a/packages/babel-parser/test/fixtures/flow/type-annotations/86/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-annotations/86/output.json @@ -54,6 +54,7 @@ } } }, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":33,"end":39,"loc":{"start":{"line":1,"column":33},"end":{"line":1,"column":39}} diff --git a/packages/babel-parser/test/fixtures/flow/type-annotations/87/output.json b/packages/babel-parser/test/fixtures/flow/type-annotations/87/output.json index 30d0d42c5f..1aefc281d4 100644 --- a/packages/babel-parser/test/fixtures/flow/type-annotations/87/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-annotations/87/output.json @@ -59,6 +59,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "GenericTypeAnnotation", "start":27,"end":28,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":28}}, diff --git a/packages/babel-parser/test/fixtures/flow/type-annotations/88/output.json b/packages/babel-parser/test/fixtures/flow/type-annotations/88/output.json index 1b22c73f74..8a5b0ea84c 100644 --- a/packages/babel-parser/test/fixtures/flow/type-annotations/88/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-annotations/88/output.json @@ -82,6 +82,7 @@ } } }, + "this": null, "returnType": { "type": "GenericTypeAnnotation", "start":37,"end":38,"loc":{"start":{"line":1,"column":37},"end":{"line":1,"column":38}}, diff --git a/packages/babel-parser/test/fixtures/flow/type-annotations/9/output.json b/packages/babel-parser/test/fixtures/flow/type-annotations/9/output.json index 7cb400a2d9..0ebc8fa0a1 100644 --- a/packages/babel-parser/test/fixtures/flow/type-annotations/9/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-annotations/9/output.json @@ -45,6 +45,7 @@ } ], "rest": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":35,"end":41,"loc":{"start":{"line":1,"column":35},"end":{"line":1,"column":41}} diff --git a/packages/babel-parser/test/fixtures/flow/type-annotations/object-type-method/output.json b/packages/babel-parser/test/fixtures/flow/type-annotations/object-type-method/output.json index b8e1ad4ce0..ab63de4100 100644 --- a/packages/babel-parser/test/fixtures/flow/type-annotations/object-type-method/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-annotations/object-type-method/output.json @@ -38,6 +38,7 @@ "start":14,"end":24,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":24}}, "params": [], "rest": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":20,"end":24,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":24}} @@ -97,6 +98,7 @@ }, "params": [], "rest": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":52,"end":56,"loc":{"start":{"line":2,"column":24},"end":{"line":2,"column":28}} @@ -144,6 +146,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":77,"end":81,"loc":{"start":{"line":3,"column":17},"end":{"line":3,"column":21}} @@ -201,6 +204,7 @@ } ] }, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":105,"end":109,"loc":{"start":{"line":4,"column":20},"end":{"line":4,"column":24}} @@ -238,6 +242,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":130,"end":136,"loc":{"start":{"line":6,"column":16},"end":{"line":6,"column":22}} @@ -307,6 +312,7 @@ } ] }, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":163,"end":169,"loc":{"start":{"line":7,"column":23},"end":{"line":7,"column":29}} @@ -356,6 +362,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":200,"end":206,"loc":{"start":{"line":9,"column":26},"end":{"line":9,"column":32}} @@ -404,6 +411,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":243,"end":249,"loc":{"start":{"line":10,"column":33},"end":{"line":10,"column":39}} @@ -443,6 +451,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":276,"end":282,"loc":{"start":{"line":11,"column":23},"end":{"line":11,"column":29}} @@ -482,6 +491,7 @@ "params": [], "rest": null, "typeParameters": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":315,"end":321,"loc":{"start":{"line":12,"column":30},"end":{"line":12,"column":36}} diff --git a/packages/babel-parser/test/fixtures/flow/type-generics/async-arrow-like/output.json b/packages/babel-parser/test/fixtures/flow/type-generics/async-arrow-like/output.json index fa4b9aa787..a65cc34508 100644 --- a/packages/babel-parser/test/fixtures/flow/type-generics/async-arrow-like/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-generics/async-arrow-like/output.json @@ -10,6 +10,18 @@ { "type": "ExpressionStatement", "start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":23}}, + "trailingComments": [ + { + "type": "CommentLine", + "value": " This looks A LOT like an async arrow function, but it isn't because", + "start":25,"end":95,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":70}} + }, + { + "type": "CommentLine", + "value": " T + U isn't a valid type parameter.", + "start":96,"end":134,"loc":{"start":{"line":4,"column":0},"end":{"line":4,"column":38}} + } + ], "expression": { "type": "BinaryExpression", "start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}}, @@ -32,6 +44,10 @@ "right": { "type": "TypeCastExpression", "start":10,"end":21,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":21}}, + "extra": { + "parenthesized": true, + "parenStart": 9 + }, "expression": { "type": "Identifier", "start":10,"end":12,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":12},"identifierName":"fn"}, @@ -45,6 +61,7 @@ "start":14,"end":21,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":21}}, "params": [], "rest": null, + "this": null, "returnType": { "type": "GenericTypeAnnotation", "start":20,"end":21,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":21}}, @@ -57,14 +74,14 @@ }, "typeParameters": null } - }, - "extra": { - "parenthesized": true, - "parenStart": 9 } } - }, - "trailingComments": [ + } + }, + { + "type": "ExpressionStatement", + "start":135,"end":167,"loc":{"start":{"line":5,"column":0},"end":{"line":5,"column":32}}, + "leadingComments": [ { "type": "CommentLine", "value": " This looks A LOT like an async arrow function, but it isn't because", @@ -75,14 +92,14 @@ "value": " T + U isn't a valid type parameter.", "start":96,"end":134,"loc":{"start":{"line":4,"column":0},"end":{"line":4,"column":38}} } - ] - }, - { - "type": "ExpressionStatement", - "start":135,"end":167,"loc":{"start":{"line":5,"column":0},"end":{"line":5,"column":32}}, + ], "expression": { "type": "TypeCastExpression", "start":136,"end":165,"loc":{"start":{"line":5,"column":1},"end":{"line":5,"column":30}}, + "extra": { + "parenthesized": true, + "parenStart": 135 + }, "expression": { "type": "BinaryExpression", "start":136,"end":156,"loc":{"start":{"line":5,"column":1},"end":{"line":5,"column":21}}, @@ -115,6 +132,10 @@ "right": { "type": "TypeCastExpression", "start":150,"end":155,"loc":{"start":{"line":5,"column":15},"end":{"line":5,"column":20}}, + "extra": { + "parenthesized": true, + "parenStart": 149 + }, "expression": { "type": "Identifier", "start":150,"end":152,"loc":{"start":{"line":5,"column":15},"end":{"line":5,"column":17},"identifierName":"fn"}, @@ -133,10 +154,6 @@ "name": "T" } } - }, - "extra": { - "parenthesized": true, - "parenStart": 149 } } }, @@ -177,24 +194,8 @@ }, "typeParameters": null } - }, - "extra": { - "parenthesized": true, - "parenStart": 135 } - }, - "leadingComments": [ - { - "type": "CommentLine", - "value": " This looks A LOT like an async arrow function, but it isn't because", - "start":25,"end":95,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":70}} - }, - { - "type": "CommentLine", - "value": " T + U isn't a valid type parameter.", - "start":96,"end":134,"loc":{"start":{"line":4,"column":0},"end":{"line":4,"column":38}} - } - ] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/flow/type-generics/async-arrow/output.json b/packages/babel-parser/test/fixtures/flow/type-generics/async-arrow/output.json index 3a4d934f13..b5cff2c4e4 100644 --- a/packages/babel-parser/test/fixtures/flow/type-generics/async-arrow/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-generics/async-arrow/output.json @@ -38,6 +38,7 @@ "start":14,"end":21,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":21}}, "params": [], "rest": null, + "this": null, "returnType": { "type": "GenericTypeAnnotation", "start":20,"end":21,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":21}}, diff --git a/packages/babel-parser/test/fixtures/flow/type-grouping/2/output.json b/packages/babel-parser/test/fixtures/flow/type-grouping/2/output.json index f2ee74dd93..95b223ebc0 100644 --- a/packages/babel-parser/test/fixtures/flow/type-grouping/2/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-grouping/2/output.json @@ -30,6 +30,7 @@ "start":8,"end":20,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":20}}, "params": [], "rest": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":14,"end":20,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":20}} @@ -41,6 +42,7 @@ "start":24,"end":36,"loc":{"start":{"line":1,"column":24},"end":{"line":1,"column":36}}, "params": [], "rest": null, + "this": null, "returnType": { "type": "StringTypeAnnotation", "start":30,"end":36,"loc":{"start":{"line":1,"column":30},"end":{"line":1,"column":36}} diff --git a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/declare-class-method-reserved-word/output.json b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/declare-class-method-reserved-word/output.json index 1fcb8935c9..4d111fa582 100644 --- a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/declare-class-method-reserved-word/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/declare-class-method-reserved-word/output.json @@ -53,6 +53,7 @@ } ] }, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":33,"end":37,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":19}} @@ -89,6 +90,7 @@ } ] }, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":54,"end":58,"loc":{"start":{"line":3,"column":15},"end":{"line":3,"column":19}} @@ -125,6 +127,7 @@ } ] }, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":74,"end":78,"loc":{"start":{"line":4,"column":14},"end":{"line":4,"column":18}} @@ -161,6 +164,7 @@ } ] }, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":91,"end":95,"loc":{"start":{"line":5,"column":11},"end":{"line":5,"column":15}} @@ -197,6 +201,7 @@ } ] }, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":119,"end":123,"loc":{"start":{"line":6,"column":22},"end":{"line":6,"column":26}} @@ -233,6 +238,7 @@ } ] }, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":147,"end":151,"loc":{"start":{"line":7,"column":22},"end":{"line":7,"column":26}} @@ -269,6 +275,7 @@ } ] }, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":174,"end":178,"loc":{"start":{"line":8,"column":21},"end":{"line":8,"column":25}} @@ -305,6 +312,7 @@ } ] }, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":198,"end":202,"loc":{"start":{"line":9,"column":18},"end":{"line":9,"column":22}} diff --git a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/declare-interface-method-reserved-word/output.json b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/declare-interface-method-reserved-word/output.json index b1a4c4bd1d..470c0092b7 100644 --- a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/declare-interface-method-reserved-word/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/declare-interface-method-reserved-word/output.json @@ -53,6 +53,7 @@ } ] }, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":37,"end":41,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":19}} @@ -89,6 +90,7 @@ } ] }, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":58,"end":62,"loc":{"start":{"line":3,"column":15},"end":{"line":3,"column":19}} @@ -125,6 +127,7 @@ } ] }, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":78,"end":82,"loc":{"start":{"line":4,"column":14},"end":{"line":4,"column":18}} @@ -161,6 +164,7 @@ } ] }, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":95,"end":99,"loc":{"start":{"line":5,"column":11},"end":{"line":5,"column":15}} diff --git a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default/output.json b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default/output.json index 767a8b8754..615b9d5216 100644 --- a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default/output.json @@ -420,6 +420,10 @@ "expression": { "type": "ClassExpression", "start":297,"end":321,"loc":{"start":{"line":10,"column":2},"end":{"line":10,"column":26}}, + "extra": { + "parenthesized": true, + "parenStart": 296 + }, "id": { "type": "Identifier", "start":303,"end":306,"loc":{"start":{"line":10,"column":8},"end":{"line":10,"column":11},"identifierName":"A10"}, @@ -446,10 +450,6 @@ "type": "ClassBody", "start":319,"end":321,"loc":{"start":{"line":10,"column":24},"end":{"line":10,"column":26}}, "body": [] - }, - "extra": { - "parenthesized": true, - "parenStart": 296 } } }, @@ -459,6 +459,10 @@ "expression": { "type": "ClassExpression", "start":325,"end":358,"loc":{"start":{"line":11,"column":2},"end":{"line":11,"column":35}}, + "extra": { + "parenthesized": true, + "parenStart": 324 + }, "id": { "type": "Identifier", "start":331,"end":334,"loc":{"start":{"line":11,"column":8},"end":{"line":11,"column":11},"identifierName":"A11"}, @@ -497,10 +501,6 @@ "type": "ClassBody", "start":356,"end":358,"loc":{"start":{"line":11,"column":33},"end":{"line":11,"column":35}}, "body": [] - }, - "extra": { - "parenthesized": true, - "parenStart": 324 } } }, @@ -510,6 +510,10 @@ "expression": { "type": "ClassExpression", "start":362,"end":398,"loc":{"start":{"line":12,"column":2},"end":{"line":12,"column":38}}, + "extra": { + "parenthesized": true, + "parenStart": 361 + }, "id": { "type": "Identifier", "start":368,"end":371,"loc":{"start":{"line":12,"column":8},"end":{"line":12,"column":11},"identifierName":"A12"}, @@ -554,10 +558,6 @@ "type": "ClassBody", "start":396,"end":398,"loc":{"start":{"line":12,"column":36},"end":{"line":12,"column":38}}, "body": [] - }, - "extra": { - "parenthesized": true, - "parenStart": 361 } } }, @@ -567,6 +567,10 @@ "expression": { "type": "ClassExpression", "start":402,"end":447,"loc":{"start":{"line":13,"column":2},"end":{"line":13,"column":47}}, + "extra": { + "parenthesized": true, + "parenStart": 401 + }, "id": { "type": "Identifier", "start":408,"end":411,"loc":{"start":{"line":13,"column":8},"end":{"line":13,"column":11},"identifierName":"A13"}, @@ -615,10 +619,6 @@ "type": "ClassBody", "start":445,"end":447,"loc":{"start":{"line":13,"column":45},"end":{"line":13,"column":47}}, "body": [] - }, - "extra": { - "parenthesized": true, - "parenStart": 401 } } }, @@ -1101,6 +1101,10 @@ "expression": { "type": "ObjectExpression", "start":836,"end":860,"loc":{"start":{"line":24,"column":2},"end":{"line":24,"column":26}}, + "extra": { + "parenthesized": true, + "parenStart": 835 + }, "properties": [ { "type": "ObjectMethod", @@ -1140,11 +1144,7 @@ ] } } - ], - "extra": { - "parenthesized": true, - "parenStart": 835 - } + ] } }, { @@ -1211,6 +1211,10 @@ "expression": { "type": "ClassExpression", "start":902,"end":938,"loc":{"start":{"line":28,"column":2},"end":{"line":30,"column":1}}, + "extra": { + "parenthesized": true, + "parenStart": 901 + }, "id": { "type": "Identifier", "start":908,"end":911,"loc":{"start":{"line":28,"column":8},"end":{"line":28,"column":11},"identifierName":"A30"}, @@ -1260,10 +1264,6 @@ } } ] - }, - "extra": { - "parenthesized": true, - "parenStart": 901 } } }, @@ -1317,6 +1317,7 @@ } ] }, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":980,"end":984,"loc":{"start":{"line":31,"column":39},"end":{"line":31,"column":43}} diff --git a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/interface-reserved-word/output.json b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/interface-reserved-word/output.json index fc8e1f196b..6b114cbbac 100644 --- a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/interface-reserved-word/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/interface-reserved-word/output.json @@ -53,6 +53,7 @@ } ] }, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":29,"end":33,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":19}} @@ -89,6 +90,7 @@ } ] }, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":50,"end":54,"loc":{"start":{"line":3,"column":15},"end":{"line":3,"column":19}} @@ -125,6 +127,7 @@ } ] }, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":70,"end":74,"loc":{"start":{"line":4,"column":14},"end":{"line":4,"column":18}} @@ -161,6 +164,7 @@ } ] }, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":87,"end":91,"loc":{"start":{"line":5,"column":11},"end":{"line":5,"column":15}} diff --git a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/type-object-reserved-word/output.json b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/type-object-reserved-word/output.json index 86e2ee528e..093870a8b2 100644 --- a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/type-object-reserved-word/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/type-object-reserved-word/output.json @@ -50,6 +50,7 @@ } ] }, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":26,"end":30,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":19}} @@ -86,6 +87,7 @@ } ] }, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":47,"end":51,"loc":{"start":{"line":3,"column":15},"end":{"line":3,"column":19}} @@ -122,6 +124,7 @@ } ] }, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":67,"end":71,"loc":{"start":{"line":4,"column":14},"end":{"line":4,"column":18}} @@ -158,6 +161,7 @@ } ] }, + "this": null, "returnType": { "type": "VoidTypeAnnotation", "start":84,"end":88,"loc":{"start":{"line":5,"column":11},"end":{"line":5,"column":15}} diff --git a/packages/babel-parser/test/fixtures/flow/typecasts/3/output.json b/packages/babel-parser/test/fixtures/flow/typecasts/3/output.json index 0e3ddeef54..abd3347382 100644 --- a/packages/babel-parser/test/fixtures/flow/typecasts/3/output.json +++ b/packages/babel-parser/test/fixtures/flow/typecasts/3/output.json @@ -13,6 +13,10 @@ "expression": { "type": "TypeCastExpression", "start":1,"end":42,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":42}}, + "extra": { + "parenthesized": true, + "parenStart": 0 + }, "expression": { "type": "ArrowFunctionExpression", "start":1,"end":17,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":17}}, @@ -69,16 +73,13 @@ } ], "rest": null, + "this": null, "returnType": { "type": "NumberTypeAnnotation", "start":36,"end":42,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":42}} }, "typeParameters": null } - }, - "extra": { - "parenthesized": true, - "parenStart": 0 } } } diff --git a/packages/babel-plugin-transform-flow-strip-types/src/index.js b/packages/babel-plugin-transform-flow-strip-types/src/index.js index a5e0fed85d..5d293ae474 100644 --- a/packages/babel-plugin-transform-flow-strip-types/src/index.js +++ b/packages/babel-plugin-transform-flow-strip-types/src/index.js @@ -130,6 +130,13 @@ export default declare((api, opts) => { Function({ node }) { if (skipStrip) return; + if ( + node.params.length > 0 && + node.params[0].type === "Identifier" && + node.params[0].name === "this" + ) { + node.params.shift(); + } for (let i = 0; i < node.params.length; i++) { const param = node.params[i]; param.optional = false; diff --git a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-this-param/input.js b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-this-param/input.js new file mode 100644 index 0000000000..0f879d0068 --- /dev/null +++ b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-this-param/input.js @@ -0,0 +1,26 @@ +function one (this : number) {} +function two (this : number, a) {} +function three (this : number, ...a) {} +function four (this : number, a, b, ...c) {} +function five (this : T) {} + +type six = (this : number) => void +type seven = { o(this : string) : void } +declare function eight (this : number) : void +declare class Nine { m (this : number) : void } + +class Ten { + m1 (this : number) {} + m2 (this : number, a) {} + m3 (this : number, ...a) {} + m4 (this : number, a, b, ...c) {} + m5 (this : T) {} +} + +let eleven = { + m1 (this : number) {}, + m2 (this : number, a) {}, + m3 (this : number, ...a) {}, + m4 (this : number, a, b, ...c) {}, + m5 (this : T) {} +} diff --git a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-this-param/output.js b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-this-param/output.js new file mode 100644 index 0000000000..4111c82691 --- /dev/null +++ b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-this-param/output.js @@ -0,0 +1,35 @@ +function one() {} + +function two(a) {} + +function three(...a) {} + +function four(a, b, ...c) {} + +function five() {} + +class Ten { + m1() {} + + m2(a) {} + + m3(...a) {} + + m4(a, b, ...c) {} + + m5() {} + +} + +let eleven = { + m1() {}, + + m2(a) {}, + + m3(...a) {}, + + m4(a, b, ...c) {}, + + m5() {} + +}; diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/parameters/flow-this-param/input.js b/packages/babel-plugin-transform-parameters/test/fixtures/parameters/flow-this-param/input.js new file mode 100644 index 0000000000..0fc352021e --- /dev/null +++ b/packages/babel-plugin-transform-parameters/test/fixtures/parameters/flow-this-param/input.js @@ -0,0 +1,3 @@ +function foo (this : number, x : number, y : string, ...z : any) { + x + y + z[0]; +} diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/parameters/flow-this-param/output.js b/packages/babel-plugin-transform-parameters/test/fixtures/parameters/flow-this-param/output.js new file mode 100644 index 0000000000..79906598b7 --- /dev/null +++ b/packages/babel-plugin-transform-parameters/test/fixtures/parameters/flow-this-param/output.js @@ -0,0 +1,3 @@ +function foo(this: number, x: number, y: string) { + x + y + (arguments.length <= 2 ? undefined : arguments[2]); +} diff --git a/packages/babel-types/src/ast-types/generated/index.ts b/packages/babel-types/src/ast-types/generated/index.ts index ced0274bc5..1084eb220b 100755 --- a/packages/babel-types/src/ast-types/generated/index.ts +++ b/packages/babel-types/src/ast-types/generated/index.ts @@ -1118,6 +1118,7 @@ export interface FunctionTypeAnnotation extends BaseNode { params: Array; rest?: FunctionTypeParam | null; returnType: FlowType; + this?: FunctionTypeParam | null; } export interface FunctionTypeParam extends BaseNode { diff --git a/packages/babel-types/src/definitions/flow.ts b/packages/babel-types/src/definitions/flow.ts index bb1015d18c..9204fe10b2 100644 --- a/packages/babel-types/src/definitions/flow.ts +++ b/packages/babel-types/src/definitions/flow.ts @@ -173,6 +173,7 @@ defineType("FunctionTypeAnnotation", { typeParameters: validateOptionalType("TypeParameterDeclaration"), params: validate(arrayOfType("FunctionTypeParam")), rest: validateOptionalType("FunctionTypeParam"), + this: validateOptionalType("FunctionTypeParam"), returnType: validateType("FlowType"), }, });