stylelint-gamut 1.2.2 → 1.2.5

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/README.md CHANGED
@@ -2,16 +2,23 @@
2
2
 
3
3
  [![npm version][npm-version-img]][npm] [![npm downloads last month][npm-downloads-img]][npm]
4
4
 
5
- **stylelint-gamut** is a [Stylelint] plugin that helps you to work with different color spaces.
5
+ **stylelint-gamut** is a [Stylelint] plugin that helps you to work with different color spaces.
6
6
 
7
- Right now it's working with **node version ≥ 16.0.0** and **Stylelint version ≥ 14.0.0**.
7
+ # Rules
8
+
9
+ * [`color-no-out-gamut-range`](./src/README.md): Throw warning if color goes out of sRGB color space and is not wrapped in `@media (color-gamut: p3) {}`.
10
+
11
+ # Requirements
12
+
13
+ - **node version ≥ 16.0.0**
14
+ - **Stylelint version ≥ 14.0.0**.
8
15
 
9
16
  # Installation
10
17
 
11
18
  1. If you haven't, install [Stylelint]:
12
19
 
13
20
  ```
14
- npm install stylelint --save-dev
21
+ npm install stylelint stylelint-config-standard --save-dev
15
22
  ```
16
23
 
17
24
  2. Install `stylelint-gamut`:
@@ -20,7 +27,7 @@ npm install stylelint --save-dev
20
27
  npm install stylelint-gamut --save-dev
21
28
  ```
22
29
 
23
- ## Usage
30
+ # Usage
24
31
 
25
32
  Add `stylelint-gamut` to your Stylelint config `plugins` array, then add rules you need to the rules list. All rules from `stylelint-gamut` need to be namespaced with `gamut`.
26
33
 
@@ -35,11 +42,16 @@ Add `stylelint-gamut` to your Stylelint config `plugins` array, then add rules y
35
42
  }
36
43
  ```
37
44
 
38
- ## Rules
45
+ # More
46
+ It is already used by the creator of [PostCSS] [Andrey Sitnik].
39
47
 
40
- * [`color-no-out-gamut-range`](./src/README.md): Throw warning if color goes out of sRGB color space and is not wrapped in `@media (color-gamut: p3) {}`.
48
+ Read more about color spaces in css:
49
+ - [LCH colors in CSS: what, why, and how?](https://lea.verou.me/2020/04/lch-colors-in-css-what-why-and-how/)
50
+ - [A Guide To Modern CSS Colors](https://www.smashingmagazine.com/2021/11/guide-modern-css-colors/)
41
51
 
42
52
  [Stylelint]: https://stylelint.io/
43
53
  [npm-version-img]: https://img.shields.io/npm/v/stylelint-gamut.svg
44
54
  [npm-downloads-img]: https://img.shields.io/npm/dm/stylelint-gamut.svg
45
55
  [npm]: https://www.npmjs.com/package/stylelint-gamut
56
+ [PostCSS]: https://github.com/postcss/postcss
57
+ [Andrey Sitnik]: https://github.com/ai
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stylelint-gamut",
3
- "version": "1.2.2",
3
+ "version": "1.2.5",
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
@@ -2,9 +2,7 @@
2
2
 
3
3
  const stylelint = require('stylelint')
4
4
  const Color = require('colorjs.io').default
5
- const isStandardSyntaxProperty = require('./utils/isStandardSyntaxProperty')
6
- const isAtRule = require('./utils/isAtRule')
7
- const declarationValueIndex = require('./utils/declarationValueIndex')
5
+ const { isInColorGamutP3MediaQuery, isStandardSyntaxProperty, declarationValueIndex, isInColorGamutRec2020MediaQuery } = require('./utils')
8
6
 
9
7
  const ruleName = 'gamut/color-no-out-gamut-range'
10
8
 
@@ -51,11 +49,13 @@ const ruleFunction = (primary) => {
51
49
  }
52
50
  }
53
51
 
54
- const isInSrgbGamut = new Color(customPropValue || value).inGamut('srgb')
52
+ const color = new Color(customPropValue || value)
55
53
 
56
- if (isInSrgbGamut) return
54
+ if (color.inGamut('srgb')) return
57
55
 
58
- if (isInColorGamutP3MediaQuery(decl)) return
56
+ if (color.inGamut('p3') && isInColorGamutP3MediaQuery(decl)) return
57
+
58
+ if (color.inGamut('rec2020') && isInColorGamutRec2020MediaQuery(decl)) return
59
59
 
60
60
  if (decl.prop && decl.prop.startsWith('--')) {
61
61
  customProperties[decl.prop] = { value: decl.value.trim(), inQuery: false }
@@ -78,20 +78,6 @@ const ruleFunction = (primary) => {
78
78
  }
79
79
  }
80
80
 
81
- /**
82
- * @param {import('postcss').Declaration} decl
83
- * @returns {boolean}
84
- */
85
- function isInColorGamutP3MediaQuery (decl) {
86
- if (decl.parent && decl.parent.parent && isAtRule(decl.parent.parent)) {
87
- const parent = decl.parent.parent
88
-
89
- return parent.name === 'media' && /\(color-gamut:\s*p3\)/.test(parent.params)
90
- }
91
-
92
- return false
93
- }
94
-
95
81
  ruleFunction.ruleName = ruleName
96
82
  ruleFunction.messages = messages
97
83
  ruleFunction.meta = meta
package/src/utils.js ADDED
@@ -0,0 +1,111 @@
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 {Node} node
33
+ * @returns {node is import('postcss').AtRule}
34
+ */
35
+ function isAtRule (node) {
36
+ return node.type === 'atrule'
37
+ }
38
+
39
+ /**
40
+ * Check whether a property is standard
41
+ *
42
+ * @param {string} property
43
+ * @return {boolean} If `true`, the property is standard
44
+ */
45
+ function isStandardSyntaxProperty (property) {
46
+ // SCSS var (e.g. $var: x), list (e.g. $list: (x)) or map (e.g. $map: (key:value))
47
+ if (property.startsWith('$')) {
48
+ return false
49
+ }
50
+
51
+ // Less var (e.g. @var: x)
52
+ if (property.startsWith('@')) {
53
+ return false
54
+ }
55
+
56
+ // SCSS or Less interpolation
57
+ if (/#{.+?}|@{.+?}|\$\(.+?\)/.test(property)) {
58
+ return false
59
+ }
60
+
61
+ return true
62
+ }
63
+
64
+ /**
65
+ * @param {import('postcss').Declaration} decl
66
+ * @returns {boolean}
67
+ */
68
+ function isInColorGamutP3MediaQuery (decl) {
69
+ const parentMediaQuery = getMediaQueryParent(decl)
70
+
71
+ if (parentMediaQuery && /\(color-gamut:\s*p3\)/.test(parentMediaQuery.params)) {
72
+ return true
73
+ }
74
+
75
+ return false
76
+ }
77
+
78
+ /**
79
+ * @param {import('postcss').Declaration} decl
80
+ * @returns {boolean}
81
+ */
82
+ function isInColorGamutRec2020MediaQuery (decl) {
83
+ const parentMediaQuery = getMediaQueryParent(decl)
84
+
85
+ if (parentMediaQuery && /\(color-gamut:\s*rec2020\)/.test(parentMediaQuery.params)) {
86
+ return true
87
+ }
88
+
89
+ return false
90
+ }
91
+
92
+ /**
93
+ * @param {import('postcss').Declaration} decl
94
+ * @returns {boolean | decl}
95
+ */
96
+ function getMediaQueryParent (decl) {
97
+ if (decl.parent && decl.parent.parent && isAtRule(decl.parent.parent)) {
98
+ const parent = decl.parent.parent
99
+
100
+ return parent.name === 'media' && parent
101
+ }
102
+
103
+ return false
104
+ }
105
+
106
+ module.exports = {
107
+ isStandardSyntaxProperty,
108
+ declarationValueIndex,
109
+ isInColorGamutP3MediaQuery,
110
+ isInColorGamutRec2020MediaQuery
111
+ }
@@ -1,29 +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
- 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
- }
@@ -1,7 +0,0 @@
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
- }
@@ -1,25 +0,0 @@
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
- }