Skip to content

Prettier misplaces JSDoc of paren-wrapped Function Expressions! #5850

@SMotaal

Description

@SMotaal

Prettier 1.16.4
Playground link

--no-bracket-spacing
--jsx-bracket-same-line
--parser typescript
--print-width 120
--single-quote
--trailing-comma all
--use-tabs

Input:

Broken: {
  // [X] Wrap IIFE function expression in parens 
  // [ ] Keep JSDoc with function expression in parens
  
  /** @type {Helpers} */
  const helpers = (
  	/** @param {Partial<Helpers>} helpers */
  	(helpers = {}) => helpers
  )();
}

Hack1: {
  // [X] Unwrap function expression in parens inside lists
  // [X] Keep comment blocks inside lists
  
  /** @type {Helpers} */
  const helpers = (
    void '',
    // TODO: await prettier fix
    (
    	/** @param {Partial<Helpers>} helpers */
    	(helpers = {}) => helpers
    )
  )();
}

Hack2: {
  // [X] Keep comment blocks inside lists
  
  /** @type {Helpers} */
  const helpers = [
    // TODO: await prettier fix
    /** @param {Partial<Helpers>} helpers */
    (helpers = {}) => helpers
  ][0]();
}

Output:

Broken: {
	// [X] Wrap IIFE function expression in parens
	// [ ] Keep JSDoc with function expression in parens

	/** @type {Helpers} */
	const helpers = /** @param {Partial<Helpers>} helpers */
	((helpers = {}) => helpers)();
}

Hack1: {
	// [X] Unwrap function expression in parens inside lists
	// [X] Keep comment blocks inside lists

	/** @type {Helpers} */
	const helpers = (void '',
	// TODO: await prettier fix
	/** @param {Partial<Helpers>} helpers */
	(helpers = {}) => helpers)();
}

Hack2: {
	// [X] Keep comment blocks inside lists

	/** @type {Helpers} */
	const helpers = [
		// TODO: await prettier fix
		/** @param {Partial<Helpers>} helpers */
		(helpers = {}) => helpers,
	][0]();
}

Expected behavior:

  • Wrap IIFE function expression in parens:

    `const ƒ = () => {}()` -> `const ƒ = (() => {})()`
    
  • Keep JSDoc with function expression in parens:

    `const ƒ = /** … */() => {}()` -> `const ƒ = (/** … */() => {})()`
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:commentsIssues with how Prettier prints commentshelp wantedWe're a small group who can't get to every issue promptly. We’d appreciate help fixing this issue!lang:javascriptIssues affecting JSlocked-due-to-inactivityPlease open a new issue and fill out the template instead of commenting.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions