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

SLCORE-1144 Fix SSF-699 #1230

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,24 @@
*/
package org.sonarsource.sonarlint.core.commons.util;

import javax.annotation.Nullable;

public class StringUtils {

private static final char RTLO = '\u202E';

public static String pluralize(long count, String word) {
var pluralizedWord = count == 1 || count == 0 ? word : (word + 's');
var pluralizedWord = count == 1 ? word : (word + 's');
return count + " " + pluralizedWord;
}

public static String sanitizeAgainstRTLO(@Nullable String input) {
if (input == null) {
return null;
}
return input.replaceAll(String.valueOf(RTLO), "");
}

private StringUtils() {
// utility class
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,28 @@
package org.sonarsource.sonarlint.core.commons;

import org.junit.jupiter.api.Test;
import org.sonarsource.sonarlint.core.commons.util.StringUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.sonarsource.sonarlint.core.commons.util.StringUtils.pluralize;
import static org.sonarsource.sonarlint.core.commons.util.StringUtils.sanitizeAgainstRTLO;

class StringUtilsTests {

@Test
void should_pluralize_words() {
assertThat(StringUtils.pluralize(0, "word")).isEqualTo("0 word");
assertThat(StringUtils.pluralize(1, "word")).isEqualTo("1 word");
assertThat(StringUtils.pluralize(2, "word")).isEqualTo("2 words");
assertThat(pluralize(0, "word")).isEqualTo("0 words");
assertThat(pluralize(1, "word")).isEqualTo("1 word");
assertThat(pluralize(2, "word")).isEqualTo("2 words");
}

@Test
void should_sanitize_against_rtlo() {
assertThat(sanitizeAgainstRTLO("This is a \u202eegassem")).isEqualTo("This is a egassem");
}

@Test
void should_sanitize_with_null() {
assertThat(sanitizeAgainstRTLO(null)).isNull();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@

import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import static org.sonarsource.sonarlint.core.commons.util.StringUtils.sanitizeAgainstRTLO;

public class ShowFixSuggestionRequestHandler implements HttpRequestHandler {

Expand Down Expand Up @@ -333,6 +334,12 @@ public boolean isValid() {
@VisibleForTesting
public record ChangesPayload(TextRangePayload beforeLineRange, String before, String after) {

public ChangesPayload(TextRangePayload beforeLineRange, String before, String after) {
this.beforeLineRange = beforeLineRange;
this.before = sanitizeAgainstRTLO(before);
this.after = sanitizeAgainstRTLO(after);
}

public boolean isValid() {
return beforeLineRange.isValid();
}
Expand Down