Update prettier, eslint + plugins, flow, husky and lint-staged (#6183)

This commit is contained in:
Daniel Tschinder
2017-08-31 22:56:29 +02:00
committed by GitHub
parent 6108bee4f9
commit 44f6ff5e85
15 changed files with 519 additions and 452 deletions

View File

@@ -97,7 +97,9 @@ export function ConditionalExpression() {
}
export function SequenceExpression() {
return this.get("expressions").pop().getTypeAnnotation();
return this.get("expressions")
.pop()
.getTypeAnnotation();
}
export function AssignmentExpression() {

View File

@@ -243,7 +243,10 @@ export default class Scope {
*/
generateUid(name: string = "temp") {
name = t.toIdentifier(name).replace(/^_+/, "").replace(/[0-9]+$/g, "");
name = t
.toIdentifier(name)
.replace(/^_+/, "")
.replace(/[0-9]+$/g, "");
let uid;
let i = 0;

View File

@@ -18,14 +18,18 @@ describe("evaluation", function() {
describe("evaluateTruthy", function() {
it("it should work with null", function() {
assert.strictEqual(
getPath("false || a.length === 0;").get("body")[0].evaluateTruthy(),
getPath("false || a.length === 0;")
.get("body")[0]
.evaluateTruthy(),
undefined,
);
});
it("it should not mistake lack of confidence for falsy", function() {
assert.strictEqual(
getPath("foo || 'bar'").get("body")[0].evaluate().value,
getPath("foo || 'bar'")
.get("body")[0]
.evaluate().value,
undefined,
);
});
@@ -100,12 +104,15 @@ describe("evaluation", function() {
const input =
"var a = 5; function x() { var a = 5; var b = a + 1; } var b = a + 2";
assert.strictEqual(
getPath(input).get("body.1.body.body.1.declarations.0.init").evaluate()
.value,
getPath(input)
.get("body.1.body.body.1.declarations.0.init")
.evaluate().value,
6,
);
assert.strictEqual(
getPath(input).get("body.2.declarations.0.init").evaluate().value,
getPath(input)
.get("body.2.declarations.0.init")
.evaluate().value,
7,
);
});
@@ -120,11 +127,15 @@ describe("evaluation", function() {
const constExample =
"const d = true; if (d && true || false) { const d = false; d && 5; }";
assert.strictEqual(
getPath(constExample).get("body.1.test").evaluate().value,
getPath(constExample)
.get("body.1.test")
.evaluate().value,
true,
);
assert.strictEqual(
getPath(constExample).get("body.1.consequent.body.1").evaluate().value,
getPath(constExample)
.get("body.1.consequent.body.1")
.evaluate().value,
false,
);
const test_alternate = "var y = (3 < 4)? 3 + 4: 3 + 4;";
@@ -153,15 +164,21 @@ describe("evaluation", function() {
it("should evaluate undefined, NaN and Infinity", () => {
assert.strictEqual(
getPath("undefined").get("body.0.expression").evaluate().confident,
getPath("undefined")
.get("body.0.expression")
.evaluate().confident,
true,
);
assert.strictEqual(
getPath("NaN").get("body.0.expression").evaluate().confident,
getPath("NaN")
.get("body.0.expression")
.evaluate().confident,
true,
);
assert.strictEqual(
getPath("Infinity").get("body.0.expression").evaluate().confident,
getPath("Infinity")
.get("body.0.expression")
.evaluate().confident,
true,
);
});
@@ -186,12 +203,16 @@ describe("evaluation", function() {
it("should work with String.raw", function() {
assert.strictEqual(
getPath("String.raw`\\d`").get("body")[0].evaluate().value,
getPath("String.raw`\\d`")
.get("body")[0]
.evaluate().value,
"\\d",
);
assert.strictEqual(
getPath("`${String.raw`\\d`}`").get("body")[0].evaluate().value,
getPath("`${String.raw`\\d`}`")
.get("body")[0]
.evaluate().value,
"\\d",
);
});

View File

@@ -64,28 +64,36 @@ describe("inference", function() {
});
describe("getTypeAnnotation", function() {
it("should infer from type cast", function() {
const 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() {
const 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() {
const 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() {
const path = getPath("new T").get("body")[0].get("expression");
const path = getPath("new T")
.get("body")[0]
.get("expression");
const type = path.getTypeAnnotation();
assert.ok(
t.isGenericTypeAnnotation(type) && type.id.name === "T",
@@ -93,49 +101,63 @@ describe("inference", function() {
);
});
it("should infer number from ++x", function() {
const 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() {
const 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() {
const 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() {
const 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() {
const 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() {
const 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() {
const path = getPath("'a' && 1").get("body")[0].get("expression");
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(
@@ -148,7 +170,9 @@ describe("inference", function() {
);
});
it("should infer type of conditional expression", function() {
const path = getPath("q ? true : 0").get("body")[0].get("expression");
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(
@@ -161,7 +185,9 @@ describe("inference", function() {
);
});
it("should infer RegExp from RegExp literal", function() {
const path = getPath("/.+/").get("body")[0].get("expression");
const path = getPath("/.+/")
.get("body")[0]
.get("expression");
const type = path.getTypeAnnotation();
assert.ok(
t.isGenericTypeAnnotation(type) && type.id.name === "RegExp",
@@ -169,7 +195,9 @@ describe("inference", function() {
);
});
it("should infer Object from object expression", function() {
const path = getPath("({ a: 5 })").get("body")[0].get("expression");
const path = getPath("({ a: 5 })")
.get("body")[0]
.get("expression");
const type = path.getTypeAnnotation();
assert.ok(
t.isGenericTypeAnnotation(type) && type.id.name === "Object",
@@ -177,7 +205,9 @@ describe("inference", function() {
);
});
it("should infer Array from array expression", function() {
const path = getPath("[ 5 ]").get("body")[0].get("expression");
const path = getPath("[ 5 ]")
.get("body")[0]
.get("expression");
const type = path.getTypeAnnotation();
assert.ok(
t.isGenericTypeAnnotation(type) && type.id.name === "Array",
@@ -222,22 +252,30 @@ describe("inference", function() {
);
});
it("should infer number from x/y", function() {
const path = getPath("x/y").get("body")[0].get("expression");
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() {
const path = getPath("x instanceof y").get("body")[0].get("expression");
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() {
const path = getPath("1 + 2").get("body")[0].get("expression");
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() {
const path = getPath("x + y").get("body")[0].get("expression");
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(

View File

@@ -24,7 +24,10 @@ describe("removal", function() {
describe("ArrowFunction", function() {
it("remove body", function() {
const rootPath = getPath("x = () => b;");
const path = rootPath.get("body")[0].get("expression").get("right");
const path = rootPath
.get("body")[0]
.get("expression")
.get("right");
const body = path.get("body");
body.remove();

View File

@@ -75,11 +75,22 @@ describe("scope", function() {
it("purity", function() {
assert.ok(
getPath("({ x: 1 })").get("body")[0].get("expression").isPure(),
getPath("({ x: 1 })")
.get("body")[0]
.get("expression")
.isPure(),
);
assert.ok(!getPath("`${a}`").get("body")[0].get("expression").isPure());
assert.ok(
getPath("let a = 1; `${a}`").get("body")[1].get("expression").isPure(),
!getPath("`${a}`")
.get("body")[0]
.get("expression")
.isPure(),
);
assert.ok(
getPath("let a = 1; `${a}`")
.get("body")[1]
.get("expression")
.isPure(),
);
assert.ok(
!getPath("let a = 1; `${a++}`")
@@ -88,10 +99,16 @@ describe("scope", function() {
.isPure(),
);
assert.ok(
!getPath("tagged`foo`").get("body")[0].get("expression").isPure(),
!getPath("tagged`foo`")
.get("body")[0]
.get("expression")
.isPure(),
);
assert.ok(
getPath("String.raw`foo`").get("body")[0].get("expression").isPure(),
getPath("String.raw`foo`")
.get("body")[0]
.get("expression")
.isPure(),
);
});