diff --git a/lib/flow_doctrine.js b/lib/flow_doctrine.js index b0b75f153..dd1d7b004 100644 --- a/lib/flow_doctrine.js +++ b/lib/flow_doctrine.js @@ -1,6 +1,8 @@ 'use strict'; /* @flow */ +const generate = require('babel-generator').default; + var namedTypes = { NumberTypeAnnotation: 'number', BooleanTypeAnnotation: 'boolean', @@ -14,12 +16,6 @@ var oneToOne = { VoidTypeAnnotation: 'VoidLiteral' }; -var literalTypes = { - BooleanLiteralTypeAnnotation: 'BooleanLiteralType', - NumericLiteralTypeAnnotation: 'NumericLiteralType', - StringLiteralTypeAnnotation: 'StringLiteralType' -}; - function propertyToField(property) { var type = flowDoctrine(property.value); if (property.optional) { @@ -116,7 +112,9 @@ function flowDoctrine(type /*: Object */) /*: DoctrineType*/ { type: 'TypeApplication', expression: { type: 'NameExpression', - name: type.id.name + name: generate(type.id, { + compact: true + }).code }, applications: type.typeParameters.params.map(flowDoctrine) }; @@ -124,7 +122,9 @@ function flowDoctrine(type /*: Object */) /*: DoctrineType*/ { return { type: 'NameExpression', - name: type.id.name + name: generate(type.id, { + compact: true + }).code }; case 'ObjectTypeAnnotation': @@ -137,20 +137,30 @@ function flowDoctrine(type /*: Object */) /*: DoctrineType*/ { return { type: 'NameExpression', - name: type.id.name + name: generate(type.id, { + compact: true + }).code + }; + case 'BooleanLiteralTypeAnnotation': + return { + type: 'BooleanLiteralType', + value: type.value + }; + case 'NumericLiteralTypeAnnotation': + return { + type: 'NumericLiteralType', + value: type.value + }; + case 'StringLiteralTypeAnnotation': + return { + type: 'StringLiteralType', + value: type.value + }; + default: + return { + type: 'AllLiteral' }; } - - if (type.type in literalTypes) { - return { - type: literalTypes[type.type], - value: type.value - }; - } - - return { - type: 'AllLiteral' - }; } module.exports = flowDoctrine; diff --git a/test/fixture/es6.input.js b/test/fixture/es6.input.js index 8ee1ff346..8381a946f 100644 --- a/test/fixture/es6.input.js +++ b/test/fixture/es6.input.js @@ -154,3 +154,8 @@ export function isArrayEqualWith( ): boolean { return true; } + +/** Regression check for #749 */ +export function paramWithMemberType(a: atype.property): boolean { + return true; +} diff --git a/test/fixture/es6.output-toc.md b/test/fixture/es6.output-toc.md index 4060592c0..4a058bf09 100644 --- a/test/fixture/es6.output-toc.md +++ b/test/fixture/es6.output-toc.md @@ -138,3 +138,13 @@ Regression check for #498 - `compareFunction` **function (a: T, b: T): [boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** (optional, default `(a:T,b:T):boolean=>a===b`) Returns **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** + +## paramWithMemberType + +Regression check for #749 + +**Parameters** + +- `a` **atype.property** + +Returns **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** diff --git a/test/fixture/es6.output.json b/test/fixture/es6.output.json index 2f66ac9b6..c9c1e99d6 100644 --- a/test/fixture/es6.output.json +++ b/test/fixture/es6.output.json @@ -2841,5 +2841,125 @@ } ], "namespace": "isArrayEqualWith" + }, + { + "description": { + "type": "root", + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "text", + "value": "Regression check for #749", + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 26, + "offset": 25 + }, + "indent": [] + } + } + ], + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 26, + "offset": 25 + }, + "indent": [] + } + } + ], + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 26, + "offset": 25 + } + } + }, + "tags": [], + "loc": { + "start": { + "line": 158, + "column": 0 + }, + "end": { + "line": 158, + "column": 32 + } + }, + "context": { + "loc": { + "start": { + "line": 159, + "column": 0 + }, + "end": { + "line": 161, + "column": 1 + } + } + }, + "augments": [], + "errors": [], + "examples": [], + "params": [ + { + "title": "param", + "name": "a", + "lineNumber": 159, + "type": { + "type": "NameExpression", + "name": "atype.property" + } + } + ], + "properties": [], + "returns": [ + { + "title": "returns", + "type": { + "type": "NameExpression", + "name": "boolean" + } + } + ], + "sees": [], + "throws": [], + "todos": [], + "name": "paramWithMemberType", + "kind": "function", + "members": { + "global": [], + "inner": [], + "instance": [], + "events": [], + "static": [] + }, + "path": [ + { + "name": "paramWithMemberType", + "kind": "function" + } + ], + "namespace": "paramWithMemberType" } ] \ No newline at end of file diff --git a/test/fixture/es6.output.md b/test/fixture/es6.output.md index 416e00c6a..8cd7b6b91 100644 --- a/test/fixture/es6.output.md +++ b/test/fixture/es6.output.md @@ -22,6 +22,7 @@ - [iAmPublic](#iampublic) - [execute](#execute) - [isArrayEqualWith](#isarrayequalwith) +- [paramWithMemberType](#paramwithmembertype) ## destructure @@ -161,3 +162,13 @@ Regression check for #498 - `compareFunction` **function (a: T, b: T): [boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** (optional, default `(a:T,b:T):boolean=>a===b`) Returns **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** + +## paramWithMemberType + +Regression check for #749 + +**Parameters** + +- `a` **atype.property** + +Returns **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** diff --git a/test/fixture/es6.output.md.json b/test/fixture/es6.output.md.json index 3c6e7eb2b..4cfaddb81 100644 --- a/test/fixture/es6.output.md.json +++ b/test/fixture/es6.output.md.json @@ -2243,6 +2243,126 @@ } ] }, + { + "type": "paragraph", + "children": [ + { + "type": "text", + "value": "Returns " + }, + { + "type": "strong", + "children": [ + { + "href": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean", + "url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean", + "type": "link", + "children": [ + { + "type": "text", + "value": "boolean" + } + ] + } + ] + }, + { + "type": "text", + "value": " " + } + ] + }, + { + "depth": 2, + "type": "heading", + "children": [ + { + "type": "text", + "value": "paramWithMemberType" + } + ] + }, + { + "type": "paragraph", + "children": [ + { + "type": "text", + "value": "Regression check for #749", + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 26, + "offset": 25 + }, + "indent": [] + } + } + ], + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 26, + "offset": 25 + }, + "indent": [] + } + }, + { + "type": "strong", + "children": [ + { + "type": "text", + "value": "Parameters" + } + ] + }, + { + "ordered": false, + "type": "list", + "children": [ + { + "type": "listItem", + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "inlineCode", + "value": "a" + }, + { + "type": "text", + "value": " " + }, + { + "type": "strong", + "children": [ + { + "type": "text", + "value": "atype.property" + } + ] + }, + { + "type": "text", + "value": " " + } + ] + } + ] + } + ] + }, { "type": "paragraph", "children": [ diff --git a/test/format_type.js b/test/format_type.js index 667c7613d..6bb3c7279 100644 --- a/test/format_type.js +++ b/test/format_type.js @@ -22,6 +22,7 @@ test('formatType', function(t) { ['null', 'null'], ['null', 'null'], ['*', 'any'], + ['namedType.typeProperty', 'namedType.typeProperty'], [ 'Array|undefined', '([Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) \\| [undefined](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined))' diff --git a/test/lib/flow_doctrine.js b/test/lib/flow_doctrine.js index 5b36b7fc1..da9d62bb7 100644 --- a/test/lib/flow_doctrine.js +++ b/test/lib/flow_doctrine.js @@ -117,6 +117,105 @@ test('flowDoctrine', function(t) { 'object' ); + t.deepEqual( + toDoctrineType('namedType.propertyOfType'), + { + type: 'NameExpression', + name: 'namedType.propertyOfType' + }, + 'named type with property' + ); + + t.deepEqual( + toDoctrineType('Array'), + { + applications: [ + { + type: 'NameExpression', + name: 'namedType.propertyOfType' + } + ], + expression: { + name: 'Array', + type: 'NameExpression' + }, + type: 'TypeApplication' + }, + 'typeapplication of named type with property' + ); + + t.deepEqual( + toDoctrineType('Array>'), + { + applications: [ + { + applications: [ + { + name: 'boolean', + type: 'NameExpression' + } + ], + expression: { + type: 'NameExpression', + name: 'namedType.propertyOfType' + }, + type: 'TypeApplication' + } + ], + expression: { + name: 'Array', + type: 'NameExpression' + }, + type: 'TypeApplication' + }, + 'nested type application' + ); + + t.deepEqual( + toDoctrineType('{ a: foo.bar }'), + { + fields: [ + { + key: 'a', + type: 'FieldType', + value: { + name: 'foo.bar', + type: 'NameExpression' + } + } + ], + type: 'RecordType' + }, + 'object type expression with subtype' + ); + + t.deepEqual( + toDoctrineType('{ a: { b: foo.bar } }'), + { + fields: [ + { + key: 'a', + type: 'FieldType', + value: { + type: 'RecordType', + fields: [ + { + key: 'b', + type: 'FieldType', + value: { + name: 'foo.bar', + type: 'NameExpression' + } + } + ] + } + } + ], + type: 'RecordType' + }, + 'nested fieldtype' + ); + t.deepEqual( toDoctrineType('{ a: 1 }'), {