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 autofix to media-feature-parentheses-space-inside #3720

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/user-guide/rules.md
Expand Up @@ -322,7 +322,7 @@ Here are all the rules within stylelint, grouped first [by category](../../VISIO
- [`media-feature-colon-space-after`](../../lib/rules/media-feature-colon-space-after/README.md): Require a single space or disallow whitespace after the colon in media features (Autofixable).
- [`media-feature-colon-space-before`](../../lib/rules/media-feature-colon-space-before/README.md): Require a single space or disallow whitespace before the colon in media features (Autofixable).
- [`media-feature-name-case`](../../lib/rules/media-feature-name-case/README.md): Specify lowercase or uppercase for media feature names (Autofixable).
- [`media-feature-parentheses-space-inside`](../../lib/rules/media-feature-parentheses-space-inside/README.md): Require a single space or disallow whitespace on the inside of the parentheses within media features.
- [`media-feature-parentheses-space-inside`](../../lib/rules/media-feature-parentheses-space-inside/README.md): Require a single space or disallow whitespace on the inside of the parentheses within media features (Autofixable).
- [`media-feature-range-operator-space-after`](../../lib/rules/media-feature-range-operator-space-after/README.md): Require a single space or disallow whitespace after the range operator in media features (Autofixable).
- [`media-feature-range-operator-space-before`](../../lib/rules/media-feature-range-operator-space-before/README.md): Require a single space or disallow whitespace before the range operator in media features (Autofixable).

Expand Down
2 changes: 2 additions & 0 deletions lib/rules/media-feature-parentheses-space-inside/README.md
Expand Up @@ -8,6 +8,8 @@ Require a single space or disallow whitespace on the inside of the parentheses w
* The space inside these two parentheses */
```

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`: `"always"|"never"`
Expand Down
Expand Up @@ -6,7 +6,7 @@ const { messages, ruleName } = rule;
testRule(rule, {
ruleName,
config: ["always"],

fix: true,
accept: [
{
code: "@media ( max-width: 300px ) {}"
Expand All @@ -31,66 +31,77 @@ testRule(rule, {
reject: [
{
code: "@media (max-width: 300px ) {}",
fixed: "@media ( max-width: 300px ) {}",
message: messages.expectedOpening,
line: 1,
column: 9
},
{
code: "@mEdIa (max-width: 300px ) {}",
fixed: "@mEdIa ( max-width: 300px ) {}",
message: messages.expectedOpening,
line: 1,
column: 9
},
{
code: "@MEDIA (max-width: /*comment*/ ) {}",
fixed: "@MEDIA ( max-width: /*comment*/ ) {}",
message: messages.expectedOpening,
line: 1,
column: 9
},
{
code: "@MEDIA (max-width: 300px ) {}",
fixed: "@MEDIA ( max-width: 300px ) {}",
message: messages.expectedOpening,
line: 1,
column: 9
},
{
code: "@media ( max-width: 300px) {}",
fixed: "@media ( max-width: 300px ) {}",
message: messages.expectedClosing,
line: 1,
column: 25
},
{
code: "@media ( max-width: /*comment*/) {}",
fixed: "@media ( max-width: /*comment*/ ) {}",
message: messages.expectedClosing,
line: 1,
column: 31
},
{
code: "@media screen and (color ), projection and ( color ) {}",
fixed: "@media screen and ( color ), projection and ( color ) {}",
message: messages.expectedOpening,
line: 1,
column: 20
},
{
code: "@media screen and ( color), projection and ( color ) {}",
fixed: "@media screen and ( color ), projection and ( color ) {}",
message: messages.expectedClosing,
line: 1,
column: 25
},
{
code: "@media screen and ( color ), projection and (color ) {}",
fixed: "@media screen and ( color ), projection and ( color ) {}",
message: messages.expectedOpening,
line: 1,
column: 46
},
{
code: "@media screen and ( color ), projection and ( color) {}",
fixed: "@media screen and ( color ), projection and ( color ) {}",
message: messages.expectedClosing,
line: 1,
column: 51
},
{
code: "@media ( grid ) and (max-width: 15em ) {}",
fixed: "@media ( grid ) and ( max-width: 15em ) {}",
message: messages.expectedOpening,
line: 1,
column: 22
Expand All @@ -101,7 +112,7 @@ testRule(rule, {
testRule(rule, {
ruleName,
config: ["never"],

fixed: true,
accept: [
{
code: "@media (max-width: 300px) {}"
Expand All @@ -126,66 +137,77 @@ testRule(rule, {
reject: [
{
code: "@media (max-width: 300px ) {}",
fixed: "@media (max-width: 300px) {}",
message: messages.rejectedClosing,
line: 1,
column: 25
},
{
code: "@mEdIa (max-width: 300px ) {}",
fixed: "@mEdIa (max-width: 300px) {}",
message: messages.rejectedClosing,
line: 1,
column: 25
},
{
code: "@MEDIA (max-width: /*comment*/ ) {}",
fixed: "@MEDIA (max-width: /*comment*/) {}",
message: messages.rejectedClosing,
line: 1,
column: 31
},
{
code: "@MEDIA (max-width: 300px ) {}",
fixed: "@MEDIA (max-width: 300px) {}",
message: messages.rejectedClosing,
line: 1,
column: 25
},
{
code: "@media ( max-width: 300px) {}",
fixed: "@media (max-width: 300px) {}",
message: messages.rejectedOpening,
line: 1,
column: 9
},
{
code: "@media ( max-width: /*comment*/) {}",
fixed: "@media (max-width: /*comment*/) {}",
message: messages.rejectedOpening,
line: 1,
column: 9
},
{
code: "@media screen and (color ), projection and (color) {}",
fixed: "@media screen and (color), projection and (color) {}",
message: messages.rejectedClosing,
line: 1,
column: 25
},
{
code: "@media screen and ( color), projection and (color) {}",
fixed: "@media screen and (color), projection and (color) {}",
message: messages.rejectedOpening,
line: 1,
column: 20
},
{
code: "@media screen and (color), projection and (color ) {}",
fixed: "@media screen and (color), projection and (color) {}",
message: messages.rejectedClosing,
line: 1,
column: 49
},
{
code: "@media screen and (color), projection and ( color) {}",
fixed: "@media screen and (color), projection and (color) {}",
message: messages.rejectedOpening,
line: 1,
column: 44
},
{
code: "@media (grid) and (max-width: 15em ) {}",
fixed: "@media (grid) and (max-width: 15em) {}",
message: messages.rejectedClosing,
line: 1,
column: 35
Expand Down
84 changes: 48 additions & 36 deletions lib/rules/media-feature-parentheses-space-inside/index.js
Expand Up @@ -4,8 +4,8 @@ const _ = require("lodash");
const atRuleParamIndex = require("../../utils/atRuleParamIndex");
const report = require("../../utils/report");
const ruleMessages = require("../../utils/ruleMessages");
const styleSearch = require("style-search");
const validateOptions = require("../../utils/validateOptions");
const valueParser = require("postcss-value-parser");

const ruleName = "media-feature-parentheses-space-inside";

Expand All @@ -16,7 +16,7 @@ const messages = ruleMessages(ruleName, {
rejectedClosing: 'Unexpected whitespace before ")"'
});

const rule = function(expectation) {
const rule = function(expectation, options, context) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
Expand All @@ -31,50 +31,62 @@ const rule = function(expectation) {
// will be at atRule.raws.params.raw
const params = _.get(atRule, "raws.params.raw", atRule.params);
const indexBoost = atRuleParamIndex(atRule);
const violations = [];

styleSearch({ source: params, target: "(" }, match => {
const nextCharIsSpace = params[match.startIndex + 1] === " ";
if (nextCharIsSpace && expectation === "never") {
report({
message: messages.rejectedOpening,
node: atRule,
index: match.startIndex + 1 + indexBoost,
result,
ruleName
});
}
if (!nextCharIsSpace && expectation === "always") {
report({
message: messages.expectedOpening,
node: atRule,
index: match.startIndex + 1 + indexBoost,
result,
ruleName
});
const parsedParams = valueParser(params).walk(node => {
if (node.type === "function") {
const len = valueParser.stringify(node).length;
if (expectation === "never") {
if (node.before === " ") {
if (context.fix) node.before = "";
violations.push({
message: messages.rejectedOpening,
index: node.sourceIndex + 1 + indexBoost
});
}
if (node.after === " ") {
if (context.fix) node.after = "";
violations.push({
message: messages.rejectedClosing,
index: node.sourceIndex - 2 + len + indexBoost
});
}
}
else if (expectation === "always") {
if (node.before === "") {
if (context.fix) node.before = " ";
violations.push({
message: messages.expectedOpening,
index: node.sourceIndex + 1 + indexBoost
});
}
if (node.after === "") {
if (context.fix) node.after = " ";
violations.push({
message: messages.expectedClosing,
index: node.sourceIndex - 2 + len + indexBoost
});
}
}
}
});

styleSearch({ source: params, target: ")" }, match => {
const prevCharIsSpace = params[match.startIndex - 1] === " ";
if (prevCharIsSpace && expectation === "never") {
report({
message: messages.rejectedClosing,
node: atRule,
index: match.startIndex - 1 + indexBoost,
result,
ruleName
});
if (violations.length) {
if (context.fix) {
atRule.params = parsedParams.toString();
return;
}
if (!prevCharIsSpace && expectation === "always") {

violations.forEach(err => {
report({
message: messages.expectedClosing,
message: err.message,
node: atRule,
index: match.startIndex - 1 + indexBoost,
index: err.index,
result,
ruleName
});
}
});
});
}
});
};
};
Expand Down