mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-15 02:17:12 +00:00
var can now also be configured to emit a warning when used.
This commit is contained in:
1 parent
8c2ea4fbc6
commit
d6f1116108
7 files changed
+44
-24
No files matched your search
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2014 The Project Lombok Authors.
|
||||
* Copyright (C) 2016 The Project Lombok Authors.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -21,17 +21,19 @@
|
||||
*/
|
||||
package lombok.core.configuration;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import lombok.ConfigurationKeys;
|
||||
|
||||
public class AllowHelper {
|
||||
private final static Set<String> allowable = new HashSet<String>(asList(
|
||||
"var"
|
||||
));
|
||||
public final class AllowHelper {
|
||||
private final static Collection<? extends ConfigurationKey<?>> ALLOWABLE = Collections.singleton(ConfigurationKeys.VAR_FLAG_USAGE);
|
||||
|
||||
public static boolean isAllowable(String feature) {
|
||||
return allowable.contains(feature);
|
||||
private AllowHelper() {
|
||||
// Prevent instantiation
|
||||
}
|
||||
|
||||
public static boolean isAllowable(ConfigurationKey<?> key) {
|
||||
return ALLOWABLE.contains(key);
|
||||
}
|
||||
}
|
||||
@@ -96,13 +96,10 @@ public class HandlerUtil {
|
||||
return Singulars.autoSingularize(plural);
|
||||
}
|
||||
public static void handleFlagUsage(LombokNode<?, ?, ?> node, ConfigurationKey<FlagUsageType> key, String featureName) {
|
||||
boolean allowable = AllowHelper.isAllowable(featureName);
|
||||
|
||||
FlagUsageType fut = node.getAst().readConfiguration(key);
|
||||
|
||||
boolean allowed = !allowable || FlagUsageType.ALLOW == fut;
|
||||
if (!allowed) {
|
||||
node.addError("Use of " + featureName + " is disabled by default. Please use flag " + FlagUsageType.ALLOW + " to enable.");
|
||||
if (fut == null && AllowHelper.isAllowable(key)) {
|
||||
node.addError("Use of " + featureName + " is disabled by default. Please add '" + key.getKeyName() + " = " + FlagUsageType.ALLOW + "' to 'lombok.config' if you want to enable is.");
|
||||
}
|
||||
|
||||
if (fut != null) {
|
||||
|
||||
@@ -57,10 +57,16 @@ public abstract class AbstractRunTests {
|
||||
|
||||
public final FileTester createTester(final DirectoryRunner.TestParams params, final File file) throws IOException {
|
||||
ConfigurationKeysLoader.LoaderLoader.loadAllConfigurationKeys();
|
||||
final LombokTestSource sourceDirectives = LombokTestSource.readDirectives(file);
|
||||
if (sourceDirectives.isIgnore()) return null;
|
||||
if (!sourceDirectives.versionWithinLimit(params.getVersion())) return null;
|
||||
if (!sourceDirectives.versionWithinLimit(getClasspathVersion())) return null;
|
||||
AssertionError directiveFailure = null;
|
||||
LombokTestSource sourceDirectives = null;
|
||||
try {
|
||||
sourceDirectives = LombokTestSource.readDirectives(file);
|
||||
if (sourceDirectives.isIgnore()) return null;
|
||||
if (!sourceDirectives.versionWithinLimit(params.getVersion())) return null;
|
||||
if (!sourceDirectives.versionWithinLimit(getClasspathVersion())) return null;
|
||||
} catch (AssertionError ae) {
|
||||
directiveFailure = ae;
|
||||
}
|
||||
|
||||
String fileName = file.getName();
|
||||
final LombokTestSource expected = LombokTestSource.read(params.getAfterDirectory(), params.getMessagesDirectory(), fileName);
|
||||
@@ -68,23 +74,26 @@ public abstract class AbstractRunTests {
|
||||
if (expected.isIgnore()) return null;
|
||||
if (!expected.versionWithinLimit(params.getVersion())) return null;
|
||||
|
||||
final LombokTestSource sourceDirectives_ = sourceDirectives;
|
||||
final AssertionError directiveFailure_ = directiveFailure;
|
||||
return new FileTester() {
|
||||
@Override public void runTest() throws Throwable {
|
||||
if (directiveFailure_ != null) throw directiveFailure_;
|
||||
LinkedHashSet<CompilerMessage> messages = new LinkedHashSet<CompilerMessage>();
|
||||
StringWriter writer = new StringWriter();
|
||||
|
||||
LombokConfiguration.overrideConfigurationResolverFactory(new ConfigurationResolverFactory() {
|
||||
@Override public ConfigurationResolver createResolver(AST<?, ?, ?> ast) {
|
||||
return sourceDirectives.getConfiguration();
|
||||
return sourceDirectives_.getConfiguration();
|
||||
}
|
||||
});
|
||||
|
||||
boolean changed = transformCode(messages, writer, file, sourceDirectives.getSpecifiedEncoding(), sourceDirectives.getFormatPreferences());
|
||||
boolean forceUnchanged = sourceDirectives.forceUnchanged() || sourceDirectives.isSkipCompareContent();
|
||||
boolean changed = transformCode(messages, writer, file, sourceDirectives_.getSpecifiedEncoding(), sourceDirectives_.getFormatPreferences());
|
||||
boolean forceUnchanged = sourceDirectives_.forceUnchanged() || sourceDirectives_.isSkipCompareContent();
|
||||
if (params.expectChanges() && !forceUnchanged && !changed) messages.add(new CompilerMessage(-1, -1, true, "not flagged modified"));
|
||||
if (!params.expectChanges() && changed) messages.add(new CompilerMessage(-1, -1, true, "unexpected modification"));
|
||||
|
||||
compare(file.getName(), expected, writer.toString(), messages, params.printErrors(), sourceDirectives.isSkipCompareContent() || expected.isSkipCompareContent());
|
||||
compare(file.getName(), expected, writer.toString(), messages, params.printErrors(), sourceDirectives_.isSkipCompareContent() || expected.isSkipCompareContent());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -130,7 +130,7 @@ public class LombokTestSource {
|
||||
|
||||
private static final Pattern IGNORE_PATTERN = Pattern.compile("^\\s*ignore\\s*(?:[-:].*)?$", Pattern.CASE_INSENSITIVE);
|
||||
private static final Pattern UNCHANGED_PATTERN = Pattern.compile("^\\s*unchanged\\s*(?:[-:].*)?$", Pattern.CASE_INSENSITIVE);
|
||||
private static final Pattern SKIP_COMPARE_CONTENT_PATTERN = Pattern.compile("^\\s*skip[- ]?compare[- ]?content\\s*(?:[-:].*)?$", Pattern.CASE_INSENSITIVE);
|
||||
private static final Pattern SKIP_COMPARE_CONTENT_PATTERN = Pattern.compile("^\\s*skip[- ]?compare[- ]?contents?\\s*(?:[-:].*)?$", Pattern.CASE_INSENSITIVE);
|
||||
|
||||
private LombokTestSource(File file, String content, List<CompilerMessageMatcher> messages, List<String> directives) {
|
||||
this.file = file;
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
//CONF: lombok.var.flagUsage = WARNING
|
||||
//skip compare contents
|
||||
import lombok.experimental.var;
|
||||
|
||||
public class VarWarning {
|
||||
public void isOkay() {
|
||||
var x = "Warning";
|
||||
x.toLowerCase();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
7 Use of var is flagged according to lombok configuration
|
||||
@@ -0,0 +1 @@
|
||||
7 Use of var is flagged according to lombok configuration
|
||||
Reference in new issue
Block a user