stylelint-plugin-logical-css 0.1.1 → 0.3.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/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # 🚀 Stylelint Plugin Logical CSS
1
+ # 🛸 Stylelint Plugin Logical CSS
2
2
 
3
3
  ![License](https://img.shields.io/github/license/yuschick/stylelint-plugin-logical-css?style=for-the-badge)
4
4
  ![NPM Version](https://img.shields.io/npm/v/stylelint-plugin-logical-css?style=for-the-badge)
@@ -24,8 +24,8 @@ npm i stylelint-plugin-logical-css --save-dev
24
24
  yarn add stylelint-plugin-logical-css --dev
25
25
  ```
26
26
 
27
- With the plugin installed, the rule(s) can now be added to the project's
28
- Stylelint configuration.
27
+ With the plugin installed, the rule(s) can be added to the project's Stylelint
28
+ configuration.
29
29
 
30
30
  ```json
31
31
  {
@@ -46,13 +46,34 @@ Let's explore each rule to better understand what it does, and does not, allow.
46
46
 
47
47
  ### `plugin/use-logical-properties-and-values`
48
48
 
49
- >
50
- > [Learn more about the properties and values supported by this rule](./src/rules/use-logical-properties-and-values)
49
+ **👉 [Learn more about this rule](./src/rules/use-logical-properties-and-values)
50
+ 👈**
51
51
 
52
52
  This rule is responsible for checking both CSS properties and values. When a
53
53
  physical property or value is found, it will be flagged.
54
54
 
55
- #### Not Allowed
55
+ #### Options
56
+
57
+ The use-logical-properties-and-values rule accepts the following options:
58
+
59
+ | Option | Description |
60
+ | --------------- | ----------------------------------------------------------------------------------------------- |
61
+ | enable-auto-fix | Use this option in addition to the native Stylelint `--fix` flag to enable auto fixing on save. |
62
+
63
+ ```json
64
+ {
65
+ "rules": {
66
+ "plugin/use-logical-properties-and-values": [
67
+ true,
68
+ { "severity": "warning", "enable-auto-fix": true }
69
+ ]
70
+ }
71
+ }
72
+ ```
73
+
74
+ #### Usage
75
+
76
+ ##### Not Allowed
56
77
 
57
78
  ```css
58
79
  .heading {
@@ -61,7 +82,7 @@ physical property or value is found, it will be flagged.
61
82
  }
62
83
  ```
63
84
 
64
- #### Allowed
85
+ ##### Allowed
65
86
 
66
87
  ```css
67
88
  .heading {
@@ -70,13 +91,11 @@ physical property or value is found, it will be flagged.
70
91
  }
71
92
  ```
72
93
 
73
- ### `plugin/use-logical-units`
94
+ ---
74
95
 
75
- >
76
- > [Learn more about the properties and values supported by this rule](./src/rules/use-logical-units)
96
+ ### `plugin/use-logical-units`
77
97
 
78
- > Read about current
79
- > [browser support for logical viewport units](https://caniuse.com/mdn-css_types_length_viewport_percentage_units_dynamic).
98
+ **👉 [Learn more about this rule](./src/rules/use-logical-units) 👈**
80
99
 
81
100
  This rule is responsible for checking that logical CSS units are used.
82
101
  Specifically, viewport units like `vw` and `vh` which stand for viewport width
@@ -84,7 +103,28 @@ and viewport height respectively will not reflect different writing modes and
84
103
  directions. Instead, this rule will enforce the logical equivalents, `vi` and
85
104
  `vb`.
86
105
 
87
- #### Not Allowed
106
+ #### Options
107
+
108
+ The use-logical-units rule accepts the following options:
109
+
110
+ | Option | Description |
111
+ | --------------- | ----------------------------------------------------------------------------------------------- |
112
+ | enable-auto-fix | Use this option in addition to the native Stylelint `--fix` flag to enable auto fixing on save. |
113
+
114
+ ```json
115
+ {
116
+ "rules": {
117
+ "plugin/use-logical-units": [
118
+ true,
119
+ { "severity": "warning", "enable-auto-fix": true }
120
+ ]
121
+ }
122
+ }
123
+ ```
124
+
125
+ #### Usage
126
+
127
+ ##### Not Allowed
88
128
 
89
129
  ```css
90
130
  body {
@@ -92,7 +132,7 @@ body {
92
132
  }
93
133
  ```
94
134
 
95
- #### Allowed
135
+ ##### Allowed
96
136
 
97
137
  ```css
98
138
  body {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stylelint-plugin-logical-css",
3
- "version": "0.1.1",
3
+ "version": "0.3.0",
4
4
  "description": "A Stylelint plugin to enforce the use of logical CSS properties, values and units.",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -7,25 +7,25 @@ const { isPhysicalProperty } = require('../../utils/isPhysicalProperty');
7
7
  const { isPhysicalValue } = require('../../utils/isPhysicalValue');
8
8
  const { physicalPropertiesMap } = require('../../utils/physicalPropertiesMap');
9
9
  const { physicalValuesMap } = require('../../utils/physicalValuesMap');
10
+ const { propsToSkip } = require('../../utils/propsToSkip');
10
11
 
11
12
  const ruleFunction = (_, options, context) => {
12
13
  return (root, result) => {
13
- const validOptions = stylelint.utils.validateOptions(result, ruleName, [
14
- {
15
- action: 'enableAutoFix',
16
- possible: [true, false],
17
- optional: true,
18
- },
19
- ]);
14
+ const validOptions = stylelint.utils.validateOptions(result, ruleName);
20
15
 
21
16
  if (!validOptions) {
22
17
  return;
23
18
  }
24
19
 
25
20
  root.walkDecls((decl) => {
21
+ if (propsToSkip.includes(decl.prop)) return;
22
+
26
23
  const propIsPhysical = isPhysicalProperty(decl.prop);
27
24
  const valueIsPhysical = isPhysicalValue(decl.value);
28
25
 
26
+ if (decl.prop.includes('grid')) {
27
+ console.log({ decl, propIsPhysical, valueIsPhysical });
28
+ }
29
29
  if (!propIsPhysical && !valueIsPhysical) return;
30
30
 
31
31
  const message = propIsPhysical
@@ -39,7 +39,7 @@ const ruleFunction = (_, options, context) => {
39
39
  physicalValuesMap[decl.prop][decl.value],
40
40
  );
41
41
 
42
- if (context.fix && options.enableAutoFix) {
42
+ if (context.fix && options?.['enable-auto-fix']) {
43
43
  if (propIsPhysical) {
44
44
  decl.prop = physicalPropertiesMap[decl.prop];
45
45
  }
@@ -6,7 +6,7 @@ const { ruleName, ruleMessages, ruleMeta } = require('./base');
6
6
  const { getValueUnit, isPhysicalUnit } = require('../../utils/isPhysicalUnit');
7
7
  const { physicalUnitsMap } = require('../../utils/physicalUnitsMap');
8
8
 
9
- const ruleFunction = () => {
9
+ const ruleFunction = (_, options, context) => {
10
10
  return (root, result) => {
11
11
  const validOptions = stylelint.utils.validateOptions(result, ruleName);
12
12
 
@@ -25,6 +25,15 @@ const ruleFunction = () => {
25
25
  physicalUnitsMap[physicalUnit],
26
26
  );
27
27
 
28
+ if (context.fix && options?.['enable-auto-fix']) {
29
+ decl.value = decl.value.replace(
30
+ physicalUnit,
31
+ physicalUnitsMap[physicalUnit],
32
+ );
33
+
34
+ return;
35
+ }
36
+
28
37
  stylelint.utils.report({
29
38
  message,
30
39
  node: decl,
@@ -0,0 +1,17 @@
1
+ /*
2
+ The properties included in this array can contain physical values like left and bottom.
3
+ However, logical values for the4se properties are not yet supported. So for the time being,
4
+ we skip linting them entirely.
5
+ */
6
+ const propsToSkip = [
7
+ 'background',
8
+ 'background-position',
9
+ 'grid-area',
10
+ 'grid-areas',
11
+ 'transform-origin',
12
+ 'vertical-align',
13
+ ];
14
+
15
+ module.exports = {
16
+ propsToSkip,
17
+ };