Parentheses are automatically wrapped around the implicit return of arrow functions

Currently, I am utilizing Visual Studio Code along with Prettier, and I have noticed that the function:

(token: string) => this.token = token

is being transformed into:

(token: string) => (this.token = token)

This modification seems to decrease readability. Is there a method to avoid this?

Answer №1

The reason for this issue is the no-return-assign rule. You can find more information about it here.

Surprisingly, your arrow function actually translates to

(token: string) => {return this.token = token}

Even though it may not seem apparent at first glance, there is indeed a return statement included due to the assignment operation.

There are only two options with this rule - either allow when parentheses are present or disallow completely.

To address the "readability issue," consider using curly braces or potentially disabling the rule (although this is not recommended).

Answer №2

If you're waiting for a more open-minded code formatter to come around, here's a workaround (let me know if one already exists).

First, locate and open the following file:

  • Windows:
    %USERPROFILE%\.vscode\extensions\esbenp.prettier-vscode-5.8.0\node_modules\prettier\index.js
  • macOS and Linux:
    ~/.vscode/extensions/esbenp.prettier-vscode-5.8.0/node_modules/prettier/index.js

(where %USERPROFILE% typically represents C:\Users\YourUsernameHere\)

Next, search for

case "AssignmentExpression":
(there are several instances) until you find this section of code. Change the displayed return true to return false. I located this on line 41587, but remember that it may shift in future Prettier versions, so rely on Ctrl + F to navigate.

After saving the file, restart VSCode and witness the magic.

I noticed that meddling with the internal workings of VSCode on macOS might trigger some undesired responses. Look for a settings icon in any error messages, choose to ignore the error, then restart VSCode for normal functionality.

Answer №3

To customize your configuration, consider adding a config file in formats like JSON, JavaScript, YAML, and more. This link might offer assistance:

When working with Arrow Functions, it's recommended to include "arrowParens": "aviod" in your .prettierrc file if you're utilizing a JSON schema to prevent automatic addition of parenthesis.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring SVG Graphics, Images, and Icons with Nativescript-vue

Can images and icons in SVG format be used, and if so, how can they be implemented? I am currently utilizing NativeScript-Vue 6.0 with TypeScript. ...

I continue to encounter the same error while attempting to deliver data to this form

Encountering an error that says: TypeError: Cannot read properties of null (reading 'persist') useEffect(() => { if (edit) { console.log(item) setValues(item!); } document.body.style.overflow = showModal ? "hidden ...

Adding a row with sorting order in AG Grid explained

Recently, I encountered an issue where rows added to the ag grid were not being sorted and instead always appeared at the bottom of the grid. I am looking to add rows with sorting order. Here is an example of what is currently happening: Actual results: ...

In what way can a piped operator in rxjs be influenced by the value returned by a subsequent operator?

When attempting to examine values between operators in an rxjs pipe, I decided to use tap to log them to the console. I added two taps, one before a map operator used for sorting an Array, and one after. Surprisingly, both taps logged the same sorted Arra ...

Retrieve the individuals within the delimiter

Looking for a solution to modify the characters within square brackets in a given string. For instance, I have a string that looks like "[A] [B] this is my [C] string". How can I update these bracketed characters by adding or removing brackets? ...

Acessing files from Azure Blob within the Aurelia UI: Download or View now!

I currently have my files stored in Azure and I am looking for a way to either download or view them on the client side. This is how I envision the process: Azure -> Api -> Client UI (Aurelia) While I have come across several C# examples, I am unsure of ...

Looping inside a loop with Angular's ngFor

I am working on an Angular + Firebase app. Within one of my components, I am retrieving elements from the Firebase DB and binding them into a template using *ngFor: <div *ngFor="let comment of (comments | async)> <div>{{ comment.text }}< ...

Resetting forms in Angular 5: What you need to know

When a user submits a current value, I am working quickly to provide them with new value. However, after resetting the form and serving the new value, there seems to be an issue where the form reset execution is asynchronous, resulting in the user receivin ...

Is it possible to create a click event for a mat-icon within a mat form field (either in the matprefix or matsuffix)?

I am working with a mat-form-field and have incorporated a custom mat-icon within it as a matprefix. However, I am now looking to create a click method for that matprefix icon. Despite my attempts to write a click method, it does not seem to be functioning ...

There is an issue with the type candidate in the Notion API, resulting in

In this instance, the troublesome code causing issues is displayed below: import {Client, LogLevel} from "@notionhq/client"; const notion = new Client({ auth: process.env.NOTION_TOKEN, logLevel: process.env.NODE_ENV !== 'product ...

A guide on transferring files to a PHP server using HttpClient and FormData within an Ionic framework, along with instructions on receiving files in PHP

I have an input file in mypage.html on Ionic and I want to send this file to PHP for uploading it onto the server. In mypage.page.html, I have created an input field for the file name and another input field for the file to be uploaded. <ion-header> ...

What is the best way to shift focus to the next input field once the character limit has been reached, especially when the input is contained

My challenge lies in having six input fields arranged side by side in a single row: In my component.html file: onDigitInput(event: any) { let element; if (event.code !== 'Backspace') element = event.srcElement.nextElementSibling; consol ...

A guide on capturing the text content of the error message from the <mat-error> element with Protractor

I have encountered an element that is giving me trouble in retrieving the error message text into a variable. <mat-error _ngcontent-c16="" class="mat-error ng-star-inserted" id="error-email-required" role="alert" ...

The 'undefined' type cannot be assigned to the 'never' type

interface A { name?: string age: number } var a: A = { name: '', age: 23 } var result:A = (Object.keys(a) as Array<keyof A>).reduce((prev, key) => { if (a[key] || a[key] === 0) { prev[key] = a[key] // an error was reporte ...

Struggling with Primeng's KeyFilter functionality?

I've implemented the KeyFilter Module of primeng in my project. Check out the code snippet below: <input type="text" pInputText [(ngModel)]="price.TintCost" [pKeyFilter]="patternDecimal" name="tintCost" required="true" /> Take a look at my Typ ...

Back buttons in Ionic V5 are failing to navigate back to the previous page

I am currently in the process of setting up my ionic application which consists of multiple pages. For navigation, I am using RouterModule from @angular/router. import { NgModule } from '@angular/core'; import { PreloadAllModules, RouterModule, ...

minimize the size of the image within a popup window

I encountered an issue with the react-popup component I am using. When I add an image to the popup, it stretches to full width and length. How can I resize the image? export default () => ( <Popup trigger={<Button className="button" ...

The specified type 'Observable<{}' cannot be assigned to the type 'Observable<HttpEvent<any>>'

After successfully migrating from angular 4 to angular 5, I encountered an error in my interceptor related to token refreshing. The code snippet below showcases how I intercept all requests and handle token refreshing upon receiving a 401 error: import { ...

What is the process for attaching a function to an object?

Here is the complete code: export interface IButton { click: Function; settings?: IButtonSettings; } abstract class Button implements IButton { click() {} } class ButtonReset extends Button { super() } The component looks like this: expor ...

Unable to access current props within useEffect block

When I use useEffect with the parameter props.quizStep, my function fn (which is a keydown event listener) is unable to access the current value of props.quizStep. I'm puzzled as to why it's not working properly. Can you help me understand? Bel ...