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

obsolete - Add support for linters #1097

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d5b0421
Define a `Lint`.
nedtwigg Jan 12, 2022
db676fc
Add a `lint` method to `FormatterStep` and let it wripple through.
nedtwigg Jan 12, 2022
5dc6271
Make Lint roundtrippable through a String.
nedtwigg Jan 13, 2022
cc71751
Formatter.lint
nedtwigg Jan 13, 2022
aac3972
Lint ripples through DirtyState and Formatter.
nedtwigg Jan 13, 2022
a25eded
SpotlessTaskImpl now generates both a formatted result and a lint res…
nedtwigg Jan 13, 2022
89339cd
SpotlessTaskImpl now generates a lint result for `check` and for `app…
nedtwigg Jan 14, 2022
cac4f70
Gradle spotlessCheck will now throw an error if all of the files are …
nedtwigg Jan 14, 2022
d4ec76b
Maven spotless:check will now throw an error if all of the files are …
nedtwigg Jan 14, 2022
7a1e72a
Default `FormatterFunc.lint` now traps errors from the formatter itse…
nedtwigg Jan 14, 2022
109c7fb
Remove `FormatExceptionPolicy`.
nedtwigg Jan 14, 2022
0049661
Remove `FormatExceptionPolicy` from the maven plugin.
nedtwigg Jan 14, 2022
dd5165f
Replace `FormatExceptionPolicy` with `LintPolicy` inside the Gradle p…
nedtwigg Jan 14, 2022
85fbe71
Lint.toFile might need to create parent directories.
nedtwigg Jan 14, 2022
e8e5df5
Lints are now quietly suppressed during `apply`, you have to do `chec…
nedtwigg Jan 14, 2022
6ec06cc
Fix spotbugs warnings.
nedtwigg Jan 14, 2022
9557c66
Fix the bug I made from fixing spotbugs :)
nedtwigg Jan 14, 2022
2844068
Make sure that FormatterFunc.Closeable's helpers support linting.
nedtwigg Jan 14, 2022
9134d8c
Fix FormatterFunc.Closeable earlier
nedtwigg Jan 14, 2022
5a9799d
Refactor PipeStepPair so that it can detect lost tags within our new …
nedtwigg Jan 14, 2022
07b39cb
Rename PipeStepPair to FenceStep.
nedtwigg Jan 14, 2022
78ddcd6
Removed `rootDir` property `Formatter` and its builder because it was…
nedtwigg Jan 14, 2022
f947c7b
`DiffMessageFormatter.Builder::formatter` needs a new `Path rootDir` …
nedtwigg Jan 14, 2022
a3eef1f
Adapt StepHarness for the new "exceptions never get thrown" environment.
nedtwigg Jan 15, 2022
3f72b7e
When a step throws an exception, we now accurately consider the entir…
nedtwigg Jan 14, 2022
1ed3149
Fix tests.
nedtwigg Jan 15, 2022
6e38887
Fix spotbugs.
nedtwigg Jan 15, 2022
eb0feed
Fix wrong SuppressFBWarnings import.
nedtwigg Jan 15, 2022
248ec28
Fix tests on windows.
nedtwigg Jan 15, 2022
3aa780e
Fix prettier error test.
nedtwigg Jan 15, 2022
e110426
Make our testing parallel.
nedtwigg Jan 15, 2022
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
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* **BREAKING** Removed `isClean`, `applyTo`, and `applyToAndReturnResultIfDirty` from `Formatter` because users should instead use `PaddedCell.check()`.
* **BREAKING** Removed `FormatterStep.Strict` because it was unnecessary and unused implementation detail.
* **BREAKING** Moved `PaddedCell.DirtyState` to its own top-level class with new methods.
* **BREAKING** Removed `FormatExceptionPolicy` and its subclasses because exceptions are now wrapped as lints.
* **BREAKING** Renamed `PipeStepPair` to `FenceStep` and changed its API.
* The old "pair" approach could not detect when a `spotless:off/on` tag pair was removed by an intermediate step, so we have no choice but to condense the "in/(all other steps)/out" into a single step.
* **BREAKING** Removed `rootDir` property `Formatter` and its builder because it was only used to annnotate errors, which is now done by the lint phase.
* This also means that `DiffMessageFormatter.Builder::formatter` needs a new `Path rootDir` parameter.
* **BREAKING** Removed deprecated methods.
* `FormatterFunc.Closeable::of(AutoCloseable, FormatterFunc)`

## [Unreleased]
### Changed
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# To fix metaspace errors
org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=512m -Dfile.encoding=UTF-8
org.gradle.parallel=true
name=spotless
description=Spotless - keep your code spotless with Gradle
org=diffplug
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -57,14 +57,16 @@ interface CleanProvider {

private static class CleanProviderFormatter implements CleanProvider {
private final Formatter formatter;
private final Path rootDir;

CleanProviderFormatter(Formatter formatter) {
CleanProviderFormatter(Path rootDir, Formatter formatter) {
this.rootDir = rootDir;
this.formatter = Objects.requireNonNull(formatter);
}

@Override
public Path getRootDir() {
return formatter.getRootDir();
return rootDir;
}

@Override
Expand Down Expand Up @@ -121,8 +123,8 @@ public Builder runToFix(String runToFix) {
return this;
}

public Builder formatter(Formatter formatter) {
this.formatter = new CleanProviderFormatter(formatter);
public Builder formatter(Path rootDir, Formatter formatter) {
this.formatter = new CleanProviderFormatter(rootDir, formatter);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,23 +24,18 @@
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

import com.diffplug.common.base.Errors;
import com.diffplug.common.base.StringPrinter;
import com.diffplug.spotless.LineEnding;
import com.diffplug.spotless.ResourceHarness;

class GitAttributesTest extends ResourceHarness {
private List<File> testFiles() {
try {
List<File> result = new ArrayList<>();
for (String path : TEST_PATHS) {
setFile(path).toContent("");
result.add(newFile(path));
}
return result;
} catch (IOException e) {
throw Errors.asRuntime(e);
List<File> result = new ArrayList<>();
for (String path : TEST_PATHS) {
setFile(path).toContent("");
result.add(newFile(path));
}
return result;
}

private static List<String> TEST_PATHS = Arrays.asList("someFile", "subfolder/someFile", "MANIFEST.MF", "subfolder/MANIFEST.MF");
Expand Down
23 changes: 23 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/DirtyState.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -137,5 +138,27 @@ public DirtyState calculateDirtyState() {
return isClean;
}
}

public List<Lint> calculateLintAgainstRaw() {
return formatter.lint(raw, file);
}

public List<Lint> calculateLintAgainstDirtyState(DirtyState dirtyState) {
if (dirtyState.isClean() || dirtyState.didNotConverge()) {
return calculateLintAgainstRaw();
} else {
String canonical = new String(dirtyState.canonicalBytes(), formatter.getEncoding());
return formatter.lint(canonical, file);
}
}

/** If {@link #calculateLintAgainstRaw()} was already called, then you might be able to reuse that value. */
public List<Lint> calculateLintAgainstDirtyState(DirtyState dirtyState, List<Lint> lintsAgainstRaw) {
if (dirtyState.isClean() || dirtyState.didNotConverge()) {
return lintsAgainstRaw;
} else {
return calculateLintAgainstDirtyState(dirtyState);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,8 +16,9 @@
package com.diffplug.spotless;

import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Nullable;
Expand All @@ -40,16 +41,24 @@ public String getName() {
public @Nullable String format(String raw, File file) throws Exception {
Objects.requireNonNull(raw, "raw");
Objects.requireNonNull(file, "file");

Matcher matcher = contentPattern.matcher(raw);

if (matcher.find()) {
if (contentPattern.matcher(raw).find()) {
return delegateStep.format(raw, file);
} else {
return raw;
}
}

@Override
public List<Lint> lint(String content, File file) throws Exception {
Objects.requireNonNull(content, "content");
Objects.requireNonNull(file, "file");
if (contentPattern.matcher(content).find()) {
return delegateStep.lint(content, file);
} else {
return Collections.emptyList();
}
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 DiffPlug
* Copyright 2016-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,8 @@
package com.diffplug.spotless;

import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -45,6 +47,17 @@ public String getName() {
}
}

@Override
public List<Lint> lint(String content, File file) throws Exception {
Objects.requireNonNull(content, "content");
Objects.requireNonNull(file, "file");
if (filter.accept(file)) {
return delegateStep.lint(content, file);
} else {
return Collections.emptyList();
}
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
39 changes: 0 additions & 39 deletions lib/src/main/java/com/diffplug/spotless/FormatExceptionPolicy.java

This file was deleted.

This file was deleted.

Loading