stylelint-gamut 1.1.0 → 1.1.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stylelint-gamut",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Stylelint plugin for working with different color spaces",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -3,15 +3,11 @@
3
3
  const valueParser = require('postcss-value-parser')
4
4
  const stylelint = require('stylelint')
5
5
  const Color = require('colorjs.io').default
6
-
7
- const {
8
- isStandardSyntaxProperty,
9
- isCustomProperty,
10
- getDeclarationValue,
11
- declarationValueIndex,
12
- isInColorGamutP3MediaQuery,
13
- isInColorGamutRec2020MediaQuery
14
- } = require('./utils')
6
+ const isStandardSyntaxProperty = require('./utils/isStandardSyntaxProperty')
7
+ const isCustomProperty = require('./utils/isCustomProperty')
8
+ const getDeclarationValue = require('./utils/getDeclarationValue')
9
+ const isAtRule = require('./utils/isAtRule')
10
+ const declarationValueIndex = require('./utils/declarationValueIndex')
15
11
 
16
12
  const ruleName = 'gamut/color-no-out-gamut-range'
17
13
 
@@ -50,17 +46,13 @@ const ruleFunction = (primary) => {
50
46
  return
51
47
  }
52
48
 
53
- const color = new Color(valueParser.stringify(nodes))
54
-
55
- const isInSrgbGamut = color.inGamut('srgb')
56
- const isInP3Query = isInColorGamutP3MediaQuery(decl)
57
- if (isInSrgbGamut || isInP3Query) return
49
+ const isInSrgbGamut = new Color(valueParser.stringify(nodes)).inGamut(
50
+ 'srgb'
51
+ )
58
52
 
59
- const isInP3Gamut = color.inGamut('p3')
60
- if (isInP3Gamut && isInP3Query) return
53
+ if (isInSrgbGamut) return
61
54
 
62
- const isInRec2020Gamut = color.inGamut('rec2020')
63
- if (isInRec2020Gamut && isInColorGamutRec2020MediaQuery(decl)) return
55
+ if (isInColorGamutP3MediaQuery(decl)) return
64
56
 
65
57
  const index = declarationValueIndex(decl) + node.sourceIndex
66
58
  const endIndex = index + decl.value.length
@@ -78,6 +70,20 @@ const ruleFunction = (primary) => {
78
70
  }
79
71
  }
80
72
 
73
+ /**
74
+ * @param {import('postcss').Declaration} decl
75
+ * @returns {boolean}
76
+ */
77
+ function isInColorGamutP3MediaQuery (decl) {
78
+ if (decl.parent && decl.parent.parent && isAtRule(decl.parent.parent)) {
79
+ const parent = decl.parent.parent
80
+
81
+ return parent.name === 'media' && parent.params === '(color-gamut: p3)'
82
+ }
83
+
84
+ return false
85
+ }
86
+
81
87
  ruleFunction.ruleName = ruleName
82
88
  ruleFunction.messages = messages
83
89
  ruleFunction.meta = meta
package/src/index.test.js CHANGED
@@ -9,16 +9,12 @@ testRule({
9
9
  accept: [
10
10
  {
11
11
  code: '@media (color-gamut: p3) { a { color: lch(48% 82 283); } }',
12
- description: 'lch out of srgb gamut but wrapped in p3 media query'
13
- },
14
- {
15
- code: '@media (color-gamut: rec2020) { a { color: lch(48% 82 283); } }',
16
- description: 'lch out of p3 gamut but wrapped in rec2020 media query'
12
+ description: 'lch out of srgb gamut but wrapped in media query'
17
13
  },
18
14
  {
19
15
  code: '@media (color-gamut: p3) { a { color: oklch(48% 82 283 / 73%); } }',
20
16
  description:
21
- 'oklch with alpha out of srgb gamut but wrapped in p3 media query'
17
+ 'oklch with alpha out of srgb gamut but wrapped in media query'
22
18
  },
23
19
  {
24
20
  code: 'a { color: lch(50% 0 0); }',
@@ -52,15 +48,6 @@ testRule({
52
48
  endLine: 1,
53
49
  endColumn: 27
54
50
  },
55
- {
56
- code: '@media (color-gamut: srgb) { a { color: lch(48% 82 283); } }',
57
- description: 'lch is out of srgb and wrapped in srgb media query',
58
- message: messages.rejected('lch(48% 82 283)'),
59
- line: 1,
60
- column: 41,
61
- endLine: 1,
62
- endColumn: 56
63
- },
64
51
  {
65
52
  code: 'a { color: lch(48% 82 283 / 72%); }',
66
53
  description: 'lch with alpha',
@@ -0,0 +1,29 @@
1
+ 'use strict'
2
+
3
+ /**
4
+ * Get the index of a declaration's value
5
+ *
6
+ * @param {import('postcss').Declaration} decl
7
+ * @returns {number}
8
+ */
9
+ module.exports = function declarationValueIndex (decl) {
10
+ const raws = decl.raws
11
+
12
+ return [
13
+ // @ts-expect-error -- TS2571: Object is of type 'unknown'.
14
+ raws.prop && raws.prop.prefix,
15
+ // @ts-expect-error -- TS2571: Object is of type 'unknown'.
16
+ (raws.prop && raws.prop.raw) || decl.prop,
17
+ // @ts-expect-error -- TS2571: Object is of type 'unknown'.
18
+ raws.prop && raws.prop.suffix,
19
+ raws.between || ':',
20
+ // @ts-expect-error -- TS2339: Property 'prefix' does not exist on type '{ value: string; raw: string; }'.
21
+ raws.value && raws.value.prefix
22
+ ].reduce((count, str) => {
23
+ if (str) {
24
+ return count + str.length
25
+ }
26
+
27
+ return count
28
+ }, 0)
29
+ }
@@ -0,0 +1,11 @@
1
+ 'use strict'
2
+
3
+ /**
4
+ * @param {import('postcss').Declaration} decl
5
+ * @returns {string}
6
+ */
7
+ module.exports = function getDeclarationValue (decl) {
8
+ const raws = decl.raws
9
+
10
+ return (raws.value && raws.value.raw) || decl.value
11
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @param {Node} node
3
+ * @returns {node is import('postcss').AtRule}
4
+ */
5
+ module.exports = function isAtRule (node) {
6
+ return node.type === 'atrule'
7
+ }
@@ -0,0 +1,10 @@
1
+ 'use strict'
2
+
3
+ /**
4
+ * Check whether a property is a custom one
5
+ * @param {string} property
6
+ * @returns {boolean}
7
+ */
8
+ module.exports = function isCustomProperty (property) {
9
+ return property.startsWith('--')
10
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Check whether a property is standard
3
+ *
4
+ * @param {string} property
5
+ * @return {boolean} If `true`, the property is standard
6
+ */
7
+
8
+ module.exports = function isStandardSyntaxProperty (property) {
9
+ // SCSS var (e.g. $var: x), list (e.g. $list: (x)) or map (e.g. $map: (key:value))
10
+ if (property.startsWith('$')) {
11
+ return false
12
+ }
13
+
14
+ // Less var (e.g. @var: x)
15
+ if (property.startsWith('@')) {
16
+ return false
17
+ }
18
+
19
+ // SCSS or Less interpolation
20
+ if (/#{.+?}|@{.+?}|\$\(.+?\)/.test(property)) {
21
+ return false
22
+ }
23
+
24
+ return true
25
+ }
package/src/utils.js DELETED
@@ -1,127 +0,0 @@
1
- 'use strict'
2
-
3
- /**
4
- * Get the index of a declaration's value
5
- *
6
- * @param {import('postcss').Declaration} decl
7
- * @returns {number}
8
- */
9
- function declarationValueIndex (decl) {
10
- const raws = decl.raws
11
-
12
- return [
13
- // @ts-expect-error -- TS2571: Object is of type 'unknown'.
14
- raws.prop && raws.prop.prefix,
15
- // @ts-expect-error -- TS2571: Object is of type 'unknown'.
16
- (raws.prop && raws.prop.raw) || decl.prop,
17
- // @ts-expect-error -- TS2571: Object is of type 'unknown'.
18
- raws.prop && raws.prop.suffix,
19
- raws.between || ':',
20
- // @ts-expect-error -- TS2339: Property 'prefix' does not exist on type '{ value: string; raw: string; }'.
21
- raws.value && raws.value.prefix
22
- ].reduce((count, str) => {
23
- if (str) {
24
- return count + str.length
25
- }
26
-
27
- return count
28
- }, 0)
29
- }
30
-
31
- /**
32
- * @param {import('postcss').Declaration} decl
33
- * @returns {string}
34
- */
35
- function getDeclarationValue (decl) {
36
- const raws = decl.raws
37
-
38
- return (raws.value && raws.value.raw) || decl.value
39
- }
40
-
41
- /**
42
- * @param {import('postcss').Declaration} decl
43
- * @returns {boolean | import('postcss').AtRule}
44
- */
45
- function getMediaQueryAtRule (decl) {
46
- if (decl.parent && decl.parent.parent && isAtRule(decl.parent.parent)) {
47
- const parent = decl.parent.parent
48
-
49
- return parent.name === 'media' ? parent : false
50
- }
51
-
52
- return false
53
- }
54
-
55
- /**
56
- * @param {Node} node
57
- * @returns {node is import('postcss').AtRule}
58
- */
59
- function isAtRule (node) {
60
- return node.type === 'atrule'
61
- }
62
-
63
- /**
64
- * Check whether a property is a custom one
65
- * @param {string} property
66
- * @returns {boolean}
67
- */
68
- function isCustomProperty (property) {
69
- return property.startsWith('--')
70
- }
71
-
72
- /**
73
- * Check whether a property is standard
74
- *
75
- * @param {string} property
76
- * @return {boolean} If `true`, the property is standard
77
- */
78
-
79
- function isStandardSyntaxProperty (property) {
80
- // SCSS var (e.g. $var: x), list (e.g. $list: (x)) or map (e.g. $map: (key:value))
81
- if (property.startsWith('$')) {
82
- return false
83
- }
84
-
85
- // Less var (e.g. @var: x)
86
- if (property.startsWith('@')) {
87
- return false
88
- }
89
-
90
- // SCSS or Less interpolation
91
- if (/#{.+?}|@{.+?}|\$\(.+?\)/.test(property)) {
92
- return false
93
- }
94
-
95
- return true
96
- }
97
-
98
- /**
99
- * @param {import('postcss').Declaration} decl
100
- * @returns {boolean}
101
- */
102
- function isInColorGamutP3MediaQuery (decl) {
103
- const mediaQueryAtRule = getMediaQueryAtRule(decl)
104
-
105
- return mediaQueryAtRule && mediaQueryAtRule.params === '(color-gamut: p3)'
106
- }
107
-
108
- /**
109
- * @param {import('postcss').Declaration} decl
110
- * @returns {boolean}
111
- */
112
- function isInColorGamutRec2020MediaQuery (decl) {
113
- const mediaQueryAtRule = getMediaQueryAtRule(decl)
114
-
115
- return mediaQueryAtRule && mediaQueryAtRule.params === '(color-gamut: rec2020)'
116
- }
117
-
118
- module.exports = {
119
- declarationValueIndex,
120
- getDeclarationValue,
121
- getMediaQueryAtRule,
122
- isCustomProperty,
123
- isStandardSyntaxProperty,
124
- isAtRule,
125
- isInColorGamutP3MediaQuery,
126
- isInColorGamutRec2020MediaQuery
127
- }