-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix insertNodeAtClassStart
for empty class with comment
#23342
Conversation
44173aa
to
37d21b5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@amcasey can you please review as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor concerns.
export function addToSeen(seen: Map<true>, key: string | number): boolean { | ||
export function addToSeen(seen: Map<true>, key: string | number): boolean; | ||
export function addToSeen<T>(seen: Map<T>, key: string | number, value: T): boolean; | ||
export function addToSeen<T>(seen: Map<T>, key: string | number, value: T = true as any): boolean { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For my own edification, why not use {}
as the default value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been using Map<true>
everywhere for sets; to me Map<{}>
would imply actually being a map from string
to something, where we didn't specify that something.
if (addToSeen(this.classesWithNodesInsertedAtStart, getNodeId(cls), cls)) { | ||
prefix = this.newLineCharacter; | ||
// For `class C {\n}`, don't add the trailing "\n" | ||
if (cls.members.length === 0 && !(positionsAreOnSameLine as any)(...getClassBraceEnds(cls, sourceFile), sourceFile)) { // TODO: GH#4130 remove 'as any' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the number of members matter? It seems like you want to omit the linebreak as long as the class is on a single line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the opposite -- if the class is on a single line I want to add line breaks on both sides to get {\n member\n}
; if the class is already multiline I only want the first line break.
} | ||
|
||
const indentation = formatting.SmartIndenter.findFirstNonWhitespaceColumn(getLineStartPositionForPosition(clsStart, sourceFile), clsStart, sourceFile, this.formatContext.options) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the indentation be omitted if the class is on a single line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's about to be multiline since we're adding a member, and that new member should be indented.
this.classesWithNodesInsertedAtStart.forEach(cls => { | ||
const sourceFile = cls.getSourceFile(); | ||
const [openBraceEnd, closeBraceEnd] = getClassBraceEnds(cls, sourceFile); | ||
// For `class C { }` remove the whitespace inside the braces. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Why? I like having a space there.
- Shouldn't this be handled by the formatter (or at least consume the formatting options)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leaving the space would result in the class ending in \n }
instead of just \n}
The formatter won't handle this since it only formats the new nodes we're adding, not the existing whitespace surrounding them.
@amcasey Could you update your review? |
Fixes #23333
Review #23343 before reviewing this