Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Nested assignments #2416

Closed
Tracked by #2403
MichaReiser opened this issue Apr 13, 2022 · 6 comments · Fixed by #2728
Closed
Tracked by #2403

Nested assignments #2416

MichaReiser opened this issue Apr 13, 2022 · 6 comments · Fixed by #2728
Assignees
Labels
A-Formatter Area: formatter I-Difficult Implementation: requires deep knowledge of the tools and the problem.

Comments

@MichaReiser
Copy link
Contributor

MichaReiser commented Apr 13, 2022

Rome's formatter breaks nested assignments onto a new line compared to Prettier that only prints the right-hand side expression on a new line. Playground

Input

// Fits
bifornCringerMoshedPerplex = bifornCringerMoshedPerplexSawder = arrayO = "test"

// Exceeding:
// One assignment
bifornCringerMoshedPerplex.bifornCringerMoshedPerplexSawder.arrayOfNumber.m= "test"

// Two assignments
bifornCringerMoshedPerplex.bifornCringerMoshedPerplexSawder.arrayOfNumber = ab = "test"

// More than two assignments and exceeding: Each assignment on their own line
bifornCringerMoshedPerplex.bifornCringerMoshedPerplexSawder.array = p = ab = "tes"


bifornCringerMoshedPerplex = bifornCringerMoshedPerplexSawder = arrayOfNumb = a = "test"

Prettier

// Fits
bifornCringerMoshedPerplex = bifornCringerMoshedPerplexSawder = arrayO = "test";

// Exceeding:
// One assignment
bifornCringerMoshedPerplex.bifornCringerMoshedPerplexSawder.arrayOfNumber.m =
	"test";

// Two assignments
bifornCringerMoshedPerplex.bifornCringerMoshedPerplexSawder.arrayOfNumber = ab =
	"test";

// More than two assignments and exceeding: Each assignment on their own line
bifornCringerMoshedPerplex.bifornCringerMoshedPerplexSawder.array =
	p =
	ab =
		"tes";

bifornCringerMoshedPerplex =
	bifornCringerMoshedPerplexSawder =
	arrayOfNumb =
	a =
		"test";

Rome

// Fits
bifornCringerMoshedPerplex = bifornCringerMoshedPerplexSawder = arrayO = "test";

// Exceeding:
// One assignment
bifornCringerMoshedPerplex.bifornCringerMoshedPerplexSawder.arrayOfNumber.m =
	"test";

// Two assignments
bifornCringerMoshedPerplex.bifornCringerMoshedPerplexSawder.arrayOfNumber =
	ab = "test";

// More than two assignments and exceeding: Each assignment on their own line
bifornCringerMoshedPerplex.bifornCringerMoshedPerplexSawder.array =
	p = ab = "tes";

bifornCringerMoshedPerplex =
	bifornCringerMoshedPerplexSawder = arrayOfNumb = a = "test";

Expected

Rome to match Prettier's output.

I'm not entirely sure if I figured out Prettier's heuristic. It could make sense to study prettier's source code before tackling this issue to confirm. But my understanding is that Prettier:

  • Breaks between the assignment operator and the right-hand side expression
  • Tries to fit up to two assignments on the same line (except if all of them fit).
  • The right-hand side is indented one level deeper than the last assignment expression.
  • 3+ assignments and exceeding line width: Each assignment on its own line.
@ematipico
Copy link
Contributor

3+ assignments and exceeding line width: Each assignment on its own line.

And different indentation too 🤔

@MichaReiser
Copy link
Contributor Author

3+ assignments and exceeding line width: Each assignment on its own line.

And different indentation too thinking

All assignment targets have the same indention (except the first one). The right hand side is indented one level deeper.

@ematipico
Copy link
Contributor

ematipico commented Apr 13, 2022

Screenshot 2022-04-13 at 14 04 02

I see two level of indentation. Is it a github rendering issue? It's the assigner that gets one more level of indentation.

@MichaReiser
Copy link
Contributor Author

  • .array: First assignment: No indention
  • p & ab: Indented one level: These are all other assignment targets
  • "test": The value that gets assigned to the assignment targets. Indented one level deeper.

@ematipico ematipico added I-Normal Implementation: normal understanding of the tool and awareness I-Difficult Implementation: requires deep knowledge of the tools and the problem. and removed I-Normal Implementation: normal understanding of the tool and awareness labels May 5, 2022
@ematipico
Copy link
Contributor

This requires understanding Prettier's heuristic.

@ematipico
Copy link
Contributor

I will work on this one

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-Formatter Area: formatter I-Difficult Implementation: requires deep knowledge of the tools and the problem.
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants