@alexlit/config-stylelint 60.13.0 → 60.13.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alexlit/config-stylelint",
3
- "version": "60.13.0",
3
+ "version": "60.13.2",
4
4
  "private": false,
5
5
  "description": "Stylelint config",
6
6
  "keywords": [
@@ -26,8 +26,7 @@
26
26
  "files": [
27
27
  "README.md",
28
28
  "index.js",
29
- "plugins",
30
- "vendor"
29
+ "plugins"
31
30
  ],
32
31
  "scripts": {
33
32
  "lint": "cd ../../ && ./scripts/lint.stylelint.sh",
@@ -43,7 +42,6 @@
43
42
  "postcss-html": "^1.8.1",
44
43
  "prettier": "^3.8.3",
45
44
  "stylelint": "17.11.0",
46
- "stylelint-color-format": "file:./vendor/stylelint-color-format",
47
45
  "stylelint-config-recommended-vue": "^1.6.1",
48
46
  "stylelint-config-standard": "^40.0.0",
49
47
  "stylelint-config-standard-scss": "^17.0.0",
@@ -1,11 +1,189 @@
1
- /**
2
- * TODO: update plugin from ^1.1.0
3
- *
4
- * @type {import('stylelint').Config}
5
- *
6
- * @see [stylelint-color-format](https://github.com/filipekiss/stylelint-color-format)
7
- */
1
+ import stylelint from 'stylelint';
2
+
3
+ const { createPlugin } = stylelint;
4
+ const { report, ruleMessages, validateOptions } = stylelint.utils;
5
+
6
+ const ruleName = 'color-format/format';
7
+
8
+ const hexToRgb = (hex) => {
9
+ const value = hex.replace('#', '');
10
+ const number = Number.parseInt(value, 16);
11
+ const r = (number >> 16) & 255;
12
+ const g = (number >> 8) & 255;
13
+ const b = number & 255;
14
+
15
+ if (value.length === 3 || value.length === 6) {
16
+ return { r, g, b, a: 1 };
17
+ }
18
+
19
+ const a = Number.parseFloat((((number >> 24) & 255) / 255).toFixed(2));
20
+
21
+ return { r, g, b, a };
22
+ };
23
+
24
+ const rgbToHsl = ({ r, g, b }) => {
25
+ const rN = r / 255;
26
+ const gN = g / 255;
27
+ const bN = b / 255;
28
+
29
+ const max = Math.max(rN, gN, bN);
30
+ const min = Math.min(rN, gN, bN);
31
+ const delta = max - min;
32
+
33
+ const lightness = (max + min) / 2;
34
+
35
+ if (delta === 0) {
36
+ return { h: 0, s: 0, l: Math.round(lightness * 100) };
37
+ }
38
+
39
+ const saturation = lightness > 0.5
40
+ ? delta / (2 - max - min)
41
+ : delta / (max + min);
42
+
43
+ let hue;
44
+
45
+ switch (max) {
46
+ case rN: {
47
+ hue = ((gN - bN) / delta + (gN < bN ? 6 : 0)) / 6;
48
+
49
+ break;
50
+ }
51
+
52
+ case gN: {
53
+ hue = ((bN - rN) / delta + 2) / 6;
54
+
55
+ break;
56
+ }
57
+
58
+ default: {
59
+ hue = ((rN - gN) / delta + 4) / 6;
60
+ }
61
+ }
62
+
63
+ return {
64
+ h: Math.round(hue * 360),
65
+ s: Math.round(saturation * 100),
66
+ l: Math.round(lightness * 100),
67
+ };
68
+ };
69
+
70
+ const formatColor = (hex, format) => {
71
+ const { r, g, b, a } = hexToRgb(hex);
72
+
73
+ if (format === 'rgb' || format === 'rgba') {
74
+ if (a < 1) {
75
+ return `rgba(${r}, ${g}, ${b}, ${a})`;
76
+ }
77
+
78
+ return `rgb(${r}, ${g}, ${b})`;
79
+ }
80
+
81
+ const { h, s, l } = rgbToHsl({ r, g, b });
82
+
83
+ if (a < 1) {
84
+ return `hsla(${h}, ${s}%, ${l}%, ${a})`;
85
+ }
86
+
87
+ return `hsl(${h}, ${s}%, ${l}%)`;
88
+ };
89
+
90
+ const styleSearch = (source, target, callback) => {
91
+ let startIndex = 0;
92
+
93
+ while (startIndex < source.length) {
94
+ const index = source.indexOf(target, startIndex);
95
+
96
+ if (index === -1) {
97
+ break;
98
+ }
99
+
100
+ callback({ startIndex: index, endIndex: index + target.length });
101
+
102
+ startIndex = index + 1;
103
+ }
104
+ };
105
+
106
+ const messages = ruleMessages(ruleName, {
107
+ custom: (message) => message,
108
+ rejected: (hex) => `Unexpected hex color "${hex}"`,
109
+ });
110
+
111
+ const rule = function (actual, options, context) {
112
+ return (root, result) => {
113
+ const validOptions = validateOptions(result, ruleName, {
114
+ actual,
115
+ possible: {
116
+ format: ['rgb', 'rgba', 'hsl', 'hsla'],
117
+ },
118
+ });
119
+
120
+ if (!validOptions) {
121
+ return;
122
+ }
123
+
124
+ root.walkDecls((decl) => {
125
+ const declString = decl.toString();
126
+ const fixPositions = [];
127
+
128
+ styleSearch(declString, '#', (match) => {
129
+ if (!/[:,\s]/.test(declString[match.startIndex - 1])) {
130
+ return;
131
+ }
132
+
133
+ const hexMatch = /^#[0-9A-Z]+/i.exec(
134
+ declString.slice(match.startIndex),
135
+ );
136
+
137
+ if (!hexMatch) {
138
+ return;
139
+ }
140
+
141
+ const hexValue = hexMatch[0];
142
+
143
+ if (context && context.fix) {
144
+ fixPositions.unshift({
145
+ fixTo: actual.format,
146
+ hexValue,
147
+ startIndex: match.startIndex,
148
+ });
149
+
150
+ return;
151
+ }
152
+
153
+ const message = actual && actual.message
154
+ ? messages.custom(actual.message)
155
+ : messages.rejected(hexValue);
156
+
157
+ report({
158
+ endIndex: match.endIndex,
159
+ index: match.startIndex,
160
+ message,
161
+ node: decl,
162
+ result,
163
+ ruleName,
164
+ });
165
+ });
166
+
167
+ if (fixPositions.length > 0) {
168
+ for (const fixPosition of fixPositions) {
169
+ const hexes = [...decl.value.matchAll(/#[0-9A-Z]+/gi)];
170
+
171
+ for (const hexMatch of hexes) {
172
+ decl.value = decl.value.replace(
173
+ hexMatch[0],
174
+ formatColor(hexMatch[0], fixPosition.fixTo),
175
+ );
176
+ }
177
+ }
178
+ }
179
+ });
180
+ };
181
+ };
182
+
183
+ rule.ruleName = ruleName;
184
+ rule.messages = messages;
185
+
8
186
  export default {
9
- plugins: ['stylelint-color-format'],
187
+ plugins: [createPlugin(ruleName, rule)],
10
188
  rules: { 'color-format/format': { format: 'rgb' } },
11
189
  };
@@ -1,5 +0,0 @@
1
- const {createPlugin} = require('stylelint');
2
-
3
- const rules = require('./rules');
4
-
5
- module.exports = [createPlugin('color-format/format', rules['format'])];
@@ -1,44 +0,0 @@
1
- {
2
- "name": "stylelint-color-format",
3
- "version": "1.1.0",
4
- "description": "Convert Hex colors to either RGBA or HSLA",
5
- "main": "index.js",
6
- "repository": {
7
- "type": "git",
8
- "url": "git+https://github.com/filipekiss/stylelint-color-format.git"
9
- },
10
- "dependencies": {
11
- "color": "^3.0.0",
12
- "style-search": "^0.1.0"
13
- },
14
- "peerDependencies": {
15
- "stylelint": ">=8.0.0"
16
- },
17
- "devDependencies": {
18
- "eslint-config-stylelint": "^8.1.0",
19
- "husky": "^0.14.3",
20
- "lint-staged": "^7.1.0",
21
- "np": "^2.20.1",
22
- "prettier": "^1.12.1",
23
- "standard-version": "^7.1.0",
24
- "stylelint": ">=8.0.0",
25
- "stylelint-test-rule-tape": "^0.2.0",
26
- "xo": "^0.24.0"
27
- },
28
- "keywords": [
29
- "stylelint",
30
- "stylelint-plugin",
31
- "css",
32
- "sass",
33
- "scss"
34
- ],
35
- "author": "Filipe Kiss <eu@filipekiss.com.br>",
36
- "license": "MIT",
37
- "bugs": {
38
- "url": "https://github.com/filipekiss/stylelint-color-format/issues"
39
- },
40
- "homepage": "https://github.com/filipekiss/stylelint-color-format#readme",
41
- "engines": {
42
- "node": ">=7"
43
- }
44
- }
@@ -1,119 +0,0 @@
1
- /* eslint-disable unicorn/no-abusive-eslint-disable */
2
- /* eslint-disable */
3
- const colorConverter = require('color');
4
- const styleSearch = require('style-search');
5
- const stylelint = require('stylelint');
6
-
7
- const {report, ruleMessages, validateOptions} = stylelint.utils;
8
-
9
- const ruleName = 'color-format/format';
10
-
11
- const fixColorFormat = (value, fixer) => {
12
- if (!value) {
13
- return value;
14
- }
15
-
16
- // Extract the color from value
17
- const hexMatch = value.match(/#[0-9A-Z]+/gi);
18
-
19
- if (!hexMatch) {
20
- return value;
21
- }
22
-
23
- const colorTranslation = hexMatch.reduce((table, hexColor) => {
24
- const color = colorConverter(hexColor);
25
-
26
- table[hexColor] = fixer.fixTo.includes('hsl')
27
- ? color.hsl().round().string()
28
- : color.rgb().string();
29
-
30
- return table;
31
- }, {});
32
-
33
- Object.entries(colorTranslation).forEach(colorTuple => {
34
- const hexColor = colorTuple[0];
35
- const fixedColor = colorTuple[1];
36
-
37
- value = value.replace(hexColor, fixedColor);
38
- });
39
-
40
- return value;
41
- };
42
-
43
- const messages = ruleMessages(ruleName, {
44
- custom: message => message,
45
- rejected: hex => `Unexpected hex color "${hex}"`,
46
- });
47
-
48
- const rule = function (actual, options, context) {
49
- return (root, result) => {
50
- const validOptions = validateOptions(result, ruleName, {
51
- actual,
52
- possible: {
53
- format: ['rgb', 'rgba', 'hsl', 'hsla'],
54
- },
55
- });
56
-
57
- if (!validOptions) {
58
- return;
59
- }
60
-
61
- root.walkDecls(decl => {
62
- const declString = decl.toString();
63
- const fixPositions = [];
64
-
65
- styleSearch({source: declString, target: '#'}, match => {
66
- // If there's not a colon, comma, or whitespace character before, we'll assume this is
67
- // not intended to be a hex color, but is instead something like the
68
- // hash in a url() argument
69
- if (!/[:,\s]/.test(declString[match.startIndex - 1])) {
70
- return;
71
- }
72
-
73
- const hexMatch = /^#[0-9A-Z]+/i.exec(
74
- declString.slice(match.startIndex)
75
- );
76
-
77
- if (!hexMatch) {
78
- return;
79
- }
80
-
81
- const hexValue = hexMatch[0];
82
-
83
- if (context && context.fix) {
84
- fixPositions.unshift({
85
- fixTo: actual.format,
86
- hexValue,
87
- startIndex: match.startIndex,
88
- });
89
-
90
- return;
91
- }
92
-
93
- const message =
94
- actual && actual.message
95
- ? messages.custom(actual.message)
96
- : messages.rejected(hexValue);
97
-
98
- report({
99
- endIndex: match.endIndex,
100
- index: match.startIndex,
101
- message: message,
102
- node: decl,
103
- result,
104
- ruleName,
105
- });
106
- });
107
-
108
- if (fixPositions.length > 0) {
109
- fixPositions.forEach(fixPosition => {
110
- decl.value = fixColorFormat(decl.value, fixPosition);
111
- });
112
- }
113
- });
114
- };
115
- };
116
-
117
- rule.ruleName = ruleName;
118
- rule.messages = messages;
119
- module.exports = rule;
@@ -1,3 +0,0 @@
1
- module.exports = {
2
- format: require('./color-format/format'),
3
- };