stylelint-logical-spec 6.0.0

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/LICENSE.md ADDED
@@ -0,0 +1,108 @@
1
+ # CC0 1.0 Universal
2
+
3
+ ## Statement of Purpose
4
+
5
+ The laws of most jurisdictions throughout the world automatically confer
6
+ exclusive Copyright and Related Rights (defined below) upon the creator and
7
+ subsequent owner(s) (each and all, an “owner”) of an original work of
8
+ authorship and/or a database (each, a “Work”).
9
+
10
+ Certain owners wish to permanently relinquish those rights to a Work for the
11
+ purpose of contributing to a commons of creative, cultural and scientific works
12
+ (“Commons”) that the public can reliably and without fear of later claims of
13
+ infringement build upon, modify, incorporate in other works, reuse and
14
+ redistribute as freely as possible in any form whatsoever and for any purposes,
15
+ including without limitation commercial purposes. These owners may contribute
16
+ to the Commons to promote the ideal of a free culture and the further
17
+ production of creative, cultural and scientific works, or to gain reputation or
18
+ greater distribution for their Work in part through the use and efforts of
19
+ others.
20
+
21
+ For these and/or other purposes and motivations, and without any expectation of
22
+ additional consideration or compensation, the person associating CC0 with a
23
+ Work (the “Affirmer”), to the extent that he or she is an owner of Copyright
24
+ and Related Rights in the Work, voluntarily elects to apply CC0 to the Work and
25
+ publicly distribute the Work under its terms, with knowledge of his or her
26
+ Copyright and Related Rights in the Work and the meaning and intended legal
27
+ effect of CC0 on those rights.
28
+
29
+ 1. Copyright and Related Rights. A Work made available under CC0 may be
30
+ protected by copyright and related or neighboring rights (“Copyright and
31
+ Related Rights”). Copyright and Related Rights include, but are not limited
32
+ to, the following:
33
+ 1. the right to reproduce, adapt, distribute, perform, display, communicate,
34
+ and translate a Work;
35
+ 2. moral rights retained by the original author(s) and/or performer(s);
36
+ 3. publicity and privacy rights pertaining to a person’s image or likeness
37
+ depicted in a Work;
38
+ 4. rights protecting against unfair competition in regards to a Work,
39
+ subject to the limitations in paragraph 4(i), below;
40
+ 5. rights protecting the extraction, dissemination, use and reuse of data in
41
+ a Work;
42
+ 6. database rights (such as those arising under Directive 96/9/EC of the
43
+ European Parliament and of the Council of 11 March 1996 on the legal
44
+ protection of databases, and under any national implementation thereof,
45
+ including any amended or successor version of such directive); and
46
+ 7. other similar, equivalent or corresponding rights throughout the world
47
+ based on applicable law or treaty, and any national implementations
48
+ thereof.
49
+
50
+ 2. Waiver. To the greatest extent permitted by, but not in contravention of,
51
+ applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and
52
+ unconditionally waives, abandons, and surrenders all of Affirmer’s Copyright
53
+ and Related Rights and associated claims and causes of action, whether now
54
+ known or unknown (including existing as well as future claims and causes of
55
+ action), in the Work (i) in all territories worldwide, (ii) for the maximum
56
+ duration provided by applicable law or treaty (including future time
57
+ extensions), (iii) in any current or future medium and for any number of
58
+ copies, and (iv) for any purpose whatsoever, including without limitation
59
+ commercial, advertising or promotional purposes (the “Waiver”). Affirmer
60
+ makes the Waiver for the benefit of each member of the public at large and
61
+ to the detriment of Affirmer’s heirs and successors, fully intending that
62
+ such Waiver shall not be subject to revocation, rescission, cancellation,
63
+ termination, or any other legal or equitable action to disrupt the quiet
64
+ enjoyment of the Work by the public as contemplated by Affirmer’s express
65
+ Statement of Purpose.
66
+
67
+ 3. Public License Fallback. Should any part of the Waiver for any reason be
68
+ judged legally invalid or ineffective under applicable law, then the Waiver
69
+ shall be preserved to the maximum extent permitted taking into account
70
+ Affirmer’s express Statement of Purpose. In addition, to the extent the
71
+ Waiver is so judged Affirmer hereby grants to each affected person a
72
+ royalty-free, non transferable, non sublicensable, non exclusive,
73
+ irrevocable and unconditional license to exercise Affirmer’s Copyright and
74
+ Related Rights in the Work (i) in all territories worldwide, (ii) for the
75
+ maximum duration provided by applicable law or treaty (including future time
76
+ extensions), (iii) in any current or future medium and for any number of
77
+ copies, and (iv) for any purpose whatsoever, including without limitation
78
+ commercial, advertising or promotional purposes (the “License”). The License
79
+ shall be deemed effective as of the date CC0 was applied by Affirmer to the
80
+ Work. Should any part of the License for any reason be judged legally
81
+ invalid or ineffective under applicable law, such partial invalidity or
82
+ ineffectiveness shall not invalidate the remainder of the License, and in
83
+ such case Affirmer hereby affirms that he or she will not (i) exercise any
84
+ of his or her remaining Copyright and Related Rights in the Work or (ii)
85
+ assert any associated claims and causes of action with respect to the Work,
86
+ in either case contrary to Affirmer’s express Statement of Purpose.
87
+
88
+ 4. Limitations and Disclaimers.
89
+ 1. No trademark or patent rights held by Affirmer are waived, abandoned,
90
+ surrendered, licensed or otherwise affected by this document.
91
+ 2. Affirmer offers the Work as-is and makes no representations or warranties
92
+ of any kind concerning the Work, express, implied, statutory or
93
+ otherwise, including without limitation warranties of title,
94
+ merchantability, fitness for a particular purpose, non infringement, or
95
+ the absence of latent or other defects, accuracy, or the present or
96
+ absence of errors, whether or not discoverable, all to the greatest
97
+ extent permissible under applicable law.
98
+ 3. Affirmer disclaims responsibility for clearing rights of other persons
99
+ that may apply to the Work or any use thereof, including without
100
+ limitation any person’s Copyright and Related Rights in the Work.
101
+ Further, Affirmer disclaims responsibility for obtaining any necessary
102
+ consents, permissions or other rights required for any use of the Work.
103
+ 4. Affirmer understands and acknowledges that Creative Commons is not a
104
+ party to this document and has no duty or obligation with respect to this
105
+ CC0 or use of the Work.
106
+
107
+ For more information, please see
108
+ http://creativecommons.org/publicdomain/zero/1.0/.
package/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # stylelint-logical-spec
2
+
3
+ A stylelint plugin to enforce the use of logical properties and values instead of physical ones.
4
+
5
+ > [!NOTE]
6
+ > This project is a fork of [`Jordan-Hall/stylelint-use-logical-spec`](https://github.com/Jordan-Hall/stylelint-use-logical-spec) (originally inspired by `csstools/stylelint-use-logical`).
7
+ >
8
+ > **Why this fork exists:**
9
+ > - **Modernization**: Full migration to TypeScript for better type safety and developer experience.
10
+ > - **Compatibility**: Support for Stylelint 16 and 17+.
11
+ > - **Standardization**: Converted to a native ES Module (ESM) and modernized testing infrastructure.
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ npm install stylelint-logical-spec --save-dev
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ Add it to your stylelint configuration:
22
+
23
+ ```json
24
+ {
25
+ "plugins": ["stylelint-logical-spec"],
26
+ "rules": {
27
+ "liberty/use-logical-spec": true
28
+ }
29
+ }
30
+ ```
31
+
32
+ ## Rules
33
+
34
+ ### `liberty/use-logical-spec`
35
+
36
+ Enforces logical properties and values like `margin-block-start` instead of physical ones like `margin-top`.
37
+
38
+ #### Options
39
+
40
+ - `direction`: `ltr` (default) or `rtl`. Controls the mapping of physical properties to logical ones.
41
+ - `except`: An array of properties or regex patterns to ignore.
42
+
43
+ ```json
44
+ {
45
+ "rules": {
46
+ "liberty/use-logical-spec": [true, { "direction": "rtl", "except": ["top"] }]
47
+ }
48
+ }
49
+ ```
50
+
51
+ Supports auto-fixing with `--fix`.
52
+
53
+ ## License
54
+
55
+ [CC0-1.0](LICENSE.md)
@@ -0,0 +1,6 @@
1
+ import stylelint from 'stylelint';
2
+ import ruleName from './lib/rule-name.js';
3
+ import messages from './lib/messages.js';
4
+ export { ruleName, messages };
5
+ declare const _default: stylelint.Plugin;
6
+ export default _default;
package/dist/index.js ADDED
@@ -0,0 +1,315 @@
1
+ import stylelint from 'stylelint';
2
+ import { physicalProp, physical2Prop, physicalShorthandProp, physical4Prop, physicalValue, migrationNoneSpec, propsThatContainPropsInValue } from './lib/maps.js';
3
+ import { validateRuleWithProps } from './lib/validate.js';
4
+ import { cssValueSplit } from './lib/value-split.js';
5
+ import ruleName from './lib/rule-name.js';
6
+ import messages from './lib/messages.js';
7
+ import walk from './lib/walk.js';
8
+ const reportedDecls = new WeakMap();
9
+ // Ignore autofix on those expression value
10
+ const expressionRegex = /^\$\{.*\}$/g;
11
+ const ruleFunction = (primary, secondaryOptions = {}, _context = {}) => {
12
+ const propExceptions = [].concat(secondaryOptions.except || []);
13
+ const dir = /^rtl$/i.test(secondaryOptions.direction || '') ? 'rtl' : 'ltr';
14
+ const physicalProps = physicalProp(dir);
15
+ const physical2Props = physical2Prop(dir);
16
+ const physicalValues = physicalValue(dir);
17
+ // Pre-compile regex for performance
18
+ const propRegexMap = new Map();
19
+ physicalProps.forEach(([props]) => {
20
+ props.forEach(searchProp => {
21
+ if (!propRegexMap.has(searchProp)) {
22
+ propRegexMap.set(searchProp, new RegExp("(?<!-)" + searchProp + "(?!-)", "g"));
23
+ }
24
+ });
25
+ });
26
+ return (root, result) => {
27
+ // validate the method
28
+ const isMethodValid = stylelint.utils.validateOptions(result, ruleName, {
29
+ actual: primary,
30
+ possible() {
31
+ return isMethodIndifferent(primary) ||
32
+ isMethodAlways(primary);
33
+ }
34
+ });
35
+ if (!isMethodValid || !isMethodAlways(primary)) {
36
+ return;
37
+ }
38
+ walk(root, node => {
39
+ migrationNoneSpec.forEach(([prop, props]) => {
40
+ validateRuleWithProps(node, prop, (...args) => {
41
+ const outDateDecl = args[0];
42
+ if (isDeclReported(outDateDecl))
43
+ return;
44
+ stylelint.utils.report({
45
+ message: messages.unsupportedProp(outDateDecl.prop, props),
46
+ node: outDateDecl,
47
+ result,
48
+ ruleName,
49
+ fix: () => {
50
+ if (expressionRegex.test(outDateDecl.value))
51
+ return;
52
+ const value = outDateDecl.value;
53
+ outDateDecl.cloneBefore({
54
+ prop: props[0],
55
+ value
56
+ });
57
+ outDateDecl.cloneAfter({
58
+ prop: props[1],
59
+ value
60
+ });
61
+ outDateDecl.remove();
62
+ }
63
+ });
64
+ reportedDecls.set(outDateDecl, true);
65
+ });
66
+ });
67
+ /* logical shorthands do not work yet in browsers */
68
+ /* but we can still use shorthand if all values are the same */
69
+ // validate or autofix 4 physical properties as logical shorthands
70
+ physical4Prop.forEach(([props, prop]) => {
71
+ validateRuleWithProps(node, props, (...args) => {
72
+ const blockStartDecl = args[0];
73
+ const inlineStartDecl = args[2];
74
+ const blockEndDecl = args[4];
75
+ const inlineEndDecl = args[6];
76
+ if (isDeclReported(blockStartDecl) || isDeclReported(inlineStartDecl) || isDeclReported(blockEndDecl) || isDeclReported(inlineEndDecl))
77
+ return;
78
+ const values = shorthandValueShorten([blockStartDecl.value, inlineStartDecl.value, blockEndDecl.value, inlineEndDecl.value]);
79
+ if (!isDeclAnException(blockStartDecl, propExceptions) &&
80
+ !isDeclAnException(inlineStartDecl, propExceptions) &&
81
+ !isDeclAnException(blockEndDecl, propExceptions) &&
82
+ !isDeclAnException(inlineEndDecl, propExceptions) &&
83
+ values.length === 1 // only report issues if there is 1 value after shortening
84
+ ) {
85
+ stylelint.utils.report({
86
+ message: messages.unexpectedProp(blockStartDecl.prop, prop),
87
+ node: blockStartDecl,
88
+ result,
89
+ ruleName,
90
+ fix: () => {
91
+ if (expressionRegex.test(blockStartDecl.value))
92
+ return;
93
+ blockStartDecl.cloneBefore({
94
+ prop,
95
+ value: values.join(' ')
96
+ });
97
+ blockStartDecl.remove();
98
+ inlineStartDecl.remove();
99
+ blockEndDecl.remove();
100
+ inlineEndDecl.remove();
101
+ }
102
+ });
103
+ reportedDecls.set(blockStartDecl, true);
104
+ reportedDecls.set(inlineStartDecl, true);
105
+ reportedDecls.set(blockEndDecl, true);
106
+ reportedDecls.set(inlineEndDecl, true);
107
+ }
108
+ });
109
+ });
110
+ // validate or autofix shorthand properties that are not supported
111
+ physicalShorthandProp.forEach((prop) => {
112
+ validateRuleWithProps(node, [prop], (...args) => {
113
+ const physicalDecl = args[0];
114
+ if (isDeclReported(physicalDecl))
115
+ return;
116
+ const inputValues = cssValueSplit(physicalDecl.value);
117
+ if (!isDeclAnException(physicalDecl, propExceptions) &&
118
+ inputValues.length !== 1) {
119
+ stylelint.utils.report({
120
+ message: messages.unexpectedProp(physicalDecl.prop, `${prop}-block and ${prop}-inline`),
121
+ node: physicalDecl,
122
+ result,
123
+ ruleName,
124
+ fix: () => {
125
+ if (expressionRegex.test(physicalDecl.value))
126
+ return;
127
+ let outputValues = convertShorthandValues(inputValues, dir);
128
+ ['block', 'inline'].forEach(type => {
129
+ physicalDecl.cloneBefore({
130
+ prop: prop + "-" + type,
131
+ value: outputValues[type]
132
+ });
133
+ });
134
+ physicalDecl.remove();
135
+ }
136
+ });
137
+ reportedDecls.set(physicalDecl, true);
138
+ }
139
+ });
140
+ });
141
+ // validate or autofix 2 physical properties as logical shorthands
142
+ physical2Props.forEach(([props, prop]) => {
143
+ validateRuleWithProps(node, props, (...args) => {
144
+ const startDecl = args[0];
145
+ const startIndex = args[1];
146
+ const endDecl = args[2];
147
+ const endStartIndex = args[3];
148
+ if (isDeclReported(startDecl) || isDeclReported(endDecl))
149
+ return;
150
+ const firstInlineDecl = startIndex < endStartIndex
151
+ ? startDecl
152
+ : endDecl;
153
+ if (!isDeclAnException(startDecl, propExceptions) && !isDeclAnException(endDecl, propExceptions)) {
154
+ stylelint.utils.report({
155
+ message: messages.unexpectedProp(firstInlineDecl.prop, prop),
156
+ node: firstInlineDecl,
157
+ result,
158
+ ruleName,
159
+ fix: () => {
160
+ if (expressionRegex.test(startDecl.value))
161
+ return;
162
+ firstInlineDecl.cloneBefore({
163
+ prop,
164
+ value: startDecl.value === endDecl.value
165
+ ? startDecl.value
166
+ : [startDecl.value, endDecl.value].join(' ')
167
+ });
168
+ startDecl.remove();
169
+ endDecl.remove();
170
+ }
171
+ });
172
+ reportedDecls.set(startDecl, true);
173
+ reportedDecls.set(endDecl, true);
174
+ }
175
+ });
176
+ });
177
+ // validate or autofix physical properties as logical
178
+ physicalProps.forEach(([props, prop]) => {
179
+ validateRuleWithProps(node, props, (...args) => {
180
+ const physicalDecl = args[0];
181
+ if (isDeclReported(physicalDecl))
182
+ return;
183
+ if (!isDeclAnException(physicalDecl, propExceptions)) {
184
+ stylelint.utils.report({
185
+ message: messages.unexpectedProp(physicalDecl.prop, prop),
186
+ node: physicalDecl,
187
+ result,
188
+ ruleName,
189
+ fix: () => {
190
+ if (expressionRegex.test(physicalDecl.value))
191
+ return;
192
+ physicalDecl.prop = prop;
193
+ }
194
+ });
195
+ reportedDecls.set(physicalDecl, true);
196
+ }
197
+ });
198
+ });
199
+ // validate or autofix physical values as logical
200
+ physicalValues.forEach(([regexp, props]) => {
201
+ const decl = node;
202
+ if (isNodeMatchingDecl(decl, regexp) && !isDeclAnException(decl, propExceptions)) {
203
+ if (isDeclReported(decl))
204
+ return;
205
+ const valuekey = decl.value.toLowerCase();
206
+ if (valuekey in props) {
207
+ const value = props[valuekey];
208
+ stylelint.utils.report({
209
+ message: messages.unexpectedValue(decl.prop, decl.value, value),
210
+ node: decl,
211
+ result,
212
+ ruleName,
213
+ fix: () => {
214
+ if (expressionRegex.test(decl.value))
215
+ return;
216
+ decl.value = value;
217
+ }
218
+ });
219
+ reportedDecls.set(decl, true);
220
+ }
221
+ }
222
+ });
223
+ // validate or autofix physical values containing properties as logical
224
+ if (isNodeMatchingDecl(node, propsThatContainPropsInValue)) {
225
+ const decl = node;
226
+ if (isDeclReported(decl))
227
+ return;
228
+ const originalValue = decl.value.toLowerCase();
229
+ let value = originalValue;
230
+ physicalProps.forEach(([props, prop]) => {
231
+ if (!isDeclAnException(decl, propExceptions)) {
232
+ props.forEach(searchProp => {
233
+ if (!isValueAnException(searchProp, propExceptions)) {
234
+ const regex = propRegexMap.get(searchProp);
235
+ if (regex && regex.test(value)) {
236
+ value = value.replace(regex, prop);
237
+ }
238
+ }
239
+ });
240
+ }
241
+ });
242
+ if (value !== originalValue) {
243
+ stylelint.utils.report({
244
+ message: messages.unexpectedValue(decl.prop, decl.value, value),
245
+ node: decl,
246
+ result,
247
+ ruleName,
248
+ fix: () => {
249
+ if (expressionRegex.test(decl.value))
250
+ return;
251
+ decl.value = value;
252
+ }
253
+ });
254
+ reportedDecls.set(decl, true);
255
+ }
256
+ }
257
+ });
258
+ };
259
+ };
260
+ ruleFunction.ruleName = ruleName;
261
+ ruleFunction.messages = messages;
262
+ ruleFunction.meta = {
263
+ url: 'https://github.com/bleeding-gums/stylelint-logical-spec',
264
+ fixable: true,
265
+ };
266
+ export { ruleName, messages };
267
+ export default stylelint.createPlugin(ruleName, ruleFunction);
268
+ const isMethodIndifferent = (method) => method === 'ignore' || method === false || method === null;
269
+ const isMethodAlways = (method) => method === 'always' || method === true;
270
+ const isNodeMatchingDecl = (decl, regexp) => decl.type === 'decl' && regexp.test(decl.prop);
271
+ const isDeclAnException = (decl, propExceptions) => isValueAnException(decl.prop, propExceptions);
272
+ const isValueAnException = (value, propExceptions) => propExceptions.some(match => match instanceof RegExp
273
+ ? match.test(value)
274
+ : String(match || '').toLowerCase() === String(value || '').toLowerCase());
275
+ const isDeclReported = (decl) => reportedDecls.has(decl);
276
+ const shorthandValueShorten = (values) => {
277
+ const map = [[1, 0], [0, 2], [1, 3]];
278
+ for (let x = values.length - 2; x >= 0; x--) {
279
+ if (values[map[x][0]] !== values[map[x][1]]) {
280
+ break;
281
+ }
282
+ values.pop();
283
+ }
284
+ return values;
285
+ };
286
+ const convertShorthandValues = (input, dir) => {
287
+ let block = '', inline = '';
288
+ if (input.length === 1) {
289
+ block = input[0];
290
+ inline = input[0];
291
+ }
292
+ if (input.length === 2) {
293
+ block = input[0];
294
+ inline = input[1];
295
+ }
296
+ if (input.length === 3) {
297
+ block = input[0] + ' ' + input[2];
298
+ inline = input[1];
299
+ }
300
+ if (input.length === 4) {
301
+ block = input[0] + ' ' + input[2];
302
+ inline = dir === 'ltr' ? input[3] + ' ' + input[1] : input[1] + ' ' + input[3];
303
+ }
304
+ return {
305
+ block: optimizeCssValues(block),
306
+ inline: optimizeCssValues(inline)
307
+ };
308
+ };
309
+ const optimizeCssValues = (value) => {
310
+ const values = cssValueSplit(value);
311
+ if (values.length === 2 && values[0] === values[1]) {
312
+ return values[0];
313
+ }
314
+ return value;
315
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ export type Direction = 'ltr' | 'rtl';
2
+ export declare const propsThatContainPropsInValue: RegExp;
3
+ export declare const physical4Prop: readonly (readonly [readonly string[], string])[];
4
+ export declare const physicalShorthandProp: readonly string[];
5
+ export declare const physical2Prop: (dir: Direction) => readonly (readonly [readonly string[], string])[];
6
+ export declare const physicalProp: (dir: Direction) => readonly (readonly [readonly string[], string])[];
7
+ export declare const physicalValue: (dir: Direction) => readonly (readonly [RegExp, Record<string, string>])[];
8
+ export declare const migrationNoneSpec: readonly (readonly [readonly string[], readonly string[]])[];
@@ -0,0 +1,82 @@
1
+ const inline = {
2
+ start: { ltr: 'left', rtl: 'right' },
3
+ end: { ltr: 'right', rtl: 'left' }
4
+ };
5
+ export const propsThatContainPropsInValue = /^(transition(-property)?|will-change)$/i;
6
+ export const physical4Prop = [
7
+ [['top', 'left', 'bottom', 'right'], 'inset'],
8
+ [['margin-top', 'margin-left', 'margin-bottom', 'margin-right'], 'margin'],
9
+ [['padding-top', 'padding-left', 'padding-bottom', 'padding-right'], 'padding']
10
+ ];
11
+ export const physicalShorthandProp = ['inset', 'margin', 'padding'];
12
+ export const physical2Prop = (dir) => [
13
+ [['top', 'bottom'], 'inset-block'],
14
+ [[inline.start[dir], inline.end[dir]], 'inset-inline'],
15
+ [['margin-top', 'margin-bottom'], 'margin-block'],
16
+ [[`margin-${inline.start[dir]}`, `margin-${inline.end[dir]}`], 'margin-inline'],
17
+ [['padding-top', 'padding-bottom'], 'padding-block'],
18
+ [[`padding-${inline.start[dir]}`, `padding-${inline.end[dir]}`], 'padding-inline'],
19
+ ];
20
+ export const physicalProp = (dir) => [
21
+ [['top'], 'inset-block-start'],
22
+ [['bottom'], 'inset-block-end'],
23
+ [[inline.start[dir]], 'inset-inline-start'],
24
+ [[inline.end[dir]], 'inset-inline-end'],
25
+ [['margin-top'], 'margin-block-start'],
26
+ [['margin-bottom'], 'margin-block-end'],
27
+ [[`margin-${inline.start[dir]}`], 'margin-inline-start'],
28
+ [[`margin-${inline.end[dir]}`], 'margin-inline-end'],
29
+ [['padding-top'], 'padding-block-start'],
30
+ [['padding-bottom'], 'padding-block-end'],
31
+ [[`padding-${inline.start[dir]}`], 'padding-inline-start'],
32
+ [[`padding-${inline.end[dir]}`], 'padding-inline-end'],
33
+ // border
34
+ [['border-top'], 'border-block-start'],
35
+ [['border-bottom'], 'border-block-end'],
36
+ [[`border-${inline.start[dir]}`], 'border-inline-start'],
37
+ [[`border-${inline.end[dir]}`], 'border-inline-end'],
38
+ [['border-top-color'], 'border-block-start-color'],
39
+ [['border-top-style'], 'border-block-start-style'],
40
+ [['border-top-width'], 'border-block-start-width'],
41
+ [['border-bottom-color'], 'border-block-end-color'],
42
+ [['border-bottom-style'], 'border-block-end-style'],
43
+ [['border-bottom-width'], 'border-block-end-width'],
44
+ [[`border-${inline.start[dir]}-color`], 'border-inline-start-color'],
45
+ [[`border-${inline.start[dir]}-style`], 'border-inline-start-style'],
46
+ [[`border-${inline.start[dir]}-width`], 'border-inline-start-width'],
47
+ [[`border-${inline.end[dir]}-color`], 'border-inline-end-color'],
48
+ [[`border-${inline.end[dir]}-style`], 'border-inline-end-style'],
49
+ [[`border-${inline.end[dir]}-width`], 'border-inline-end-width'],
50
+ [[`border-top-${inline.start[dir]}-radius`], 'border-start-start-radius'],
51
+ [[`border-bottom-${inline.start[dir]}-radius`], 'border-end-start-radius'],
52
+ [[`border-top-${inline.end[dir]}-radius`], 'border-start-end-radius'],
53
+ [[`border-bottom-${inline.end[dir]}-radius`], 'border-end-end-radius'],
54
+ [['height'], 'block-size'],
55
+ [['min-height'], 'min-block-size'],
56
+ [['max-height'], 'max-block-size'],
57
+ [['width'], 'inline-size'],
58
+ [['min-width'], 'min-inline-size'],
59
+ [['max-width'], 'max-inline-size'],
60
+ ];
61
+ export const physicalValue = (dir) => [
62
+ [/^clear$/i, {
63
+ [inline.start[dir]]: 'inline-start',
64
+ [inline.end[dir]]: 'inline-end'
65
+ }],
66
+ [/^float$/i, {
67
+ [inline.start[dir]]: 'inline-start',
68
+ [inline.end[dir]]: 'inline-end'
69
+ }],
70
+ [/^text-align$/i, {
71
+ [inline.start[dir]]: 'start',
72
+ [inline.end[dir]]: 'end'
73
+ }]
74
+ ];
75
+ export const migrationNoneSpec = [
76
+ [['margin-start'], ['margin-block-start', 'margin-inline-start']],
77
+ [['margin-end'], ['margin-block-end', 'margin-inline-end']],
78
+ [['padding-start'], ['padding-block-start', 'padding-inline-start']],
79
+ [['padding-end'], ['padding-block-end', 'padding-inline-end']],
80
+ [['inset-start'], ['inset-inline-start', 'inset-block-start']],
81
+ [['inset-end'], ['inset-inline-end', 'inset-block-end']],
82
+ ];
@@ -0,0 +1,6 @@
1
+ declare const _default: {
2
+ unexpectedProp: (physicalProperty: string, logicalProperty: string) => string;
3
+ unexpectedValue: (property: string, physicalValue: string, logicalValue: string) => string;
4
+ unsupportedProp: (physicalProperty: string, logicalProperty: [string, string]) => string;
5
+ };
6
+ export default _default;
@@ -0,0 +1,13 @@
1
+ import stylelint from 'stylelint';
2
+ import ruleName from './rule-name.js';
3
+ export default stylelint.utils.ruleMessages(ruleName, {
4
+ unexpectedProp(physicalProperty, logicalProperty) {
5
+ return `Unexpected "${physicalProperty}" property. Use "${logicalProperty}".`;
6
+ },
7
+ unexpectedValue(property, physicalValue, logicalValue) {
8
+ return `Unexpected "${physicalValue}" value in "${property}" property. Use "${logicalValue}".`;
9
+ },
10
+ unsupportedProp(physicalProperty, logicalProperty) {
11
+ return `"${physicalProperty}" is not part of CSS specs. Use "${logicalProperty[0]}" and "${logicalProperty[1]}".`;
12
+ },
13
+ });
@@ -0,0 +1,2 @@
1
+ declare const _default: "liberty/use-logical-spec";
2
+ export default _default;
@@ -0,0 +1 @@
1
+ export default 'liberty/use-logical-spec';
@@ -0,0 +1,4 @@
1
+ import type { ChildNode, Container, Declaration } from 'postcss';
2
+ type ValidationCallback = (...args: (Declaration | number)[]) => void;
3
+ export declare const validateRuleWithProps: (root: Container | ChildNode, props: string[], fn: ValidationCallback) => void;
4
+ export {};
@@ -0,0 +1,19 @@
1
+ export const validateRuleWithProps = (root, props, fn) => {
2
+ // For supporting css-in-js
3
+ const { nodes = [root] } = root;
4
+ // conditionally walk nodes with children
5
+ if (nodes && nodes.length) {
6
+ const args = [];
7
+ const hasProps = props.every(prop => {
8
+ const declIndex = nodes.findIndex(child => child.type === 'decl' && child.prop === prop);
9
+ const decl = nodes[declIndex];
10
+ if (decl) {
11
+ args.push(decl, declIndex);
12
+ }
13
+ return decl;
14
+ });
15
+ if (hasProps) {
16
+ fn(...args);
17
+ }
18
+ }
19
+ };
@@ -0,0 +1 @@
1
+ export declare const cssValueSplit: (value: string) => string[];
@@ -0,0 +1,48 @@
1
+ export const cssValueSplit = (value) => {
2
+ const ret = [];
3
+ const stack = [];
4
+ let part = '';
5
+ let esc = false;
6
+ let q = '';
7
+ for (let i = 0; i < value.length; i++) {
8
+ const c = value[i];
9
+ if (esc) {
10
+ esc = false;
11
+ }
12
+ else if (c === '\\') {
13
+ esc = true;
14
+ }
15
+ else if (q) {
16
+ if (c === q) {
17
+ q = '';
18
+ }
19
+ }
20
+ else if (c === "'" || c === '"') {
21
+ q = c;
22
+ }
23
+ else if (c === ' ' && stack.length === 0) {
24
+ if (part) {
25
+ ret.push(part);
26
+ }
27
+ part = '';
28
+ continue;
29
+ }
30
+ else if (c === '(') {
31
+ stack.push(')');
32
+ }
33
+ else if (c === '{') {
34
+ stack.push('}');
35
+ }
36
+ else if (c === '[') {
37
+ stack.push(']');
38
+ }
39
+ else if (stack.length && c === stack[stack.length - 1]) {
40
+ stack.length--;
41
+ }
42
+ part += c;
43
+ }
44
+ if (part) {
45
+ ret.push(part);
46
+ }
47
+ return ret;
48
+ };
@@ -0,0 +1,2 @@
1
+ import type { ChildNode, Container } from 'postcss';
2
+ export default function walk(node: Container | ChildNode, fn: (child: ChildNode) => void): void;
@@ -0,0 +1,18 @@
1
+ // walk all container nodes
2
+ export default function walk(node, fn) {
3
+ const container = node;
4
+ if (container.nodes && container.nodes.length) {
5
+ const nodes = container.nodes.slice();
6
+ const length = nodes.length;
7
+ let index = -1;
8
+ while (++index < length) {
9
+ const child = nodes[index];
10
+ if (!isDirRule(child)) {
11
+ fn(child);
12
+ walk(child, fn);
13
+ }
14
+ }
15
+ }
16
+ }
17
+ const dirSelectorRegExp = /:dir\(ltr|rtl\)/i;
18
+ const isDirRule = (node) => node.type === 'rule' && dirSelectorRegExp.test(node.selector);
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "stylelint-logical-spec",
3
+ "version": "6.0.0",
4
+ "description": "Enforce usage of logical properties and values in CSS",
5
+ "main": "./dist/index.js",
6
+ "exports": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "type": "module",
9
+ "files": [
10
+ "dist"
11
+ ],
12
+ "scripts": {
13
+ "build": "tsc",
14
+ "prepack": "npm run build",
15
+ "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/bleeding-gums/stylelint-logical-spec.git"
20
+ },
21
+ "homepage": "https://github.com/bleeding-gums/stylelint-logical-spec#readme",
22
+ "bugs": "https://github.com/bleeding-gums/stylelint-logical-spec/issues",
23
+ "keywords": [
24
+ "stylelint-plugin",
25
+ "css",
26
+ "logical",
27
+ "properties"
28
+ ],
29
+ "license": "CC0-1.0",
30
+ "devDependencies": {
31
+ "@types/jest": "^29.5.12",
32
+ "@types/node": "^20.11.24",
33
+ "@types/stylelint": "^14.0.0",
34
+ "jest": "^29.7.0",
35
+ "jest-preset-stylelint": "^9.1.0",
36
+ "stylelint": "^17.1.1",
37
+ "ts-jest": "^29.1.2",
38
+ "typescript": "^5.3.3"
39
+ },
40
+ "peerDependencies": {
41
+ "stylelint": "^16.0.0 || ^17.0.0"
42
+ }
43
+ }