stylelint-plugin-logical-css 0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Fedya Petrakov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # Stylelint Plugin Logical CSS
2
+
3
+ Proper docs to come, but this plugin will expose two rules:
4
+
5
+ - `use-logical-properties-and-values`
6
+
7
+ This rule will enforce the use of logical properties and values
8
+
9
+ - `use-logical-units`
10
+
11
+ This rule will enforce the use of logical viewport units
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "stylelint-plugin-logical-css",
3
+ "version": "0.0.1",
4
+ "description": "A Stylelint plugin to enforce the use of logical CSS properties, values and units.",
5
+ "main": "index.js",
6
+ "files": [
7
+ "src/**/*.js",
8
+ "!**/**/*.test.js"
9
+ ],
10
+ "scripts": {
11
+ "test": "jest"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/yuschick/stylelint-plugin-logical-css.git"
16
+ },
17
+ "author": "Daniel Yuschick",
18
+ "license": "MIT",
19
+ "bugs": {
20
+ "url": "https://github.com/yuschick/stylelint-plugin-logical-css/issues"
21
+ },
22
+ "homepage": "https://github.com/yuschick/stylelint-plugin-logical-css#readme",
23
+ "engines": {
24
+ "node": ">=16.0.0"
25
+ },
26
+ "keywords": [
27
+ "Stylelint",
28
+ "Plugin",
29
+ "CSS",
30
+ "Logical CSS",
31
+ "Internationalization",
32
+ "i18n"
33
+ ],
34
+ "peerDependencies": {
35
+ "stylelint": "^12.0.0 || ^13.0.0 || ^14.0.0 || ^15.0.0"
36
+ },
37
+ "devDependencies": {
38
+ "jest": "^29.4.3",
39
+ "jest-cli": "^29.4.3",
40
+ "jest-preset-stylelint": "^6.1.0",
41
+ "stylelint": "^15.2.0"
42
+ }
43
+ }
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+
3
+ const stylelint = require("stylelint");
4
+
5
+ const ruleName = "plugin/use-logical-properties-and-values";
6
+
7
+ const ruleMessages = stylelint.utils.ruleMessages(ruleName, {
8
+ unexpectedProp(physicalProperty, logicalProperty) {
9
+ return `Unexpected "${physicalProperty}" property. Use "${logicalProperty}".`;
10
+ },
11
+ unexpectedValue(property, physicalValue, logicalValue) {
12
+ return `Unexpected "${physicalValue}" value in "${property}" property. Use "${logicalValue}".`;
13
+ },
14
+ });
15
+
16
+ const ruleMeta = {
17
+ url: "https://github.com/yuschick/stylelint-plugin-logical-css",
18
+ };
19
+
20
+ module.exports = {
21
+ ruleName,
22
+ ruleMessages,
23
+ ruleMeta,
24
+ };
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+
3
+ const stylelint = require("stylelint");
4
+
5
+ const { ruleName, ruleMessages, ruleMeta } = require("./base");
6
+ const { isPhysicalProperty } = require("../../utils/isPhysicalProperty");
7
+ const { isPhysicalValue } = require("../../utils/isPhysicalValue");
8
+ const { physicalPropertiesMap } = require("../../utils/physicalPropertiesMap");
9
+ const { physicalValuesMap } = require("../../utils/physicalValuesMap");
10
+
11
+ const ruleFunction = () => {
12
+ return (root, result) => {
13
+ const validOptions = stylelint.utils.validateOptions(result, ruleName);
14
+
15
+ if (!validOptions) {
16
+ return;
17
+ }
18
+
19
+ root.walkDecls((decl) => {
20
+ const propIsPhysical = isPhysicalProperty(decl.prop);
21
+ const valueIsPhysical = isPhysicalValue(decl.value);
22
+
23
+ if (!propIsPhysical && !valueIsPhysical) return;
24
+
25
+ const message = propIsPhysical
26
+ ? ruleMessages.unexpectedProp(
27
+ decl.prop,
28
+ physicalPropertiesMap[decl.prop]
29
+ )
30
+ : ruleMessages.unexpectedValue(
31
+ decl.prop,
32
+ decl.value,
33
+ physicalValuesMap[decl.prop][decl.value]
34
+ );
35
+
36
+ stylelint.utils.report({
37
+ message,
38
+ node: decl,
39
+ result,
40
+ ruleName,
41
+ });
42
+ });
43
+ };
44
+ };
45
+
46
+ ruleFunction.ruleName = ruleName;
47
+ ruleFunction.messages = ruleMessages;
48
+ ruleFunction.meta = ruleMeta;
49
+
50
+ module.exports = stylelint.createPlugin(ruleName, ruleFunction);
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+
3
+ const stylelint = require("stylelint");
4
+
5
+ const ruleName = "plugin/use-logical-units";
6
+
7
+ const ruleMessages = stylelint.utils.ruleMessages(ruleName, {
8
+ unexpectedUnit(physicalUnit, logicalUnit) {
9
+ return `Unexpected "${physicalUnit}" unit. Use "${logicalUnit}".`;
10
+ },
11
+ });
12
+
13
+ const ruleMeta = {
14
+ url: "https://github.com/yuschick/stylelint-plugin-logical-css",
15
+ };
16
+
17
+ module.exports = {
18
+ ruleName,
19
+ ruleMessages,
20
+ ruleMeta,
21
+ };
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+
3
+ const stylelint = require("stylelint");
4
+
5
+ const { ruleName, ruleMessages, ruleMeta } = require("./base");
6
+ const { getValueUnit, isPhysicalUnit } = require("../../utils/isPhysicalUnit");
7
+ const { physicalUnitsMap } = require("../../utils/physicalUnitsMap");
8
+
9
+ const ruleFunction = () => {
10
+ return (root, result) => {
11
+ const validOptions = stylelint.utils.validateOptions(result, ruleName);
12
+
13
+ if (!validOptions) {
14
+ return;
15
+ }
16
+
17
+ root.walkDecls((decl) => {
18
+ const unitIsPhysical = isPhysicalUnit(decl.value);
19
+
20
+ if (!unitIsPhysical) return;
21
+
22
+ const physicalUnit = getValueUnit(decl.value);
23
+ const message = ruleMessages.unexpectedUnit(
24
+ physicalUnit,
25
+ physicalUnitsMap[physicalUnit]
26
+ );
27
+
28
+ stylelint.utils.report({
29
+ message,
30
+ node: decl,
31
+ result,
32
+ ruleName,
33
+ });
34
+ });
35
+ };
36
+ };
37
+
38
+ ruleFunction.ruleName = ruleName;
39
+ ruleFunction.messages = ruleMessages;
40
+ ruleFunction.meta = ruleMeta;
41
+
42
+ module.exports = stylelint.createPlugin(ruleName, ruleFunction);
@@ -0,0 +1,13 @@
1
+ const { physicalPropertiesMap } = require("./physicalPropertiesMap");
2
+
3
+ function isPhysicalProperty(property) {
4
+ const physicalProperties = Object.keys(physicalPropertiesMap);
5
+
6
+ const propIsPhysical = physicalProperties.includes(property);
7
+
8
+ return propIsPhysical;
9
+ }
10
+
11
+ module.exports = {
12
+ isPhysicalProperty,
13
+ };
@@ -0,0 +1,30 @@
1
+ const { physicalUnits } = require("./physical");
2
+
3
+ const expression = /(\d+\s?)(dvh|dvw|lvh|lvw|svh|svw|vh|vw|)(\s+|$)/;
4
+
5
+ function getValueUnit(value) {
6
+ const match = value.match(expression);
7
+
8
+ if (!match) return false;
9
+
10
+ const matchedUnit = match.find((item) =>
11
+ Object.values(physicalUnits).includes(item)
12
+ );
13
+
14
+ return matchedUnit;
15
+ }
16
+
17
+ function isPhysicalUnit(value) {
18
+ const unit = getValueUnit(value);
19
+
20
+ if (!unit) return false;
21
+
22
+ const unitIsPhysical = Object.values(physicalUnits).includes(unit);
23
+
24
+ return unitIsPhysical;
25
+ }
26
+
27
+ module.exports = {
28
+ getValueUnit,
29
+ isPhysicalUnit,
30
+ };
@@ -0,0 +1,16 @@
1
+ const { physicalAxis, physicalValues } = require("./physical");
2
+
3
+ function isPhysicalValue(value) {
4
+ const values = [
5
+ ...Object.values(physicalValues),
6
+ ...Object.values(physicalAxis),
7
+ ];
8
+
9
+ const valueIsPhysical = values.some((direction) => value.includes(direction));
10
+
11
+ return valueIsPhysical;
12
+ }
13
+
14
+ module.exports = {
15
+ isPhysicalValue,
16
+ };
@@ -0,0 +1,98 @@
1
+ const logicalAxis = Object.freeze({
2
+ block: "block",
3
+ inline: "inline",
4
+ });
5
+
6
+ const logicalInlinePoints = Object.freeze({
7
+ end: "end",
8
+ start: "start",
9
+ });
10
+
11
+ const logicalProperties = Object.freeze({
12
+ blockSize: "block-size",
13
+ borderBlock: "border-block",
14
+ borderBlockColor: "border-block-color",
15
+ borderBlockEnd: "border-block-end",
16
+ borderBlockEndColor: "border-block-end-color",
17
+ borderBlockEndStyle: "border-block-end-style",
18
+ borderBlockEndWidth: "border-block-end-width",
19
+ borderBlockStart: "border-block-start",
20
+ borderBlockStartColor: "border-block-start-color",
21
+ borderBlockStartStyle: "border-block-start-style",
22
+ borderBlockStartWidth: "border-block-start-width",
23
+ borderBlockStyle: "border-block-style",
24
+ borderBlockWidth: "border-block-width",
25
+ borderColor: "border-color",
26
+ borderEndEndRadius: "border-end-end-radius",
27
+ borderEndStartRadius: "border-end-start-radius",
28
+ borderInline: "border-inline",
29
+ borderInlineColor: "border-inline-color",
30
+ borderInlineEnd: "border-inline-end",
31
+ borderInlineEndColor: "border-inline-end-color",
32
+ borderInlineEndStyle: "border-inline-end-style",
33
+ borderInlineEndWidth: "border-inline-end-width",
34
+ borderInlineStart: "border-inline-start",
35
+ borderInlineStartColor: "border-inline-start-color",
36
+ borderInlineStartStyle: "border-inline-start-style",
37
+ borderInlineStartWidth: "border-inline-start-width",
38
+ borderInlineStyle: "border-inline-style",
39
+ borderInlineWidth: "border-inline-width",
40
+ borderStartEndRadius: "border-start-end-radius",
41
+ borderStartStartRadius: "border-start-start-radius",
42
+ borderStyle: "border-style",
43
+ borderWidth: "border-width",
44
+ inlineSize: "inline-size",
45
+ inset: "inset",
46
+ insetBlock: "inset-block",
47
+ insetBlockEnd: "inset-block-end",
48
+ insetBlockStart: "inset-block-start",
49
+ insetInline: "inset-inline",
50
+ insetInlineEnd: "inset-inline-end",
51
+ insetInlineStart: "inset-inline-start",
52
+ marginBlock: "margin-block",
53
+ marginBlockEnd: "margin-block-end",
54
+ marginBlockStart: "margin-block-start",
55
+ marginInline: "margin-inline",
56
+ marginInlineEnd: "margin-inline-end",
57
+ marginInlineStart: "margin-inline-start",
58
+ maxBlockSize: "max-block-size",
59
+ maxInlineSize: "max-inline-size",
60
+ minBlockSize: "min-block-size",
61
+ minInlineSize: "min-inline-size",
62
+ overflowBlock: "overflow-block",
63
+ overflowInline: "overflow-inline",
64
+ overscrollBehaviorBlock: "overscroll-behavior-block",
65
+ overscrollBehaviorInline: "overscroll-behavior-inline",
66
+ paddingBlock: "padding-block",
67
+ paddingBlockEnd: "padding-block-end",
68
+ paddingBlockStart: "padding-block-start",
69
+ paddingInline: "padding-inline",
70
+ paddingInlineEnd: "padding-inline-end",
71
+ paddingInlineStart: "padding-inline-start",
72
+ });
73
+
74
+ const logicalUnits = Object.freeze({
75
+ dvb: "dvb",
76
+ dvi: "dvi",
77
+ lvb: "lvb",
78
+ lvi: "lvi",
79
+ svb: "svb",
80
+ svi: "svi",
81
+ vb: "vb",
82
+ vi: "vi",
83
+ });
84
+
85
+ const logicalValues = Object.freeze({
86
+ blockEnd: "block-end",
87
+ blockStart: "block-start",
88
+ inlineEnd: "inline-end",
89
+ inlineStart: "inline-start",
90
+ });
91
+
92
+ module.exports = {
93
+ logicalAxis,
94
+ logicalInlinePoints,
95
+ logicalProperties,
96
+ logicalUnits,
97
+ logicalValues,
98
+ };
@@ -0,0 +1,81 @@
1
+ const physicalAxis = Object.freeze({
2
+ horizontal: "horizontal",
3
+ vertical: "vertical",
4
+ x: "x",
5
+ y: "y",
6
+ });
7
+
8
+ const physicalProperties = Object.freeze({
9
+ borderBottom: "border-bottom",
10
+ borderBottomColor: "border-bottom-color",
11
+ borderBottomLeftRadius: "border-bottom-left-radius",
12
+ borderBottomRightRadius: "border-bottom-right-radius",
13
+ borderBottomStyle: "border-bottom-style",
14
+ borderBottomWidth: "border-bottom-width",
15
+ borderLeft: "border-left",
16
+ borderLeftColor: "border-left-color",
17
+ borderLeftStyle: "border-left-style",
18
+ borderLeftWidth: "border-left-width",
19
+ borderRight: "border-right",
20
+ borderRightColor: "border-right-color",
21
+ borderRightStyle: "border-right-style",
22
+ borderRightWidth: "border-right-width",
23
+ borderTop: "border-top",
24
+ borderTopColor: "border-top-color",
25
+ borderTopLeftRadius: "border-top-left-radius",
26
+ borderTopRightRadius: "border-top-right-radius",
27
+ borderTopStyle: "border-top-style",
28
+ borderTopWidth: "border-top-width",
29
+ bottom: "bottom",
30
+ captionSide: "caption-side",
31
+ clear: "clear",
32
+ float: "float",
33
+ height: "height",
34
+ left: "left",
35
+ marginBottom: "margin-bottom",
36
+ marginLeft: "margin-left",
37
+ marginRight: "margin-right",
38
+ marginTop: "margin-top",
39
+ maxHeight: "max-height",
40
+ maxWidth: "max-width",
41
+ minHeight: "min-height",
42
+ minWidth: "min-width",
43
+ overflowX: "overflow-x",
44
+ overflowY: "overflow-y",
45
+ overscrollBehaviorX: "overscroll-behavior-x",
46
+ overscrollBehaviorY: "overscroll-behavior-y",
47
+ paddingBottom: "padding-bottom",
48
+ paddingLeft: "padding-left",
49
+ paddingRight: "padding-right",
50
+ paddingTop: "padding-top",
51
+ resize: "resize",
52
+ right: "right",
53
+ textAlign: "text-align",
54
+ top: "top",
55
+ width: "width",
56
+ });
57
+
58
+ const physicalUnits = Object.freeze({
59
+ dvh: "dvh",
60
+ dvw: "dvw",
61
+ lvh: "lvh",
62
+ lvw: "lvw",
63
+ svh: "svh",
64
+ svw: "svw",
65
+ vh: "vh",
66
+ vw: "vw",
67
+ });
68
+
69
+ const physicalValues = Object.freeze({
70
+ bottom: "bottom",
71
+ left: "left",
72
+ right: "right",
73
+ top: "top",
74
+ });
75
+
76
+ module.exports = {
77
+ physicalAxis,
78
+ physicalProperties,
79
+ physicalUnits,
80
+ physicalValues,
81
+ };
@@ -0,0 +1,60 @@
1
+ const { logicalProperties } = require("./logical");
2
+ const { physicalProperties } = require("./physical");
3
+
4
+ const physicalPropertiesMap = Object.freeze({
5
+ [physicalProperties.borderBottom]: logicalProperties.borderBlockEnd,
6
+ [physicalProperties.borderBottomColor]: logicalProperties.borderBlockEndColor,
7
+ [physicalProperties.borderBottomLeftRadius]:
8
+ logicalProperties.borderEndStartRadius,
9
+ [physicalProperties.borderBottomRightRadius]:
10
+ logicalProperties.borderEndEndRadius,
11
+ [physicalProperties.borderBottomStyle]: logicalProperties.borderBlockEndStyle,
12
+ [physicalProperties.borderBottomWidth]: logicalProperties.borderBlockEndWidth,
13
+ [physicalProperties.borderLeft]: logicalProperties.borderInlineStart,
14
+ [physicalProperties.borderLeftColor]:
15
+ logicalProperties.borderInlineStartColor,
16
+ [physicalProperties.borderLeftStyle]:
17
+ logicalProperties.borderInlineStartStyle,
18
+ [physicalProperties.borderLeftWidth]:
19
+ logicalProperties.borderInlineStartWidth,
20
+ [physicalProperties.borderRight]: logicalProperties.borderInlineEnd,
21
+ [physicalProperties.borderRightColor]: logicalProperties.borderInlineEndColor,
22
+ [physicalProperties.borderRightStyle]: logicalProperties.borderInlineEndStyle,
23
+ [physicalProperties.borderRightWidth]: logicalProperties.borderInlineEndWidth,
24
+ [physicalProperties.borderTop]: logicalProperties.borderBlockStart,
25
+ [physicalProperties.borderTopColor]: logicalProperties.borderBlockStartColor,
26
+ [physicalProperties.borderTopLeftRadius]:
27
+ logicalProperties.borderStartStartRadius,
28
+ [physicalProperties.borderTopRightRadius]:
29
+ logicalProperties.borderStartEndRadius,
30
+ [physicalProperties.borderTopStyle]: logicalProperties.borderBlockStartStyle,
31
+ [physicalProperties.borderTopWidth]: logicalProperties.borderBlockStartWidth,
32
+ [physicalProperties.bottom]: logicalProperties.insetBlockEnd,
33
+ [physicalProperties.height]: logicalProperties.blockSize,
34
+ [physicalProperties.left]: logicalProperties.insetInlineStart,
35
+ [physicalProperties.marginBottom]: logicalProperties.marginBlockEnd,
36
+ [physicalProperties.marginLeft]: logicalProperties.marginInlineStart,
37
+ [physicalProperties.marginRight]: logicalProperties.marginInlineEnd,
38
+ [physicalProperties.marginTop]: logicalProperties.marginBlockStart,
39
+ [physicalProperties.maxHeight]: logicalProperties.maxBlockSize,
40
+ [physicalProperties.maxWidth]: logicalProperties.maxInlineSize,
41
+ [physicalProperties.minHeight]: logicalProperties.minBlockSize,
42
+ [physicalProperties.minWidth]: logicalProperties.minInlineSize,
43
+ [physicalProperties.overflowX]: logicalProperties.overflowInline,
44
+ [physicalProperties.overflowY]: logicalProperties.overflowBlock,
45
+ [physicalProperties.overscrollBehaviorX]:
46
+ logicalProperties.overscrollBehaviorInline,
47
+ [physicalProperties.overscrollBehaviorY]:
48
+ logicalProperties.overscrollBehaviorBlock,
49
+ [physicalProperties.paddingBottom]: logicalProperties.paddingBlockEnd,
50
+ [physicalProperties.paddingLeft]: logicalProperties.paddingInlineStart,
51
+ [physicalProperties.paddingRight]: logicalProperties.paddingInlineEnd,
52
+ [physicalProperties.paddingTop]: logicalProperties.paddingBlockStart,
53
+ [physicalProperties.right]: logicalProperties.insetInlineEnd,
54
+ [physicalProperties.top]: logicalProperties.insetBlockStart,
55
+ [physicalProperties.width]: logicalProperties.inlineSize,
56
+ });
57
+
58
+ module.exports = {
59
+ physicalPropertiesMap,
60
+ };
@@ -0,0 +1,17 @@
1
+ const { logicalUnits } = require("./logical");
2
+ const { physicalUnits } = require("./physical");
3
+
4
+ const physicalUnitsMap = Object.freeze({
5
+ [physicalUnits.dvh]: logicalUnits.dvb,
6
+ [physicalUnits.dvw]: logicalUnits.dvi,
7
+ [physicalUnits.lvh]: logicalUnits.lvb,
8
+ [physicalUnits.lvw]: logicalUnits.lvi,
9
+ [physicalUnits.svh]: logicalUnits.svb,
10
+ [physicalUnits.svw]: logicalUnits.svi,
11
+ [physicalUnits.vh]: logicalUnits.vb,
12
+ [physicalUnits.vw]: logicalUnits.vi,
13
+ });
14
+
15
+ module.exports = {
16
+ physicalUnitsMap,
17
+ };
@@ -0,0 +1,39 @@
1
+ const {
2
+ logicalAxis,
3
+ logicalInlinePoints,
4
+ logicalValues,
5
+ } = require("./logical");
6
+ const {
7
+ physicalAxis,
8
+ physicalProperties,
9
+ physicalValues,
10
+ } = require("./physical");
11
+
12
+ const physicalValuesMap = Object.freeze({
13
+ [physicalProperties.captionSide]: {
14
+ [physicalValues.bottom]: logicalValues.blockEnd,
15
+ [physicalValues.left]: logicalValues.inlineStart,
16
+ [physicalValues.right]: logicalValues.inlineEnd,
17
+ [physicalValues.top]: logicalValues.blockStart,
18
+ },
19
+ [physicalProperties.clear]: {
20
+ [physicalValues.left]: logicalValues.inlineStart,
21
+ [physicalValues.right]: logicalValues.inlineEnd,
22
+ },
23
+ [physicalProperties.float]: {
24
+ [physicalValues.left]: logicalValues.inlineStart,
25
+ [physicalValues.right]: logicalValues.inlineEnd,
26
+ },
27
+ [physicalProperties.resize]: {
28
+ [physicalAxis.horizontal]: logicalAxis.inline,
29
+ [physicalAxis.vertical]: logicalAxis.block,
30
+ },
31
+ [physicalProperties.textAlign]: {
32
+ [physicalValues.left]: logicalInlinePoints.start,
33
+ [physicalValues.right]: logicalInlinePoints.end,
34
+ },
35
+ });
36
+
37
+ module.exports = {
38
+ physicalValuesMap,
39
+ };