Skip to content

Commit 8c14481

Browse files
committed
Treat flow nullable types as nullable
Previously they were treated as optional params which is not the same thing.
1 parent 74445b6 commit 8c14481

File tree

6 files changed

+321
-26
lines changed

6 files changed

+321
-26
lines changed

lib/flow_doctrine.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function flowDoctrine(type) {
4343

4444
if (type.type === 'NullableTypeAnnotation') {
4545
return {
46-
type: 'OptionalType',
46+
type: 'NullableType',
4747
expression: flowDoctrine(type.typeAnnotation)
4848
};
4949
}

test/fixture/sync/flow-types.input.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ type Point = {
2828
*/
2929
type Two = {
3030
x: number,
31-
y: number
31+
y: number,
32+
z: ?number
3233
};
3334

3435
/**
@@ -45,3 +46,9 @@ function veryImportantTransform(
4546
): string {
4647
return "42";
4748
}
49+
50+
51+
/**
52+
* Function with optional parameter.
53+
*/
54+
function optionalFunc(x: number = 42) {}

test/fixture/sync/flow-types.output.json

Lines changed: 134 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"column": 1
7575
}
7676
},
77-
"code": "/**\n * This function returns the number one.\n */\nfunction addThem(a: Point, b: string, c: ?boolean, d: Array<number>, e: Object, f: Named): number {\n return a + b + c + d + e;\n}\n\n/**\n * A 2D point.\n *\n * @property {number} x this is a prop\n */\ntype Point = {\n x: number,\n y: number,\n rgb: {\n hex: string\n },\n props: {\n radius: {\n x: number\n }\n }\n};\n\n/**\n * A type with entirely derived properties\n */\ntype Two = {\n x: number,\n y: number\n};\n\n/**\n * Just an alias for an array of strings\n */\ntype T = Array<string>;\n\n/**\n * Very Important Transform\n */\nfunction veryImportantTransform(\n input: Array<string>,\n options: Object = {}\n): string {\n return \"42\";\n}\n"
77+
"code": "/**\n * This function returns the number one.\n */\nfunction addThem(a: Point, b: string, c: ?boolean, d: Array<number>, e: Object, f: Named): number {\n return a + b + c + d + e;\n}\n\n/**\n * A 2D point.\n *\n * @property {number} x this is a prop\n */\ntype Point = {\n x: number,\n y: number,\n rgb: {\n hex: string\n },\n props: {\n radius: {\n x: number\n }\n }\n};\n\n/**\n * A type with entirely derived properties\n */\ntype Two = {\n x: number,\n y: number,\n z: ?number\n};\n\n/**\n * Just an alias for an array of strings\n */\ntype T = Array<string>;\n\n/**\n * Very Important Transform\n */\nfunction veryImportantTransform(\n input: Array<string>,\n options: Object = {}\n): string {\n return \"42\";\n}\n\n\n/**\n * Function with optional parameter.\n */\nfunction optionalFunc(x: number = 42) {}\n"
7878
},
7979
"errors": [],
8080
"name": "addThem",
@@ -103,7 +103,7 @@
103103
"name": "c",
104104
"lineNumber": 4,
105105
"type": {
106-
"type": "OptionalType",
106+
"type": "NullableType",
107107
"expression": {
108108
"type": "NameExpression",
109109
"name": "boolean"
@@ -253,7 +253,7 @@
253253
"column": 2
254254
}
255255
},
256-
"code": "/**\n * This function returns the number one.\n */\nfunction addThem(a: Point, b: string, c: ?boolean, d: Array<number>, e: Object, f: Named): number {\n return a + b + c + d + e;\n}\n\n/**\n * A 2D point.\n *\n * @property {number} x this is a prop\n */\ntype Point = {\n x: number,\n y: number,\n rgb: {\n hex: string\n },\n props: {\n radius: {\n x: number\n }\n }\n};\n\n/**\n * A type with entirely derived properties\n */\ntype Two = {\n x: number,\n y: number\n};\n\n/**\n * Just an alias for an array of strings\n */\ntype T = Array<string>;\n\n/**\n * Very Important Transform\n */\nfunction veryImportantTransform(\n input: Array<string>,\n options: Object = {}\n): string {\n return \"42\";\n}\n"
256+
"code": "/**\n * This function returns the number one.\n */\nfunction addThem(a: Point, b: string, c: ?boolean, d: Array<number>, e: Object, f: Named): number {\n return a + b + c + d + e;\n}\n\n/**\n * A 2D point.\n *\n * @property {number} x this is a prop\n */\ntype Point = {\n x: number,\n y: number,\n rgb: {\n hex: string\n },\n props: {\n radius: {\n x: number\n }\n }\n};\n\n/**\n * A type with entirely derived properties\n */\ntype Two = {\n x: number,\n y: number,\n z: ?number\n};\n\n/**\n * Just an alias for an array of strings\n */\ntype T = Array<string>;\n\n/**\n * Very Important Transform\n */\nfunction veryImportantTransform(\n input: Array<string>,\n options: Object = {}\n): string {\n return \"42\";\n}\n\n\n/**\n * Function with optional parameter.\n */\nfunction optionalFunc(x: number = 42) {}\n"
257257
},
258258
"errors": [],
259259
"properties": [
@@ -463,11 +463,11 @@
463463
"column": 0
464464
},
465465
"end": {
466-
"line": 32,
466+
"line": 33,
467467
"column": 2
468468
}
469469
},
470-
"code": "/**\n * This function returns the number one.\n */\nfunction addThem(a: Point, b: string, c: ?boolean, d: Array<number>, e: Object, f: Named): number {\n return a + b + c + d + e;\n}\n\n/**\n * A 2D point.\n *\n * @property {number} x this is a prop\n */\ntype Point = {\n x: number,\n y: number,\n rgb: {\n hex: string\n },\n props: {\n radius: {\n x: number\n }\n }\n};\n\n/**\n * A type with entirely derived properties\n */\ntype Two = {\n x: number,\n y: number\n};\n\n/**\n * Just an alias for an array of strings\n */\ntype T = Array<string>;\n\n/**\n * Very Important Transform\n */\nfunction veryImportantTransform(\n input: Array<string>,\n options: Object = {}\n): string {\n return \"42\";\n}\n"
470+
"code": "/**\n * This function returns the number one.\n */\nfunction addThem(a: Point, b: string, c: ?boolean, d: Array<number>, e: Object, f: Named): number {\n return a + b + c + d + e;\n}\n\n/**\n * A 2D point.\n *\n * @property {number} x this is a prop\n */\ntype Point = {\n x: number,\n y: number,\n rgb: {\n hex: string\n },\n props: {\n radius: {\n x: number\n }\n }\n};\n\n/**\n * A type with entirely derived properties\n */\ntype Two = {\n x: number,\n y: number,\n z: ?number\n};\n\n/**\n * Just an alias for an array of strings\n */\ntype T = Array<string>;\n\n/**\n * Very Important Transform\n */\nfunction veryImportantTransform(\n input: Array<string>,\n options: Object = {}\n): string {\n return \"42\";\n}\n\n\n/**\n * Function with optional parameter.\n */\nfunction optionalFunc(x: number = 42) {}\n"
471471
},
472472
"errors": [],
473473
"name": "Two",
@@ -490,6 +490,18 @@
490490
"type": "NameExpression",
491491
"name": "number"
492492
}
493+
},
494+
{
495+
"title": "property",
496+
"name": "z",
497+
"lineNumber": 32,
498+
"type": {
499+
"type": "NullableType",
500+
"expression": {
501+
"type": "NameExpression",
502+
"name": "number"
503+
}
504+
}
493505
}
494506
],
495507
"members": {
@@ -560,26 +572,26 @@
560572
"tags": [],
561573
"loc": {
562574
"start": {
563-
"line": 34,
575+
"line": 35,
564576
"column": 0
565577
},
566578
"end": {
567-
"line": 36,
579+
"line": 37,
568580
"column": 3
569581
}
570582
},
571583
"context": {
572584
"loc": {
573585
"start": {
574-
"line": 37,
586+
"line": 38,
575587
"column": 0
576588
},
577589
"end": {
578-
"line": 37,
590+
"line": 38,
579591
"column": 23
580592
}
581593
},
582-
"code": "/**\n * This function returns the number one.\n */\nfunction addThem(a: Point, b: string, c: ?boolean, d: Array<number>, e: Object, f: Named): number {\n return a + b + c + d + e;\n}\n\n/**\n * A 2D point.\n *\n * @property {number} x this is a prop\n */\ntype Point = {\n x: number,\n y: number,\n rgb: {\n hex: string\n },\n props: {\n radius: {\n x: number\n }\n }\n};\n\n/**\n * A type with entirely derived properties\n */\ntype Two = {\n x: number,\n y: number\n};\n\n/**\n * Just an alias for an array of strings\n */\ntype T = Array<string>;\n\n/**\n * Very Important Transform\n */\nfunction veryImportantTransform(\n input: Array<string>,\n options: Object = {}\n): string {\n return \"42\";\n}\n"
594+
"code": "/**\n * This function returns the number one.\n */\nfunction addThem(a: Point, b: string, c: ?boolean, d: Array<number>, e: Object, f: Named): number {\n return a + b + c + d + e;\n}\n\n/**\n * A 2D point.\n *\n * @property {number} x this is a prop\n */\ntype Point = {\n x: number,\n y: number,\n rgb: {\n hex: string\n },\n props: {\n radius: {\n x: number\n }\n }\n};\n\n/**\n * A type with entirely derived properties\n */\ntype Two = {\n x: number,\n y: number,\n z: ?number\n};\n\n/**\n * Just an alias for an array of strings\n */\ntype T = Array<string>;\n\n/**\n * Very Important Transform\n */\nfunction veryImportantTransform(\n input: Array<string>,\n options: Object = {}\n): string {\n return \"42\";\n}\n\n\n/**\n * Function with optional parameter.\n */\nfunction optionalFunc(x: number = 42) {}\n"
583595
},
584596
"errors": [],
585597
"name": "T",
@@ -652,26 +664,26 @@
652664
"tags": [],
653665
"loc": {
654666
"start": {
655-
"line": 39,
667+
"line": 40,
656668
"column": 0
657669
},
658670
"end": {
659-
"line": 41,
671+
"line": 42,
660672
"column": 3
661673
}
662674
},
663675
"context": {
664676
"loc": {
665677
"start": {
666-
"line": 42,
678+
"line": 43,
667679
"column": 0
668680
},
669681
"end": {
670-
"line": 47,
682+
"line": 48,
671683
"column": 1
672684
}
673685
},
674-
"code": "/**\n * This function returns the number one.\n */\nfunction addThem(a: Point, b: string, c: ?boolean, d: Array<number>, e: Object, f: Named): number {\n return a + b + c + d + e;\n}\n\n/**\n * A 2D point.\n *\n * @property {number} x this is a prop\n */\ntype Point = {\n x: number,\n y: number,\n rgb: {\n hex: string\n },\n props: {\n radius: {\n x: number\n }\n }\n};\n\n/**\n * A type with entirely derived properties\n */\ntype Two = {\n x: number,\n y: number\n};\n\n/**\n * Just an alias for an array of strings\n */\ntype T = Array<string>;\n\n/**\n * Very Important Transform\n */\nfunction veryImportantTransform(\n input: Array<string>,\n options: Object = {}\n): string {\n return \"42\";\n}\n"
686+
"code": "/**\n * This function returns the number one.\n */\nfunction addThem(a: Point, b: string, c: ?boolean, d: Array<number>, e: Object, f: Named): number {\n return a + b + c + d + e;\n}\n\n/**\n * A 2D point.\n *\n * @property {number} x this is a prop\n */\ntype Point = {\n x: number,\n y: number,\n rgb: {\n hex: string\n },\n props: {\n radius: {\n x: number\n }\n }\n};\n\n/**\n * A type with entirely derived properties\n */\ntype Two = {\n x: number,\n y: number,\n z: ?number\n};\n\n/**\n * Just an alias for an array of strings\n */\ntype T = Array<string>;\n\n/**\n * Very Important Transform\n */\nfunction veryImportantTransform(\n input: Array<string>,\n options: Object = {}\n): string {\n return \"42\";\n}\n\n\n/**\n * Function with optional parameter.\n */\nfunction optionalFunc(x: number = 42) {}\n"
675687
},
676688
"errors": [],
677689
"name": "veryImportantTransform",
@@ -680,7 +692,7 @@
680692
{
681693
"title": "param",
682694
"name": "input",
683-
"lineNumber": 43,
695+
"lineNumber": 44,
684696
"type": {
685697
"type": "TypeApplication",
686698
"expression": {
@@ -727,5 +739,111 @@
727739
}
728740
],
729741
"namespace": "veryImportantTransform"
742+
},
743+
{
744+
"description": {
745+
"type": "root",
746+
"children": [
747+
{
748+
"type": "paragraph",
749+
"children": [
750+
{
751+
"type": "text",
752+
"value": "Function with optional parameter.",
753+
"position": {
754+
"start": {
755+
"line": 1,
756+
"column": 1,
757+
"offset": 0
758+
},
759+
"end": {
760+
"line": 1,
761+
"column": 34,
762+
"offset": 33
763+
},
764+
"indent": []
765+
}
766+
}
767+
],
768+
"position": {
769+
"start": {
770+
"line": 1,
771+
"column": 1,
772+
"offset": 0
773+
},
774+
"end": {
775+
"line": 1,
776+
"column": 34,
777+
"offset": 33
778+
},
779+
"indent": []
780+
}
781+
}
782+
],
783+
"position": {
784+
"start": {
785+
"line": 1,
786+
"column": 1,
787+
"offset": 0
788+
},
789+
"end": {
790+
"line": 1,
791+
"column": 34,
792+
"offset": 33
793+
}
794+
}
795+
},
796+
"tags": [],
797+
"loc": {
798+
"start": {
799+
"line": 51,
800+
"column": 0
801+
},
802+
"end": {
803+
"line": 53,
804+
"column": 3
805+
}
806+
},
807+
"context": {
808+
"loc": {
809+
"start": {
810+
"line": 54,
811+
"column": 0
812+
},
813+
"end": {
814+
"line": 54,
815+
"column": 40
816+
}
817+
},
818+
"code": "/**\n * This function returns the number one.\n */\nfunction addThem(a: Point, b: string, c: ?boolean, d: Array<number>, e: Object, f: Named): number {\n return a + b + c + d + e;\n}\n\n/**\n * A 2D point.\n *\n * @property {number} x this is a prop\n */\ntype Point = {\n x: number,\n y: number,\n rgb: {\n hex: string\n },\n props: {\n radius: {\n x: number\n }\n }\n};\n\n/**\n * A type with entirely derived properties\n */\ntype Two = {\n x: number,\n y: number,\n z: ?number\n};\n\n/**\n * Just an alias for an array of strings\n */\ntype T = Array<string>;\n\n/**\n * Very Important Transform\n */\nfunction veryImportantTransform(\n input: Array<string>,\n options: Object = {}\n): string {\n return \"42\";\n}\n\n\n/**\n * Function with optional parameter.\n */\nfunction optionalFunc(x: number = 42) {}\n"
819+
},
820+
"errors": [],
821+
"name": "optionalFunc",
822+
"kind": "function",
823+
"params": [
824+
{
825+
"title": "param",
826+
"name": "x",
827+
"default": "42",
828+
"type": {
829+
"type": "OptionalType",
830+
"expression": {
831+
"type": "NameExpression",
832+
"name": "number"
833+
}
834+
}
835+
}
836+
],
837+
"members": {
838+
"instance": [],
839+
"static": []
840+
},
841+
"path": [
842+
{
843+
"name": "optionalFunc",
844+
"kind": "function"
845+
}
846+
],
847+
"namespace": "optionalFunc"
730848
}
731849
]

test/fixture/sync/flow-types.output.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This function returns the number one.
66

77
- `a` **Point**
88
- `b` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**
9-
- `c` **\[[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]**
9+
- `c` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?**
1010
- `d` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>**
1111
- `e` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
1212
- `f` **Named**
@@ -35,6 +35,7 @@ A type with entirely derived properties
3535

3636
- `x` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)**
3737
- `y` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)**
38+
- `z` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
3839

3940
# T
4041

@@ -50,3 +51,11 @@ Very Important Transform
5051
- `options` **\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)]** (optional, default `{}`)
5152

5253
Returns **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**
54+
55+
# optionalFunc
56+
57+
Function with optional parameter.
58+
59+
**Parameters**
60+
61+
- `x` **\[[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)]** (optional, default `42`)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy