stylelint-gamut 1.1.3 → 1.1.6

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
@@ -1,5 +1,7 @@
1
1
  # stylelint-gamut
2
2
 
3
+ [![npm version][npm-version-img]][npm] [![npm downloads last month][npm-downloads-img]][npm]
4
+
3
5
  **stylelint-gamut** is a [Stylelint] plugin that helps you to work with different color spaces.
4
6
 
5
7
  Right now it's working with **node version ≥ 16.0.0**.
@@ -38,3 +40,6 @@ Add `stylelint-gamut` to your Stylelint config `plugins` array, then add rules y
38
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) {}`.
39
41
 
40
42
  [Stylelint]: https://stylelint.io/
43
+ [npm-version-img]: https://img.shields.io/npm/v/stylelint-gamut.svg
44
+ [npm-downloads-img]: https://img.shields.io/npm/dm/stylelint-gamut.svg
45
+ [npm]: https://www.npmjs.com/package/stylelint-gamut
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stylelint-gamut",
3
- "version": "1.1.3",
3
+ "version": "1.1.6",
4
4
  "description": "Stylelint plugin for working with different color spaces",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -13,6 +13,10 @@
13
13
  "type": "git",
14
14
  "url": "git+https://github.com/fpetrakov/stylelint-gamut.git"
15
15
  },
16
+ "files": [
17
+ "src/**/*.js",
18
+ "!**/**/*.test.js"
19
+ ],
16
20
  "keywords": [
17
21
  "stylelint",
18
22
  "stylelint-plugin",
@@ -29,7 +33,7 @@
29
33
  },
30
34
  "homepage": "https://github.com/fpetrakov/stylelint-gamut#readme",
31
35
  "peerDependencies": {
32
- "stylelint": ">=14.10.0"
36
+ "stylelint": "^14.0.0"
33
37
  },
34
38
  "devDependencies": {
35
39
  "eslint": "^8.22.0",
package/src/index.js CHANGED
@@ -5,7 +5,6 @@ const stylelint = require('stylelint')
5
5
  const Color = require('colorjs.io').default
6
6
  const isStandardSyntaxProperty = require('./utils/isStandardSyntaxProperty')
7
7
  const isCustomProperty = require('./utils/isCustomProperty')
8
- const getDeclarationValue = require('./utils/getDeclarationValue')
9
8
  const isAtRule = require('./utils/isAtRule')
10
9
  const declarationValueIndex = require('./utils/declarationValueIndex')
11
10
 
@@ -19,8 +18,6 @@ const meta = {
19
18
  url: 'https://stylelint.io/user-guide/rules/list/color-no-out-gamut-range'
20
19
  }
21
20
 
22
- const COLOR_FUNCTIONS = new Set(['lch', 'lab', 'oklch', 'oklab'])
23
-
24
21
  /** @type {import('stylelint').Rule} */
25
22
  const ruleFunction = (primary) => {
26
23
  return (root, result) => {
@@ -37,35 +34,42 @@ const ruleFunction = (primary) => {
37
34
 
38
35
  if (isCustomProperty(decl.prop)) return
39
36
 
40
- const parsedValue = valueParser(getDeclarationValue(decl))
37
+ if (decl.value.includes('lch(') || decl.value.includes('lab(')) {
38
+ const colors = decl.value.match(/(oklch|oklab|lab|lch)\([^)]+\)/g)
39
+ if (colors) {
40
+ for (const color of colors) {
41
+ checkColor(color)
42
+ }
43
+ }
44
+ }
41
45
 
42
- parsedValue.walk((node, _index, nodes) => {
43
- if (node.type !== 'function') return
46
+ function checkColor (color) {
47
+ const parsedValue = valueParser(color)
44
48
 
45
- if (!COLOR_FUNCTIONS.has(node.value)) {
46
- return
47
- }
49
+ parsedValue.walk((node, _index, nodes) => {
50
+ if (node.type !== 'function') return
48
51
 
49
- const isInSrgbGamut = new Color(valueParser.stringify(nodes)).inGamut(
50
- 'srgb'
51
- )
52
+ const isInSrgbGamut = new Color(valueParser.stringify(nodes)).inGamut(
53
+ 'srgb'
54
+ )
52
55
 
53
- if (isInSrgbGamut) return
56
+ if (isInSrgbGamut) return
54
57
 
55
- if (isInColorGamutP3MediaQuery(decl)) return
58
+ if (isInColorGamutP3MediaQuery(decl)) return
56
59
 
57
- const index = declarationValueIndex(decl) + node.sourceIndex
58
- const endIndex = index + decl.value.length
60
+ const index = declarationValueIndex(decl) + node.sourceIndex
61
+ const endIndex = index + decl.value.length
59
62
 
60
- stylelint.utils.report({
61
- message: messages.rejected(decl.value),
62
- node: decl,
63
- index,
64
- endIndex,
65
- result,
66
- ruleName
63
+ stylelint.utils.report({
64
+ message: messages.rejected(color),
65
+ node: decl,
66
+ index,
67
+ endIndex,
68
+ result,
69
+ ruleName
70
+ })
67
71
  })
68
- })
72
+ }
69
73
  })
70
74
  }
71
75
  }
@@ -78,7 +82,7 @@ function isInColorGamutP3MediaQuery (decl) {
78
82
  if (decl.parent && decl.parent.parent && isAtRule(decl.parent.parent)) {
79
83
  const parent = decl.parent.parent
80
84
 
81
- return parent.name === 'media' && parent.params === '(color-gamut: p3)'
85
+ return parent.name === 'media' && /\(color-gamut:\s*p3\)/.test(parent.params)
82
86
  }
83
87
 
84
88
  return false
package/.eslintrc.js DELETED
@@ -1,15 +0,0 @@
1
- module.exports = {
2
- env: {
3
- browser: true,
4
- commonjs: true,
5
- es2021: true
6
- },
7
- extends: [
8
- 'standard'
9
- ],
10
- parserOptions: {
11
- ecmaVersion: 'latest'
12
- },
13
- rules: {
14
- }
15
- }
@@ -1,30 +0,0 @@
1
- name: Test
2
- on:
3
- push:
4
- branches:
5
- - main
6
- pull_request:
7
- env:
8
- FORCE_COLOR: 2
9
- jobs:
10
- test:
11
- runs-on: ubuntu-latest
12
- strategy:
13
- matrix:
14
- node-version:
15
- - 18
16
- - 16
17
- name: Node.js Latest Full
18
- runs-on: ubuntu-latest
19
- steps:
20
- - name: Checkout the repository
21
- uses: actions/checkout@v3
22
- - name: Install Node.js ${{ matrix.node-version }}
23
- uses: actions/setup-node@v3
24
- with:
25
- node-version: ${{ matrix.node-version }}
26
- cache: npm
27
- - name: Install dependencies
28
- run: npm install --ignore-scripts
29
- - name: Run tests
30
- run: npm test
package/jest.config.js DELETED
@@ -1,7 +0,0 @@
1
- module.exports = {
2
- preset: 'jest-preset-stylelint',
3
- clearMocks: true,
4
- setupFiles: ['./jest.setup.js'],
5
- testEnvironment: 'node',
6
- roots: ['src']
7
- }
package/jest.setup.js DELETED
@@ -1,5 +0,0 @@
1
- 'use strict'
2
-
3
- const { getTestRule } = require('jest-preset-stylelint')
4
-
5
- global.testRule = getTestRule({ plugins: ['./src'] })
package/src/README.md DELETED
@@ -1,88 +0,0 @@
1
- # color-no-out-gamut-range
2
-
3
- Disallow out of gamut range colors.
4
-
5
- <!-- prettier-ignore -->
6
- ```css
7
- a { color: lch(48% 82 283) }
8
- /** ↑
9
- * This color is out of sRGB gamut range */
10
- ```
11
-
12
- Colors declared using lch, oklch, lab and oklab functions can move from sRGB to P3 color space, which is not supported by most of screens right now. It can happen by mistake. For instance, by converting colors from one space to another.
13
-
14
- This rule checks if the color is in sRGB space. If it's not, you should wrap it in media query like this:
15
-
16
- ```css
17
- @media (color-gamut: p3) {
18
- a {
19
- color: lch(48% 82 283);
20
- }
21
- }
22
- ```
23
-
24
- Please note that conditional imports are not checked, e.g.:
25
-
26
- ```html
27
- <link href="p3-custom-props.css" rel="stylesheet" media="(color-gamut: p3)" />
28
- ```
29
-
30
- ```css
31
- @import url("p3-custom-props.css") (color-gamut: p3);
32
- ```
33
-
34
- ## Options
35
-
36
- ### `true`
37
-
38
- The following patterns are considered problems:
39
-
40
- <!-- prettier-ignore -->
41
- ```css
42
- a { color: lch(48% 82 283) }
43
- ```
44
-
45
- <!-- prettier-ignore -->
46
- ```css
47
- a { color: oklch(48% 82 283) }
48
- ```
49
-
50
- <!-- prettier-ignore -->
51
- ```css
52
- a { color: lab(48% 82 283) }
53
- ```
54
-
55
- <!-- prettier-ignore -->
56
- ```css
57
- a { color: oklab(48% 82 283) }
58
- ```
59
-
60
- The following patterns are _not_ considered problems:
61
-
62
- <!-- prettier-ignore -->
63
- ```css
64
- @media (color-gamut: p3) {
65
- a { color: lch(48% 82 283) }
66
- }
67
- ```
68
-
69
- <!-- prettier-ignore -->
70
- ```css
71
- @media (color-gamut: p3) {
72
- a { color: oklch(48% 82 283) }
73
- }
74
- ```
75
-
76
- <!-- prettier-ignore -->
77
- ```css
78
- @media (color-gamut: p3) {
79
- a { color: lab(48% 82 283) }
80
- }
81
- ```
82
-
83
- <!-- prettier-ignore -->
84
- ```css
85
- @media (color-gamut: p3) {
86
- a { color: oklab(48% 82 283) }
87
- }
88
- ```
package/src/index.test.js DELETED
@@ -1,89 +0,0 @@
1
- 'use strict'
2
-
3
- const { rule: { messages, ruleName } } = require('./index.js')
4
-
5
- testRule({
6
- ruleName,
7
- config: [true],
8
- plugins: ['./index.js'],
9
- accept: [
10
- {
11
- code: '@media (color-gamut: p3) { a { color: lch(48% 82 283); } }',
12
- description: 'lch out of srgb gamut but wrapped in media query'
13
- },
14
- {
15
- code: '@media (color-gamut: p3) { a { color: oklch(48% 82 283 / 73%); } }',
16
- description:
17
- 'oklch with alpha out of srgb gamut but wrapped in media query'
18
- },
19
- {
20
- code: 'a { color: lch(50% 0 0); }',
21
- description: 'in srgb gamut'
22
- },
23
- {
24
- code: 'a { color: red; }',
25
- description: 'ignore not lch color declaration'
26
- },
27
- {
28
- code: ':root { --clr: lch(48% 82 283 / 75%) } a { color: var(--clr) }',
29
- description: 'ignore css variables'
30
- },
31
- {
32
- code: '@myVariable: lch(48% 82 283); a { color: @myVariable; }',
33
- description: 'ignore Less variable'
34
- },
35
- {
36
- code: ':root { $clr: lch(48% 82 283 / 67%) } a { color: $clr; }',
37
- description: 'ignore scss variables'
38
- }
39
- ],
40
-
41
- reject: [
42
- {
43
- code: 'a { color: lch(48% 82 283); }',
44
- description: 'lch',
45
- message: messages.rejected('lch(48% 82 283)'),
46
- line: 1,
47
- column: 12,
48
- endLine: 1,
49
- endColumn: 27
50
- },
51
- {
52
- code: 'a { color: lch(48% 82 283 / 72%); }',
53
- description: 'lch with alpha',
54
- message: messages.rejected('lch(48% 82 283 / 72%)'),
55
- line: 1,
56
- column: 12,
57
- endLine: 1,
58
- endColumn: 33
59
- },
60
- {
61
- code: 'a { color: oklch(98% 49 129); }',
62
- description: 'oklch',
63
- message: messages.rejected('oklch(98% 49 129)'),
64
- line: 1,
65
- column: 12,
66
- endLine: 1,
67
- endColumn: 29
68
- },
69
-
70
- {
71
- code: 'a { color: lab(98% 49 129); }',
72
- description: 'lab',
73
- message: messages.rejected('lab(98% 49 129)'),
74
- line: 1,
75
- column: 12,
76
- endLine: 1,
77
- endColumn: 27
78
- },
79
- {
80
- code: 'a { color: oklab(98% 49 129); }',
81
- description: 'oklab',
82
- message: messages.rejected('oklab(98% 49 129)'),
83
- line: 1,
84
- column: 12,
85
- endLine: 1,
86
- endColumn: 29
87
- }
88
- ]
89
- })
@@ -1,11 +0,0 @@
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
- }