|
1 | | -import type {FileInfo, API, Options} from 'jscodeshift'; |
2 | | -import postcss, {Plugin} from 'postcss'; |
3 | 1 | import valueParser from 'postcss-value-parser'; |
4 | 2 |
|
5 | | -import {POLARIS_MIGRATOR_COMMENT} from '../../constants'; |
6 | 3 | import { |
7 | | - createInlineComment, |
8 | 4 | getFunctionArgs, |
9 | 5 | isNumericOperator, |
10 | 6 | isSassFunction, |
11 | 7 | isTransformableLength, |
12 | 8 | namespace, |
13 | | - NamespaceOptions, |
14 | 9 | toTransformablePx, |
15 | 10 | StopWalkingFunctionNodes, |
| 11 | + createStylelintRule, |
16 | 12 | } from '../../utilities/sass'; |
17 | 13 | import {isKeyOf} from '../../utilities/type-guards'; |
18 | 14 |
|
19 | | -export default function replaceSpacingLengths( |
20 | | - fileInfo: FileInfo, |
21 | | - _: API, |
22 | | - options: Options, |
23 | | -) { |
24 | | - return postcss(plugin(options)).process(fileInfo.source, { |
25 | | - syntax: require('postcss-scss'), |
26 | | - }).css; |
27 | | -} |
| 15 | +export default createStylelintRule( |
| 16 | + 'replace-sass-space', |
| 17 | + (_, {methods, options}, context) => { |
| 18 | + const namespacedRem = namespace('rem', options); |
28 | 19 |
|
29 | | -const processed = Symbol('processed'); |
| 20 | + return (root) => { |
| 21 | + methods.walkDecls(root, (decl, parsedValue) => { |
| 22 | + if (!spaceProps.has(decl.prop)) return; |
30 | 23 |
|
31 | | -interface PluginOptions extends Options, NamespaceOptions {} |
| 24 | + let hasNumericOperator = false; |
32 | 25 |
|
33 | | -const plugin = (options: PluginOptions = {}): Plugin => { |
34 | | - const namespacedRem = namespace('rem', options); |
| 26 | + handleSpaceProps(); |
35 | 27 |
|
36 | | - return { |
37 | | - postcssPlugin: 'replace-sass-space', |
38 | | - Declaration(decl) { |
39 | | - // @ts-expect-error - Skip if processed so we don't process it again |
40 | | - if (decl[processed]) return; |
| 28 | + if (hasNumericOperator) { |
| 29 | + methods.report({ |
| 30 | + node: decl, |
| 31 | + severity: 'warning', |
| 32 | + message: 'Numeric operator detected.', |
| 33 | + }); |
| 34 | + } |
41 | 35 |
|
42 | | - if (!spaceProps.has(decl.prop)) return; |
43 | | - |
44 | | - /** |
45 | | - * A collection of transformable values to migrate (e.g. decl lengths, functions, etc.) |
46 | | - * |
47 | | - * Note: This is evaluated at the end of each visitor execution to determine whether |
48 | | - * or not to replace the declaration or insert a comment. |
49 | | - */ |
50 | | - const targets: {replaced: boolean}[] = []; |
51 | | - let hasNumericOperator = false; |
52 | | - const parsedValue = valueParser(decl.value); |
53 | | - |
54 | | - handleSpaceProps(); |
55 | | - |
56 | | - if (targets.some(({replaced}) => !replaced || hasNumericOperator)) { |
57 | | - decl.before( |
58 | | - createInlineComment(POLARIS_MIGRATOR_COMMENT, {prose: true}), |
59 | | - ); |
60 | | - decl.before( |
61 | | - createInlineComment(`${decl.prop}: ${parsedValue.toString()};`), |
62 | | - ); |
63 | | - } else { |
64 | | - decl.value = parsedValue.toString(); |
65 | | - } |
66 | | - |
67 | | - // |
68 | | - // Handlers |
69 | | - // |
70 | | - |
71 | | - function handleSpaceProps() { |
72 | | - parsedValue.walk((node) => { |
73 | | - if (isNumericOperator(node)) { |
74 | | - hasNumericOperator = true; |
75 | | - return; |
76 | | - } |
77 | | - |
78 | | - if (node.type === 'word') { |
79 | | - if (globalValues.has(node.value)) return; |
80 | | - |
81 | | - const dimension = valueParser.unit(node.value); |
82 | | - |
83 | | - if (!isTransformableLength(dimension)) return; |
84 | | - |
85 | | - targets.push({replaced: false}); |
86 | | - |
87 | | - const valueInPx = toTransformablePx(node.value); |
88 | | - |
89 | | - if (!isKeyOf(spaceMap, valueInPx)) return; |
90 | | - |
91 | | - targets[targets.length - 1]!.replaced = true; |
92 | | - |
93 | | - node.value = `var(${spaceMap[valueInPx]})`; |
94 | | - return; |
95 | | - } |
| 36 | + function handleSpaceProps() { |
| 37 | + parsedValue.walk((node) => { |
| 38 | + if (isNumericOperator(node)) { |
| 39 | + hasNumericOperator = true; |
| 40 | + return; |
| 41 | + } |
96 | 42 |
|
97 | | - if (node.type === 'function') { |
98 | | - if (isSassFunction(namespacedRem, node)) { |
99 | | - targets.push({replaced: false}); |
| 43 | + if (node.type === 'word') { |
| 44 | + if (globalValues.has(node.value)) return; |
100 | 45 |
|
101 | | - const args = getFunctionArgs(node); |
| 46 | + const dimension = valueParser.unit(node.value); |
102 | 47 |
|
103 | | - if (args.length !== 1) return; |
| 48 | + if (!isTransformableLength(dimension)) return; |
104 | 49 |
|
105 | | - const valueInPx = toTransformablePx(args[0]); |
| 50 | + const valueInPx = toTransformablePx(node.value); |
106 | 51 |
|
107 | | - if (!isKeyOf(spaceMap, valueInPx)) return; |
| 52 | + if (!isKeyOf(spaceMap, valueInPx)) { |
| 53 | + methods.report({ |
| 54 | + node: decl, |
| 55 | + severity: 'error', |
| 56 | + message: `Non-tokenizable value '${node.value}'`, |
| 57 | + }); |
| 58 | + return; |
| 59 | + } |
108 | 60 |
|
109 | | - targets[targets.length - 1]!.replaced = true; |
| 61 | + if (context.fix) { |
| 62 | + node.value = `var(${spaceMap[valueInPx]})`; |
| 63 | + return; |
| 64 | + } |
110 | 65 |
|
111 | | - node.value = 'var'; |
112 | | - node.nodes = [ |
113 | | - { |
114 | | - type: 'word', |
115 | | - value: spaceMap[valueInPx], |
116 | | - sourceIndex: node.nodes[0]?.sourceIndex ?? 0, |
117 | | - sourceEndIndex: spaceMap[valueInPx].length, |
118 | | - }, |
119 | | - ]; |
| 66 | + methods.report({ |
| 67 | + node: decl, |
| 68 | + severity: 'error', |
| 69 | + message: `Prefer var(${spaceMap[valueInPx]}) Polaris token.`, |
| 70 | + }); |
| 71 | + return; |
120 | 72 | } |
121 | 73 |
|
122 | | - return StopWalkingFunctionNodes; |
123 | | - } |
124 | | - }); |
125 | | - } |
126 | | - }, |
127 | | - }; |
128 | | -}; |
| 74 | + if (node.type === 'function') { |
| 75 | + if (isSassFunction(namespacedRem, node)) { |
| 76 | + const args = getFunctionArgs(node); |
| 77 | + |
| 78 | + if (args.length !== 1) { |
| 79 | + methods.report({ |
| 80 | + node: decl, |
| 81 | + severity: 'error', |
| 82 | + message: `Expected 1 argument, got ${args.length}`, |
| 83 | + }); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + const valueInPx = toTransformablePx(args[0]); |
| 88 | + |
| 89 | + if (!isKeyOf(spaceMap, valueInPx)) { |
| 90 | + methods.report({ |
| 91 | + node: decl, |
| 92 | + severity: 'error', |
| 93 | + message: `Non-tokenizable value '${args[0].trim()}'`, |
| 94 | + }); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + if (context.fix) { |
| 99 | + node.value = 'var'; |
| 100 | + node.nodes = [ |
| 101 | + { |
| 102 | + type: 'word', |
| 103 | + value: spaceMap[valueInPx], |
| 104 | + sourceIndex: node.nodes[0]?.sourceIndex ?? 0, |
| 105 | + sourceEndIndex: spaceMap[valueInPx].length, |
| 106 | + }, |
| 107 | + ]; |
| 108 | + return; |
| 109 | + } |
| 110 | + methods.report({ |
| 111 | + node: decl, |
| 112 | + severity: 'error', |
| 113 | + message: `Prefer var(${spaceMap[valueInPx]}) Polaris token.`, |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + return StopWalkingFunctionNodes; |
| 118 | + } |
| 119 | + }); |
| 120 | + } |
| 121 | + }); |
| 122 | + }; |
| 123 | + }, |
| 124 | +); |
129 | 125 |
|
130 | 126 | const globalValues = new Set(['inherit', 'initial', 'unset']); |
131 | 127 |
|
|
0 commit comments