Skip to content

Conversation

ghost
Copy link

@ghost ghost commented Mar 13, 2018

Alternative to #22517 -- there are no remaining uses where we need to adjust an end position with replaceNode, so make that the hardcoded behavior. In some cases we were adjusting the end, then adding { suffix: newLineCharacter }, which means we remove and then replace the newline.

@ghost ghost requested a review from amcasey March 13, 2018 22:08
@ghost ghost force-pushed the replaceNode_end branch from 67496a0 to 3137cc4 Compare March 13, 2018 22:46
@amcasey
Copy link
Member

amcasey commented Mar 14, 2018

I believe the adjusted end position includes trailing comments, not just the newline. Also, why would the start and the end not be symmetric?

@ghost
Copy link
Author

ghost commented Mar 14, 2018

Yeah, looks like we should never be adjusting the start position either.
Basically, when replacing a node we should replace just the node's content -- not the text surrounding it.

* If pos\end should be interpreted literally 'useNonAdjustedStartPosition' or 'useNonAdjustedEndPosition' should be set to true
*/
export type ConfigurableStartEnd = ConfigurableStart & ConfigurableEnd;
export interface ConfigurableStartEnd extends ConfigurableStart, ConfigurableEnd {}
Copy link
Author

Choose a reason for hiding this comment

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

Converting to an interface because of #19296

@ghost
Copy link
Author

ghost commented Mar 20, 2018

@amcasey Could you re-review?

@amcasey
Copy link
Member

amcasey commented Mar 20, 2018

This change only partially resolves #21246, correct?

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.

Personally, I think this change feels incomplete - it's neither a targeted fix nor a complete revision.

const pos = getAdjustedStartPosition(sourceFile, oldNode, options, Position.Start);
const end = getAdjustedEndPosition(sourceFile, oldNode, options);
return this.replaceRange(sourceFile, { pos, end }, newNode, options);
public replaceNode(sourceFile: SourceFile, oldNode: Node, newNode: Node, options: InsertNodeOptions = {}) {
Copy link
Member

Choose a reason for hiding this comment

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

I was only expecting the default to change but this looks like it makes the alternative (i.e. adjusting start and end positions) impossible. Was that intentional?

Copy link
Member

Choose a reason for hiding this comment

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

Also it seems strange to update this signature without making corresponding changes to (e.g.) replaceNodeRange.

Copy link
Author

Choose a reason for hiding this comment

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

I don't think we're likely to have any reason to replace the a node's comments while replacing it -- if we do end up needing that, we can add it back.

Copy link
Author

Choose a reason for hiding this comment

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

Updated replaceNodeRange

Copy link
Member

Choose a reason for hiding this comment

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

I guess I think of replace(Node | Range | NodeRange)(WithNodes)? as a family of related functions that should have parallel signatures. Notice that the WithNodes variants already default to using non-adjusted positions, which should make things easier.

Copy link
Author

Choose a reason for hiding this comment

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

Added the option back where it makes sense, but not for replaceRange which should only accept InsertNodeOptions -- it doesn't adjust pos and end since it accepts a range.

Copy link
Author

Choose a reason for hiding this comment

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

Created #22813 which will be easier to review independently, then can merge into this.

this.replaceRange(sourceFile, { pos, end: pos }, createToken(modifier), { suffix: " " });
}

public insertCommentBeforeLine(sourceFile: SourceFile, lineNumber: number, position: number, commentText: string): void {
Copy link
Member

Choose a reason for hiding this comment

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

I'm surprised that we need to compute line numbers for this operation (I would have guessed, perhaps incorrectly, that that was expensive), but I see that this is just a refactoring.

Copy link
Author

Choose a reason for hiding this comment

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

It could probably be done without the line numbers by counting back to the beginning of the line. Though, getting the line number is just a binary search given that sourceFile.lineMap is cached.

@ghost ghost force-pushed the replaceNode_end branch 2 times, most recently from 65184f1 to a6be0a6 Compare March 26, 2018 15:18
@ghost ghost force-pushed the replaceNode_end branch from a6be0a6 to 558e9a5 Compare March 26, 2018 17:12
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.

Thanks for cleaning this up!

@ghost ghost merged commit ea6740f into master Mar 27, 2018
@ghost ghost deleted the replaceNode_end branch March 27, 2018 17:34
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 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.

1 participant