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

Add optional name to extended rule object #119

Merged
merged 3 commits into from
May 30, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
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
5 changes: 5 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