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

refactor: fix typo "bloc" -> "block" #10418

Merged
merged 1 commit into from
Jul 27, 2024
Merged
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 @@ -91,7 +91,7 @@ private String cleanupLineBreaks(Indentation indentation, String content) {
}

private String replaceScripts(Indentation indentation, Scripts scripts, String content) {
return JsonAction.replace().blocName("scripts").jsonContent(content).indentation(indentation).entries(scriptEntries(scripts)).apply();
return JsonAction.replace().blockName("scripts").jsonContent(content).indentation(indentation).entries(scriptEntries(scripts)).apply();
}

private List<JsonEntry> scriptEntries(Scripts scripts) {
Expand All @@ -100,7 +100,7 @@ private List<JsonEntry> scriptEntries(Scripts scripts) {

private String replaceDevDependencies(Indentation indentation, PackageJsonDependencies devDependencies, String content) {
return JsonAction.replace()
.blocName("devDependencies")
.blockName("devDependencies")
.jsonContent(content)
.indentation(indentation)
.entries(dependenciesEntries(devDependencies))
Expand All @@ -109,7 +109,7 @@ private String replaceDevDependencies(Indentation indentation, PackageJsonDepend

private String removeDevDependencies(Indentation indentation, PackageJsonDependencies dependenciesToRemove, String content) {
return JsonAction.remove()
.blocName("devDependencies")
.blockName("devDependencies")
.jsonContent(content)
.indentation(indentation)
.entries(dependenciesEntries(dependenciesToRemove))
Expand All @@ -118,7 +118,7 @@ private String removeDevDependencies(Indentation indentation, PackageJsonDepende

private String replaceDependencies(Indentation indentation, PackageJsonDependencies dependencies, String content) {
return JsonAction.replace()
.blocName("dependencies")
.blockName("dependencies")
.jsonContent(content)
.indentation(indentation)
.entries(dependenciesEntries(dependencies))
Expand All @@ -127,15 +127,15 @@ private String replaceDependencies(Indentation indentation, PackageJsonDependenc

private String removeDependencies(Indentation indentation, PackageJsonDependencies dependenciesToRemove, String content) {
return JsonAction.remove()
.blocName("dependencies")
.blockName("dependencies")
.jsonContent(content)
.indentation(indentation)
.entries(dependenciesEntries(dependenciesToRemove))
.apply();
}

private String replaceType(Indentation indentation, PackageJsonType packageJsonType, String content) {
return JsonAction.replace().blocName("type").jsonContent(content).indentation(indentation).blocValue(packageJsonType.type()).apply();
return JsonAction.replace().blockName("type").jsonContent(content).indentation(indentation).blockValue(packageJsonType.type()).apply();
}

private List<JsonEntry> dependenciesEntries(PackageJsonDependencies devDependencies) {
Expand Down Expand Up @@ -167,20 +167,20 @@ private void write(Path file, String content) {

private static final class JsonAction {

private final String blocName;
private final String blockName;
private final String jsonContent;
private final Indentation indentation;
private final Collection<JsonEntry> entries;
private final JsonActionType action;
private final String blocValue;
private final String blockValue;

private JsonAction(JsonActionBuilder builder) {
blocName = builder.blocName;
blockName = builder.blockName;
jsonContent = builder.jsonContent;
indentation = builder.indentation;
entries = builder.entries;
action = builder.action;
blocValue = builder.blocValue;
blockValue = builder.blockValue;
}

public static JsonActionBuilder replace() {
Expand All @@ -194,7 +194,7 @@ public static JsonActionBuilder remove() {
public String handle() {
Assert.notNull("action", action);

if (blocValue != null) {
if (blockValue != null) {
return appendNewRootEntry(jsonContent);
}

Expand All @@ -211,10 +211,10 @@ public String handle() {
private String replaceEntries() {
String result = removeExistingEntries();

Matcher blocMatcher = buildBlocMatcher(result);
Matcher blockMatcher = buildBlockMatcher(result);

if (blocMatcher.find()) {
result = appendEntries(blocMatcher);
if (blockMatcher.find()) {
result = appendEntries(blockMatcher);
} else {
result = appendNewBlock(result);
}
Expand All @@ -226,68 +226,68 @@ private String removeEntries() {
return removeExistingEntries();
}

private String appendEntries(Matcher blocMatcher) {
return blocMatcher.replaceFirst(match -> {
private String appendEntries(Matcher blockMatcher) {
return blockMatcher.replaceFirst(match -> {
StringBuilder result = new StringBuilder().append(match.group(1)).append(LINE_BREAK);

result.append(entriesBloc(indentation, entries));
result.append(entriesBlock(indentation, entries));

result.append(LINE_END);
return result.toString();
});
}

private String appendNewRootEntry(String result) {
String jsonBloc = new StringBuilder()
String jsonBlock = new StringBuilder()
.append(LINE_SEPARATOR)
.append(indentation.spaces())
.append(QUOTE)
.append(blocName)
.append(blockName)
.append(QUOTE)
.append(": ")
.append(QUOTE)
.append(blocValue)
.append(blockValue)
.append(QUOTE)
.toString();

return result.replaceFirst("(\\s{1,10})}(\\s{1,10})$", jsonBloc + "$1}$2");
return result.replaceFirst("(\\s{1,10})}(\\s{1,10})$", jsonBlock + "$1}$2");
}

private String appendNewBlock(String result) {
String jsonBloc = new StringBuilder()
String jsonBlock = new StringBuilder()
.append(LINE_SEPARATOR)
.append(indentation.spaces())
.append(QUOTE)
.append(blocName)
.append(blockName)
.append(QUOTE)
.append(": {")
.append(LINE_BREAK)
.append(entriesBloc(indentation, entries))
.append(entriesBlock(indentation, entries))
.append(LINE_BREAK)
.append(indentation.spaces())
.append("}")
.toString();

return result.replaceFirst("(\\s{1,10})}(\\s{1,10})$", jsonBloc + "$1}$2");
return result.replaceFirst("(\\s{1,10})}(\\s{1,10})$", jsonBlock + "$1}$2");
}

private Matcher buildBlocMatcher(String result) {
return Pattern.compile("(\"" + blocName + "\"\\s*:\\s*\\{)").matcher(result);
private Matcher buildBlockMatcher(String result) {
return Pattern.compile("(\"" + blockName + "\"\\s*:\\s*\\{)").matcher(result);
}

@ExcludeFromGeneratedCodeCoverage(reason = "Combiner can't be tested and an implementation detail")
private String removeExistingEntries() {
return entries
.stream()
.map(toEntryPattern(blocName, indentation))
.map(toEntryPattern(blockName, indentation))
.reduce(jsonContent, (json, entryPattern) -> entryPattern.matcher(json).replaceAll("$1$2"), (first, second) -> first);
}

private Function<JsonEntry, Pattern> toEntryPattern(String blocName, Indentation indentation) {
private Function<JsonEntry, Pattern> toEntryPattern(String blockName, Indentation indentation) {
return entry -> {
String pattern = new StringBuilder()
.append("(\"")
.append(blocName)
.append(blockName)
.append("\"\\s*:\\s*\\{)([^}]*)(\"")
.append(entry.key())
.append("\"\\s*:\\s*\"[^\\r\\n]+[\\r\\n]{1,2}(\\s{")
Expand All @@ -299,25 +299,25 @@ private Function<JsonEntry, Pattern> toEntryPattern(String blocName, Indentation
};
}

private String entriesBloc(Indentation indentation, Collection<JsonEntry> entries) {
private String entriesBlock(Indentation indentation, Collection<JsonEntry> entries) {
return entries.stream().map(entry -> entry.toJson(indentation)).collect(Collectors.joining(LINE_SEPARATOR));
}

private static final class JsonActionBuilder {

private String blocName;
private String blockName;
private String jsonContent;
private Indentation indentation;
private Collection<JsonEntry> entries;
private final JsonActionType action;
private String blocValue;
private String blockValue;

private JsonActionBuilder(JsonActionType action) {
this.action = action;
}

private JsonActionBuilder blocName(String blocName) {
this.blocName = blocName;
private JsonActionBuilder blockName(String blockName) {
this.blockName = blockName;

return this;
}
Expand All @@ -344,8 +344,8 @@ private String apply() {
return new JsonAction(this).handle();
}

public JsonActionBuilder blocValue(String blocValue) {
this.blocValue = blocValue;
public JsonActionBuilder blockValue(String blockValue) {
this.blockValue = blockValue;

return this;
}
Expand Down