stylelint-gamut 1.2.4 → 1.2.7
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 +3 -3
- package/package.json +1 -1
- package/src/index.js +6 -20
- package/src/utils.js +111 -0
- package/src/utils/declarationValueIndex.js +0 -29
- package/src/utils/isAtRule.js +0 -7
- package/src/utils/isStandardSyntaxProperty.js +0 -25
package/README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
#
|
|
1
|
+
# <img src="./logo/logo.png" width="100" height="100">
|
|
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]
|
|
5
|
+
**stylelint-gamut** is a [Stylelint] plugin that helps you to work with different color spaces.
|
|
6
6
|
|
|
7
7
|
# Rules
|
|
8
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) {}`.
|
|
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) {}` or `@media (color-gamut: rec2020) {}`.
|
|
10
10
|
|
|
11
11
|
# Requirements
|
|
12
12
|
|
package/package.json
CHANGED
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
|
|
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
|
|
52
|
+
const color = new Color(customPropValue || value)
|
|
55
53
|
|
|
56
|
-
if (
|
|
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
|
-
}
|
package/src/utils/isAtRule.js
DELETED
|
@@ -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
|
-
}
|