From 08762098470541e9d4d70ba28ed92bcefe3f112c Mon Sep 17 00:00:00 2001 From: Franky Chung Date: Sun, 19 Jul 2015 12:01:40 +0900 Subject: [PATCH] Add number literal type annotations for flow --- .../babel/src/generation/generators/flow.js | 6 + packages/babel/src/types/definitions/flow.js | 4 + .../flow/number-literal-types/actual.js | 5 + .../flow/number-literal-types/expected.js | 5 + packages/babylon/src/plugins/flow.js | 6 + packages/babylon/test/tests-flow.js | 202 ++++++++++++++++++ 6 files changed, 228 insertions(+) create mode 100644 packages/babel/test/fixtures/generation/flow/number-literal-types/actual.js create mode 100644 packages/babel/test/fixtures/generation/flow/number-literal-types/expected.js diff --git a/packages/babel/src/generation/generators/flow.js b/packages/babel/src/generation/generators/flow.js index 5c061ce9a6..761a19928a 100644 --- a/packages/babel/src/generation/generators/flow.js +++ b/packages/babel/src/generation/generators/flow.js @@ -177,6 +177,12 @@ export function NullableTypeAnnotation(node, print) { print.plain(node.typeAnnotation); } +/** + * Prints NumberLiteralTypeAnnotation, prints value. + */ + +export { Literal as NumberLiteralTypeAnnotation } from "./types"; + /** * Prints NumberTypeAnnotation. */ diff --git a/packages/babel/src/types/definitions/flow.js b/packages/babel/src/types/definitions/flow.js index 448300e123..4380f13cf6 100644 --- a/packages/babel/src/types/definitions/flow.js +++ b/packages/babel/src/types/definitions/flow.js @@ -82,6 +82,10 @@ define("NullableTypeAnnotation", { aliases: ["Flow"] }); +define("NumberLiteralTypeAnnotation", { + aliases: ["Flow"] +}); + define("NumberTypeAnnotation", { aliases: ["Flow", "FlowBaseAnnotation"] }); diff --git a/packages/babel/test/fixtures/generation/flow/number-literal-types/actual.js b/packages/babel/test/fixtures/generation/flow/number-literal-types/actual.js new file mode 100644 index 0000000000..8aa0ffac4b --- /dev/null +++ b/packages/babel/test/fixtures/generation/flow/number-literal-types/actual.js @@ -0,0 +1,5 @@ +var a: 123; +var a: 123.0; +var a: 0x7B; +var a: 0b1111011; +var a: 0o173; diff --git a/packages/babel/test/fixtures/generation/flow/number-literal-types/expected.js b/packages/babel/test/fixtures/generation/flow/number-literal-types/expected.js new file mode 100644 index 0000000000..c8e24031f1 --- /dev/null +++ b/packages/babel/test/fixtures/generation/flow/number-literal-types/expected.js @@ -0,0 +1,5 @@ +var a: 123; +var a: 123.0; +var a: 0x7B; +var a: 123; +var a: 123; diff --git a/packages/babylon/src/plugins/flow.js b/packages/babylon/src/plugins/flow.js index c85d4b3330..1f7e058b82 100644 --- a/packages/babylon/src/plugins/flow.js +++ b/packages/babylon/src/plugins/flow.js @@ -505,6 +505,12 @@ pp.flowParsePrimaryType = function () { this.next(); return this.finishNode(node, "StringLiteralTypeAnnotation"); + case tt.num: + node.value = this.value; + node.raw = this.input.slice(this.start, this.end); + this.next(); + return this.finishNode(node, "NumberLiteralTypeAnnotation"); + default: if (this.type.keyword === "typeof") { return this.flowParseTypeofType(); diff --git a/packages/babylon/test/tests-flow.js b/packages/babylon/test/tests-flow.js index f65101449a..a9dc643cd9 100644 --- a/packages/babylon/test/tests-flow.js +++ b/packages/babylon/test/tests-flow.js @@ -10094,6 +10094,208 @@ var fbTestFixture = { } }, }, + 'Number Literal Types': { + 'var a: 123': { + type: 'VariableDeclaration', + declarations: [{ + type: 'VariableDeclarator', + id: { + type: 'Identifier', + name: 'a', + typeAnnotation: { + type: 'TypeAnnotation', + typeAnnotation: { + type: 'NumberLiteralTypeAnnotation', + value: 123, + raw: '123', + range: [7, 10], + loc: { + start: { line: 1, column: 7 }, + end: { line: 1, column: 10 } + } + }, + range: [5, 10], + loc: { + start: { line: 1, column: 5 }, + end: { line: 1, column: 10 } + } + } + }, + init: null, + range: [4, 10], + loc: { + start: { line: 1, column: 4 }, + end: { line: 1, column: 10 } + } + }], + kind: 'var', + range: [0, 10], + loc: { + start: { line: 1, column: 0 }, + end: { line: 1, column: 10 } + } + }, + 'var a: 123.0': { + type: 'VariableDeclaration', + declarations: [{ + type: 'VariableDeclarator', + id: { + type: 'Identifier', + name: 'a', + typeAnnotation: { + type: 'TypeAnnotation', + typeAnnotation: { + type: 'NumberLiteralTypeAnnotation', + value: 123, + raw: '123.0', + range: [7, 12], + loc: { + start: { line: 1, column: 7 }, + end: { line: 1, column: 12 } + } + }, + range: [5, 12], + loc: { + start: { line: 1, column: 5 }, + end: { line: 1, column: 12 } + } + } + }, + init: null, + range: [4, 12], + loc: { + start: { line: 1, column: 4 }, + end: { line: 1, column: 12 } + } + }], + kind: 'var', + range: [0, 12], + loc: { + start: { line: 1, column: 0 }, + end: { line: 1, column: 12 } + } + }, + 'var a: 0x7B': { + type: 'VariableDeclaration', + declarations: [{ + type: 'VariableDeclarator', + id: { + type: 'Identifier', + name: 'a', + typeAnnotation: { + type: 'TypeAnnotation', + typeAnnotation: { + type: 'NumberLiteralTypeAnnotation', + value: 123, + raw: '0x7B', + range: [7, 11], + loc: { + start: { line: 1, column: 7 }, + end: { line: 1, column: 11 } + } + }, + range: [5, 11], + loc: { + start: { line: 1, column: 5 }, + end: { line: 1, column: 11 } + } + } + }, + init: null, + range: [4, 11], + loc: { + start: { line: 1, column: 4 }, + end: { line: 1, column: 11 } + } + }], + kind: 'var', + range: [0, 11], + loc: { + start: { line: 1, column: 0 }, + end: { line: 1, column: 11 } + } + }, + 'var a: 0b1111011': { + type: 'VariableDeclaration', + declarations: [{ + type: 'VariableDeclarator', + id: { + type: 'Identifier', + name: 'a', + typeAnnotation: { + type: 'TypeAnnotation', + typeAnnotation: { + type: 'NumberLiteralTypeAnnotation', + value: 123, + raw: '0b1111011', + range: [7, 16], + loc: { + start: { line: 1, column: 7 }, + end: { line: 1, column: 16 } + } + }, + range: [5, 16], + loc: { + start: { line: 1, column: 5 }, + end: { line: 1, column: 16 } + } + } + }, + init: null, + range: [4, 16], + loc: { + start: { line: 1, column: 4 }, + end: { line: 1, column: 16 } + } + }], + kind: 'var', + range: [0, 16], + loc: { + start: { line: 1, column: 0 }, + end: { line: 1, column: 16 } + } + }, + 'var a: 0o173': { + type: 'VariableDeclaration', + declarations: [{ + type: 'VariableDeclarator', + id: { + type: 'Identifier', + name: 'a', + typeAnnotation: { + type: 'TypeAnnotation', + typeAnnotation: { + type: 'NumberLiteralTypeAnnotation', + value: 123, + raw: '0o173', + range: [7, 12], + loc: { + start: { line: 1, column: 7 }, + end: { line: 1, column: 12 } + } + }, + range: [5, 12], + loc: { + start: { line: 1, column: 5 }, + end: { line: 1, column: 12 } + } + } + }, + init: null, + range: [4, 12], + loc: { + start: { line: 1, column: 4 }, + end: { line: 1, column: 12 } + } + }], + kind: 'var', + range: [0, 12], + loc: { + start: { line: 1, column: 0 }, + end: { line: 1, column: 12 } + } + } + }, 'Qualified Generic Type': { 'var a : A.B': { type: 'VariableDeclaration',