Skip to content

Commit

Permalink
Enable fix option for font-weight-notation
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroppy committed Feb 7, 2018
1 parent 8a6d344 commit 045da2d
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/user-guide/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,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
81 changes: 81 additions & 0 deletions lib/rules/font-weight-notation/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,84 @@ testRule(rule, {
}
]
});

testRule(rule, {
ruleName,
config: ["numeric"],
fix: true,
accept: [
{
code: "a { font-weight: 400; }"
},
{
code: "a { font-weight: 700; }"
},
{
code: "a { font-weight: 800; }"
},
{
code: "a { font: italic 400 20px; }"
}
],
reject: [
{
code: "a { font-weight: normal; }",
fixed: "a { font-weight: 400; }",
message: messages.expected("numeric")
},
{
code: "a { font-weight: bold; }",
fixed: "a { font-weight: 700; }",
message: messages.expected("numeric")
},
{
code: "a { font: italic normal 20px; }",
fixed: "a { font: italic 400 20px; }",
message: messages.expected("numeric")
},
{
code: "a { font: italic bold 20px; }",
fixed: "a { font: italic 700 20px; }",
message: messages.expected("numeric")
}
]
});

testRule(rule, {
ruleName,
config: ["named-where-possible"],
fix: true,
accept: [
{
code: "a { font-weight: normal; }"
},
{
code: "a { font-weight: bold; }"
},
{
code: "a { font: italic bold 20px; }"
}
],
reject: [
{
code: "a { font-weight: 400; }",
fixed: "a { font-weight: normal; }",
message: messages.expected("named")
},
{
code: "a { font-weight: 700; }",
fixed: "a { font-weight: bold; }",
message: messages.expected("named")
},
{
code: "a { font: italic 400 20px; }",
fixed: "a { font: italic normal 20px; }",
message: messages.expected("named")
},
{
code: "a { font: italic 700 20px; }",
fixed: "a { font: italic bold 20px; }",
message: messages.expected("named")
}
]
});
18 changes: 17 additions & 1 deletion lib/rules/font-weight-notation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ const messages = ruleMessages(ruleName, {
const INHERIT_KEYWORD = "inherit";
const INITIAL_KEYWORD = "initial";
const NORMAL_KEYWORD = "normal";
const BOLD_KEYWORD = "bold";
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 Down Expand Up @@ -53,6 +54,10 @@ const rule = function(expectation, options) {
if (decl.prop.toLowerCase() === "font") {
checkFont(decl);
}

if (context.fix) {
decl.value = replaceWord(expectation, decl.value);
}
});

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

function replaceWord(optionName, input) {
if (optionName === "named-where-possible") {
return input.replace(/400/g, NORMAL_KEYWORD).replace(/700/g, BOLD_KEYWORD);
}
if (optionName === "numeric") {
return input
.replace(/normal/g, WEIGHTS_WITH_KEYWORD_EQUIVALENTS[0])
.replace(/bold/g, WEIGHTS_WITH_KEYWORD_EQUIVALENTS[1]);
}
}

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

0 comments on commit 045da2d

Please sign in to comment.