From 9c20ace184a027c161a61afd264a8888faabab79 Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Thu, 3 Dec 2015 23:58:25 -0800 Subject: [PATCH 1/2] Add support for null literal type --- .../babel-generator/src/generators/flow.js | 4 + .../flow/null-literal-types/actual.js | 1 + .../flow/null-literal-types/expected.js | 1 + packages/babel-types/src/definitions/flow.js | 7 ++ packages/babylon/src/plugins/flow.js | 5 + .../flow/literal-types/null/actual.js | 1 + .../flow/literal-types/null/expected.js | 114 ++++++++++++++++++ 7 files changed, 133 insertions(+) create mode 100644 packages/babel-generator/test/fixtures/flow/null-literal-types/actual.js create mode 100644 packages/babel-generator/test/fixtures/flow/null-literal-types/expected.js create mode 100644 packages/babylon/test/fixtures/flow/literal-types/null/actual.js create mode 100644 packages/babylon/test/fixtures/flow/literal-types/null/expected.js diff --git a/packages/babel-generator/src/generators/flow.js b/packages/babel-generator/src/generators/flow.js index 5e9a149e9e..20d29d7cc4 100644 --- a/packages/babel-generator/src/generators/flow.js +++ b/packages/babel-generator/src/generators/flow.js @@ -20,6 +20,10 @@ export function BooleanLiteralTypeAnnotation(node: Object) { this.push(node.value ? "true" : "false"); } +export function NullLiteralTypeAnnotation() { + this.push("null"); +} + export function DeclareClass(node: Object) { this.push("declare class "); this._interfaceish(node); diff --git a/packages/babel-generator/test/fixtures/flow/null-literal-types/actual.js b/packages/babel-generator/test/fixtures/flow/null-literal-types/actual.js new file mode 100644 index 0000000000..20ff854dbd --- /dev/null +++ b/packages/babel-generator/test/fixtures/flow/null-literal-types/actual.js @@ -0,0 +1 @@ +var foo: null; diff --git a/packages/babel-generator/test/fixtures/flow/null-literal-types/expected.js b/packages/babel-generator/test/fixtures/flow/null-literal-types/expected.js new file mode 100644 index 0000000000..20ff854dbd --- /dev/null +++ b/packages/babel-generator/test/fixtures/flow/null-literal-types/expected.js @@ -0,0 +1 @@ +var foo: null; diff --git a/packages/babel-types/src/definitions/flow.js b/packages/babel-types/src/definitions/flow.js index d750273cfa..615d24d1c2 100644 --- a/packages/babel-types/src/definitions/flow.js +++ b/packages/babel-types/src/definitions/flow.js @@ -31,6 +31,13 @@ defineType("BooleanLiteralTypeAnnotation", { } }); +defineType("NullLiteralTypeAnnotation", { + aliases: ["Flow"], + fields: { + // todo + } +}); + defineType("ClassImplements", { visitor: ["id", "typeParameters"], aliases: ["Flow"], diff --git a/packages/babylon/src/plugins/flow.js b/packages/babylon/src/plugins/flow.js index 060f623649..7e561ac56d 100644 --- a/packages/babylon/src/plugins/flow.js +++ b/packages/babylon/src/plugins/flow.js @@ -514,6 +514,11 @@ pp.flowParsePrimaryType = function () { this.next(); return this.finishNode(node, "NumericLiteralTypeAnnotation"); + case tt._null: + node.value = this.match(tt._null); + this.next(); + return this.finishNode(node, "NullLiteralTypeAnnotation"); + default: if (this.state.type.keyword === "typeof") { return this.flowParseTypeofType(); diff --git a/packages/babylon/test/fixtures/flow/literal-types/null/actual.js b/packages/babylon/test/fixtures/flow/literal-types/null/actual.js new file mode 100644 index 0000000000..8595ff5eeb --- /dev/null +++ b/packages/babylon/test/fixtures/flow/literal-types/null/actual.js @@ -0,0 +1 @@ +var foo: null diff --git a/packages/babylon/test/fixtures/flow/literal-types/null/expected.js b/packages/babylon/test/fixtures/flow/literal-types/null/expected.js new file mode 100644 index 0000000000..78ebec7ba0 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/literal-types/null/expected.js @@ -0,0 +1,114 @@ +{ + "type": "File", + "start": 0, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "name": "foo", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 7, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "typeAnnotation": { + "type": "NullLiteralTypeAnnotation", + "start": 9, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "value": true + } + } + }, + "init": null + } + ], + "kind": "var" + } + ] + } +} From 37797c4d501cb33b11c0813a10387c3ecbb001bd Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Fri, 4 Dec 2015 00:12:37 -0800 Subject: [PATCH 2/2] Test name, and remove todos --- packages/babel-types/src/definitions/flow.js | 8 ++------ .../literal-types/null/{expected.js => expected.json} | 0 2 files changed, 2 insertions(+), 6 deletions(-) rename packages/babylon/test/fixtures/flow/literal-types/null/{expected.js => expected.json} (100%) diff --git a/packages/babel-types/src/definitions/flow.js b/packages/babel-types/src/definitions/flow.js index 615d24d1c2..d743b1c55c 100644 --- a/packages/babel-types/src/definitions/flow.js +++ b/packages/babel-types/src/definitions/flow.js @@ -26,16 +26,12 @@ defineType("BooleanTypeAnnotation", { defineType("BooleanLiteralTypeAnnotation", { aliases: ["Flow"], - fields: { - // todo - } + fields: {} }); defineType("NullLiteralTypeAnnotation", { aliases: ["Flow"], - fields: { - // todo - } + fields: {} }); defineType("ClassImplements", { diff --git a/packages/babylon/test/fixtures/flow/literal-types/null/expected.js b/packages/babylon/test/fixtures/flow/literal-types/null/expected.json similarity index 100% rename from packages/babylon/test/fixtures/flow/literal-types/null/expected.js rename to packages/babylon/test/fixtures/flow/literal-types/null/expected.json