From c9243e8e97535c40f3f7eecf5c292cb80c59ad88 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 29 Mar 2015 16:44:36 +1100 Subject: [PATCH] fixes #1114 - The visitor keys for `ObjectTypeAnnotation`s were incorrect so those nodes weren't being traversed so comments weren't attached for them. - The type parser wasn't eating the semicolons for the nodes so the `end` location of each of the type properties wasn't accurate which threw off the code generation newline algorithm. - Type properties hadn't been given the `UserWhitespacable` alias. --- src/acorn/plugins/flow.js | 14 ++++++++++---- src/babel/generation/generators/flow.js | 12 +++++++++++- src/babel/generation/index.js | 2 +- src/babel/types/alias-keys.json | 6 +++--- src/babel/types/visitor-keys.json | 2 +- 5 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/acorn/plugins/flow.js b/src/acorn/plugins/flow.js index 2021a7a4d9..4f57bf078d 100644 --- a/src/acorn/plugins/flow.js +++ b/src/acorn/plugins/flow.js @@ -215,6 +215,7 @@ pp.flow_parseObjectTypeIndexer = function (node, isStatic) { this.expect(tt.colon) node.value = this.flow_parseType() + this.flow_objectTypeSemicolon() return this.finishNode(node, "ObjectTypeIndexer") } @@ -251,6 +252,7 @@ pp.flow_parseObjectTypeMethod = function (start, isStatic, key) { node.static = isStatic node.key = key node.optional = false + this.flow_objectTypeSemicolon() return this.finishNode(node, "ObjectTypeProperty") } @@ -258,6 +260,7 @@ pp.flow_parseObjectTypeCallProperty = function (node, isStatic) { var valueNode = this.startNode() node.static = isStatic node.value = this.flow_parseObjectTypeMethodish(valueNode) + this.flow_objectTypeSemicolon() return this.finishNode(node, "ObjectTypeCallProperty") } @@ -307,13 +310,10 @@ pp.flow_parseObjectType = function (allowStatic) { node.value = this.flow_parseType() node.optional = optional node.static = isStatic + this.flow_objectTypeSemicolon() nodeStart.properties.push(this.finishNode(node, "ObjectTypeProperty")) } } - - if (!this.eat(tt.semi) && this.type !== tt.braceR) { - this.unexpected() - } } this.expect(tt.braceR) @@ -321,6 +321,12 @@ pp.flow_parseObjectType = function (allowStatic) { return this.finishNode(nodeStart, "ObjectTypeAnnotation") } +pp.flow_objectTypeSemicolon = function () { + if (!this.eat(tt.semi) && this.type !== tt.braceR) { + this.unexpected() + } +} + pp.flow_parseGenericType = function (start, id) { var node = this.startNodeAt(start) diff --git a/src/babel/generation/generators/flow.js b/src/babel/generation/generators/flow.js index 7ae499c22a..1c55f9ee5b 100644 --- a/src/babel/generation/generators/flow.js +++ b/src/babel/generation/generators/flow.js @@ -167,17 +167,25 @@ export { TypeParameterInstantiation as TypeParameterDeclaration }; export function ObjectTypeAnnotation(node, print) { this.push("{"); var props = node.properties.concat(node.callProperties, node.indexers); + if (props.length) { this.space(); - print.list(props, { indent: true, separator: "; " }); + + print.list(props, { + separator: false, + indent: true + }); + this.space(); } + this.push("}"); } export function ObjectTypeCallProperty(node, print) { if (node.static) this.push("static "); print(node.value); + this.semicolon(); } export function ObjectTypeIndexer(node, print) { @@ -191,6 +199,7 @@ export function ObjectTypeIndexer(node, print) { this.push(":"); this.space(); print(node.value); + this.semicolon(); } export function ObjectTypeProperty(node, print) { @@ -202,6 +211,7 @@ export function ObjectTypeProperty(node, print) { this.space(); } print(node.value); + this.semicolon(); } export function QualifiedTypeIdentifier(node, print) { diff --git a/src/babel/generation/index.js b/src/babel/generation/index.js index 35f088f250..4a52df5a40 100644 --- a/src/babel/generation/index.js +++ b/src/babel/generation/index.js @@ -100,7 +100,7 @@ class CodeGenerator { }; print.list = function (items, opts = {}) { - opts.separator ||= ", "; + if (opts.separator == null) opts.separator = ", "; print.join(items, opts); }; diff --git a/src/babel/types/alias-keys.json b/src/babel/types/alias-keys.json index 4736caa737..df6bdecf4b 100644 --- a/src/babel/types/alias-keys.json +++ b/src/babel/types/alias-keys.json @@ -98,9 +98,9 @@ "TypeParameterDeclaration": ["Flow"], "TypeParameterInstantiation": ["Flow"], "ObjectTypeAnnotation": ["Flow"], - "ObjectTypeCallProperty": ["Flow"], - "ObjectTypeIndexer": ["Flow"], - "ObjectTypeProperty": ["Flow"], + "ObjectTypeCallProperty": ["Flow", "UserWhitespacable"], + "ObjectTypeIndexer": ["Flow", "UserWhitespacable"], + "ObjectTypeProperty": ["Flow", "UserWhitespacable"], "QualifiedTypeIdentifier": ["Flow"], "UnionTypeAnnotation": ["Flow"], "VoidTypeAnnotation": ["Flow"], diff --git a/src/babel/types/visitor-keys.json b/src/babel/types/visitor-keys.json index f67fb969c0..6df9c31634 100644 --- a/src/babel/types/visitor-keys.json +++ b/src/babel/types/visitor-keys.json @@ -98,7 +98,7 @@ "TypeCastExpression": ["expression", "typeAnnotation"], "TypeParameterDeclaration": ["params"], "TypeParameterInstantiation": ["params"], - "ObjectTypeAnnotation": ["key", "value"], + "ObjectTypeAnnotation": ["properties", "indexers", "callProperties"], "ObjectTypeCallProperty": ["value"], "ObjectTypeIndexer": ["id", "key", "value"], "ObjectTypeProperty": ["key", "value"],