Skip to content

Commit

Permalink
Add ignoreFontFamilyName option to font-family-no-duplicate (#2314)
Browse files Browse the repository at this point in the history
* Adds tests for ignoreFontFamilyName option to font-family-no-duplicate-names

* Add secondary option ignoreFontFamilyNames to font-family-no-duplicate-names

* Add documentation for secondary option ignoreFontFamilyNames

* Fix missing final newline in MD file

* Add documentation describing regex

* Add an additional test for case sensitivity in regex ignoreFontFamilyNames rule

* Improve explanation of ignoreFontFamilyNames
  • Loading branch information
CAYdenberg authored and davidtheclark committed Feb 11, 2017
1 parent e0a9876 commit 74d9d05
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
21 changes: 21 additions & 0 deletions lib/rules/font-family-no-duplicate-names/README.md
Expand Up @@ -45,3 +45,24 @@ a { font: 1em "Arial", "sans-serif", sans-serif; }
```css
a { font: normal 14px/32px -apple-system, BlinkMacSystemFont, sans-serif; }
```

## Optional secondary options

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

Given:

```js
["/^My Font /", "monospace"]
```

The following patterns are *not* considered warnings:

```css
font-family: monospace, monospace
```

```css
font-family: "My Font Family", "My Font Family", monospace
```

24 changes: 24 additions & 0 deletions lib/rules/font-family-no-duplicate-names/__tests__/index.js
Expand Up @@ -55,3 +55,27 @@ testRule(rule, {
} ],

})

testRule(rule, {
ruleName,
config: [ true, { ignoreFontFamilyNames: [ "monospace", "/my-/" ] } ],

accept: [ {
code: "pre { font-family: monospace, monospace }",
}, {
code: "pre { font: 1em monospace, monospace}",
}, {
code: "pre { font-family: monospace, \"Roberto Mono\", monospace}",
}, {
code: "pre { font-family: my-font, \"My-font\", sans-serif}",
}, {
code: "pre { font-family: \"My-font\", \"My-font\", sans-serif}",
} ],

reject: [{
code: "pre { font-family: \"Roberto Mono\", \"Roberto Mono\", monospace}",
message: messages.rejected("Roberto Mono"),
line: 1,
column: 36,
}],
})
24 changes: 17 additions & 7 deletions lib/rules/font-family-no-duplicate-names/index.js
Expand Up @@ -6,6 +6,8 @@ const report = require("../../utils/report")
const ruleMessages = require("../../utils/ruleMessages")
const validateOptions = require("../../utils/validateOptions")
const keywordSets = require("../../reference/keywordSets")
const _ = require("lodash")
const optionsMatches = require("../../utils/optionsMatches")

const ruleName = "font-family-no-duplicate-names"

Expand All @@ -15,9 +17,15 @@ const messages = ruleMessages(ruleName, {

const isFamilyNameKeyword = node => !node.quote && keywordSets.fontFamilyKeywords.has(node.value.toLowerCase())

const rule = function (actual) {
const rule = function (actual, options) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, { actual })
const validOptions = validateOptions(result, ruleName, { actual }, {
actual: options,
possible: {
ignoreFontFamilyNames: [_.isString],
},
optional: true,
})
if (!validOptions) {
return
}
Expand All @@ -33,10 +41,14 @@ const rule = function (actual) {
}

fontFamilies.forEach(fontFamilyNode => {
if (isFamilyNameKeyword(fontFamilyNode)) {
const family = fontFamilyNode.value.toLowerCase()
const family = fontFamilyNode.value.trim()

if (keywords.has(family)) {
if (optionsMatches(options, "ignoreFontFamilyNames", fontFamilyNode.value.trim())) {
return
}

if (isFamilyNameKeyword(fontFamilyNode)) {
if (keywords.has(family.toLowerCase())) {
complain(messages.rejected(family), declarationValueIndex(decl) + fontFamilyNode.sourceIndex, decl)
return
}
Expand All @@ -45,8 +57,6 @@ const rule = function (actual) {
return
}

const family = fontFamilyNode.value.trim()

if (familyNames.has(family)) {
complain(messages.rejected(family), declarationValueIndex(decl) + fontFamilyNode.sourceIndex, decl)
return
Expand Down

0 comments on commit 74d9d05

Please sign in to comment.