From fa723deee29a3ee21d5bec5210c688c7d499a408 Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Fri, 6 Dec 2019 18:18:38 +0900 Subject: [PATCH] TypeScript: remove extra indentation for arrow function on variable declaration with comment (#7094) * Remove extra indent * add changelog * Fix postprocess for range of TypeScript * Remove locend from postprocess * Add test for no-smei * Remove hasLeadingComment --- changelog_unreleased/typescript/pr-7094.md | 26 +++++ src/language-js/postprocess.js | 8 +- .../__snapshots__/jsfmt.spec.js.snap | 105 ++++++++++++++++++ tests/typescript_arrow/comments.js | 10 ++ tests/typescript_arrow/jsfmt.spec.js | 1 + 5 files changed, 144 insertions(+), 6 deletions(-) create mode 100644 changelog_unreleased/typescript/pr-7094.md create mode 100644 tests/typescript_arrow/comments.js diff --git a/changelog_unreleased/typescript/pr-7094.md b/changelog_unreleased/typescript/pr-7094.md new file mode 100644 index 000000000..ee30e2bf4 --- /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)) + + +```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 5828ae3fe..e2daf1396 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 => { @@ -48,7 +48,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] @@ -60,10 +60,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) { diff --git a/tests/typescript_arrow/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_arrow/__snapshots__/jsfmt.spec.js.snap index 394c70a0b..837d1324e 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 000000000..f4fc55cb1 --- /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 2ea3bb6eb..ba52aeb62 100644 --- a/tests/typescript_arrow/jsfmt.spec.js +++ b/tests/typescript_arrow/jsfmt.spec.js @@ -1 +1,2 @@ run_spec(__dirname, ["typescript"]); +run_spec(__dirname, ["typescript"], { semi: false });