Skip to content

Commit

Permalink
Add optional name to extended rule object (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
pajter authored May 30, 2020
1 parent 11604e6 commit 5d2a8ca
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
## Head

* Fixed `order` not reporting warnings, if autofix didn't fix them.
* Added `name` option to extended rule object to improve error messaging.

## 4.0.0

Expand Down
1 change: 1 addition & 0 deletions rules/order/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ Object parameters:
* `selector`: `string`|`regex`. Selector pattern. A string will be translated into a RegExp — `new RegExp(yourString)` — so _be sure to escape properly_. Examples:
* `selector: /^&:[\w-]+$/` matches simple pseudo-classes. E. g., `&:hover`, `&:first-child`. Doesn't match complex pseudo-classes, e. g. `&:not(.is-visible)`.
* `selector: /^&::[\w-]+$/` matches pseudo-elements. E. g. `&::before`, `&::placeholder`.
* `name`: `string`. Selector name (optional). Will be used in error output to help identify extended rule object.

Matches all rules:

Expand Down
5 changes: 4 additions & 1 deletion rules/order/getDescription.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ module.exports = function getDescription(item) {
if (item.type === 'rule') {
text = 'rule';

if (item.selector) {
if (item.name) {
// Prefer 'name' property for better error messaging
text += ` "${item.name}"`;
} else if (item.selector) {
text += ` with selector matching "${item.selector}"`;
}
}
Expand Down
77 changes: 77 additions & 0 deletions rules/order/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,83 @@ testRule({
],
});

testRule({
ruleName,
config: [
[
{
type: 'rule',
},
{
type: 'rule',
selector: /^&:\w/,
name: 'State',
},
{
type: 'rule',
selector: /^&/,
name: 'Child',
},
],
],
fix: true,

accept: [
{
code: `
a {
b & {}
&:hover {}
& b {}
}
`,
},
{
code: `
a {
b & {}
& b {}
}
`,
},
],

reject: [
{
code: `
a {
b & {}
& b {}
&:hover {}
}
`,
fixed: `
a {
b & {}
&:hover {}
& b {}
}
`,
message: messages.expected('rule "State"', 'rule "Child"'),
},
{
code: `
a {
&:hover {}
b & {}
}
`,
fixed: `
a {
b & {}
&:hover {}
}
`,
message: messages.expected('rule', 'rule "State"'),
},
],
});

testRule({
ruleName,
syntax: 'less',
Expand Down
59 changes: 59 additions & 0 deletions rules/order/tests/validate-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ testConfig({
type: 'rule',
selector: '^&:\\w',
},
{
type: 'rule',
selector: /^&::\w/,
name: 'Pseudo',
},
{
type: 'rule',
},
Expand Down Expand Up @@ -264,6 +269,60 @@ testConfig({
message: `Invalid option "[{"type":"rule","selector":null}]" for rule ${ruleName}`,
});

testConfig({
ruleName,
description: 'invalid. name is empty',
valid: false,
config: [
{
type: 'rule',
name: '',
},
],
message: `Invalid option "[{"type":"rule","name":""}]" for rule ${ruleName}`,
});

testConfig({
ruleName,
description: 'invalid. name is not a string',
valid: false,
config: [
{
type: 'rule',
name: null,
},
],
message: `Invalid option "[{"type":"rule","name":null}]" for rule ${ruleName}`,
});

testConfig({
ruleName,
description: 'invalid. selector is valid, but name is invalid',
valid: false,
config: [
{
type: 'rule',
selector: '^&:hover',
name: null,
},
],
message: `Invalid option "[{"type":"rule","selector":"^&:hover","name":null}]" for rule ${ruleName}`,
});

testConfig({
ruleName,
description: 'invalid. name is valid, but select is invalid',
valid: false,
config: [
{
type: 'rule',
selector: null,
name: 'Element',
},
],
message: `Invalid option "[{"type":"rule","selector":null,"name":"Element"}]" for rule ${ruleName}`,
});

testConfig({
ruleName,
description: 'disableFix true',
Expand Down
4 changes: 4 additions & 0 deletions rules/order/validatePrimaryOption.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ module.exports = function validatePrimaryOption(actualOptions) {
(_.isString(item.selector) && item.selector.length) ||
_.isRegExp(item.selector);
}

if (result && !_.isUndefined(item.name)) {
result = _.isString(item.name) && item.name.length;
}
}

return result;
Expand Down

0 comments on commit 5d2a8ca

Please sign in to comment.