Repository: prettier/prettier (JavaScript)
Date: 2019-12-04 16:15:58+00:00
Task type: Bug Fix

Issue:
[TS parser] Incorrect leading spaces inserted
**Prettier 1.19.1**
[Playground link](https://prettier.io/playground/#N4Igxg9gdgLgprEAuEBLAtgBwgJxgAgCp8BDAZ3zgA84wT8AzHCdfAcmtpLYB0oNsefAHMIjZqzYA6APSjeUPpChkC6Eqij4AvKTIBPKGHwAKAJQ6AfPmB989-MtWUax3aJOc6Zuw6cEwdAATHRcuX3snCAAbOCloiGETCIcSAHcNAi8TNkxNYQAvNgAafABtNgBaMBL2AFZatgAONgBdUtstB26SaOikfBgcAFc4YpT7AF8fLvsZqb5fGRl8ACVhqCg4HD4AbhNyQ2MGDbAYVGhzG18h-WvZ0gzUNQ0oc19JxxIYMAALUzgFk63Rgv2YaUoHz403MUjoP1+Jk8Fm01iisSk22YOGRMxAxRAEEw52gZGQoBIOHBAAVKQgySgSAA3CCoIL4kAAIxwJDAAGs4DAAMqYXn5ZBDUYE34wdDRADqv2ecDIorAcCF9OeqCZz30yHAZDJBM0ZG2MGpPOE6mQDF6ZoJACsyFQAEI8-mCoUkdBwAAymjgtvtYxAzqoQvysQAisMIPBg9EHSBRTgzTgDTB9JgVWAcKhiRzMPnYPK2aDkE0AAwE4sQM3ynmYA3FlXbJlBgkARzj8EtRIZIHIlS2cCCY45ODgPdQU8tJGtJETybN6FQy9DZCjcFj8aDSEloZgJE5ZaCFaQACYCUMNNF8gBhFg2lBQaCdkDDM0AFRPDLtSahkyowAJJQOOsBCnmBYwAAguBQpZrEG6TJMQA)
```sh
--parser typescript
--no-semi
--single-quote
```

**Input:**
```tsx
import * as execa from 'execa'
import go from './go'

const main = async () => {
    const exec = go(execa)
    const cmd = execa
    console.log(
      await exec('pingz', ['-c', '5', '8'], {
        all: true,
      })
    )
  }

  // Runner
;(async function() {
  try {
    await main()
  } catch (e) {
    throw e
  }
})().catch((e) => console.error(e))

```

**Output:**
```tsx
import * as execa from 'execa'
import go from './go'

const main = async () => {
    const exec = go(execa)
    const cmd = execa
    console.log(
      await exec('pingz', ['-c', '5', '8'], {
        all: true
      })
    )
  }

  // Runner
;(async function() {
  try {
    await main()
  } catch (e) {
    throw e
  }
})().catch(e => console.error(e))

```

**Expected behavior:**

Note that the main block is indented and so is the Runner comment. This only happens with the TypeScript parser.


Issue discussion:

Solution patch:
diff --git a/changelog_unreleased/typescript/pr-7094.md b/changelog_unreleased/typescript/pr-7094.md
new file mode 100644
index 000000000000..ee30e2bf486e
--- /dev/null
+++ b/changelog_unreleased/typescript/pr-7094.md
@@ -0,0 +1,26 @@
+#### Remove extra indentation for arrow function on variable declaration with comment([#7094](https://github.com/prettier/prettier/pull/7094) by [@sosukesuzuki](https://github.com/sosukesuzuki))
+
+<!-- prettier-ignore -->
+```ts
+// Input
+const foo = () => {
+  return;
+}
+
+// foo
+;
+
+// Prettier stable
+const foo = () => {
+    return;
+  };
+
+  // foo
+
+// Prettier master
+const foo = () => {
+  return;
+};
+
+// foo
+```
diff --git a/src/language-js/postprocess.js b/src/language-js/postprocess.js
index bf4a9d178282..4a262c85a551 100644
--- a/src/language-js/postprocess.js
+++ b/src/language-js/postprocess.js
@@ -1,7 +1,7 @@
 "use strict";
 
 const { getLast } = require("../common/util");
-const { composeLoc } = require("./loc");
+const { composeLoc, locEnd } = require("./loc");
 
 function postprocess(ast, options) {
   visitNode(ast, node => {
@@ -64,7 +64,7 @@ function postprocess(ast, options) {
     if (options.originalText[locEnd(toOverrideNode)] === ";") {
       return;
     }
-    if (options.parser === "flow") {
+    if (Array.isArray(toBeOverriddenNode.range)) {
       toBeOverriddenNode.range = [
         toBeOverriddenNode.range[0],
         toOverrideNode.range[1]
@@ -76,10 +76,6 @@ function postprocess(ast, options) {
       end: toBeOverriddenNode.loc.end
     });
   }
-
-  function locEnd(node) {
-    return options.parser === "flow" ? node.range[1] : node.end;
-  }
 }
 
 function visitNode(node, fn, parent, property) {


Test patch:
diff --git a/tests/typescript_arrow/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_arrow/__snapshots__/jsfmt.spec.js.snap
index 394c70a0b64d..837d1324ef5b 100644
--- a/tests/typescript_arrow/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/typescript_arrow/__snapshots__/jsfmt.spec.js.snap
@@ -40,3 +40,108 @@ app.get("/", (req, res): void => {
 
 ================================================================================
 `;
+
+exports[`arrow_regression.js 2`] = `
+====================================options=====================================
+parsers: ["typescript"]
+printWidth: 80
+semi: false
+                                                                                | printWidth
+=====================================input======================================
+const bar = (...varargs:any[]) => {
+  console.log(varargs);
+};
+
+const foo = (x:string):void => (
+  bar(
+    x,
+    () => {},
+    () => {}
+  )
+);
+
+app.get("/", (req, res): void => {
+  res.send("Hello world");
+});
+
+=====================================output=====================================
+const bar = (...varargs: any[]) => {
+  console.log(varargs)
+}
+
+const foo = (x: string): void =>
+  bar(
+    x,
+    () => {},
+    () => {}
+  )
+
+app.get("/", (req, res): void => {
+  res.send("Hello world")
+})
+
+================================================================================
+`;
+
+exports[`comments.js 1`] = `
+====================================options=====================================
+parsers: ["typescript"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+const fn1 = () => {
+  return;
+} /* foo */;
+
+const fn2 = () => {
+  return;
+}
+
+// foo
+;
+
+=====================================output=====================================
+const fn1 = () => {
+  return;
+}; /* foo */
+
+const fn2 = () => {
+  return;
+};
+
+// foo
+
+================================================================================
+`;
+
+exports[`comments.js 2`] = `
+====================================options=====================================
+parsers: ["typescript"]
+printWidth: 80
+semi: false
+                                                                                | printWidth
+=====================================input======================================
+const fn1 = () => {
+  return;
+} /* foo */;
+
+const fn2 = () => {
+  return;
+}
+
+// foo
+;
+
+=====================================output=====================================
+const fn1 = () => {
+  return
+} /* foo */
+
+const fn2 = () => {
+  return
+}
+
+// foo
+
+================================================================================
+`;
diff --git a/tests/typescript_arrow/comments.js b/tests/typescript_arrow/comments.js
new file mode 100644
index 000000000000..f4fc55cb10bb
--- /dev/null
+++ b/tests/typescript_arrow/comments.js
@@ -0,0 +1,10 @@
+const fn1 = () => {
+  return;
+} /* foo */;
+
+const fn2 = () => {
+  return;
+}
+
+// foo
+;
diff --git a/tests/typescript_arrow/jsfmt.spec.js b/tests/typescript_arrow/jsfmt.spec.js
index 2ea3bb6eb2e4..ba52aeb62efa 100644
--- a/tests/typescript_arrow/jsfmt.spec.js
+++ b/tests/typescript_arrow/jsfmt.spec.js
@@ -1,1 +1,2 @@
 run_spec(__dirname, ["typescript"]);
+run_spec(__dirname, ["typescript"], { semi: false });