Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into 1480-speedup-npm-in…
Browse files Browse the repository at this point in the history
…stall-step-for-npm-based-formatters

# Conflicts:
#	CHANGES.md
#	plugin-gradle/CHANGES.md
#	plugin-maven/CHANGES.md
  • Loading branch information
simschla committed Feb 27, 2023
2 parents 78ccbc9 + 6716228 commit 8ebb896
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (

## [Unreleased]
### Added
* `gradlew equoIde` opens a repeatable clean Spotless dev environment. ([#1523](https://github.com/diffplug/spotless/pull/1523))
* `cleanthat` added `includeDraft` option, to include draft mutators from composite mutators. ([#1574](https://github.com/diffplug/spotless/pull/1574))
* `npm`-based formatters now support caching of `node_modules` directory ([#1590](https://github.com/diffplug/spotless/pull/1590))
### Fixed
* `JacksonJsonFormatterFunc` handles json files with an Array as root. ([#1585](https://github.com/diffplug/spotless/pull/1585))
### Changes
* Bump default `cleanthat` version to latest `2.1` -> `2.6` ([#1569](https://github.com/diffplug/spotless/pull/1569) and [#1574](https://github.com/diffplug/spotless/pull/1574))
* Reduce logging-noise created by `npm`-based formatters ([#1590](https://github.com/diffplug/spotless/pull/1590) fixes [#1582](https://github.com/diffplug/spotless/issues/1582))

## [2.35.0] - 2023-02-10
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Contributing to Spotless

Pull requests are welcome, preferably against `main`. Feel free to develop spotless any way you like.
Pull requests are welcome, preferably against `main`. Feel free to develop spotless any way you like, but if you like Eclipse and Gradle Buildship then [`gradlew equoIde` will install an IDE and set it up for you](https://github.com/equodev/equo-ide).

## How Spotless works

Expand Down
14 changes: 14 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
plugins {
// https://github.com/equodev/equo-ide/blob/main/plugin-gradle/CHANGELOG.md
id 'dev.equo.ide' version '0.16.0'
}
equoIde {
branding().title('Spotless').icon(file('_images/spotless_logo.png'))
welcome().openUrl('https://github.com/diffplug/spotless/blob/main/CONTRIBUTING.md')
gradleBuildship()
}

repositories {
mavenCentral()
}

apply from: rootProject.file('gradle/java-publish.gradle')
apply from: rootProject.file('gradle/changelog.gradle')
allprojects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.diffplug.spotless.glue.json;

import java.io.IOException;
import java.util.Map;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down Expand Up @@ -49,7 +48,7 @@ public String apply(String input) throws Exception {
protected String format(ObjectMapper objectMapper, String input) throws IllegalArgumentException, IOException {
try {
// ObjectNode is not compatible with SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS
Map objectNode = objectMapper.readValue(input, Map.class);
Object objectNode = objectMapper.readValue(input, inferType(input));
String output = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(objectNode);

return output;
Expand All @@ -58,6 +57,13 @@ protected String format(ObjectMapper objectMapper, String input) throws IllegalA
}
}

/**
*
* @param input
* @return the {@link Class} into which the String has to be deserialized
*/
protected abstract Class<?> inferType(String input);

/**
* @return a {@link JsonFactory}. May be overridden to handle alternative formats.
* @see <a href="https://github.com/FasterXML/jackson-dataformats-text">jackson-dataformats-text</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package com.diffplug.spotless.glue.json;

import java.util.Collection;
import java.util.Map;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonFactoryBuilder;
import com.fasterxml.jackson.core.JsonGenerator;
Expand All @@ -37,6 +40,15 @@ public JacksonJsonFormatterFunc(JacksonJsonConfig jacksonConfig) {
this.jacksonConfig = jacksonConfig;
}

@Override
protected Class<?> inferType(String input) {
if (input.trim().startsWith("[")) {
return Collection.class;
} else {
return Map.class;
}
}

/**
* @return a {@link JsonFactory}. May be overridden to handle alternative formats.
* @see <a href="https://github.com/FasterXML/jackson-dataformats-text">jackson-dataformats-text</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,18 @@ protected JsonFactory makeJsonFactory() {
return yamlFactoryBuilder.build();
}

@Override
protected Class<?> inferType(String input) {
return JsonNode.class;
}

@Override
protected String format(ObjectMapper objectMapper, String input) throws IllegalArgumentException, IOException {
try {
// https://stackoverflow.com/questions/25222327/deserialize-pojos-from-multiple-yaml-documents-in-a-single-file-in-jackson
// https://github.com/FasterXML/jackson-dataformats-text/issues/66#issuecomment-375328648
JsonParser yamlParser = objectMapper.getFactory().createParser(input);
List<JsonNode> documents = objectMapper.readValues(yamlParser, JsonNode.class).readAll();
List<?> documents = objectMapper.readValues(yamlParser, inferType(input)).readAll();

// https://github.com/FasterXML/jackson-dataformats-text/issues/66#issuecomment-554265055
// https://github.com/FasterXML/jackson-dataformats-text/issues/66#issuecomment-554265055
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@ParametersAreNonnullByDefault
@ReturnValuesAreNonnullByDefault
package com.diffplug.spotless.extra.json.java;
package com.diffplug.spotless.json;

import javax.annotation.ParametersAreNonnullByDefault;

Expand Down
8 changes: 5 additions & 3 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (

## [Unreleased]
### Added
* Add `includeDraft` option, to include draft mutators from composite mutators ([#1574](https://github.com/diffplug/spotless/pull/1574))
* `cleanthat` now has `includeDraft` option, to include draft mutators from composite mutators. ([#1574](https://github.com/diffplug/spotless/pull/1574))
* `npm`-based formatters (`prettier`, `tsfmt` and `eslint`) now support caching of `node_modules` directory.
To enable it, provide `npmInstallCache()` option. ([#1590](https://github.com/diffplug/spotless/pull/1590))
### Fixed
* `json { jackson()` can now handle `Array` as a root element. ([#1585](https://github.com/diffplug/spotless/pull/1585))
* Reduce logging-noise created by `npm`-based formatters ([#1590](https://github.com/diffplug/spotless/pull/1590) fixes [#1582](https://github.com/diffplug/spotless/issues/1582))
### Changes
* Bump default `cleanthat` version to latest `2.2` -> `2.6` ([#1574](https://github.com/diffplug/spotless/pull/1574))
* Bump default `cleanthat` version to latest `2.1` -> `2.2` ([#1569](https://github.com/diffplug/spotless/pull/1569))
* Bump default `cleanthat` version to latest `2.1` -> `2.6`. ([#1569](https://github.com/diffplug/spotless/pull/1569) and [#1574](https://github.com/diffplug/spotless/pull/1574))

## [6.15.0] - 2023-02-10
### Added
Expand Down
8 changes: 5 additions & 3 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (

## [Unreleased]
### Added
* Add `includeDraft` option, to include draft mutators from composite mutators ([#XXX](https://github.com/diffplug/spotless/pull/XXX))
* `cleanthat` added `includeDraft` option, to include draft mutators from composite mutators. ([#1574](https://github.com/diffplug/spotless/pull/1574))
* `npm`-based formatters (`prettier`, `tsfmt` and `eslint`) now support caching of `node_modules` directory.
To enable it, provide the `<npmInstallCache>` option. ([#1590](https://github.com/diffplug/spotless/pull/1590))
### Fixed
* `<json><jackson>` can now handle `Array` as a root element. ([#1585](https://github.com/diffplug/spotless/pull/1585))
* Reduce logging-noise created by `npm`-based formatters ([#1590](https://github.com/diffplug/spotless/pull/1590) fixes [#1582](https://github.com/diffplug/spotless/issues/1582))
### Changes
* Bump default `cleanthat` version to latest `2.2` -> `2.6` ([#1574](https://github.com/diffplug/spotless/pull/1574))
* Bump default `cleanthat` version to latest `2.1` -> `2.2` ([#1569](https://github.com/diffplug/spotless/pull/1569))
* Bump default `cleanthat` version to latest `2.1` -> `2.6` ([#1569](https://github.com/diffplug/spotless/pull/1569) and [#1574](https://github.com/diffplug/spotless/pull/1574))

## [2.33.0] - 2023-02-10
### Added
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ 1, 2, 3, 4 ]
2 changes: 1 addition & 1 deletion testlib/src/main/resources/json/singletonArrayBefore.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[ 1, 2, 3, 4 ]
[ 1 , 2, 3, 4 ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2021-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.json;

import org.junit.jupiter.api.Test;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.StepHarness;
import com.diffplug.spotless.TestProvisioner;

class JacksonJsonStepTest {

private static final int INDENT = 4;

private final FormatterStep step = JsonSimpleStep.create(INDENT, TestProvisioner.mavenCentral());
private final StepHarness stepHarness = StepHarness.forStep(step);

@Test
void canSetCustomIndentationLevel() {
FormatterStep step = JacksonJsonStep.create(TestProvisioner.mavenCentral());
StepHarness stepHarness = StepHarness.forStep(step);

String before = "json/singletonArrayBefore.json";
String after = "json/singletonArrayAfter_Jackson.json";
stepHarness.testResource(before, after);
}
}

0 comments on commit 8ebb896

Please sign in to comment.