Skip to content
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

Merged
2 commits merged into from
Apr 26, 2018

Conversation

ghost
Copy link

@ghost ghost commented Apr 11, 2018

Fixes #23333
Review #23343 before reviewing this

@ghost ghost requested a review from amcasey April 11, 2018 20:16
@ghost ghost force-pushed the insertNodeAtClassStart branch from 44173aa to 37d21b5 Compare April 11, 2018 20:27
Copy link
Contributor

@mhegazy mhegazy left a 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.

Copy link
Member

@amcasey amcasey left a 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 {
Copy link
Member

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?

Copy link
Author

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'
Copy link
Member

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.

Copy link
Author

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)
Copy link
Member

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?

Copy link
Author

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Why? I like having a space there.
  2. Shouldn't this be handled by the formatter (or at least consume the formatting options)?

Copy link
Author

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.

@ghost
Copy link
Author

ghost commented Apr 16, 2018

@amcasey Could you update your review?

@ghost ghost merged commit aa10243 into master Apr 26, 2018
@ghost ghost deleted the insertNodeAtClassStart branch April 26, 2018 15:00
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants