Skip to content

Commit

Permalink
Merge f4f4984 into e57bb33
Browse files Browse the repository at this point in the history
  • Loading branch information
KamiKillertO committed Aug 5, 2020
2 parents e57bb33 + f4f4984 commit 4e0d45e
Show file tree
Hide file tree
Showing 21 changed files with 321 additions and 221 deletions.
6 changes: 3 additions & 3 deletions lib/legacy/config.js
@@ -1,4 +1,4 @@
const { isBool } = require("../validate_option");
const { is_boolean } = require("../validate_option");
const pull = require("lodash.pull");

/**
Expand Down Expand Up @@ -71,7 +71,7 @@ class Config {
this.addOption({
name: rule.name,
rules: [rule.name],
validateConfig: rule.validateConfig || isBool
validateConfig: rule.validateConfig || is_boolean
});
}
}
Expand Down Expand Up @@ -115,7 +115,7 @@ class Config {
option.rules = [option.name];
}
option.active = false;
option.validateConfig = option.validateConfig || isBool;
option.validateConfig = option.validateConfig || is_boolean;
this.options[option.name] = option;

if (oldOption && oldOption.active) {
Expand Down
31 changes: 7 additions & 24 deletions lib/rules/attr-name-style/index.js
@@ -1,37 +1,21 @@
/* eslint-disable-next-line */
const { isRegExp } = require("util");
const proc = require("../../process_option");
const match_format = require("../../utils/check_format");
const { is_tag_node } = require("../../knife/tag_utils");
const { create_string_or_regexp_validator } = require("../../validate_option");

module.exports = {
name: "attr-name-style",
on: ["dom"],
need: "dom",
validateConfig(format) {
if (typeof format === "string" || isRegExp(format) === true) {
return format;
}
throw new Error(`Configuration for rule "${this.name}" is invalid: Expected string or RegExp got ${typeof format}`);
},
validateConfig: create_string_or_regexp_validator(),
options: [
// REMOVE: For the v1
// Need to duplicate validateConfig to make it works with the old and the new Config system ><
{
validateConfig(format) {
if (typeof format === "string" || isRegExp(format) === true) {
return format;
}
throw new Error(`Configuration for rule "${this.name}" is invalid: Expected string or RegExp got ${typeof format}`);
}
validateConfig: create_string_or_regexp_validator()
},
{
name: "attr-name-ignore-regex",
validateConfig(format) {
if (typeof format === "string" || isRegExp(format) === true) {
return format;
}
throw new Error(`Configuration for rule attr-name-ignore-regex is invalid: Expected string or RegExp got ${typeof format}`);
},
validateConfig: create_string_or_regexp_validator(),
rules: []
}
]
Expand All @@ -47,12 +31,11 @@ module.exports.lint = function(node, config, { report }) {
let attributes = node.attributes.filter(({ name }) => /^¤+$/.test(name.chars) === false);
const ignore = config["attr-name-ignore-regex"];
if (ignore) {
const R = proc.regex(ignore);
attributes = attributes.filter(({ name }) => R.test(name.chars) === false);
attributes = attributes.filter(({ name }) => match_format(ignore, name.chars) === false);
}

attributes.forEach(({ name }) => {
if (proc.regex(format).test(name.chars) === false) {
if (match_format(format, name.chars) === false) {
report({
code: "E002",
position: name.loc,
Expand Down
8 changes: 4 additions & 4 deletions lib/rules/attr-quote-style/__tests__/index.js
Expand Up @@ -51,15 +51,15 @@ describe("legacy linter | attr-quote-style", function() {
const html = "";
expect(() => linter.lint(html))
.to
.throw("Configuration for rule \"attr-quote-style\" is invalid: Expected \"double\", \"simple\" or \"quoted\" got \"unknown\".");
.throw("Configuration for rule \"attr-quote-style\" is invalid: \"unknown\" is not accepted. Accepted values are \"double\", \"single\" and \"quoted\".");
});

it("Should throw an error when an invalid config is provided (invalid type)", function() {
const linter = createLinter({ "attr-quote-style": 3 });
const html = "";
expect(() => linter.lint(html))
.to
.throw("Configuration for rule \"attr-quote-style\" is invalid: Expected \"double\", \"simple\" or \"quoted\" got number.");
.throw("Configuration for rule \"attr-quote-style\" is invalid: Expected string got number.");
});
});

Expand Down Expand Up @@ -141,7 +141,7 @@ describe("attr-quote-style", function() {
};
expect(() => createLinter(config))
.to
.throw("Configuration for rule \"attr-quote-style\" is invalid: Expected \"double\", \"simple\" or \"quoted\" got \"unknown\".");
.throw("Configuration for rule \"attr-quote-style\" is invalid: \"unknown\" is not accepted. Accepted values are \"double\", \"single\" and \"quoted\".");
});

it("Should throw an error when an invalid config is provided (invalid type)", function() {
Expand All @@ -153,6 +153,6 @@ describe("attr-quote-style", function() {
};
expect(() => createLinter(config))
.to
.throw("Configuration for rule \"attr-quote-style\" is invalid: Expected \"double\", \"simple\" or \"quoted\" got number.");
.throw("Configuration for rule \"attr-quote-style\" is invalid: Expected string got number.");
});
});
11 changes: 2 additions & 9 deletions lib/rules/attr-quote-style/index.js
@@ -1,4 +1,5 @@
const { is_tag_node } = require("../../knife/tag_utils");
const { create_list_value_validator } = require("../../validate_option");

const formats = {
double: { regex: /^"/, desc: "double quoted" },
Expand All @@ -11,15 +12,7 @@ module.exports = {
on: ["dom"],
need: "dom",

validateConfig(option) {
if (typeof option !== "string") {
throw new Error(`Configuration for rule "${this.name}" is invalid: Expected "double", "simple" or "quoted" got ${typeof option}.`);
}
if (/^(double|single|quoted)$/.test(option) === false) {
throw new Error(`Configuration for rule "${this.name}" is invalid: Expected "double", "simple" or "quoted" got "${option}".`);
}
return option;
}
validateConfig: create_list_value_validator(["double", "single", "quoted"], false)
};

module.exports.lint = function(node, opts, { report }) {
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/class-style/__tests__/index.js
Expand Up @@ -122,7 +122,7 @@ describe("legacy linter | class-style", function() {

expect(() => linter.lint(html))
.to
.throw("Configuration for rule \"class-style\" is invalid: Expected string|regexp got number");
.throw("Configuration for rule \"class-style\" is invalid: Expected string or RegExp got number");
});

it("Should throw an error for invalid config (invalid string value)", function() {
Expand Down Expand Up @@ -331,7 +331,7 @@ describe("class-style", function() {

expect(() => createLinter(config))
.to
.throw("Configuration for rule \"class-style\" is invalid: Expected string|regexp got number");
.throw("Configuration for rule \"class-style\" is invalid: Expected string or RegExp got number");
});

it("Should throw an error for invalid config (invalid string value)", function() {
Expand Down
20 changes: 4 additions & 16 deletions lib/rules/class-style/index.js
@@ -1,22 +1,12 @@
/* eslint-disable-next-line */
const { isRegExp } = require("util");
const proc = require("../../process_option");
const match_format = require("../../utils/check_format");
const { is_tag_node, attribute_value, has_attribute } = require("../../knife/tag_utils");
const { create_list_value_validator } = require("../../validate_option");

module.exports = {
name: "class-style",
on: ["dom"],
need: "dom",
validateConfig(option) {
if (typeof option !== "string" && isRegExp(option) === false) {
throw new Error(`Configuration for rule "${this.name}" is invalid: Expected string|regexp got ${typeof option}`);
}

if (["none", "lowercase", "underscore", "dash", "camel", "bem"].indexOf(option) === -1 && isRegExp(option) === false) {
throw new Error(`Configuration for rule "${this.name}" is invalid: "${option}" is not accepted. Accepted values are "none", "lowercase", "underscore", "dash", "camel" and "bem".`);
}
return option;
}
validateConfig: create_list_value_validator(["none", "lowercase", "underscore", "dash", "camel", "bem"])
};

function getClasses(class_attribute) {
Expand Down Expand Up @@ -48,9 +38,7 @@ function lint(node, options, { report }) {
const class_attribute = attribute_value(node, "class");
const classes = filterClasses(getClasses(class_attribute), options);

const regex = proc.regex(format);

classes.filter(_class => !regex.test(_class))
classes.filter(_class => !match_format(format, _class))
.forEach(_class => report({
code: "E011",
position: class_attribute.loc, // should be the location of the class and not the class_attribute
Expand Down
14 changes: 3 additions & 11 deletions lib/rules/free-options.js
Expand Up @@ -5,26 +5,18 @@
* never called to contain them. It will be imported with the other
* rules.
*/
const { stringOrRegexp } = require("../validate_option");
const { create_number_validator, create_string_or_regexp_validator } = require("../validate_option");

module.exports = {
name: "free-options",
options: [
{
name: "maxerr",
validateConfig(option) {
if (typeof option !== "number") {
throw new Error(`Configuration for rule "${this.name}" is invalid: Expected number got ${typeof option}`);
}
if (option < 0) {
throw new Error(`Configuration for rule "${this.name}" is invalid: Only positive numbers are allowed.`);
}
return option;
}
validateConfig: create_number_validator(false)
},
{
name: "raw-ignore-regex",
validateConfig: stringOrRegexp
validateConfig: create_string_or_regexp_validator()
}
]
};
Expand Down
11 changes: 2 additions & 9 deletions lib/rules/href-style/index.js
@@ -1,18 +1,11 @@
const { is_tag_node, has_attribute, attribute_value, get_attribute } = require("../../knife/tag_utils");
const { create_list_value_validator } = require("../../validate_option");

module.exports = {
name: "href-style",
on: ["dom"],
need: "dom",
validateConfig(option) {
if (typeof option !== "string") {
throw new Error(`Configuration for rule "${this.name}" is invalid: Expected string got ${typeof option}`);
}
if (["absolute", "relative"].indexOf(option) === -1) {
throw new Error(`Configuration for rule "${this.name}" is invalid: "${option}" is not accepted. Accepted values are "absolute" and "relative".`);
}
return option;
}
validateConfig: create_list_value_validator(["absolute", "relative"], false)
};

module.exports.lint = function(node, opts, { report }) {
Expand Down
47 changes: 42 additions & 5 deletions lib/rules/id-style/__tests__/index.js
Expand Up @@ -109,7 +109,24 @@ describe("legacy linter | id-style", function() {
.to
.throw("Configuration for rule \"id-class-ignore-regex\" is invalid: You provide an empty string value");
});

it("should throw an error if rule config is empty", function() {
const linter = createLinter({ "id-style": "" });

expect(() => linter.lint(""))
.to
.throw("Configuration for rule \"id-style\" is invalid: \"\" is not accepted. Accepted values are \"lowercase\", \"underscore\", \"dash\", \"camel\" and \"bem\"");
});

it("should throw an error if rule config is provided with an invalid format", function() {
const linter = createLinter({ "id-style": "foo" });

expect(() => linter.lint(""))
.to
.throw("Configuration for rule \"id-style\" is invalid: \"foo\" is not accepted. Accepted values are \"lowercase\", \"underscore\", \"dash\", \"camel\" and \"bem\"");
});
});

describe("id-style", function() {
function createLinter(rules) {
return linthtml.fromConfig({ rules });
Expand Down Expand Up @@ -272,9 +289,29 @@ describe("id-style", function() {
});
});

// it("Should throw an error if `id-class-ignore-regex` is empty", function() {
// expect(() => linthtml.fromConfig({ "id-class-ignore-regex": "" }))
// .to
// .throw("Configuration for rule \"id-class-ignore-regex\" is invalid: You provide an empty string value");
// });
it("should throw an error if rule config is empty", function() {
const config = {
"id-style": [
true,
""
]
};

expect(() => createLinter(config))
.to
.throw("Configuration for rule \"id-style\" is invalid: \"\" is not accepted. Accepted values are \"lowercase\", \"underscore\", \"dash\", \"camel\" and \"bem\"");
});

it("should throw an error if rule config is provided with an invalid format", function() {
const config = {
"id-style": [
true,
"foo"
]
};

expect(() => createLinter(config))
.to
.throw("Configuration for rule \"id-style\" is invalid: \"foo\" is not accepted. Accepted values are \"lowercase\", \"underscore\", \"dash\", \"camel\" and \"bem\"");
});
});
46 changes: 10 additions & 36 deletions lib/rules/id-style/index.js
@@ -1,43 +1,26 @@
const proc = require("../../process_option");
/* eslint-disable-next-line */
const { isRegExp } = require("util");
const match_format = require("../../utils/check_format");
const { is_tag_node } = require("../../knife/tag_utils");
const lintClassStyle = require("../class-style").lint;
const {
create_list_value_validator,
create_string_or_regexp_validator
} = require("../../validate_option");

module.exports = {
name: "id-style",
on: ["dom"],
need: "dom",
validateConfig(format) {
if (typeof format === "string" || isRegExp(format) === true) {
return format;
}
throw new Error(`Configuration for rule "${this.name}" is invalid: Expected string or RegExp got ${typeof format}`);
},
validateConfig: create_list_value_validator(["lowercase", "underscore", "dash", "camel", "bem"]),
options: [
// REMOVE: For the v1
// Need to duplicate validateConfig to make it works with the old and the new Config system ><
{
need: "dom",
validateConfig(format) {
if (typeof format === "string" || isRegExp(format) === true) {
return format;
}
throw new Error(`Configuration for rule "${this.name}" is invalid: Expected string or RegExp got ${typeof format}`);
}
validateConfig: create_list_value_validator(["lowercase", "underscore", "dash", "camel", "bem"])
},
{
name: "id-class-style", // REMOVE: A rule be standalone and not call other rules
validateConfig(option) {
if (typeof option !== "string" && isRegExp(option) === false) {
throw new Error(`Configuration for rule "${this.name}" is invalid: Expected string|regexp got ${typeof option}`);
}

if (["none", "lowercase", "underscore", "dash", "camel", "bem"].indexOf(option) === -1 && isRegExp(option) === false) {
throw new Error(`Configuration for rule "${this.name}" is invalid: "${option}" is not accepted. Accepted values are "none", "lowercase", "underscore", "dash", "camel" and "bem".`);
}
return option;
},
validateConfig: create_list_value_validator(["lowercase", "underscore", "dash", "camel", "bem"]),
rules: ["class-style", "id-style"],
lint(node, opts, { report, rules }) {
if (!!rules["id-style"] === false && rules["id-class-style"]) {
Expand All @@ -51,15 +34,7 @@ module.exports = {
},
{
name: "id-class-ignore-regex",
validateConfig(options) {
if ((typeof options === "string" && options !== "") || isRegExp(options) === true) {
return options;
}
if (typeof options === "string") {
throw new Error(`Configuration for rule "${this.name}" is invalid: You provide an empty string value`);
}
throw new Error(`Configuration for rule "${this.name}" is invalid: Expected string or RegExp got ${typeof options}`);
},
validateConfig: create_string_or_regexp_validator(false),
rules: [] // 'class', 'id-style'
}
]
Expand Down Expand Up @@ -91,8 +66,7 @@ function lint(node, format, ignore, report) {
}
attributes.forEach(attribute => {
const id = attribute.value.chars;
const regex = proc.regex(format);
if (regex.test(id) === false) {
if (match_format(format, id) === false) {
report({
code: "E011",
position: attribute.loc,
Expand Down

0 comments on commit 4e0d45e

Please sign in to comment.