Skip to content

feat: adds suggestions to QuoteSpacing#3724

Open
hippietrail wants to merge 6 commits into
Automattic:masterfrom
hippietrail:quote-spacing-suggestions
Open

feat: adds suggestions to QuoteSpacing#3724
hippietrail wants to merge 6 commits into
Automattic:masterfrom
hippietrail:quote-spacing-suggestions

Conversation

@hippietrail

@hippietrail hippietrail commented Jun 28, 2026

Copy link
Copy Markdown
Collaborator

Issues

N/A

Description

I noticed in an old GitHub comment of mine that I'd made a typo missing a space next to a quote and Harper was flagging it.

But it didn't suggest a fix.

This PR suggests both fixes, a space before the quote and a space after the quote.

I used an AI agent when I realized my naïve attempt was the wrong way to count " tokens to decide whether to suggest inserting the space before or after should be the first choice.

I also used more AI than I prefer to help refactor the assertion functions to be able to add a new one to test that the suggestion you want to be the first one actually is the first one.

I've gone through every line of the code the AI made and I'm pretty sure there's nothing I don't understand.

How Has This Been Tested?

AI Disclosure

  • I am a human and didn't use any AI.
  • I used LLM features of my editor, but not an agent.
  • I used an AI agent interactively.
  • I am an agent or I got an agent to do the work autonomously.

If Your PR Implements or Enhances a Linter

  • I made up the sentences in the unit tests.
  • The sentences in the unit tests were generated by an AI.
  • I'm using examples from the bug report / feature request.
  • I collected real-world sentences for the unit tests.

Checklist

  • I have performed a self-review of my own code
  • I have added tests to cover my changes
  • I have considered splitting this into smaller pull requests.

Comment on lines +39 to +53
// Count quotes before this one to determine if it's opening (even) or closing (odd)
let quote_index = source[..quote_span.start]
.iter()
.filter(|&&c| c == '"')
.count();

let mut suggestions = vec![
super::Suggestion::ReplaceWith(vec![' ', '"']),
super::Suggestion::ReplaceWith(vec!['"', ' ']),
];

// Every odd-numbered quote is likely a close quote, ie space likely should be after it.
if quote_index % 2 == 1 {
suggestions.swap(0, 1);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You should not need to do this. Quote tokens include this information already using a pretty reliable algorithm.

Comment on lines +102 to +108
fn fix_by_inserting_space_after() {
assert_suggestion_result(
"It's not a complete sentence since it lacks a verb but it would be valid as a title or answer to a question since \"dog runs\"are things which exist",
QuoteSpacing::default(),
"It's not a complete sentence since it lacks a verb but it would be valid as a title or answer to a question since \"dog runs\" are things which exist",
);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice! I will want to fuzz it as well. This is a pretty high-profile rule.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants