From 58b0fe07160880f3f711d1c717ef7fb44fcac98f Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sun, 28 Mar 2021 14:03:41 -0500 Subject: [PATCH] Add Tell Prettier To Not Format A Statement as a javascript til --- README.md | 3 +- ...tell-prettier-to-not-format-a-statement.md | 43 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 javascript/tell-prettier-to-not-format-a-statement.md diff --git a/README.md b/README.md index 3c7d4ab..8d15ed2 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud). -_1101 TILs and counting..._ +_1102 TILs and counting..._ --- @@ -407,6 +407,7 @@ _1101 TILs and counting..._ - [Start Node Process In Specific Timezone](javascript/start-node-process-in-specific-timezone.md) - [String Interpolation With Template Literals](javascript/string-interpolation-with-template-literals.md) - [Support Nested Matching In Custom Jest Matchers](javascript/support-nested-matching-in-custom-jest-matchers.md) +- [Tell Prettier To Not Format A Statement](javascript/tell-prettier-to-not-format-a-statement.md) - [Test Coverage Stats With Jest](javascript/test-coverage-stats-with-jest.md) - [Test Timing-Based Code With Jest Fake Timers](javascript/test-timing-based-code-with-jest-fake-timers.md) - [The Comma Operator](javascript/the-comma-operator.md) diff --git a/javascript/tell-prettier-to-not-format-a-statement.md b/javascript/tell-prettier-to-not-format-a-statement.md new file mode 100644 index 0000000..82c214b --- /dev/null +++ b/javascript/tell-prettier-to-not-format-a-statement.md @@ -0,0 +1,43 @@ +# Tell Prettier To Not Format A Statement + +[Prettier](https://prettier.io/) is a boon to productivity because individuals +and teams don't have to make any decisions about the fine details of how their +code is formatted. Generally, let `prettier` do its thing. + +There are some situations where you want to preserve your own formatting, +especially if it improves readability. + +Here is some `prettier` formatted code: + +```javascript +const relativeCoords = { + A: [xPos - 1, yPos - 1], + B: [xPos, yPos - 1], + C: [xPos + 1, yPos - 1], + D: [xPos - 1, yPos], + E: [xPos + 1, yPos], + F: [xPos - 1, yPos + 1], + G: [xPos, yPos + 1], + H: [xPos + 1, yPos + 1], +}; +``` + +Originally, I included some whitespace to keep things visually aligned. If I +include a `prettier-ignore` comment, the statement immediately following it +will not be touched by prettier. + +```javascript +// prettier-ignore +const relativeCoords = { + A: [xPos - 1, yPos - 1], + B: [xPos , yPos - 1], + C: [xPos + 1, yPos - 1], + D: [xPos - 1, yPos ], + E: [xPos + 1, yPos ], + F: [xPos - 1, yPos + 1], + G: [xPos , yPos + 1], + H: [xPos + 1, yPos + 1], +}; +``` + +[source](https://prettier.io/docs/en/ignore.html#javascript)