forked from stylelint/stylelint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add font-family-no-duplicate-names rule (stylelint#2020)
- Loading branch information
1 parent
0ba6142
commit bfe5049
Showing
6 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# font-family-no-duplicate-names | ||
|
||
Disallow duplicate font family names. | ||
|
||
```css | ||
a { font-family: serif, serif; } | ||
/** ↑ ↑ | ||
* These font family names */ | ||
``` | ||
|
||
This rule checks the `font` and `font-family` properties. | ||
|
||
This rule ignores `$sass`, `@less`, and `var(--custom-property)` variable syntaxes. | ||
|
||
**Caveat:** This rule will stumble on *unquoted* multi-word font names and *unquoted* font names containing escape sequences. Wrap these font names in quotation marks, and everything should be fine. | ||
|
||
## Options | ||
|
||
### `true` | ||
|
||
The following patterns are considered warnings: | ||
|
||
```css | ||
a { font-family: 'Times', Times, serif; } | ||
``` | ||
|
||
```css | ||
a { font: 1em "Arial", 'Arial', sans-serif; } | ||
``` | ||
|
||
```css | ||
a { font: normal 14px/32px -apple-system, BlinkMacSystemFont, sans-serif, sans-serif; } | ||
``` | ||
|
||
The following patterns are *not* considered warnings: | ||
|
||
```css | ||
a { font-family: Times, serif; } | ||
``` | ||
|
||
```css | ||
a { font: 1em "Arial", "sans-serif", sans-serif; } | ||
``` | ||
|
||
```css | ||
a { font: normal 14px/32px -apple-system, BlinkMacSystemFont, sans-serif; } | ||
``` |
58 changes: 58 additions & 0 deletions
58
src/rules/font-family-no-duplicate-names/__tests__/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { | ||
messages, | ||
ruleName, | ||
} from ".." | ||
import rules from "../../../rules" | ||
import { testRule } from "../../../testUtils" | ||
|
||
const rule = rules[ruleName] | ||
|
||
testRule(rule, { | ||
ruleName, | ||
config: [true], | ||
|
||
accept: [ { | ||
code: "a { font-family: \"Lucida Grande\", \"Arial\", sans-serif; }", | ||
}, { | ||
code: "a { font: 1em \"Lucida Grande\", \'Arial\', sans-serif; }", | ||
}, { | ||
code: "a { font: 1em \"Lucida Grande\", \'Arial\', \"sans-serif\", sans-serif; }", | ||
}, { | ||
code: "a { font-family: Times, serif; }", | ||
}, { | ||
code: "b { font: normal 14px/32px -apple-system, BlinkMacSystemFont, sans-serif; }", | ||
} ], | ||
|
||
reject: [ { | ||
code: "a { font-family: \"Lucida Grande\", \'Arial\', sans-serif, sans-serif; }", | ||
message: messages.rejected("sans-serif"), | ||
line: 1, | ||
column: 56, | ||
}, { | ||
code: "a { font-family: \'Arial\', \"Lucida Grande\", Arial, sans-serif; }", | ||
message: messages.rejected("Arial"), | ||
line: 1, | ||
column: 44, | ||
}, { | ||
code: "a { fOnT-fAmIlY: \' Lucida Grande \', \"Lucida Grande\", sans-serif; }", | ||
message: messages.rejected("Lucida Grande"), | ||
line: 1, | ||
column: 38, | ||
}, { | ||
code: "a { font-family: \'Times\', Times, \"serif\", serif; }", | ||
message: messages.rejected("Times"), | ||
line: 1, | ||
column: 27, | ||
}, { | ||
code: "a { FONT: italic 300 16px/30px Arial, \" Arial\", serif; }", | ||
message: messages.rejected("Arial"), | ||
line: 1, | ||
column: 39, | ||
}, { | ||
code: "b { font: normal 14px/32px -apple-system, BlinkMacSystemFont, sans-serif, sans-serif; }", | ||
message: messages.rejected("sans-serif"), | ||
line: 1, | ||
column: 75, | ||
} ], | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { | ||
declarationValueIndex, | ||
findFontFamily, | ||
report, | ||
ruleMessages, | ||
validateOptions, | ||
} from "../../utils" | ||
import { fontFamilyKeywords } from "../../reference/keywordSets" | ||
|
||
export const ruleName = "font-family-no-duplicate-names" | ||
|
||
export const messages = ruleMessages(ruleName, { | ||
rejected: (name) => `Unexpected duplicate name ${name}`, | ||
}) | ||
|
||
const isFamilyNameKeyword = (node) => !node.quote && fontFamilyKeywords.has(node.value.toLowerCase()) | ||
|
||
export default function (actual) { | ||
return (root, result) => { | ||
const validOptions = validateOptions(result, ruleName, { actual }) | ||
if (!validOptions) { return } | ||
|
||
root.walkDecls(/^font(-family)?$/i, decl => { | ||
const keywords = new Set() | ||
const familyNames = new Set() | ||
|
||
const fontFamilies = findFontFamily(decl.value) | ||
|
||
if (fontFamilies.length === 0) { return } | ||
|
||
fontFamilies.forEach(fontFamilyNode => { | ||
if (isFamilyNameKeyword(fontFamilyNode)) { | ||
const family = fontFamilyNode.value.toLowerCase() | ||
|
||
if (keywords.has(family)) { | ||
complain(messages.rejected(family), declarationValueIndex(decl) + fontFamilyNode.sourceIndex, decl) | ||
return | ||
} | ||
|
||
keywords.add(family) | ||
return | ||
} | ||
|
||
const family = fontFamilyNode.value.trim() | ||
|
||
if (familyNames.has(family)) { | ||
complain(messages.rejected(family), declarationValueIndex(decl) + fontFamilyNode.sourceIndex, decl) | ||
return | ||
} | ||
|
||
familyNames.add(family) | ||
}) | ||
}) | ||
|
||
function complain(message, index, decl) { | ||
report({ | ||
result, | ||
ruleName, | ||
message, | ||
node: decl, | ||
index, | ||
}) | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters