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

x/tools/gopls: simplify interpreted string literals by converting to raw string literals #71480

Closed
merrickclay opened this issue Jan 30, 2025 · 5 comments
Labels
gopls Issues related to the Go language server, gopls. Proposal ToolProposal Issues describing a requested change to a Go tool or command-line program. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@merrickclay
Copy link
Contributor

merrickclay commented Jan 30, 2025

Proposal Details

Proposal

Interpreted string literals with escaped double quotes and/or backslashes unnecessarily reduce readability. I propose an analyzer be added to gopls that simplifies such string literals by converting them to raw string literals.

Example

a := "{\"name\":\"Merrick\"}"
b := "these \\ are \\ backslashes \\"
c := "this \\ has \" double quotes \\ and \"  backslashes \\"
d := "\"John\" likes dogs\n"
e := "{\"name\":\"```Merrick```\"}"

Suggested fixes:

a := `{"name":"Merrick"}`
b := `these \ are \ backslashes \`
c := `this \ has " double quotes \ and " backslashes \`
d := "\"John\" likes dogs\n" // not changed because it contains an escape other than backslash and double quotes
e := "{\"name\":\"```Merrick```\"}" // not changed because it contains backticks
@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Jan 30, 2025
@gopherbot gopherbot added this to the Unreleased milestone Jan 30, 2025
@merrickclay merrickclay changed the title x/tools/gopls: simplify interpreted string literals using by converting to raw string literals x/tools/gopls: simplify interpreted string literals by converting to raw string literals Jan 30, 2025
@gabyhelp
Copy link

Related Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@gabyhelp gabyhelp added the ToolProposal Issues describing a requested change to a Go tool or command-line program. label Jan 30, 2025
@findleyr
Copy link
Member

@merrickclay gopls already supports this: when querying code actions inside an interpreted literal, you should have an option to convert to a raw string literal, and vice-versa.

(this was #51200)

Let me know if you have any ideas to make this more discoverable.

@merrickclay
Copy link
Contributor Author

This proposal is to make this analysis run without any interaction from the developer and provide suggestions, like the simplify ranges analyzer does.

@findleyr
Copy link
Member

findleyr commented Jan 30, 2025

CC @adonovan

We have been talking about which code actions should be presented as quick fixes.

One of the heuristics we discussed about was that the quick fix should always improve the code. In this case, that does not apply, because there will always be a code action to translate format.

The other heuristics we discussed were:

  • Frequency: this should be an action that the user frequently wants to apply.
  • Accuracy: the fix always succeeds.

This DOES meet those heuristics.

@adonovan
Copy link
Member

Interpreted string literals with escaped double quotes and/or backslashes unnecessarily reduce readability.

This is less a fact than an opinion, and I suspect a minority one at that. One could equally argue that double quotes permit escape sequences, making it easier and more concise to denote tabs, newlines, quotation marks, and all kinds of things that are difficult or even impossible to express in a raw string literal. In any case, double-quoted strings are the normal way to write string literals in Go, and that isn't something we could change at this point even if we wanted to.

I agree with @findleyr that the double-quote-to-backquote transformation doesn't make sense as a quick fix because it doesn't unequivocally improve the code, as evidenced by the fact that gopls offers the inverse operation too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls Issues related to the Go language server, gopls. Proposal ToolProposal Issues describing a requested change to a Go tool or command-line program. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

5 participants