From 153bf4cad9ca6c0a781becf70f2ab232c1ddc3e6 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Sat, 4 Feb 2017 18:36:48 -0800 Subject: [PATCH] Remove first line special case (#597) If you removed it before #561, it would trigger an exception in the globalPreceding branch. But now that we don't have this hack anymore, it works fine. Fixes #543 --- src/comments.js | 4 +--- .../comments/__snapshots__/jsfmt.spec.js.snap | 18 ++++++++++++++++++ tests/comments/first-line.js | 2 ++ .../annot/__snapshots__/jsfmt.spec.js.snap | 4 +--- .../lib/__snapshots__/jsfmt.spec.js.snap | 3 +-- .../lib/__snapshots__/jsfmt.spec.js.snap | 3 +-- .../fakelib/__snapshots__/jsfmt.spec.js.snap | 3 +-- .../__snapshots__/jsfmt.spec.js.snap | 3 +-- .../__snapshots__/jsfmt.spec.js.snap | 3 +-- .../__snapshots__/jsfmt.spec.js.snap | 3 +-- .../foo/__snapshots__/jsfmt.spec.js.snap | 3 +-- .../flow/rec/__snapshots__/jsfmt.spec.js.snap | 7 ++----- .../record/__snapshots__/jsfmt.spec.js.snap | 3 +-- .../__snapshots__/jsfmt.spec.js.snap | 3 +-- 14 files changed, 33 insertions(+), 29 deletions(-) create mode 100644 tests/comments/first-line.js diff --git a/src/comments.js b/src/comments.js index 42d8bd5bf..b18943739 100644 --- a/src/comments.js +++ b/src/comments.js @@ -135,11 +135,9 @@ function attach(comments, ast, text) { const precedingNode = comment.precedingNode; const enclosingNode = comment.enclosingNode; const followingNode = comment.followingNode; - const isStartOfFile = comment.loc.start.line === 1; if ( - util.hasNewline(text, locStart(comment), { backwards: true }) || - isStartOfFile + util.hasNewline(text, locStart(comment), { backwards: true }) ) { // If a comment exists on its own line, prefer a leading comment. // We also need to check if it's the first line of the file. diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap index c009020e4..1cafa5080 100644 --- a/tests/comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap @@ -74,6 +74,24 @@ declare class Foo extends Qux {/* dangling */} " `; +exports[`test first-line.js 1`] = ` +"a // comment +b +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +a; // comment +b; +" +`; + +exports[`test first-line.js 2`] = ` +"a // comment +b +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +a; // comment +b; +" +`; + exports[`test if.js 1`] = ` "if (1) // comment diff --git a/tests/comments/first-line.js b/tests/comments/first-line.js new file mode 100644 index 000000000..4c01d05a2 --- /dev/null +++ b/tests/comments/first-line.js @@ -0,0 +1,2 @@ +a // comment +b diff --git a/tests/flow/annot/__snapshots__/jsfmt.spec.js.snap b/tests/flow/annot/__snapshots__/jsfmt.spec.js.snap index 76159fc45..194b4f92a 100644 --- a/tests/flow/annot/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/annot/__snapshots__/jsfmt.spec.js.snap @@ -148,9 +148,7 @@ function foo() { class MyClass { } // looked up above } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -let myClassInstance: MyClass = null; - -// forward ref ok, null ~> class error +let myClassInstance: MyClass = null; // forward ref ok, null ~> class error function bar(): MyClass { return null; // forward ref ok, null ~> class error diff --git a/tests/flow/get-def2/lib/__snapshots__/jsfmt.spec.js.snap b/tests/flow/get-def2/lib/__snapshots__/jsfmt.spec.js.snap index 501df81a4..db39ea781 100644 --- a/tests/flow/get-def2/lib/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/get-def2/lib/__snapshots__/jsfmt.spec.js.snap @@ -7,8 +7,7 @@ type $JSXIntrinsics = { span: $JSXIntrinsic<{id: string, class: string}>, }; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -declare var $React: $Exports<\"react\">; -// fake import +declare var $React: $Exports<\"react\">; // fake import type $JSXIntrinsic = Class<$React.Component>; type $JSXIntrinsics = { diff --git a/tests/flow/jsx_intrinsics.custom/lib/__snapshots__/jsfmt.spec.js.snap b/tests/flow/jsx_intrinsics.custom/lib/__snapshots__/jsfmt.spec.js.snap index 501df81a4..db39ea781 100644 --- a/tests/flow/jsx_intrinsics.custom/lib/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/jsx_intrinsics.custom/lib/__snapshots__/jsfmt.spec.js.snap @@ -7,8 +7,7 @@ type $JSXIntrinsics = { span: $JSXIntrinsic<{id: string, class: string}>, }; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -declare var $React: $Exports<\"react\">; -// fake import +declare var $React: $Exports<\"react\">; // fake import type $JSXIntrinsic = Class<$React.Component>; type $JSXIntrinsics = { diff --git a/tests/flow/new_react/fakelib/__snapshots__/jsfmt.spec.js.snap b/tests/flow/new_react/fakelib/__snapshots__/jsfmt.spec.js.snap index afd9ad152..386be1228 100644 --- a/tests/flow/new_react/fakelib/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/new_react/fakelib/__snapshots__/jsfmt.spec.js.snap @@ -6,8 +6,7 @@ exports[`test type_aliases.js 1`] = ` type _ReactElement, C: $React.Component> = $React.Element; type $jsx = _ReactElement<*, *, *, C>; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -declare var $React: $Exports<\"react\">; -// fake import +declare var $React: $Exports<\"react\">; // fake import // Strawman: revised definition of $jsx (alternatively, React.Element). // Using bounded poly to specify a constraint on a type parameter, and // existentials to elide type arguments. diff --git a/tests/flow/node_tests/basic_file/__snapshots__/jsfmt.spec.js.snap b/tests/flow/node_tests/basic_file/__snapshots__/jsfmt.spec.js.snap index 0b97ddb4b..cfa42aa79 100644 --- a/tests/flow/node_tests/basic_file/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/node_tests/basic_file/__snapshots__/jsfmt.spec.js.snap @@ -9,8 +9,7 @@ exports[`test foo.js 1`] = ` "var x = require(\'./bar\'); // bar.js does not work console.log(x); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -var x = require(\"./bar\"); -// bar.js does not work +var x = require(\"./bar\"); // bar.js does not work console.log(x); " `; diff --git a/tests/flow/node_tests/basic_package/__snapshots__/jsfmt.spec.js.snap b/tests/flow/node_tests/basic_package/__snapshots__/jsfmt.spec.js.snap index e2a82d3b3..68d3d5387 100644 --- a/tests/flow/node_tests/basic_package/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/node_tests/basic_package/__snapshots__/jsfmt.spec.js.snap @@ -2,8 +2,7 @@ exports[`test foo.js 1`] = ` "var x = require(\'./bar_lib\'); // \'bar_lib\' does not work! console.log(x); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -var x = require(\"./bar_lib\"); -// \'bar_lib\' does not work! +var x = require(\"./bar_lib\"); // \'bar_lib\' does not work! console.log(x); " `; diff --git a/tests/flow/node_tests/package_file/__snapshots__/jsfmt.spec.js.snap b/tests/flow/node_tests/package_file/__snapshots__/jsfmt.spec.js.snap index cf9e4dca2..91c60d46b 100644 --- a/tests/flow/node_tests/package_file/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/node_tests/package_file/__snapshots__/jsfmt.spec.js.snap @@ -9,8 +9,7 @@ exports[`test foo.js 1`] = ` "var x: string = require(\'./bar_lib\'); // \'bar_lib\' does not work! console.log(x); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -var x: string = require(\"./bar_lib\"); -// \'bar_lib\' does not work! +var x: string = require(\"./bar_lib\"); // \'bar_lib\' does not work! console.log(x); " `; diff --git a/tests/flow/node_tests/package_file_node_modules/foo/__snapshots__/jsfmt.spec.js.snap b/tests/flow/node_tests/package_file_node_modules/foo/__snapshots__/jsfmt.spec.js.snap index b47f3c573..c478de1f6 100644 --- a/tests/flow/node_tests/package_file_node_modules/foo/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/node_tests/package_file_node_modules/foo/__snapshots__/jsfmt.spec.js.snap @@ -2,8 +2,7 @@ exports[`test foo.js 1`] = ` "var x: string = require(\'bar_lib\'); // \'bar_lib\' does not work! console.log(x); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -var x: string = require(\"bar_lib\"); -// \'bar_lib\' does not work! +var x: string = require(\"bar_lib\"); // \'bar_lib\' does not work! console.log(x); " `; diff --git a/tests/flow/rec/__snapshots__/jsfmt.spec.js.snap b/tests/flow/rec/__snapshots__/jsfmt.spec.js.snap index c254b820e..c384a6371 100644 --- a/tests/flow/rec/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/rec/__snapshots__/jsfmt.spec.js.snap @@ -93,9 +93,7 @@ pstar = (new P: P>); // OK ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ class P { x: X; -} - -// this is like Promise +} // this is like Promise type Pstar = X | Pstar>; // this is like Promise* @@ -146,8 +144,7 @@ class B extends D { } class C extends B { } ((new C: C): D) // error: number ~/~ string ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -var a = []; -// Array ~> a +var a = []; // Array ~> a function bar() { a = a.concat([]); // terminate despite expanding types: // a ~> .concat(Array) diff --git a/tests/flow/record/__snapshots__/jsfmt.spec.js.snap b/tests/flow/record/__snapshots__/jsfmt.spec.js.snap index 5db3f33b7..14bd118fa 100644 --- a/tests/flow/record/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/record/__snapshots__/jsfmt.spec.js.snap @@ -24,8 +24,7 @@ class D extends C<{foo: number, bar: string}> { type AnyKey = $Keys; var o3: {[key: AnyKey]: number} = { foo: 0 }; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -type Key1 = \"foo\" | \"bar\"; -// make an enum type with known key set +type Key1 = \"foo\" | \"bar\"; // make an enum type with known key set var o1: { [key: Key1]: number } = { foo: 0, bar: \"\" // error: string ~/~ number diff --git a/tests/flow/type-destructors/__snapshots__/jsfmt.spec.js.snap b/tests/flow/type-destructors/__snapshots__/jsfmt.spec.js.snap index e5d639ae3..ddbf4f683 100644 --- a/tests/flow/type-destructors/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/type-destructors/__snapshots__/jsfmt.spec.js.snap @@ -62,8 +62,7 @@ var x1: $NonMaybeType = true; // err, boolean ~> number|string var x2: $PropertyType<{p:number}|{p:string},\'p\'> = 0; // ok, number ~> number|string var x3: $PropertyType<{p:number}|{p:string},\'p\'> = true; // err, boolean ~> number|string ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -var x0: $NonMaybeType = 0; -// ok, number ~> number|string +var x0: $NonMaybeType = 0; // ok, number ~> number|string var x1: $NonMaybeType = true; // err, boolean ~> number|string var x2: $PropertyType<{ p: number } | { p: string }, \"p\"> = 0; // ok, number ~> number|string var x3: $PropertyType<{ p: number } | { p: string }, \"p\"> = true; // err, boolean ~> number|string