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

feat: add autofix for rule "selector-type-case" #3668

Merged
merged 1 commit into from
Sep 24, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion docs/user-guide/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ Here are all the rules within stylelint, grouped first [by category](../../VISIO
- [`selector-pseudo-class-parentheses-space-inside`](../../lib/rules/selector-pseudo-class-parentheses-space-inside/README.md): Require a single space or disallow whitespace on the inside of the parentheses within pseudo-class selectors.
- [`selector-pseudo-element-case`](../../lib/rules/selector-pseudo-element-case/README.md): Specify lowercase or uppercase for pseudo-element selectors.
- [`selector-pseudo-element-colon-notation`](../../lib/rules/selector-pseudo-element-colon-notation/README.md): Specify single or double colon notation for applicable pseudo-elements (Autofixable).
- [`selector-type-case`](../../lib/rules/selector-type-case/README.md): Specify lowercase or uppercase for type selector.
- [`selector-type-case`](../../lib/rules/selector-type-case/README.md): Specify lowercase or uppercase for type selector (Autofixable).

#### Selector list

Expand Down
2 changes: 2 additions & 0 deletions lib/rules/selector-type-case/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Specify lowercase or uppercase for type selectors.
* This is type selector */
```

The `--fix` option on the [command line](../../../docs/user-guide/cli.md#autofixing-errors) can automatically fix all of the problems reported by this rule.

## Options

`string`: `"lower"|"upper"`
Expand Down
53 changes: 53 additions & 0 deletions lib/rules/selector-type-case/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { messages, ruleName } = rule;
testRule(rule, {
ruleName,
config: ["lower"],
fix: true,
EliazTray marked this conversation as resolved.
Show resolved Hide resolved

accept: [
{
Expand Down Expand Up @@ -84,37 +85,62 @@ testRule(rule, {
{
code: "html { --custom-property-set: {} }",
description: "custom property set in selector"
},
{
code: "a, /*comments */\n b {}",
description: "comments in the selector"
},
{
code: "a /*comments */\n b {}",
description: "comments in the selector"
}
],

reject: [
{
code: "A {}",
fixed: "a {}",
message: messages.expected("A", "a")
},
{
code: "DIV::before {}",
fixed: "div::before {}",
message: messages.expected("DIV", "div")
},
{
code: "a B {}",
fixed: "a b {}",
message: messages.expected("B", "b")
},
{
code: "a { & B {}}",
fixed: "a { & b {}}",
message: messages.expected("B", "b")
},
{
code: "A:nth-child(even) {}",
fixed: "a:nth-child(even) {}",
message: messages.expected("A", "a")
},
{
code: "A:nth-child(-n) {}",
fixed: "a:nth-child(-n) {}",
message: messages.expected("A", "a")
},
{
code: "A { &:nth-child(3n + 1) {} }",
fixed: "a { &:nth-child(3n + 1) {} }",
message: messages.expected("A", "a")
},
{
code: "a, /*comments */\n B {}",
fixed: "a, /*comments */\n b {}",
message: messages.expected("B", "b")
},
{
code: "a /*comments */\n B {}",
fixed: "a /*comments */\n b {}",
message: messages.expected("B", "b")
}
]
});
Expand All @@ -123,6 +149,7 @@ testRule(rule, {
ruleName,
config: ["upper"],
skipBasicChecks: true,
fix: true,

accept: [
{
Expand Down Expand Up @@ -187,37 +214,62 @@ testRule(rule, {
},
{
code: ":root { --custom-property-set: {} }"
},
{
code: "A, /*comments */\n B {}",
description: "comments in the selector"
},
{
code: "A /*comments */\n B {}",
description: "comments in the selector"
}
],

reject: [
{
code: "a {}",
fixed: "A {}",
message: messages.expected("a", "A")
},
{
code: "div::before {}",
fixed: "DIV::before {}",
message: messages.expected("div", "DIV")
},
{
code: "a B {}",
fixed: "A B {}",
message: messages.expected("a", "A")
},
{
code: "a { & B {}}",
fixed: "A { & B {}}",
message: messages.expected("a", "A")
},
{
code: "a:nth-child(even) {}",
fixed: "A:nth-child(even) {}",
message: messages.expected("a", "A")
},
{
code: "a:nth-child(-n) {}",
fixed: "A:nth-child(-n) {}",
message: messages.expected("a", "A")
},
{
code: "a { &:nth-child(3n + 1) {} }",
fixed: "A { &:nth-child(3n + 1) {} }",
message: messages.expected("a", "A")
},
{
code: "A, /*comments */\n b {}",
fixed: "A, /*comments */\n B {}",
message: messages.expected("b", "B")
},
{
code: "A /*comments */\n b {}",
fixed: "A /*comments */\n B {}",
message: messages.expected("b", "B")
}
]
});
Expand All @@ -227,6 +279,7 @@ testRule(rule, {
config: ["upper"],
skipBasicChecks: true,
syntax: "scss",
fix: true,

accept: [
{
Expand Down
22 changes: 20 additions & 2 deletions lib/rules/selector-type-case/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";

const _ = require("lodash");
const isKeyframeSelector = require("../../utils/isKeyframeSelector");
const isStandardSyntaxRule = require("../../utils/isStandardSyntaxRule");
const isStandardSyntaxSelector = require("../../utils/isStandardSyntaxSelector");
Expand All @@ -15,7 +16,7 @@ const messages = ruleMessages(ruleName, {
expected: (actual, expected) => `Expected "${actual}" to be "${expected}"`
});

const rule = function(expectation) {
const rule = function(expectation, options, context) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
Expand All @@ -26,7 +27,8 @@ const rule = function(expectation) {
}

root.walkRules(rule => {
const selector = rule.selector;
let hasComments = _.get(rule, "raws.selector.raw");
const selector = hasComments ? hasComments : rule.selector;
const selectors = rule.selectors;

if (!isStandardSyntaxRule(rule)) {
Expand Down Expand Up @@ -55,6 +57,22 @@ const rule = function(expectation) {
return;
}

if (context.fix) {
if (hasComments) {
hasComments =
hasComments.slice(0, sourceIndex) +
expectedValue +
hasComments.slice(sourceIndex + value.length);
_.set(rule, "raws.selector.raw", hasComments);
} else {
rule.selector =
rule.selector.slice(0, sourceIndex) +
expectedValue +
rule.selector.slice(sourceIndex + value.length);
}
return;
}

report({
message: messages.expected(value, expectedValue),
node: rule,
Expand Down