Skip to content

Commit

Permalink
Add ignoreFontFamilies option to font-family-no-missing-generic-famil…
Browse files Browse the repository at this point in the history
…y-keyword (#4656)
  • Loading branch information
mattfelten committed Apr 3, 2020
1 parent db585b3 commit 90f72ab
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
24 changes: 24 additions & 0 deletions lib/rules/font-family-no-missing-generic-family-keyword/README.md
Expand Up @@ -53,3 +53,27 @@ a { font: inherit; }
```css
a { font: caption; }
```

## Optional secondary options

### `ignoreFontFamilies: ["/regex/", /regex/, "string"]`

Given:

```
["custom-font"]
```

The following pattern is _not_ considered a violation:

<!-- prettier-ignore -->
```css
a { font-family: custom-font; }
```

The following pattern is considered a violation:

<!-- prettier-ignore -->
```css
a { font-family: invalid-custom-font; }
```
Expand Up @@ -6,7 +6,6 @@ const { messages, ruleName } = rule;
testRule(rule, {
ruleName,
config: [true],

accept: [
{
code: 'a { font-family: "Lucida Grande", "Arial", sans-serif; }',
Expand Down Expand Up @@ -130,3 +129,53 @@ testRule(rule, {
},
],
});

testRule(rule, {
ruleName,
config: [
true,
{
ignoreFontFamilies: ['custom-font', /foo/, '/baz[0-9]/'],
},
],
accept: [
{
code: 'a { font-family: custom-font; }',
},
{
code: 'a { font: 1em Lucida Grande, Arial, custom-font }',
},
{
code: 'a { font: foo-font }',
},
{
code: 'a { font: baz7 }',
},
],
reject: [
{
code: 'a { font-family: Arial; }',
message: messages.rejected,
line: 1,
column: 18,
},
{
code: 'a { font: 1em Lucida Grande, Arial, "sans-serif" }',
message: messages.rejected,
line: 1,
column: 37,
},
{
code: 'a { font: bar-font }',
message: messages.rejected,
line: 1,
column: 11,
},
{
code: 'a { font: baz-7 }',
message: messages.rejected,
line: 1,
column: 11,
},
],
});
22 changes: 20 additions & 2 deletions lib/rules/font-family-no-missing-generic-family-keyword/index.js
Expand Up @@ -3,10 +3,13 @@
const declarationValueIndex = require('../../utils/declarationValueIndex');
const findFontFamily = require('../../utils/findFontFamily');
const keywordSets = require('../../reference/keywordSets');
const optionsMatches = require('../../utils/optionsMatches');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');

const _ = require('lodash');

const ruleName = 'font-family-no-missing-generic-family-keyword';

const messages = ruleMessages(ruleName, {
Expand All @@ -16,9 +19,20 @@ const messages = ruleMessages(ruleName, {
const isFamilyNameKeyword = (node) =>
!node.quote && keywordSets.fontFamilyKeywords.has(node.value.toLowerCase());

function rule(actual) {
function rule(actual, options) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, { actual });
const validOptions = validateOptions(
result,
ruleName,
{ actual },
{
actual: options,
possible: {
ignoreFontFamilies: [_.isString, _.isRegExp],
},
optional: true,
},
);

if (!validOptions) {
return;
Expand Down Expand Up @@ -48,6 +62,10 @@ function rule(actual) {
return;
}

if (fontFamilies.some((node) => optionsMatches(options, 'ignoreFontFamilies', node.value))) {
return;
}

report({
result,
ruleName,
Expand Down

0 comments on commit 90f72ab

Please sign in to comment.