From fc521c4d03fb6f1e4017694cf429e848712acabe Mon Sep 17 00:00:00 2001 From: Michael Ficarra Date: Tue, 10 Jan 2017 12:03:36 -0800 Subject: [PATCH] fixes #56: indent cases inside switch statements --- src/printer.js | 6 +- .../binding/__snapshots__/jsfmt.spec.js.snap | 58 +++---- tests/break/__snapshots__/jsfmt.spec.js.snap | 40 ++--- .../__snapshots__/jsfmt.spec.js.snap | 78 ++++----- .../__snapshots__/jsfmt.spec.js.snap | 10 +- tests/init/__snapshots__/jsfmt.spec.js.snap | 110 ++++++------- tests/locals/__snapshots__/jsfmt.spec.js.snap | 10 +- tests/react/__snapshots__/jsfmt.spec.js.snap | 8 +- tests/refi/__snapshots__/jsfmt.spec.js.snap | 68 ++++---- .../__snapshots__/jsfmt.spec.js.snap | 124 +++++++------- tests/switch/__snapshots__/jsfmt.spec.js.snap | 152 +++++++++--------- 11 files changed, 333 insertions(+), 331 deletions(-) diff --git a/src/printer.js b/src/printer.js index d8660208..c912c1a0 100644 --- a/src/printer.js +++ b/src/printer.js @@ -996,8 +996,10 @@ function genericPrintNoParens(path, options, print) { "switch (", path.call(print, "discriminant"), ") {", - hardline, - join(hardline, path.map(print, "cases")), + indent( + options.tabWidth, + concat([ hardline, join(hardline, path.map(print, "cases"))]) + ), hardline, "}" ]); diff --git a/tests/binding/__snapshots__/jsfmt.spec.js.snap b/tests/binding/__snapshots__/jsfmt.spec.js.snap index 10bc5b5e..7bb830c0 100644 --- a/tests/binding/__snapshots__/jsfmt.spec.js.snap +++ b/tests/binding/__snapshots__/jsfmt.spec.js.snap @@ -575,16 +575,16 @@ function switch_scope(x: string) { let a: number = 0; var b: number = 0; switch (x) { - case \"foo\": - let a = \"\"; - // ok: local to switch - var b = \"\"; - // error: string ~> number - break; - case \"bar\": - let a = \"\"; - // error: a already bound in switch - break; + case \"foo\": + let a = \"\"; + // ok: local to switch + var b = \"\"; + // error: string ~> number + break; + case \"bar\": + let a = \"\"; + // error: a already bound in switch + break; } } @@ -593,25 +593,25 @@ function switch_scope(x: string) { // yet perfect. function switch_scope2(x: number) { switch (x) { - case 0: - a = \"\"; - // error: assign before declaration - break; - case 1: - var b = a; - // error: use before declaration - break; - case 2: - let a = \"\"; - break; - case 3: - a = \"\"; - // error: skipped initializer - break; - case 4: - var c: string = a; - // error: skipped initializer - break; + case 0: + a = \"\"; + // error: assign before declaration + break; + case 1: + var b = a; + // error: use before declaration + break; + case 2: + let a = \"\"; + break; + case 3: + a = \"\"; + // error: skipped initializer + break; + case 4: + var c: string = a; + // error: skipped initializer + break; } a = \"\"; // error: a no longer in scope } diff --git a/tests/break/__snapshots__/jsfmt.spec.js.snap b/tests/break/__snapshots__/jsfmt.spec.js.snap index e402a732..1bae9c78 100644 --- a/tests/break/__snapshots__/jsfmt.spec.js.snap +++ b/tests/break/__snapshots__/jsfmt.spec.js.snap @@ -86,15 +86,15 @@ function bar(b) { if (x == null) return; switch (\"\") { - case 0: - var y: number = x; - // error: boolean !~> number - x = \"\"; - case 1: - var z: number = x; - // 2 errors: (boolean | string) !~> number - break; - case 2: + case 0: + var y: number = x; + // error: boolean !~> number + x = \"\"; + case 1: + var z: number = x; + // 2 errors: (boolean | string) !~> number + break; + case 2: } var w: number = x; // 2 errors: (boolean | string) !~> number } @@ -104,17 +104,17 @@ function bar2(b) { if (x == null) return; switch (\"\") { - case 0: { - let y: number = x; - // error: boolean !~> number - x = \"\"; - } - case 1: { - let z: number = x; - // 2 errors: (boolean | string) !~> number - break; - } - case 2: + case 0: { + let y: number = x; + // error: boolean !~> number + x = \"\"; + } + case 1: { + let z: number = x; + // 2 errors: (boolean | string) !~> number + break; + } + case 2: } var w: number = x; // 2 errors: (boolean | string) !~> number } diff --git a/tests/disjoint-union-perf/__snapshots__/jsfmt.spec.js.snap b/tests/disjoint-union-perf/__snapshots__/jsfmt.spec.js.snap index 8b7d63fe..601e8403 100644 --- a/tests/disjoint-union-perf/__snapshots__/jsfmt.spec.js.snap +++ b/tests/disjoint-union-perf/__snapshots__/jsfmt.spec.js.snap @@ -238,55 +238,55 @@ function getBinaryOp( op: \"plus\" | \"minus\" | \"divide\" | \"multiply\" ): \"+\" | \"-\" | \"*\" | \"/\" { switch (op) { - case \"plus\": - return \"+\"; - case \"minus\": - return \"-\"; - case \"divide\": - return \"/\"; - case \"multiply\": - return \"*\"; - default: - throw new Error(\"Invalid binary operator: \" + op); + case \"plus\": + return \"+\"; + case \"minus\": + return \"-\"; + case \"divide\": + return \"/\"; + case \"multiply\": + return \"*\"; + default: + throw new Error(\"Invalid binary operator: \" + op); } } export function emitExpression(node: TypedNode): t.Expression { switch (node.exprNodeType) { - case \"string_literal\": - // FALLTHROUGH - case \"number\": - return b.literal(node.value); - case \"variable\": - return b.memberExpression( - b.identifier(\"vars\"), - b.identifier(node.name), - false - ); - case \"binary_op\": { - const lhs = emitExpression(node.lhs); - const rhs = emitExpression(node.rhs); - - const op = getBinaryOp(node.binaryOp); - return b.binaryExpression(op, lhs, rhs); - } - case \"unary_minus\": { - const operand = emitExpression(node.op); - return b.unaryExpression(\"-\", operand, true); - } - case \"function_invocation\": { - const callee = b.memberExpression( - b.identifier(\"fns\"), + case \"string_literal\": + // FALLTHROUGH + case \"number\": + return b.literal(node.value); + case \"variable\": + return b.memberExpression( + b.identifier(\"vars\"), b.identifier(node.name), false ); + case \"binary_op\": { + const lhs = emitExpression(node.lhs); + const rhs = emitExpression(node.rhs); - const args = node.parameters.map(n => emitExpression(n)); + const op = getBinaryOp(node.binaryOp); + return b.binaryExpression(op, lhs, rhs); + } + case \"unary_minus\": { + const operand = emitExpression(node.op); + return b.unaryExpression(\"-\", operand, true); + } + case \"function_invocation\": { + const callee = b.memberExpression( + b.identifier(\"fns\"), + b.identifier(node.name), + false + ); - return b.callExpression(callee, args); - } - default: - throw new Error(\"Unknown expression type: \" + node.type); + const args = node.parameters.map(n => emitExpression(n)); + + return b.callExpression(callee, args); + } + default: + throw new Error(\"Unknown expression type: \" + node.type); } }" `; diff --git a/tests/geolocation/__snapshots__/jsfmt.spec.js.snap b/tests/geolocation/__snapshots__/jsfmt.spec.js.snap index cdbf6bf3..f9f1420e 100644 --- a/tests/geolocation/__snapshots__/jsfmt.spec.js.snap +++ b/tests/geolocation/__snapshots__/jsfmt.spec.js.snap @@ -31,11 +31,11 @@ var id = geolocation.watchPosition( e => { var message: string = e.message; switch (e.code) { - case e.PERMISSION_DENIED: - case e.POSITION_UNAVAILABLE: - case e.TIMEOUT: - default: - break; + case e.PERMISSION_DENIED: + case e.POSITION_UNAVAILABLE: + case e.TIMEOUT: + default: + break; } } ); diff --git a/tests/init/__snapshots__/jsfmt.spec.js.snap b/tests/init/__snapshots__/jsfmt.spec.js.snap index 8daeb988..5dd14f32 100644 --- a/tests/init/__snapshots__/jsfmt.spec.js.snap +++ b/tests/init/__snapshots__/jsfmt.spec.js.snap @@ -451,12 +451,12 @@ function if_post_init(b) { function switch_partial_post_init(i) { var x: number; switch (i) { - case 0: - x = 0; - break; - case 1: - x = 1; - break; + case 0: + x = 0; + break; + case 1: + x = 1; + break; } var y: number = x; // error, possibly uninitialized } @@ -465,14 +465,14 @@ function switch_partial_post_init(i) { function switch_post_init(i) { var x: number; switch (i) { - case 0: - x = 0; - break; - case 1: - x = 1; - break; - default: - x = 2; + case 0: + x = 0; + break; + case 1: + x = 1; + break; + default: + x = 2; } var y: number = x; // no error, all cases covered } @@ -480,9 +480,9 @@ function switch_post_init(i) { // local use of annotated var in switch is ok function switch_scoped_init_1(i) { switch (i) { - case 0: - var x: number = 0; - var y: number = x; + case 0: + var x: number = 0; + var y: number = x; } } @@ -491,16 +491,16 @@ function switch_scoped_init_2(i) { var y: number = x; // error switch (i) { - case 0: - var x: number = 0; + case 0: + var x: number = 0; } } // ...and after function switch_scoped_init_3(i) { switch (i) { - case 0: - var x: number = 0; + case 0: + var x: number = 0; } var y: number = x; // error } @@ -508,11 +508,11 @@ function switch_scoped_init_3(i) { // ...and in a fallthrough case without initialization function switch_scoped_init_4(i) { switch (i) { - case 0: - var x: number = 0; - // error - case 1: - var y: number = x; + case 0: + var x: number = 0; + // error + case 1: + var y: number = x; } } @@ -853,12 +853,12 @@ function if_post_init(b) { function switch_partial_post_init(i) { let x: number; switch (i) { - case 0: - x = 0; - break; - case 1: - x = 1; - break; + case 0: + x = 0; + break; + case 1: + x = 1; + break; } var y: number = x; // error, possibly uninitialized } @@ -867,14 +867,14 @@ function switch_partial_post_init(i) { function switch_post_init(i) { let x: number; switch (i) { - case 0: - x = 0; - break; - case 1: - x = 1; - break; - default: - x = 2; + case 0: + x = 0; + break; + case 1: + x = 1; + break; + default: + x = 2; } var y: number = x; // no error, all cases covered } @@ -882,11 +882,11 @@ function switch_post_init(i) { // use in a switch after a skipped decl is an error function switch_scoped_init_2(i) { switch (i) { - case 0: - let x: number; - // error, skipped declaration - case 1: - let y: number = x; + case 0: + let x: number; + // error, skipped declaration + case 1: + let y: number = x; } } @@ -930,11 +930,11 @@ function for_of_post_init() { function switch_post_init2(i): number { let bar; switch (i) { - case 1: - bar = 3; - break; - default: - throw new Error(\"Invalid state\"); + case 1: + bar = 3; + break; + default: + throw new Error(\"Invalid state\"); } return bar; // ok, definitely initialized } @@ -943,11 +943,11 @@ function switch_post_init2(i): number { function switch_post_init2(i): number { let bar; switch (i) { - case 1: - bar = 3; - break; - default: - throw new Error(\"Invalid state\"); + case 1: + bar = 3; + break; + default: + throw new Error(\"Invalid state\"); } return bar; // ok, definitely initialized } diff --git a/tests/locals/__snapshots__/jsfmt.spec.js.snap b/tests/locals/__snapshots__/jsfmt.spec.js.snap index 839e7ce0..44fb7e5f 100644 --- a/tests/locals/__snapshots__/jsfmt.spec.js.snap +++ b/tests/locals/__snapshots__/jsfmt.spec.js.snap @@ -64,11 +64,11 @@ function switch_scope(x: mixed) { let a = \"\"; let b = \"\"; switch (x) { - case \"foo\": - let a; - a = 0; - // doesn\'t add lower bound to outer a - b = 0; + case \"foo\": + let a; + a = 0; + // doesn\'t add lower bound to outer a + b = 0; } (a: string); // OK diff --git a/tests/react/__snapshots__/jsfmt.spec.js.snap b/tests/react/__snapshots__/jsfmt.spec.js.snap index f36fe4b8..8f64d0d1 100644 --- a/tests/react/__snapshots__/jsfmt.spec.js.snap +++ b/tests/react/__snapshots__/jsfmt.spec.js.snap @@ -250,10 +250,10 @@ var React = React.createClass({ // So this would have been an error in 0.21.0 if we didn\'t make this.props // Object switch (this.props.name) { - case \"a\": - return \"Bob\"; - default: - return \"Alice\"; + case \"a\": + return \"Bob\"; + default: + return \"Alice\"; } }, render() { diff --git a/tests/refi/__snapshots__/jsfmt.spec.js.snap b/tests/refi/__snapshots__/jsfmt.spec.js.snap index 7be65df8..28f9fabb 100644 --- a/tests/refi/__snapshots__/jsfmt.spec.js.snap +++ b/tests/refi/__snapshots__/jsfmt.spec.js.snap @@ -624,10 +624,10 @@ function block_scope(x: string | number) { function switch_scope(x: string | number) { switch (x) { - // doesn\'t refine outer x - default: - let x; - x = \"\"; + // doesn\'t refine outer x + default: + let x; + x = \"\"; } (x: string); // error: number ~> string } @@ -1187,13 +1187,13 @@ function exhaustion3(x): number { // @flow function foo(a, b, c) { switch (c) { - case a.x.y: - // OK - case b.x.y: + case a.x.y: // OK - return; - default: - return; + case b.x.y: + // OK + return; + default: + return; } } @@ -1201,13 +1201,13 @@ function foo(a, b, c) { function exhaustion1(x): number { var foo; switch (x) { - case 0: - // falls through - case 1: - foo = 3; - break; - default: - throw new Error(\"Invalid state\"); + case 0: + // falls through + case 1: + foo = 3; + break; + default: + throw new Error(\"Invalid state\"); } return foo; // no error } @@ -1215,11 +1215,11 @@ function exhaustion1(x): number { function exhaustion2(x, y): number { var foo; switch (x) { - case 0: - if (y) { - break; // leaks uninitialized foo out of switch - } - /** + case 0: + if (y) { + break; // leaks uninitialized foo out of switch + } + /** * TODO this shouldn\'t cause an error, because the path that * runs it will always go on to assign a number to foo. But * we\'ll need true isolation in env snapshots to model this. @@ -1228,11 +1228,11 @@ function exhaustion2(x, y): number { * foo = \"\"; */ - case 1: - foo = 3; - break; - default: - throw new Error(\"Invalid state\"); + case 1: + foo = 3; + break; + default: + throw new Error(\"Invalid state\"); } return foo; // error, possibly uninitialized } @@ -1240,13 +1240,13 @@ function exhaustion2(x, y): number { function exhaustion3(x): number { let foo = null; switch (x) { - case 0: - // falls through - case 1: - foo = 3; - break; - default: - throw new Error(\"Invalid state\"); + case 0: + // falls through + case 1: + foo = 3; + break; + default: + throw new Error(\"Invalid state\"); } return foo; // no error }" diff --git a/tests/refinements/__snapshots__/jsfmt.spec.js.snap b/tests/refinements/__snapshots__/jsfmt.spec.js.snap index 531afc6a..b30eb89e 100644 --- a/tests/refinements/__snapshots__/jsfmt.spec.js.snap +++ b/tests/refinements/__snapshots__/jsfmt.spec.js.snap @@ -1399,14 +1399,14 @@ let tests = [ }, function(mode: Mode) { switch (mode) { - case 0: - (mode: 0); - break; + case 0: + (mode: 0); + break; - case 1: - case 2: - (mode: 1 | 2); - break; + case 1: + case 2: + (mode: 1 | 2); + break; } }, function(x: number): 0 { @@ -1972,14 +1972,14 @@ let tests = [ }, function(mode: Mode) { switch (mode) { - case \"a\": - (mode: \"a\"); - break; + case \"a\": + (mode: \"a\"); + break; - case \"b\": - case \"c\": - (mode: \"b\" | \"c\"); - break; + case \"b\": + case \"c\": + (mode: \"b\" | \"c\"); + break; } }, function(x: string): \"\" { @@ -2120,55 +2120,55 @@ function corge(text: string | number | Array): string { function foo(text: string | number): string { switch (typeof text) { - case \"string\": - return text; - // error, should return string - case \"number\": - return text; - default: - return \"wat\"; + case \"string\": + return text; + // error, should return string + case \"number\": + return text; + default: + return \"wat\"; } } function bar(text: string | number): string { switch (typeof text) { - case \"string\": - return text[0]; - default: - return text++ + \"\"; + case \"string\": + return text[0]; + default: + return text++ + \"\"; } } function baz1(text: string | number): string { switch (typeof text) { - case \"number\": - // error, [0] on number - case \"string\": - return text[0]; - default: - return \"wat\"; + case \"number\": + // error, [0] on number + case \"string\": + return text[0]; + default: + return \"wat\"; } } function baz2(text: string | number): string { switch (typeof text) { - case \"string\": - // error, [0] on number - case \"number\": - return text[0]; - default: - return \"wat\"; + case \"string\": + // error, [0] on number + case \"number\": + return text[0]; + default: + return \"wat\"; } } function corge(text: string | number | Array): string { switch (typeof text) { - case \"object\": - return text[0]; - case \"string\": - case // using ++ since it isn\'t valid on arrays or strings. - // should only error for string since Array was filtered out. - \"number\": - return text++ + \"\"; + case \"object\": + return text[0]; + case \"string\": + case // using ++ since it isn\'t valid on arrays or strings. + // should only error for string since Array was filtered out. + \"number\": + return text++ + \"\"; - default: - return \"wat\"; + default: + return \"wat\"; } }" `; @@ -2463,10 +2463,10 @@ function list(n) { } function length(l) { switch (l.kind) { - case \"cons\": - return 1 + length(l.next); - default: - return 0; + case \"cons\": + return 1 + length(l.next); + default: + return 0; } } function check(n) { @@ -2482,13 +2482,13 @@ type B = { kind: 2, B: number }; type C = { kind: 3, C: number }; function kind(x: A | B | C): number { switch (x.kind) { - case EnumKind.A: - return x.A; - case EnumKind.B: - return x.B; - // error, x: C and property A not found in type C - default: - return x.A; + case EnumKind.A: + return x.A; + case EnumKind.B: + return x.B; + // error, x: C and property A not found in type C + default: + return x.A; } } kind({ kind: EnumKind.A, A: 1 }); @@ -2704,11 +2704,11 @@ type Error = { type: typeof ERROR, error: string }; function handleStatus(status: Success | Error) { switch (status.type) { - case SUCCESS: - console.log(\`Successful: \${status.message}\`); - break; - default: - console.log(\`Errored: \${status.error}\`); + case SUCCESS: + console.log(\`Successful: \${status.message}\`); + break; + default: + console.log(\`Errored: \${status.error}\`); } }" `; diff --git a/tests/switch/__snapshots__/jsfmt.spec.js.snap b/tests/switch/__snapshots__/jsfmt.spec.js.snap index f54ba636..c196d037 100644 --- a/tests/switch/__snapshots__/jsfmt.spec.js.snap +++ b/tests/switch/__snapshots__/jsfmt.spec.js.snap @@ -30,32 +30,32 @@ function baz(x): number { function foo(x): number { switch (x) { - case 0: - case 1: - return 1; - default: - throw new Error(\"hi\"); + case 0: + case 1: + return 1; + default: + throw new Error(\"hi\"); } } function bar(x) { switch (x) { - case 0: - break; - default: - return; + case 0: + break; + default: + return; } 1; } function baz(x): number { switch (x) { - case 0: - break; - case 1: - return 1; - default: - throw new Error(\"hi\"); + case 0: + break; + case 1: + return 1; + default: + throw new Error(\"hi\"); } return 2; }" @@ -98,28 +98,28 @@ function qux(b) { */ function foo(): number { switch (\"foo\") { - case \"foo\": - return 1; + case \"foo\": + return 1; } return 2; } function bar() { switch (\"bar\") { - case \"bar\": - break; - default: - break; + case \"bar\": + break; + default: + break; } } function qux(b) { var x = b ? 0 : \"\"; switch (\"qux\") { - case \"\": - x = 0; - case \"qux\": - x = x * x; + case \"\": + x = 0; + case \"qux\": + x = x * x; } }" `; @@ -181,10 +181,10 @@ function foo(x: mixed): string { var b = \"\"; switch (x) { - case \"foo\": - a = 0; - default: - b = 0; + case \"foo\": + a = 0; + default: + b = 0; } // a is now string | number @@ -203,13 +203,13 @@ function baz(x: mixed): number { var b = \"\"; switch (x) { - case \"baz\": - a = 0; - break; - case \"bar\": - a = \"\"; - default: - b = 0; + case \"baz\": + a = 0; + break; + case \"bar\": + a = \"\"; + default: + b = 0; } // a is now string | number @@ -316,18 +316,18 @@ function f1(i) { var x; switch (i) { - case 0: - x = 0; - break; - case 1: - x = 1; - break; - default: - x = -1; - break; - case 2: - x = \"2\"; - break; + case 0: + x = 0; + break; + case 1: + x = 1; + break; + default: + x = -1; + break; + case 2: + x = \"2\"; + break; } var y: number = x; // error, number | string ~/> number @@ -337,13 +337,13 @@ function f2(i) { var x; switch (i) { - case 0: - case 1: - default: - x = 1; - break; - // does not fall through default - case 2: + case 0: + case 1: + default: + x = 1; + break; + // does not fall through default + case 2: } var y: number = x; // error, number | uninitialized ~/> number @@ -353,12 +353,12 @@ function f3(i) { var x; switch (i) { - case 0: - case 1: - default: - // falls through to subsequent cases - case 2: - x = 1; + case 0: + case 1: + default: + // falls through to subsequent cases + case 2: + x = 1; } var y: number = x; // no error @@ -366,32 +366,32 @@ function f3(i) { function foo(x): number { switch (x) { - case 0: - default: - throw new Error(\"hi\"); - case 1: - return 1; + case 0: + default: + throw new Error(\"hi\"); + case 1: + return 1; } } function bar(x) { switch (x) { - default: - return; - case 0: - break; + default: + return; + case 0: + break; } 1; } function baz(x): number { switch (x) { - case 0: - break; - default: - throw new Error(\"hi\"); - case 1: - return 1; + case 0: + break; + default: + throw new Error(\"hi\"); + case 1: + return 1; } return 2; }"