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

Fix false positives for variables in font-family-no-missing-generic-family-keyword #5240

Merged
merged 1 commit into from Apr 20, 2021
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
10 changes: 10 additions & 0 deletions lib/rules/font-family-no-missing-generic-family-keyword/README.md
Expand Up @@ -54,6 +54,16 @@ a { font: inherit; }
a { font: caption; }
```

<!-- prettier-ignore -->
```css
a { font-family: var(--font-family-common); }
```

<!-- prettier-ignore -->
```css
a { font-family: Helvetica, var(--font-family-common); }
```

Copy link
Member Author

Choose a reason for hiding this comment

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

[note] I think it valuable to add examples which are using variables.

## Optional secondary options

### `ignoreFontFamilies: ["/regex/", /regex/, "string"]`
Expand Down
Expand Up @@ -75,12 +75,24 @@ testRule({
{
code: 'a { font: @font-family; }',
},
{
code: 'a { font: "font", @font-family; }',
},
{
code: 'a { font: $font-family; }',
},
{
code: 'a { font: "font", $font-family; }',
},
{
code: 'a { font: namespace.$font-family; }',
},
{
code: 'a { font: "font", namespace.$font-family; }',
},
{
code: 'a { font-family: var(--font); }',
},
{
code: 'a { font-family: "font", var(--font); }',
},
Expand Down
15 changes: 11 additions & 4 deletions lib/rules/font-family-no-missing-generic-family-keyword/index.js
Expand Up @@ -4,6 +4,7 @@

const declarationValueIndex = require('../../utils/declarationValueIndex');
const findFontFamily = require('../../utils/findFontFamily');
const isStandardSyntaxValue = require('../../utils/isStandardSyntaxValue');
const isVariable = require('../../utils/isVariable');
const keywordSets = require('../../reference/keywordSets');
const optionsMatches = require('../../utils/optionsMatches');
Expand All @@ -23,6 +24,12 @@ const messages = ruleMessages(ruleName, {
const isFamilyNameKeyword = (node) =>
!node.quote && keywordSets.fontFamilyKeywords.has(node.value.toLowerCase());

const isLastFontFamilyVariable = (value) => {
const lastValue = postcss.list.comma(value).pop();
Copy link
Member Author

Choose a reason for hiding this comment

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

[note] postcss.list.comma() seems more suitable than postcss.list.space() in this case.

See also https://postcss.org/api/#list


return isVariable(lastValue) || !isStandardSyntaxValue(lastValue);
};

function rule(actual, options) {
return (root, result) => {
const validOptions = validateOptions(
Expand Down Expand Up @@ -56,6 +63,10 @@ function rule(actual, options) {
return;
}

if (isLastFontFamilyVariable(decl.value)) {
return;
}

const fontFamilies = findFontFamily(decl.value);

if (fontFamilies.length === 0) {
Expand All @@ -66,10 +77,6 @@ function rule(actual, options) {
return;
}

if (postcss.list.space(decl.value).some(isVariable)) {
return;
}

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