@alfalab/stylelint-core-vars 3.0.0 → 3.0.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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # stylelint-core-vars
2
2
 
3
+ ## 3.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#225](https://github.com/core-ds/arui-presets-lint/pull/225) [`ee304c5`](https://github.com/core-ds/arui-presets-lint/commit/ee304c5ea428056986a3eacc90b83c751b44f5fc) Thanks [@dmitrbrvsk](https://github.com/dmitrbrvsk)! - исправлены поиск и автофикс устаревших CSS токенов в составных значениях
8
+
3
9
  ## 3.0.0
4
10
 
5
11
  ### Major Changes
package/README.md CHANGED
@@ -6,13 +6,13 @@
6
6
 
7
7
  ## Установка
8
8
 
9
- ```
9
+ ```sh
10
10
  yarn add --dev stylelint @alfalab/stylelint-core-vars
11
11
  ```
12
12
 
13
13
  или
14
14
 
15
- ```
15
+ ```sh
16
16
  npm install --save-dev stylelint @alfalab/stylelint-core-vars
17
17
  ```
18
18
 
@@ -20,7 +20,7 @@ npm install --save-dev stylelint @alfalab/stylelint-core-vars
20
20
 
21
21
  Добавьте в свой stylelint конфиг:
22
22
 
23
- ```
23
+ ```jsonc
24
24
  {
25
25
  "plugins": [
26
26
  "@alfalab/stylelint-core-vars"
package/dist/lib/index.js CHANGED
@@ -21,8 +21,9 @@ export const messages = {
21
21
  [RULE_DO_NOT_USE_OLD_VARS]: typedRuleMessages(RULE_DO_NOT_USE_OLD_VARS, {
22
22
  expected: (variables, value, fixable = true) => {
23
23
  const valueStr = String(value);
24
- if (!variables.length)
24
+ if (!variables.length) {
25
25
  return `Ask you designer how to replace old token '${valueStr}'`;
26
+ }
26
27
  const startMsg = fixable ? 'Use' : 'Cant find proper token, but you can use';
27
28
  if (variables.length === 1) {
28
29
  return `${startMsg} variable 'var(${variables[0]})' instead of old '${valueStr}'`;
@@ -79,6 +80,37 @@ const checkVars = (decl, result, context, ruleName, matcher, shouldReport, optio
79
80
  });
80
81
  }
81
82
  };
83
+ const checkOldVars = (decl, result, context) => {
84
+ // PostCSS хранит комментарии между частями значения отдельно от decl.value.
85
+ const value = decl.raws.value?.value === decl.value ? decl.raws.value.raw : decl.value;
86
+ const substitutions = findOldVars(value, decl.prop);
87
+ for (const substitution of substitutions) {
88
+ if (context.fix && substitution.fixable) {
89
+ continue;
90
+ }
91
+ const index = substitution.index + decl.prop.length + (decl.raws.between?.length ?? 0);
92
+ stylelint.utils.report({
93
+ result,
94
+ ruleName: RULE_DO_NOT_USE_OLD_VARS,
95
+ message: messages[RULE_DO_NOT_USE_OLD_VARS].expected(substitution.variables, substitution.value, substitution.fixable),
96
+ node: decl,
97
+ index,
98
+ endIndex: index + substitution.value.length,
99
+ });
100
+ }
101
+ if (context.fix) {
102
+ let fixedValue = value;
103
+ // Замены справа налево сохраняют исходные позиции остальных токенов.
104
+ for (const substitution of substitutions.toReversed()) {
105
+ if (substitution.fixable) {
106
+ fixedValue = `${fixedValue.slice(0, substitution.index)}${substitution.fixedValue}${fixedValue.slice(substitution.index + substitution.value.length)}`;
107
+ }
108
+ }
109
+ if (fixedValue !== value) {
110
+ decl.value = fixedValue;
111
+ }
112
+ }
113
+ };
82
114
  const checkTypography = (rule, result, context, ruleName) => {
83
115
  const typographyProps = (rule.nodes ?? []).reduce((acc, node) => {
84
116
  if (node.type === 'decl' &&
@@ -88,11 +120,13 @@ const checkTypography = (rule, result, context, ruleName) => {
88
120
  return acc;
89
121
  }, {});
90
122
  const hasTypography = 'font-size' in typographyProps;
91
- if (!hasTypography)
123
+ if (!hasTypography) {
92
124
  return;
125
+ }
93
126
  const foundMixins = findTypographyMixins(typographyProps);
94
- if (!foundMixins || !foundMixins.length)
127
+ if (!foundMixins?.length) {
95
128
  return;
129
+ }
96
130
  const exactMixin = foundMixins.length === 1;
97
131
  let fixed = false;
98
132
  if (context.fix && exactMixin) {
@@ -174,7 +208,7 @@ export default [
174
208
  }
175
209
  return (root, result) => {
176
210
  root.walkDecls((decl) => {
177
- checkVars(decl, result, context, RULE_DO_NOT_USE_OLD_VARS, findOldVars, (_fixable, fixed) => !fixed);
211
+ checkOldVars(decl, result, context);
178
212
  });
179
213
  };
180
214
  })),
@@ -25,6 +25,6 @@ export declare const VARS_AVAILABLE: boolean;
25
25
  export declare function findVars(cssValue: string, prop: string, options: {
26
26
  allowNumericValues?: boolean;
27
27
  }): SubstitutionResult | undefined;
28
- export declare function findOldVars(cssValue: string, prop: string): SubstitutionResult | undefined;
28
+ export declare function findOldVars(cssValue: string, prop: string): SubstitutionResult[];
29
29
  export declare function findTypographyMixins(ruleProps: MixinPropsMap): MixinDef[] | null;
30
30
  export declare function toOneLine(value: string): string;
package/dist/lib/utils.js CHANGED
@@ -3,6 +3,7 @@ import fs from 'node:fs';
3
3
  import { createRequire } from 'node:module';
4
4
  import path from 'node:path';
5
5
  import postcss from 'postcss';
6
+ import valueParser from 'postcss-value-parser';
6
7
  import oldNewMap from './old-new-map.js';
7
8
  const require = createRequire(import.meta.url);
8
9
  const { packages } = getPackagesSync(process.cwd());
@@ -16,8 +17,9 @@ export const vars = {
16
17
  borderRadiuses: loadVars('border-radius.css'),
17
18
  };
18
19
  export const colorsSet = Object.values(vars.colors).reduce((acc, colorVars) => {
19
- for (const colorVar of colorVars)
20
+ for (const colorVar of colorVars) {
20
21
  acc.add(colorVar);
22
+ }
21
23
  return acc;
22
24
  }, new Set());
23
25
  export const mixins = {
@@ -177,16 +179,18 @@ function choiceVars(variables, prop, group) {
177
179
  }
178
180
  export function findVars(cssValue, prop, options) {
179
181
  const propVars = varsByProperties[prop];
180
- if (!propVars)
182
+ if (!propVars) {
181
183
  return undefined;
184
+ }
182
185
  const group = getVarsGroup(propVars);
183
186
  if ((group === 'gaps' || group === 'borderRadiuses') && options.allowNumericValues) {
184
187
  return undefined;
185
188
  }
186
189
  for (const [value, variables] of Object.entries(propVars)) {
187
190
  const chosen = choiceVars(variables, prop, group);
188
- if (!chosen || !chosen.length)
191
+ if (!chosen?.length) {
189
192
  continue;
193
+ }
190
194
  const index = findSubstring(cssValue, value);
191
195
  if (index !== -1) {
192
196
  return {
@@ -201,26 +205,31 @@ export function findVars(cssValue, prop, options) {
201
205
  return undefined;
202
206
  }
203
207
  export function findOldVars(cssValue, prop) {
204
- const matches = /var\(([^),]+)/g.exec(cssValue);
205
- if (!matches)
206
- return undefined;
207
- const oldVar = matches[1];
208
- const replacements = oldNewMap[oldVar];
209
- if (!replacements)
210
- return undefined;
208
+ const substitutions = [];
211
209
  const variants = getColorVariants(prop);
212
- let variables = replacements.filter((r) => variants.some((v) => r.includes(v)));
213
- const fixable = variables.length === 1;
214
- if (!variables.length) {
215
- variables = replacements;
216
- }
217
- return {
218
- index: cssValue.indexOf(oldVar),
219
- value: oldVar,
220
- variables,
221
- fixedValue: variables.length ? variables[0] : 'NON_FIXABLE',
222
- fixable,
223
- };
210
+ valueParser(cssValue).walk((node) => {
211
+ if (node.type !== 'function' || node.value !== 'var') {
212
+ return;
213
+ }
214
+ const token = node.nodes.find((child) => child.type !== 'space' && child.type !== 'comment');
215
+ if (token?.type !== 'word' || !token.value.startsWith('--')) {
216
+ return;
217
+ }
218
+ const replacements = oldNewMap[token.value];
219
+ if (!replacements) {
220
+ return;
221
+ }
222
+ const chosen = replacements.filter((replacement) => variants.some((variant) => replacement.includes(variant)));
223
+ const variables = chosen.length ? chosen : replacements;
224
+ substitutions.push({
225
+ index: token.sourceIndex,
226
+ value: token.value,
227
+ variables,
228
+ fixedValue: variables[0] ?? token.value,
229
+ fixable: chosen.length === 1,
230
+ });
231
+ });
232
+ return substitutions;
224
233
  }
225
234
  export function findTypographyMixins(ruleProps) {
226
235
  const findMixin = (exact) => Object.entries(mixins.typography)
@@ -246,12 +255,13 @@ function getVarsGroup(varsSet) {
246
255
  }
247
256
  function sortVarsByUsage(arr, sortingArr) {
248
257
  return arr.toSorted((a, b) => {
249
- const aUsage = a.slice(2).split('-')[2];
250
- const bUsage = b.slice(2).split('-')[2];
258
+ const aUsage = a.slice(2).split('-', 3)[2];
259
+ const bUsage = b.slice(2).split('-', 3)[2];
251
260
  const aIndex = sortingArr.indexOf(aUsage);
252
261
  const bIndex = sortingArr.indexOf(bUsage);
253
- if (aIndex === -1 || bIndex === -1)
262
+ if (aIndex === -1 || bIndex === -1) {
254
263
  return 0;
264
+ }
255
265
  return aIndex - bIndex;
256
266
  });
257
267
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alfalab/stylelint-core-vars",
3
- "version": "3.0.0",
3
+ "version": "3.0.1",
4
4
  "description": "core-components css linter",
5
5
  "license": "MIT",
6
6
  "author": "core-ds contributors",
@@ -22,7 +22,6 @@
22
22
  },
23
23
  "scripts": {
24
24
  "build": "rimraf dist && tsc -p tsconfig.build.json",
25
- "prepack": "tsx scripts/strip-dev-deps.ts",
26
25
  "test": "tsc --noEmit && yarn lint && yarn test:unit",
27
26
  "test:unit": "vitest --run",
28
27
  "lint": "yarn lint:scripts && yarn format:check",
@@ -33,9 +32,20 @@
33
32
  },
34
33
  "dependencies": {
35
34
  "@manypkg/get-packages": "^3.1.0",
36
- "postcss": "^8.5.13"
35
+ "postcss": "^8.5.13",
36
+ "postcss-value-parser": "^4.2.0"
37
+ },
38
+ "devDependencies": {
39
+ "@alfalab/core-components-vars": "7.0.0",
40
+ "@vitest/coverage-istanbul": "5.0.5",
41
+ "jest-preset-stylelint": "^9.0.0",
42
+ "rimraf": "6.1.3",
43
+ "stylelint": "17.16.0",
44
+ "tsx": "4.23.15",
45
+ "typescript": "6.0.3",
46
+ "vite": "8.3.5",
47
+ "vitest": "5.0.5"
37
48
  },
38
- "devDependencies": {},
39
49
  "peerDependencies": {
40
50
  "stylelint": "^17.0.0"
41
51
  },
@@ -1 +0,0 @@
1
- export {};
@@ -1,7 +0,0 @@
1
- // Удаляем devDependencies перед публикацией
2
- // Для того чтобы зависимости с версией `workspace:*` не попали в релиз
3
- import { readFile, writeFile } from 'node:fs/promises';
4
- const pkgPath = 'package.json';
5
- const pkg = JSON.parse(await readFile(pkgPath, 'utf8'));
6
- pkg.devDependencies = {};
7
- await writeFile(pkgPath, `${JSON.stringify(pkg, null, 4)}\n`);