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 font-weight-notation #3159

Closed
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 @@ -210,7 +210,7 @@ Here are all the rules within stylelint, grouped first [by category](../../VISIO

#### Font weight

- [`font-weight-notation`](../../lib/rules/font-weight-notation/README.md): Require numeric or named (where possible) `font-weight` values.
- [`font-weight-notation`](../../lib/rules/font-weight-notation/README.md): Require numeric or named (where possible) `font-weight` values (Autofixable).

#### Function

Expand Down
11 changes: 11 additions & 0 deletions lib/rules/font-weight-notation/__tests__/index.js
Expand Up @@ -6,6 +6,7 @@ const { messages, ruleName } = rule;
testRule(rule, {
ruleName,
config: ["numeric"],
fixed: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be fix: true. And all four testRule should have it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, I fixed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not fixed. I see fixed: true in ever testRule. It should be fix: true. Otherwise tests for autofixing don't executed.


accept: [
{
Expand Down Expand Up @@ -89,30 +90,35 @@ testRule(rule, {
reject: [
{
code: "a { font-weight: normal; }",
fixed: "a { font-weight: 400; }",
message: messages.expected("numeric"),
line: 1,
column: 18
},
{
code: "a { fOnT-wEiGhT: normal; }",
fixed: "a { fOnT-wEiGhT: 400; }",
message: messages.expected("numeric"),
line: 1,
column: 18
},
{
code: "a { FONT-WEIGHT: normal; }",
fixed: "a { FONT-WEIGHT: 400; }",
message: messages.expected("numeric"),
line: 1,
column: 18
},
{
code: "a { font-weight: nOrMaL; }",
fixed: "a { font-weight: 400; }",
message: messages.expected("numeric"),
line: 1,
column: 18
},
{
code: "a { font-weight: NORMAL; }",
fixed: "a { font-weight: 400; }",
message: messages.expected("numeric"),
line: 1,
column: 18
Expand Down Expand Up @@ -150,6 +156,7 @@ testRule(rule, {
testRule(rule, {
ruleName,
config: ["numeric", { ignore: ["relative"] }],
fixed: true,

accept: [
{
Expand All @@ -175,6 +182,7 @@ testRule(rule, {
reject: [
{
code: "a { font-weight: normal; }",
fixed: "a { font-weight: 400}",
message: messages.expected("numeric"),
line: 1,
column: 18
Expand All @@ -185,6 +193,7 @@ testRule(rule, {
testRule(rule, {
ruleName,
config: ["named-where-possible"],
fixed: true,

accept: [
{
Expand Down Expand Up @@ -263,6 +272,7 @@ testRule(rule, {
reject: [
{
code: "a { font-weight: 400; }",
fixed: "a { font-weight: normal; }",
message: messages.expected("named"),
line: 1,
column: 18
Expand All @@ -276,6 +286,7 @@ testRule(rule, {
},
{
code: "a { font: italic small-caps 700 16px/3 cursive; }",
fixed: "a { font: italic small-caps bold 16px/3 cursive; }",
message: messages.expected("named"),
line: 1,
column: 29
Expand Down
51 changes: 48 additions & 3 deletions lib/rules/font-weight-notation/index.js
Expand Up @@ -10,6 +10,7 @@ const optionsMatches = require("../../utils/optionsMatches");
const postcss = require("postcss");
const report = require("../../utils/report");
const ruleMessages = require("../../utils/ruleMessages");
const styleSearch = require("style-search");
const validateOptions = require("../../utils/validateOptions");

const ruleName = "font-weight-notation";
Expand All @@ -24,7 +25,7 @@ const INITIAL_KEYWORD = "initial";
const NORMAL_KEYWORD = "normal";
const WEIGHTS_WITH_KEYWORD_EQUIVALENTS = ["400", "700"];

const rule = function(expectation, options) {
const rule = function(expectation, options, context) {
return (root, result) => {
const validOptions = validateOptions(
result,
Expand All @@ -46,13 +47,42 @@ const rule = function(expectation, options) {
}

root.walkDecls(decl => {
if (decl.prop.toLowerCase() === "font-weight") {
const propName = decl.prop.toLowerCase();

if (propName === "font-weight") {
checkWeight(decl.value, decl);
}

if (decl.prop.toLowerCase() === "font") {
if (propName === "font") {
checkFont(decl);
}

if (context.fix) {
// delete property name
const declString = decl
.toString()
.replace(`${propName}:`, "")
.trim();

if (expectation === "named-where-possible") {
replaceWord(declString, "400", "normal", output => {
decl.value = output;
});

replaceWord(declString, "700", "bold", output => {
decl.value = output;
});
}
if (expectation === "numeric") {
replaceWord(declString, "normal", "400", output => {
decl.value = output;
});

replaceWord(declString, "bold", "700", output => {
decl.value = output;
});
}
}
});

function checkFont(decl) {
Expand Down Expand Up @@ -133,6 +163,21 @@ const rule = function(expectation, options) {
};
};

function replaceWord(input, searchString, replaceString, cb) {
styleSearch({ source: input, target: searchString }, match => {
if (match.insideParens || match.insideFunctionArguments) {
// e.g. var(--bold)
return cb(input);
}

const offset = match.startIndex;
const stringStart = input.slice(0, offset);
const stringEnd = input.slice(offset + searchString.length);

cb(`${stringStart}${replaceString}${stringEnd}`);
});
}

rule.ruleName = ruleName;
rule.messages = messages;
module.exports = rule;