@primer/stylelint-config 12.3.1-rc.cb3fa53 → 12.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 12.3.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#215](https://github.com/primer/stylelint-config/pull/215) [`66b16ae`](https://github.com/primer/stylelint-config/commit/66b16ae2edd81f8c8949f83c96d7011e5d395cc0) Thanks [@jonrohan](https://github.com/jonrohan)! - Making linter pick up separate function groups
8
+
3
9
  ## 12.3.1
4
10
 
5
11
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@primer/stylelint-config",
3
- "version": "12.3.1-rc.cb3fa53",
3
+ "version": "12.3.2",
4
4
  "description": "Sharable stylelint config used by GitHub's CSS",
5
5
  "homepage": "http://primer.style/css/tools/linting",
6
6
  "author": "GitHub, Inc.",
@@ -20,7 +20,7 @@
20
20
  "plugins/"
21
21
  ],
22
22
  "scripts": {
23
- "test": "jest",
23
+ "test": "jest --coverage false",
24
24
  "lint": "eslint .",
25
25
  "release": "changeset publish"
26
26
  },
@@ -2,6 +2,7 @@ const stylelint = require('stylelint')
2
2
  const declarationValueIndex = require('stylelint/lib/utils/declarationValueIndex')
3
3
  const valueParser = require('postcss-value-parser')
4
4
 
5
+ // TODO: Pull this in from primer/primitives
5
6
  const spacerValues = {
6
7
  '$spacer-1': '4px',
7
8
  '$spacer-2': '8px',
@@ -34,6 +35,17 @@ const messages = stylelint.utils.ruleMessages(ruleName, {
34
35
  }
35
36
  })
36
37
 
38
+ const walkGroups = (root, validate) => {
39
+ for (const node of root.nodes) {
40
+ if (node.type === 'function') {
41
+ walkGroups(node, validate)
42
+ } else {
43
+ validate(node)
44
+ }
45
+ }
46
+ return root
47
+ }
48
+
37
49
  // eslint-disable-next-line no-unused-vars
38
50
  module.exports = stylelint.createPlugin(ruleName, (enabled, options = {}, context) => {
39
51
  if (!enabled) {
@@ -47,63 +59,55 @@ module.exports = stylelint.createPlugin(ruleName, (enabled, options = {}, contex
47
59
  }
48
60
 
49
61
  const problems = []
50
- let containsMath = false
51
- let conatinsVariable = false
52
- const parsedValue = valueParser(decl.value).walk(declValue => {
62
+
63
+ const parsedValue = walkGroups(valueParser(decl.value), node => {
64
+ // Remove leading negative sign, if any.
65
+ const cleanValue = node.value.replace(/^-/g, '')
66
+
53
67
  // Only check word types. https://github.com/TrySound/postcss-value-parser#word
54
- if (!['word', 'function'].includes(declValue.type)) {
55
- return false
68
+ if (node.type !== 'word') {
69
+ return
56
70
  }
57
71
 
58
- // Ignore values that are not numbers.
59
- if (['0', 'auto', 'inherit', 'initial'].includes(declValue.value)) {
60
- return noop
72
+ // Exact values to ignore.
73
+ if (['*', '+', '-', '/', '0', 'auto', 'inherit', 'initial'].includes(node.value)) {
74
+ return
75
+ }
76
+
77
+ const valueUnit = valueParser.unit(cleanValue)
78
+
79
+ if (valueUnit && (valueUnit.unit === '' || !/^[0-9]+$/.test(valueUnit.number))) {
80
+ return
61
81
  }
62
- // Remove leading negative sign, if any.
63
- const cleanDeclValue = declValue.value.replace(/^-/g, '')
64
82
 
65
83
  // If the a variable is found in the value, skip it.
66
84
  if (
67
85
  Object.keys(spacerValues).some(variable =>
68
- new RegExp(`${variable.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`).test(cleanDeclValue)
86
+ new RegExp(`${variable.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`).test(cleanValue)
69
87
  )
70
88
  ) {
71
- conatinsVariable = true
72
- return noop
89
+ return
73
90
  }
74
91
 
75
- // For now we're going to ignore math.
76
- if (['*', '+', '-', '/'].includes(declValue.value)) {
77
- containsMath = true
78
- return noop
79
- }
92
+ const replacement = Object.keys(spacerValues).find(spacer => spacerValues[spacer] === cleanValue) || null
93
+ const valueMatch = replacement ? spacerValues[replacement] : node.value
80
94
 
81
- let valueMatch = null
82
- if ((valueMatch = Object.keys(spacerValues).find(spacer => spacerValues[spacer] === cleanDeclValue))) {
83
- if (context.fix) {
84
- declValue.value = declValue.value.replace(spacerValues[valueMatch], valueMatch)
85
- } else {
86
- problems.push({
87
- index: declarationValueIndex(decl) + declValue.sourceIndex,
88
- message: messages.rejected(spacerValues[valueMatch], valueMatch)
89
- })
90
- }
91
- } else if (declValue.value !== '' && declValue.type !== 'function' && !containsMath) {
95
+ if (replacement && context.fix) {
96
+ node.value = node.value.replace(valueMatch, replacement)
97
+ } else {
92
98
  problems.push({
93
- index: declarationValueIndex(decl) + declValue.sourceIndex,
94
- message: messages.rejected(declValue.value, null)
99
+ index: declarationValueIndex(decl) + node.sourceIndex,
100
+ message: messages.rejected(valueMatch, replacement)
95
101
  })
96
102
  }
103
+
104
+ return
97
105
  })
98
106
 
99
107
  if (context.fix) {
100
108
  decl.value = parsedValue.toString()
101
109
  }
102
110
 
103
- if (containsMath && conatinsVariable) {
104
- return noop
105
- }
106
-
107
111
  if (problems.length) {
108
112
  for (const err of problems) {
109
113
  stylelint.utils.report({