From 5bb1a91bd556f8a7d5f6db4b6344dd89bc5d3db0 Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Fri, 23 Sep 2016 19:41:06 +0300 Subject: [PATCH 1/6] Add tests for path type inferers --- .../src/path/inference/inferers.js | 8 +- packages/babel-traverse/test/inference.js | 125 +++++++++++++++++- 2 files changed, 130 insertions(+), 3 deletions(-) diff --git a/packages/babel-traverse/src/path/inference/inferers.js b/packages/babel-traverse/src/path/inference/inferers.js index 7a2c86f7dab8..ec372f6825d7 100644 --- a/packages/babel-traverse/src/path/inference/inferers.js +++ b/packages/babel-traverse/src/path/inference/inferers.js @@ -137,7 +137,13 @@ function Func() { return t.genericTypeAnnotation(t.identifier("Function")); } -export { Func as Function, Func as Class }; +export { + Func as FunctionExpression, + Func as ArrowFunctionExpression, + Func as FunctionDeclaration, + Func as ClassExpression, + Func as ClassDeclaration +}; export function CallExpression() { return resolveCall(this.get("callee")); diff --git a/packages/babel-traverse/test/inference.js b/packages/babel-traverse/test/inference.js index a89fbeca7a7b..68325dc71699 100644 --- a/packages/babel-traverse/test/inference.js +++ b/packages/babel-traverse/test/inference.js @@ -1,9 +1,10 @@ let traverse = require("../lib").default; let assert = require("assert"); let parse = require("babylon").parse; +let t = require("babel-types"); function getPath(code) { - let ast = parse(code); + let ast = parse(code, {plugins: ["flow", "asyncGenerators"]}); let path; traverse(ast, { Program: function (_path) { @@ -31,7 +32,7 @@ describe("inference", function () { let right = path.get("right"); let strictMatch = left.baseTypeStrictlyMatches(right); - assert.ok(strictMatch, "null should be equal to null"); + assert.ok(strictMatch, "number should be equal to number"); }); it("it should bail when type changes", function () { @@ -53,4 +54,124 @@ describe("inference", function () { assert.ok(!strictMatch, "null should not match undefined"); }); }); + describe("getTypeAnnotation", function () { + it("should infer from type cast", function () { + var path = getPath("(x: number)").get("body")[0].get("expression"); + assert.ok(t.isNumberTypeAnnotation(path.getTypeAnnotation()), "should be number"); + + }); + it("should infer string from template literal", function () { + var path = getPath("`hey`").get("body")[0].get("expression"); + assert.ok(t.isStringTypeAnnotation(path.getTypeAnnotation()), "should be string"); + }); + it("should infer number from +x", function () { + var path = getPath("+x").get("body")[0].get("expression"); + assert.ok(t.isNumberTypeAnnotation(path.getTypeAnnotation()), "should be number"); + }); + it("should infer T from new T", function () { + var path = getPath("new T").get("body")[0].get("expression"); + var type = path.getTypeAnnotation(); + assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === 'T', "should be T"); + }); + it("should infer number from ++x", function () { + var path = getPath("++x").get("body")[0].get("expression"); + assert.ok(t.isNumberTypeAnnotation(path.getTypeAnnotation()), "should be number"); + }); + it("should infer number from --x", function () { + var path = getPath("--x").get("body")[0].get("expression"); + assert.ok(t.isNumberTypeAnnotation(path.getTypeAnnotation()), "should be number"); + }); + it("should infer void from void x", function () { + var path = getPath("void x").get("body")[0].get("expression"); + assert.ok(t.isVoidTypeAnnotation(path.getTypeAnnotation()), "should be void"); + }); + it("should infer string from typeof x", function () { + var path = getPath("typeof x").get("body")[0].get("expression"); + assert.ok(t.isStringTypeAnnotation(path.getTypeAnnotation()), "should be string"); + }); + it("should infer boolean from !x", function () { + var path = getPath("!x").get("body")[0].get("expression"); + assert.ok(t.isBooleanTypeAnnotation(path.getTypeAnnotation()), "should be boolean"); + }); + it("should infer type of sequence expression", function () { + var path = getPath("a,1").get("body")[0].get("expression"); + assert.ok(t.isNumberTypeAnnotation(path.getTypeAnnotation()), "should be number"); + }); + it("should infer type of logical expression", function () { + var path = getPath("'a' && 1").get("body")[0].get("expression"); + var type = path.getTypeAnnotation(); + assert.ok(t.isUnionTypeAnnotation(type), "should be a union"); + assert.ok(t.isStringTypeAnnotation(type.types[0]), "first type in union should be string"); + assert.ok(t.isNumberTypeAnnotation(type.types[1]), "second type in union should be number"); + }); + it("should infer type of conditional expression", function () { + var path = getPath("q ? true : 0").get("body")[0].get("expression"); + var type = path.getTypeAnnotation(); + assert.ok(t.isUnionTypeAnnotation(type), "should be a union"); + assert.ok(t.isBooleanTypeAnnotation(type.types[0]), "first type in union should be boolean"); + assert.ok(t.isNumberTypeAnnotation(type.types[1]), "second type in union should be number"); + }); + it("should infer RegExp from RegExp literal", function () { + var path = getPath("/.+/").get("body")[0].get("expression"); + var type = path.getTypeAnnotation(); + assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === 'RegExp', "should be RegExp"); + }); + it("should infer Object from object expression", function () { + var path = getPath("({ a: 5 })").get("body")[0].get("expression"); + var type = path.getTypeAnnotation(); + assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === 'Object', "should be Object"); + }); + it("should infer Array from array expression", function () { + var path = getPath("[ 5 ]").get("body")[0].get("expression"); + var type = path.getTypeAnnotation(); + assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === 'Array', "should be Array"); + }); + it("should infer Function from function", function () { + var path = getPath("(function (): string {})").get("body")[0].get("expression"); + var type = path.getTypeAnnotation(); + assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === 'Function', "should be Function"); + }); + it("should infer call return type using function", function () { + var path = getPath("(function (): string {})()").get("body")[0].get("expression"); + var type = path.getTypeAnnotation(); + assert.ok(t.isStringTypeAnnotation(type), "should be string"); + }); + it("should infer call return type using async function", function () { + var path = getPath("(async function (): string {})()").get("body")[0].get("expression"); + var type = path.getTypeAnnotation(); + assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === 'Promise', "should be Promise"); + }); + it("should infer call return type using async generator function", function () { + var path = getPath("(async function * (): string {})()").get("body")[0].get("expression"); + var type = path.getTypeAnnotation(); + assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === 'AsyncIterator', "should be AsyncIterator"); + }); + it("should infer number from x/y", function () { + var path = getPath("x/y").get("body")[0].get("expression"); + var type = path.getTypeAnnotation(); + assert.ok(t.isNumberTypeAnnotation(type), "should be number"); + }); + it("should infer boolean from x instanceof y", function () { + var path = getPath("x instanceof y").get("body")[0].get("expression"); + var type = path.getTypeAnnotation(); + assert.ok(t.isBooleanTypeAnnotation(type), "should be boolean"); + }); + it("should infer number from 1 + 2", function () { + var path = getPath("1 + 2").get("body")[0].get("expression"); + var type = path.getTypeAnnotation(); + assert.ok(t.isNumberTypeAnnotation(type), "should be number"); + }); + it("should infer string|number from x + y", function () { + var path = getPath("x + y").get("body")[0].get("expression"); + var type = path.getTypeAnnotation(); + assert.ok(t.isUnionTypeAnnotation(type), "should be a union"); + assert.ok(t.isStringTypeAnnotation(type.types[0]), "first type in union should be string"); + assert.ok(t.isNumberTypeAnnotation(type.types[1]), "second type in union should be number"); + }); + it("should infer type of tagged template literal", function () { + var path = getPath("(function (): RegExp {}) `hey`").get("body")[0].get("expression"); + var type = path.getTypeAnnotation(); + assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === 'RegExp', "should be RegExp"); + }); + }); }); From 32ba02371953bc546ce1968943be17d038544160 Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Mon, 17 Oct 2016 11:15:23 +0300 Subject: [PATCH 2/6] Add test for babel-types.valueToNode --- packages/babel-types/test/converters.js | 48 +++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 packages/babel-types/test/converters.js diff --git a/packages/babel-types/test/converters.js b/packages/babel-types/test/converters.js new file mode 100644 index 000000000000..215327bdea5e --- /dev/null +++ b/packages/babel-types/test/converters.js @@ -0,0 +1,48 @@ +import * as t from "../lib"; +import { assert } from "chai"; + +describe("converters", function () { + describe("valueToNode", function () { + it("number", function () { + assert.deepEqual(t.valueToNode(Math.PI), t.numericLiteral(Math.PI)); + assert.deepEqual(t.valueToNode(-Infinity), t.numericLiteral(-Infinity)); + assert.deepEqual(t.valueToNode(NaN), t.numericLiteral(NaN)); + }); + it("string", function () { + assert.deepEqual(t.valueToNode("This is a \"string\""), t.stringLiteral("This is a \"string\"")); + }); + it("boolean", function () { + assert.deepEqual(t.valueToNode(true), t.booleanLiteral(true)); + assert.deepEqual(t.valueToNode(false), t.booleanLiteral(false)); + }); + it("null", function () { + assert.deepEqual(t.valueToNode(null), t.nullLiteral()); + }); + it("undefined", function () { + assert.deepEqual(t.valueToNode(undefined), t.identifier("undefined")); + }); + it("RegExp", function () { + assert.deepEqual(t.valueToNode(/abc.+/gm), t.regExpLiteral("abc.+", "gm")); + }); + it("array", function () { + assert.deepEqual(t.valueToNode([1, "a"]), t.arrayExpression([t.numericLiteral(1), t.stringLiteral("a")])); + }); + it("object", function () { + assert.deepEqual(t.valueToNode({ + a: 1, + "b c": 2 + }), t.objectExpression([ + t.objectProperty(t.identifier("a"), t.numericLiteral(1)), + t.objectProperty(t.stringLiteral("b c"), t.numericLiteral(2)) + ])); + }); + it("throws if cannot convert", function () { + assert.throws(function () { + t.valueToNode(Object); + }); + assert.throws(function () { + t.valueToNode(Symbol()); + }); + }); + }); +}); From 1e328d6b4418fe07bd11100307b8f63dd280f201 Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Mon, 17 Oct 2016 11:18:12 +0300 Subject: [PATCH 3/6] Add tests for babel-types.toKeyAlias --- packages/babel-types/test/converters.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/babel-types/test/converters.js b/packages/babel-types/test/converters.js index 215327bdea5e..0c4b9d76a20d 100644 --- a/packages/babel-types/test/converters.js +++ b/packages/babel-types/test/converters.js @@ -45,4 +45,16 @@ describe("converters", function () { }); }); }); + describe("toKeyAlias", function () { + beforeEach(function () { + // make tests deterministic + t.toKeyAlias.uid = 0; + }); + it("doesn't change string literals", function () { + assert.equal(t.toKeyAlias(t.objectProperty(t.stringLiteral("a"), t.nullLiteral())), "\"a\""); + }); + it("wraps around at Number.MAX_SAFE_INTEGER", function () { + assert.equal(t.toKeyAlias(t.objectMethod("method", t.identifier("a"), [], t.blockStatement([]))), "0"); + }); + }); }); From 336aad4deb5fc360e88cb2299e4b8ecb96615a01 Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Mon, 17 Oct 2016 11:33:20 +0300 Subject: [PATCH 4/6] Add tests for babel-types.toStatement --- packages/babel-types/test/converters.js | 46 +++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/packages/babel-types/test/converters.js b/packages/babel-types/test/converters.js index 0c4b9d76a20d..f631d6bfb3a4 100644 --- a/packages/babel-types/test/converters.js +++ b/packages/babel-types/test/converters.js @@ -57,4 +57,50 @@ describe("converters", function () { assert.equal(t.toKeyAlias(t.objectMethod("method", t.identifier("a"), [], t.blockStatement([]))), "0"); }); }); + describe("toStatement", function () { + it("noop on statements", function () { + const node = t.emptyStatement(); + assert.equal(t.toStatement(node), node); + t.assertEmptyStatement(node); + }); + it("mutate class expression to declaration", function () { + const node = t.classExpression(t.identifier("A"), null, t.classBody([]), []); + t.toStatement(node); + t.assertClassDeclaration(node); + }); + it("fail if class expression has no id", function () { + const node = t.classExpression(null, null, t.classBody([]), []); + assert.throws(function() { + t.toStatement(node); + }); + assert.strictEqual(t.toStatement(node, /* ignore = */ true), false); + t.assertClassExpression(node); + }); + it("mutate function expression to declaration", function () { + const node = t.functionExpression(t.identifier("A"), [], t.blockStatement([])); + t.toStatement(node); + t.assertFunctionDeclaration(node); + }); + it("fail if function expression has no id", function () { + const node = t.functionExpression(null, [], t.blockStatement([])); + assert.throws(function() { + t.toStatement(node); + }); + assert.strictEqual(t.toStatement(node, /* ignore = */ true), false); + t.assertFunctionExpression(node); + }); + it("assignment expression", function () { + const node = t.assignmentExpression("+=", t.identifier("x"), t.numericLiteral(1)); + t.assertExpressionStatement(t.toStatement(node)); + t.assertAssignmentExpression(node); + }); + it("fail if cannot convert node type", function () { + const node = t.yieldExpression(t.identifier("foo")); + assert.throws(function() { + t.toStatement(node); + }); + assert.strictEqual(t.toStatement(node, /* ignore = */ true), false); + t.assertYieldExpression(node); + }); + }); }); From 3934d564581e7392d20afb13ca31afe8ff4feeab Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Mon, 17 Oct 2016 11:40:40 +0300 Subject: [PATCH 5/6] Add tests for babel-types.toExpression --- packages/babel-types/test/converters.js | 41 +++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/packages/babel-types/test/converters.js b/packages/babel-types/test/converters.js index f631d6bfb3a4..56dabf658604 100644 --- a/packages/babel-types/test/converters.js +++ b/packages/babel-types/test/converters.js @@ -103,4 +103,45 @@ describe("converters", function () { t.assertYieldExpression(node); }); }); + describe("toExpression", function () { + it("noop on expressions", function () { + const node = t.identifier("a"); + assert.equal(t.toExpression(node), node); + t.assertIdentifier(node); + }); + it("mutate class declaration to expression", function () { + const node = t.classDeclaration(t.identifier("A"), null, t.classBody([]), []); + t.toExpression(node); + t.assertClassExpression(node); + }); + it("mutate function declaration to expression", function () { + const node = t.functionDeclaration(t.identifier("A"), [], t.blockStatement([])); + t.toExpression(node); + t.assertFunctionExpression(node); + }); + it("mutate object method to expression", function () { + const node = t.objectMethod("method", t.identifier("A"), [], t.blockStatement([])); + t.toExpression(node); + t.assertFunctionExpression(node); + }); + it("mutate class method to expression", function () { + const node = t.classMethod("constructor", t.identifier("A"), [], t.blockStatement([])); + t.toExpression(node); + t.assertFunctionExpression(node); + }); + it("expression statement", function () { + const inner = t.yieldExpression(t.identifier("foo")); + const node = t.expressionStatement(inner); + t.assertYieldExpression(t.toExpression(node)); + assert.equal(t.toExpression(node), inner); + t.assertExpressionStatement(node); + }); + it("fail if cannot convert node type", function () { + const node = t.program([]); + assert.throws(function() { + t.toExpression(node); + }); + t.assertProgram(node); + }); + }); }); From dfbd78f7bbe3107293d92976b397133d510c89e0 Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Mon, 17 Oct 2016 13:24:34 +0300 Subject: [PATCH 6/6] Lint fixes in babel-traverse/test/inference --- packages/babel-traverse/test/inference.js | 136 +++++++++++----------- 1 file changed, 68 insertions(+), 68 deletions(-) diff --git a/packages/babel-traverse/test/inference.js b/packages/babel-traverse/test/inference.js index 68325dc71699..5819cd1b06d0 100644 --- a/packages/babel-traverse/test/inference.js +++ b/packages/babel-traverse/test/inference.js @@ -1,10 +1,10 @@ -let traverse = require("../lib").default; -let assert = require("assert"); -let parse = require("babylon").parse; -let t = require("babel-types"); +import traverse from "../lib"; +import assert from "assert"; +import { parse } from "babylon"; +import * as t from "babel-types"; function getPath(code) { - let ast = parse(code, {plugins: ["flow", "asyncGenerators"]}); + const ast = parse(code, {plugins: ["flow", "asyncGenerators"]}); let path; traverse(ast, { Program: function (_path) { @@ -18,160 +18,160 @@ function getPath(code) { describe("inference", function () { describe("baseTypeStrictlyMatches", function () { it("it should work with null", function () { - let path = getPath("var x = null; x === null").get("body")[1].get("expression"); - let left = path.get("left"); - let right = path.get("right"); - let strictMatch = left.baseTypeStrictlyMatches(right); + const path = getPath("var x = null; x === null").get("body")[1].get("expression"); + const left = path.get("left"); + const right = path.get("right"); + const strictMatch = left.baseTypeStrictlyMatches(right); assert.ok(strictMatch, "null should be equal to null"); }); it("it should work with numbers", function () { - let path = getPath("var x = 1; x === 2").get("body")[1].get("expression"); - let left = path.get("left"); - let right = path.get("right"); - let strictMatch = left.baseTypeStrictlyMatches(right); + const path = getPath("var x = 1; x === 2").get("body")[1].get("expression"); + const left = path.get("left"); + const right = path.get("right"); + const strictMatch = left.baseTypeStrictlyMatches(right); assert.ok(strictMatch, "number should be equal to number"); }); it("it should bail when type changes", function () { - let path = getPath("var x = 1; if (foo) x = null;else x = 3; x === 2").get("body")[2].get("expression"); - let left = path.get("left"); - let right = path.get("right"); + const path = getPath("var x = 1; if (foo) x = null;else x = 3; x === 2").get("body")[2].get("expression"); + const left = path.get("left"); + const right = path.get("right"); - let strictMatch = left.baseTypeStrictlyMatches(right); + const strictMatch = left.baseTypeStrictlyMatches(right); assert.ok(!strictMatch, "type might change in if statement"); }); it("it should differentiate between null and undefined", function () { - let path = getPath("var x; x === null").get("body")[1].get("expression"); - let left = path.get("left"); - let right = path.get("right"); - let strictMatch = left.baseTypeStrictlyMatches(right); + const path = getPath("var x; x === null").get("body")[1].get("expression"); + const left = path.get("left"); + const right = path.get("right"); + const strictMatch = left.baseTypeStrictlyMatches(right); assert.ok(!strictMatch, "null should not match undefined"); }); }); describe("getTypeAnnotation", function () { it("should infer from type cast", function () { - var path = getPath("(x: number)").get("body")[0].get("expression"); + const path = getPath("(x: number)").get("body")[0].get("expression"); assert.ok(t.isNumberTypeAnnotation(path.getTypeAnnotation()), "should be number"); }); it("should infer string from template literal", function () { - var path = getPath("`hey`").get("body")[0].get("expression"); + const path = getPath("`hey`").get("body")[0].get("expression"); assert.ok(t.isStringTypeAnnotation(path.getTypeAnnotation()), "should be string"); }); it("should infer number from +x", function () { - var path = getPath("+x").get("body")[0].get("expression"); + const path = getPath("+x").get("body")[0].get("expression"); assert.ok(t.isNumberTypeAnnotation(path.getTypeAnnotation()), "should be number"); }); it("should infer T from new T", function () { - var path = getPath("new T").get("body")[0].get("expression"); - var type = path.getTypeAnnotation(); - assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === 'T', "should be T"); + const path = getPath("new T").get("body")[0].get("expression"); + const type = path.getTypeAnnotation(); + assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === "T", "should be T"); }); it("should infer number from ++x", function () { - var path = getPath("++x").get("body")[0].get("expression"); + const path = getPath("++x").get("body")[0].get("expression"); assert.ok(t.isNumberTypeAnnotation(path.getTypeAnnotation()), "should be number"); }); it("should infer number from --x", function () { - var path = getPath("--x").get("body")[0].get("expression"); + const path = getPath("--x").get("body")[0].get("expression"); assert.ok(t.isNumberTypeAnnotation(path.getTypeAnnotation()), "should be number"); }); it("should infer void from void x", function () { - var path = getPath("void x").get("body")[0].get("expression"); + const path = getPath("void x").get("body")[0].get("expression"); assert.ok(t.isVoidTypeAnnotation(path.getTypeAnnotation()), "should be void"); }); it("should infer string from typeof x", function () { - var path = getPath("typeof x").get("body")[0].get("expression"); + const path = getPath("typeof x").get("body")[0].get("expression"); assert.ok(t.isStringTypeAnnotation(path.getTypeAnnotation()), "should be string"); }); it("should infer boolean from !x", function () { - var path = getPath("!x").get("body")[0].get("expression"); + const path = getPath("!x").get("body")[0].get("expression"); assert.ok(t.isBooleanTypeAnnotation(path.getTypeAnnotation()), "should be boolean"); }); it("should infer type of sequence expression", function () { - var path = getPath("a,1").get("body")[0].get("expression"); + const path = getPath("a,1").get("body")[0].get("expression"); assert.ok(t.isNumberTypeAnnotation(path.getTypeAnnotation()), "should be number"); }); it("should infer type of logical expression", function () { - var path = getPath("'a' && 1").get("body")[0].get("expression"); - var type = path.getTypeAnnotation(); + const path = getPath("'a' && 1").get("body")[0].get("expression"); + const type = path.getTypeAnnotation(); assert.ok(t.isUnionTypeAnnotation(type), "should be a union"); assert.ok(t.isStringTypeAnnotation(type.types[0]), "first type in union should be string"); assert.ok(t.isNumberTypeAnnotation(type.types[1]), "second type in union should be number"); }); it("should infer type of conditional expression", function () { - var path = getPath("q ? true : 0").get("body")[0].get("expression"); - var type = path.getTypeAnnotation(); + const path = getPath("q ? true : 0").get("body")[0].get("expression"); + const type = path.getTypeAnnotation(); assert.ok(t.isUnionTypeAnnotation(type), "should be a union"); assert.ok(t.isBooleanTypeAnnotation(type.types[0]), "first type in union should be boolean"); assert.ok(t.isNumberTypeAnnotation(type.types[1]), "second type in union should be number"); }); it("should infer RegExp from RegExp literal", function () { - var path = getPath("/.+/").get("body")[0].get("expression"); - var type = path.getTypeAnnotation(); - assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === 'RegExp', "should be RegExp"); + const path = getPath("/.+/").get("body")[0].get("expression"); + const type = path.getTypeAnnotation(); + assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === "RegExp", "should be RegExp"); }); it("should infer Object from object expression", function () { - var path = getPath("({ a: 5 })").get("body")[0].get("expression"); - var type = path.getTypeAnnotation(); - assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === 'Object', "should be Object"); + const path = getPath("({ a: 5 })").get("body")[0].get("expression"); + const type = path.getTypeAnnotation(); + assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === "Object", "should be Object"); }); it("should infer Array from array expression", function () { - var path = getPath("[ 5 ]").get("body")[0].get("expression"); - var type = path.getTypeAnnotation(); - assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === 'Array', "should be Array"); + const path = getPath("[ 5 ]").get("body")[0].get("expression"); + const type = path.getTypeAnnotation(); + assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === "Array", "should be Array"); }); it("should infer Function from function", function () { - var path = getPath("(function (): string {})").get("body")[0].get("expression"); - var type = path.getTypeAnnotation(); - assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === 'Function', "should be Function"); + const path = getPath("(function (): string {})").get("body")[0].get("expression"); + const type = path.getTypeAnnotation(); + assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === "Function", "should be Function"); }); it("should infer call return type using function", function () { - var path = getPath("(function (): string {})()").get("body")[0].get("expression"); - var type = path.getTypeAnnotation(); + const path = getPath("(function (): string {})()").get("body")[0].get("expression"); + const type = path.getTypeAnnotation(); assert.ok(t.isStringTypeAnnotation(type), "should be string"); }); it("should infer call return type using async function", function () { - var path = getPath("(async function (): string {})()").get("body")[0].get("expression"); - var type = path.getTypeAnnotation(); - assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === 'Promise', "should be Promise"); + const path = getPath("(async function (): string {})()").get("body")[0].get("expression"); + const type = path.getTypeAnnotation(); + assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === "Promise", "should be Promise"); }); it("should infer call return type using async generator function", function () { - var path = getPath("(async function * (): string {})()").get("body")[0].get("expression"); - var type = path.getTypeAnnotation(); - assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === 'AsyncIterator', "should be AsyncIterator"); + const path = getPath("(async function * (): string {})()").get("body")[0].get("expression"); + const type = path.getTypeAnnotation(); + assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === "AsyncIterator", "should be AsyncIterator"); }); it("should infer number from x/y", function () { - var path = getPath("x/y").get("body")[0].get("expression"); - var type = path.getTypeAnnotation(); + const path = getPath("x/y").get("body")[0].get("expression"); + const type = path.getTypeAnnotation(); assert.ok(t.isNumberTypeAnnotation(type), "should be number"); }); it("should infer boolean from x instanceof y", function () { - var path = getPath("x instanceof y").get("body")[0].get("expression"); - var type = path.getTypeAnnotation(); + const path = getPath("x instanceof y").get("body")[0].get("expression"); + const type = path.getTypeAnnotation(); assert.ok(t.isBooleanTypeAnnotation(type), "should be boolean"); }); it("should infer number from 1 + 2", function () { - var path = getPath("1 + 2").get("body")[0].get("expression"); - var type = path.getTypeAnnotation(); + const path = getPath("1 + 2").get("body")[0].get("expression"); + const type = path.getTypeAnnotation(); assert.ok(t.isNumberTypeAnnotation(type), "should be number"); }); it("should infer string|number from x + y", function () { - var path = getPath("x + y").get("body")[0].get("expression"); - var type = path.getTypeAnnotation(); + const path = getPath("x + y").get("body")[0].get("expression"); + const type = path.getTypeAnnotation(); assert.ok(t.isUnionTypeAnnotation(type), "should be a union"); assert.ok(t.isStringTypeAnnotation(type.types[0]), "first type in union should be string"); assert.ok(t.isNumberTypeAnnotation(type.types[1]), "second type in union should be number"); }); it("should infer type of tagged template literal", function () { - var path = getPath("(function (): RegExp {}) `hey`").get("body")[0].get("expression"); - var type = path.getTypeAnnotation(); - assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === 'RegExp', "should be RegExp"); + const path = getPath("(function (): RegExp {}) `hey`").get("body")[0].get("expression"); + const type = path.getTypeAnnotation(); + assert.ok(t.isGenericTypeAnnotation(type) && type.id.name === "RegExp", "should be RegExp"); }); }); });