
com.github.jknack.mwa.wro4j.LintOptions Maven / Gradle / Ivy
package com.github.jknack.mwa.wro4j;
import static org.apache.commons.lang3.Validate.notEmpty;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
/**
* Configure JSHint/JSLint/CSSLint options.
*
* @author edgar.espina
* @since 0.1
*/
public final class LintOptions {
/**
* Default number of errors.
*/
private static final int MAX_ERRORS = 50;
/**
* Max number of chars per line.
*/
private static final int MAX_LEN = 80;
/**
* The option list.
*/
private final Map options = new LinkedHashMap();
/**
* Predefined/global variables.
*/
private final List predefined = new ArrayList();
/**
* Use one of the static method.
*/
private LintOptions() {
}
/**
* Creates a defaults JS Hint/Lint options. After creation you can safely add
* new options.
*
* @return A new options.
*/
public static LintOptions jsDefaults() {
return new LintOptions()
.options(
"browser",
"jquery",
"devel",
"forin",
"noarg",
"noempty",
"eqeqeq",
"bitwise",
"undef",
"curly"
)
.option("maxerr", MAX_ERRORS)
.option("globalstrict", false)
.option("strict", false)
.option("indent", 2)
.option("maxlen", MAX_LEN);
}
/**
* Creates a 'white' JS Hint/Lint options using the JavaScript Good Parts tips.
*
* @return A new options.
*/
public static LintOptions jsWhite() {
return jsDefaults().options("white", "trailing");
}
/**
*
* Available rules:
*
*
* - import: Don't use @import, use <link> instead.
*
- adjoining-classes: Don't use adjoining classes.
*
- important: Be careful when using !important declaration.
*
- box-sizing: The box-sizing properties isn't supported in IE6 and IE7.
*
- box-model: Don't use width or height when using padding or border.
*
- known-properties: Properties should be known (listed in CSS specification) or be a
* vendor-prefixed property.
*
- duplicate-background-images: Every background-image should be unique. Use a common class
* for e.g. sprites.
*
- compatible-vendor-prefixes: Include all compatible vendor prefixes to reach a wider range
* of users.
*
- display-property-grouping: Certain properties shouldn't be used with certain display
* property values.
*
- overqualified-elements: Don't use classes or IDs with elements (a.foo or a#foo).
*
- fallback-colors: For older browsers that don't support RGBA, HSL, or HSLA, provide a
* fallback color.
*
- duplicate-properties: Duplicate properties must appear one after the other.
*
- empty-rules: Rules without any properties specified should be removed.
*
- errors: This rule looks for recoverable syntax errors.
*
- rules-count: Track how many rules there are.
*
- ids: Selectors should not contain IDs.
*
- font-sizes: Checks the number of font-size declarations.
*
- font-faces: Too many different web fonts in the same stylesheet.
*
- gradients: When using a vendor-prefixed gradient, make sure to use them all.
*
- floats: This rule tests if the float property is used too many times
*
- outline-none: Use of outline: none or outline: 0 should be limited to :focus rules.
*
- qualified-headings: Headings should not be qualified (namespaced).
*
- regex-selectors: Selectors that look like regular expressions are slow and should be
* avoided.
*
- shorthand: Use shorthand properties where possible.
*
- text-indent: Checks for text indent less than -99px.
*
- unique-headings: Headings should be defined only once.
*
- universal-selector: The universal selector (*) is known to be slow.
*
- unqualified-attributes: Unqualified attribute selectors are known to be slow.
*
- vendor-prefix: When using a vendor-prefixed property, make sure to include the standard
* one.
*
- zero-units: You don't need to specify units when a value is 0.
*
*
* @return A new options.
*/
public static LintOptions cssDefaults() {
return new LintOptions()
.option("important")
.option("box-sizing")
.option("box-model")
.option("known-properties")
.option("duplicate-background-images")
.option("compatible-vendor-prefixes")
.option("display-property-grouping")
.option("overqualified-elements")
.option("fallback-colors")
.option("duplicate-properties")
.option("empty-rules")
.option("errors")
.option("ids")
.option("font-sizes")
.option("font-faces")
.option("gradients")
.option("floats")
.option("outline-none")
.option("qualified-headings")
.option("regex-selectors")
.option("shorthand")
.option("text-indent")
.option("unique-headings")
.option("universal-selector")
.option("unqualified-attributes")
.option("vendor-prefix")
.option("zero-units");
}
/**
* Creates a new {@link LintOptions}. After creation you can safely add
* new options.
*
* @param options A list of option's.
* @return A new {@link LintOptions}.
*/
public static LintOptions creates(final String... options) {
notEmpty(options, "The option's are required.");
LintOptions lintOptions = new LintOptions();
for (String option : options) {
lintOptions.option(option);
}
return lintOptions;
}
/**
* Enable all the given options.
*
* @param options All the other options. Required.
* @return This options.
*/
public LintOptions options(final String... options) {
for (String option : options) {
option(option);
}
return this;
}
/**
* Enable or disable the given option.
*
* @param name The option's name. Required.
* @param value The option's value. Required.
* @return This options.
*/
public LintOptions option(final String name, final boolean value) {
Validate.notEmpty(name, "The option's name is required.");
options.put(name, value);
return this;
}
/**
* Enable the given option. Same as {@link #option(String, true)}
*
* @param name The option's name. Required.
* @return This options.
*/
public LintOptions option(final String name) {
return option(name, true);
}
/**
* Remove an option.
*
* @param name The option's name. Required.
* @return This options.
*/
public LintOptions remove(final String name) {
options.remove(name);
return this;
}
/**
* Enable the given option.
*
* @param name The option's name. Required.
* @param value The option's value. Required.
* @return This options.
*/
public LintOptions option(final String name, final int value) {
Validate.notEmpty(name, "The option's name is required.");
options.put(name, value);
return this;
}
/**
* Enable the given option.
*
* @param name The option's name. Required.
* @param value The option's value. Required.
* @return This options.
*/
public LintOptions option(final String name, final String value) {
Validate.notEmpty(name, "The option's name is required.");
Validate.notEmpty(value, "The option's value is required.");
options.put(name, value);
return this;
}
/**
* Append a new global/predefined variable.
*
* @param names The variable's name, like: jquery, $, etc.. Required.
* @return This options.
*/
public LintOptions predefined(final String... names) {
Validate.notEmpty(names, "The global variable's name is required.");
for (String name : names) {
predefined.add("'" + name + "'");
}
return this;
}
/**
* Publish all the options as String array.
*
* @return The string array required by JS Lint/Hint.
*/
/* package */String[] build() {
List options = new ArrayList(this.options.size());
for (Entry option : this.options.entrySet()) {
if (Boolean.valueOf(option.getValue().toString()) == Boolean.TRUE) {
options.add(option.getKey());
} else {
options.add(option.getKey() + "=" + option.getValue());
}
}
if (predefined.size() > 0) {
options.add(
"predef=[" + StringUtils.join(predefined, ",") + "]");
}
return options.toArray(new String[options.size()]);
}
@Override
public String toString() {
return StringUtils.join(build(), ";");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy