Created
March 16, 2018 05:22
-
-
Save devenbansod/e519bde4b2e58bbc74ea4ace732c8d22 to your computer and use it in GitHub Desktop.
Added a matcher to match TypeAnnotations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/packages/babel-traverse/test/inference.js b/packages/babel-traverse/test/inference.js | |
index 1cc499d0c..c92c89ad0 100644 | |
--- a/packages/babel-traverse/test/inference.js | |
+++ b/packages/babel-traverse/test/inference.js | |
@@ -2,6 +2,26 @@ import traverse from "../lib"; | |
import { parse } from "babylon"; | |
import * as t from "@babel/types"; | |
+expect.extend({ | |
+ toBeAnnotationOfType(received, expectedType) { | |
+ const expectedTypeCheckFn = "is" + expectedType + "TypeAnnotation"; | |
+ | |
+ if (t[expectedTypeCheckFn](received)) { | |
+ return { | |
+ message: () => | |
+ `expected ${received.type} not to be ${expectedType}TypeAnnotation`, | |
+ pass: true, | |
+ }; | |
+ } else { | |
+ return { | |
+ message: () => | |
+ `expected ${received.type} to be ${expectedType}TypeAnnotation`, | |
+ pass: false, | |
+ }; | |
+ } | |
+ }, | |
+}); | |
+ | |
function getPath(code) { | |
const ast = parse(code, { plugins: ["flow", "asyncGenerators"] }); | |
let path; | |
@@ -66,194 +86,186 @@ describe("inference", function() { | |
const path = getPath("(x: number)") | |
.get("body")[0] | |
.get("expression"); | |
- expect(t.isNumberTypeAnnotation(path.getTypeAnnotation())).toBeTruthy(); | |
+ expect(path.getTypeAnnotation()).toBeAnnotationOfType("Number"); | |
}); | |
it("should infer string from template literal", function() { | |
const path = getPath("`hey`") | |
.get("body")[0] | |
.get("expression"); | |
- expect(t.isStringTypeAnnotation(path.getTypeAnnotation())).toBeTruthy(); | |
+ expect(path.getTypeAnnotation()).toBeAnnotationOfType("String"); | |
}); | |
it("should infer number from +x", function() { | |
const path = getPath("+x") | |
.get("body")[0] | |
.get("expression"); | |
- expect(t.isNumberTypeAnnotation(path.getTypeAnnotation())).toBeTruthy(); | |
+ expect(path.getTypeAnnotation()).toBeAnnotationOfType("Number"); | |
}); | |
it("should infer T from new T", function() { | |
const path = getPath("new T") | |
.get("body")[0] | |
.get("expression"); | |
const type = path.getTypeAnnotation(); | |
- expect( | |
- t.isGenericTypeAnnotation(type) && type.id.name === "T", | |
- ).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("Generic"); | |
+ expect(type.id.name).toBe("T"); | |
}); | |
it("should infer number from ++x", function() { | |
const path = getPath("++x") | |
.get("body")[0] | |
.get("expression"); | |
- expect(t.isNumberTypeAnnotation(path.getTypeAnnotation())).toBeTruthy(); | |
+ expect(path.getTypeAnnotation()).toBeAnnotationOfType("Number"); | |
}); | |
it("should infer number from --x", function() { | |
const path = getPath("--x") | |
.get("body")[0] | |
.get("expression"); | |
- expect(t.isNumberTypeAnnotation(path.getTypeAnnotation())).toBeTruthy(); | |
+ expect(path.getTypeAnnotation()).toBeAnnotationOfType("Number"); | |
}); | |
it("should infer void from void x", function() { | |
const path = getPath("void x") | |
.get("body")[0] | |
.get("expression"); | |
- expect(t.isVoidTypeAnnotation(path.getTypeAnnotation())).toBeTruthy(); | |
+ expect(path.getTypeAnnotation()).toBeAnnotationOfType("Void"); | |
}); | |
it("should infer string from typeof x", function() { | |
const path = getPath("typeof x") | |
.get("body")[0] | |
.get("expression"); | |
- expect(t.isStringTypeAnnotation(path.getTypeAnnotation())).toBeTruthy(); | |
+ expect(path.getTypeAnnotation()).toBeAnnotationOfType("String"); | |
}); | |
it("should infer boolean from !x", function() { | |
const path = getPath("!x") | |
.get("body")[0] | |
.get("expression"); | |
- expect(t.isBooleanTypeAnnotation(path.getTypeAnnotation())).toBeTruthy(); | |
+ expect(path.getTypeAnnotation()).toBeAnnotationOfType("Boolean"); | |
}); | |
it("should infer type of sequence expression", function() { | |
const path = getPath("a,1") | |
.get("body")[0] | |
.get("expression"); | |
- expect(t.isNumberTypeAnnotation(path.getTypeAnnotation())).toBeTruthy(); | |
+ expect(path.getTypeAnnotation()).toBeAnnotationOfType("Number"); | |
}); | |
it("should infer type of logical expression", function() { | |
const path = getPath("'a' && 1") | |
.get("body")[0] | |
.get("expression"); | |
const type = path.getTypeAnnotation(); | |
- expect(t.isUnionTypeAnnotation(type)).toBeTruthy(); | |
- expect(t.isStringTypeAnnotation(type.types[0])).toBeTruthy(); | |
- expect(t.isNumberTypeAnnotation(type.types[1])).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("Union"); | |
+ expect(type.types[0]).toBeAnnotationOfType("String"); | |
+ expect(type.types[1]).toBeAnnotationOfType("Number"); | |
}); | |
it("should infer type of conditional expression", function() { | |
const path = getPath("q ? true : 0") | |
.get("body")[0] | |
.get("expression"); | |
const type = path.getTypeAnnotation(); | |
- expect(t.isUnionTypeAnnotation(type)).toBeTruthy(); | |
- expect(t.isBooleanTypeAnnotation(type.types[0])).toBeTruthy(); | |
- expect(t.isNumberTypeAnnotation(type.types[1])).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("Union"); | |
+ expect(type.types[0]).toBeAnnotationOfType("Boolean"); | |
+ expect(type.types[1]).toBeAnnotationOfType("Number"); | |
}); | |
it("should infer RegExp from RegExp literal", function() { | |
const path = getPath("/.+/") | |
.get("body")[0] | |
.get("expression"); | |
const type = path.getTypeAnnotation(); | |
- expect( | |
- t.isGenericTypeAnnotation(type) && type.id.name === "RegExp", | |
- ).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("Generic"); | |
+ expect(type.id.name).toBe("RegExp"); | |
}); | |
it("should infer Object from object expression", function() { | |
const path = getPath("({ a: 5 })") | |
.get("body")[0] | |
.get("expression"); | |
const type = path.getTypeAnnotation(); | |
- expect( | |
- t.isGenericTypeAnnotation(type) && type.id.name === "Object", | |
- ).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("Generic"); | |
+ expect(type.id.name).toBe("Object"); | |
}); | |
it("should infer Array from array expression", function() { | |
const path = getPath("[ 5 ]") | |
.get("body")[0] | |
.get("expression"); | |
const type = path.getTypeAnnotation(); | |
- expect( | |
- t.isGenericTypeAnnotation(type) && type.id.name === "Array", | |
- ).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("Generic"); | |
+ expect(type.id.name).toBe("Array"); | |
}); | |
it("should infer Function from function", function() { | |
const path = getPath("(function (): string {})") | |
.get("body")[0] | |
.get("expression"); | |
const type = path.getTypeAnnotation(); | |
- expect( | |
- t.isGenericTypeAnnotation(type) && type.id.name === "Function", | |
- ).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("Generic"); | |
+ expect(type.id.name).toBe("Function"); | |
}); | |
it("should infer call return type using function", function() { | |
const path = getPath("(function (): string {})()") | |
.get("body")[0] | |
.get("expression"); | |
const type = path.getTypeAnnotation(); | |
- expect(t.isStringTypeAnnotation(type)).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("String"); | |
}); | |
it("should infer call return type using async function", function() { | |
const path = getPath("(async function (): string {})()") | |
.get("body")[0] | |
.get("expression"); | |
const type = path.getTypeAnnotation(); | |
- expect( | |
- t.isGenericTypeAnnotation(type) && type.id.name === "Promise", | |
- ).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("Generic"); | |
+ expect(type.id.name).toBe("Promise"); | |
}); | |
it("should infer call return type using async generator function", function() { | |
const path = getPath("(async function * (): string {})()") | |
.get("body")[0] | |
.get("expression"); | |
const type = path.getTypeAnnotation(); | |
- expect( | |
- t.isGenericTypeAnnotation(type) && type.id.name === "AsyncIterator", | |
- ).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("Generic"); | |
+ expect(type.id.name).toBe("AsyncIterator"); | |
}); | |
it("should infer number from x/y", function() { | |
const path = getPath("x/y") | |
.get("body")[0] | |
.get("expression"); | |
const type = path.getTypeAnnotation(); | |
- expect(t.isNumberTypeAnnotation(type)).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("Number"); | |
}); | |
it("should infer boolean from x instanceof y", function() { | |
const path = getPath("x instanceof y") | |
.get("body")[0] | |
.get("expression"); | |
const type = path.getTypeAnnotation(); | |
- expect(t.isBooleanTypeAnnotation(type)).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("Boolean"); | |
}); | |
it("should infer number from 1 + 2", function() { | |
const path = getPath("1 + 2") | |
.get("body")[0] | |
.get("expression"); | |
const type = path.getTypeAnnotation(); | |
- expect(t.isNumberTypeAnnotation(type)).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("Number"); | |
}); | |
it("should infer string|number from x + y", function() { | |
const path = getPath("x + y") | |
.get("body")[0] | |
.get("expression"); | |
const type = path.getTypeAnnotation(); | |
- expect(t.isUnionTypeAnnotation(type)).toBeTruthy(); | |
- expect(t.isStringTypeAnnotation(type.types[0])).toBeTruthy(); | |
- expect(t.isNumberTypeAnnotation(type.types[1])).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("Union"); | |
+ expect(type.types[0]).toBeAnnotationOfType("String"); | |
+ expect(type.types[1]).toBeAnnotationOfType("Number"); | |
}); | |
it("should infer type of tagged template literal", function() { | |
const path = getPath("(function (): RegExp {}) `hey`") | |
.get("body")[0] | |
.get("expression"); | |
const type = path.getTypeAnnotation(); | |
- expect( | |
- t.isGenericTypeAnnotation(type) && type.id.name === "RegExp", | |
- ).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("Generic"); | |
+ expect(type.id.name).toBe("RegExp"); | |
}); | |
it("should infer constant identifier", function() { | |
const path = getPath("const x = 0; x").get("body.1.expression"); | |
const type = path.getTypeAnnotation(); | |
- expect(t.isNumberTypeAnnotation(type)).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("Number"); | |
}); | |
it("should infer indirect constant identifier", function() { | |
const path = getPath("const x = 0; const y = x; y").get( | |
"body.2.expression", | |
); | |
const type = path.getTypeAnnotation(); | |
- expect(t.isNumberTypeAnnotation(type)).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("Number"); | |
}); | |
it("should infer identifier type from if statement (===)", function() { | |
const path = getPath( | |
@@ -262,7 +274,7 @@ describe("inference", function() { | |
}`, | |
).get("body.0.body.body.0.consequent.expression"); | |
const type = path.getTypeAnnotation(); | |
- expect(t.isBooleanTypeAnnotation(type)).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("Boolean"); | |
}); | |
it("should infer identifier type from if statement (typeof)", function() { | |
let path = getPath( | |
@@ -271,14 +283,14 @@ describe("inference", function() { | |
}`, | |
).get("body.0.body.body.0.consequent.expression"); | |
let type = path.getTypeAnnotation(); | |
- expect(t.isStringTypeAnnotation(type)).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("String"); | |
path = getPath( | |
`function test(x) { | |
if (typeof x === 'number') x; | |
}`, | |
).get("body.0.body.body.0.consequent.expression"); | |
type = path.getTypeAnnotation(); | |
- expect(t.isNumberTypeAnnotation(type)).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("Number"); | |
}); | |
it("should infer identifier type from if statement (&&)", function() { | |
let path = getPath( | |
@@ -287,23 +299,23 @@ describe("inference", function() { | |
}`, | |
).get("body.0.body.body.0.consequent.expression"); | |
let type = path.getTypeAnnotation(); | |
- expect(t.isUnionTypeAnnotation(type)).toBeTruthy(); | |
- expect(t.isStringTypeAnnotation(type.types[0])).toBeTruthy(); | |
- expect(t.isNumberTypeAnnotation(type.types[1])).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("Union"); | |
+ expect(type.types[0]).toBeAnnotationOfType("String"); | |
+ expect(type.types[1]).toBeAnnotationOfType("Number"); | |
path = getPath( | |
`function test(x) { | |
if (true && x === 3) x; | |
}`, | |
).get("body.0.body.body.0.consequent.expression"); | |
type = path.getTypeAnnotation(); | |
- expect(t.isNumberTypeAnnotation(type)).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("Number"); | |
path = getPath( | |
`function test(x) { | |
if (x === 'test' && true) x; | |
}`, | |
).get("body.0.body.body.0.consequent.expression"); | |
type = path.getTypeAnnotation(); | |
- expect(t.isStringTypeAnnotation(type)).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("String"); | |
}); | |
it("should infer identifier type from if statement (||)", function() { | |
const path = getPath( | |
@@ -312,7 +324,7 @@ describe("inference", function() { | |
}`, | |
).get("body.0.body.body.0.consequent.expression"); | |
const type = path.getTypeAnnotation(); | |
- expect(t.isAnyTypeAnnotation(type)).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("Any"); | |
}); | |
it("should not infer identifier type from incorrect binding", function() { | |
const path = getPath( | |
@@ -325,7 +337,7 @@ describe("inference", function() { | |
}`, | |
).get("body.0.body.body.0.consequent.body.0.body.body.0.expression"); | |
const type = path.getTypeAnnotation(); | |
- expect(t.isAnyTypeAnnotation(type)).toBeTruthy(); | |
+ expect(type).toBeAnnotationOfType("Any"); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment