aleman 1.3.2 → 1.4.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 CHANGED
@@ -1,3 +1,18 @@
1
+ 2025.09.06, v1.4.1
2
+
3
+ feature:
4
+ - f745059 aleman: menu: build-menu: simplify
5
+ - 88f4f81 aleman: menu: rules: hide-submenu: siplify
6
+ - ffe78e2 aleman: menu: jsx-operator: containsClass
7
+ - 779f1e6 aleman: menu: rules: menu: simplify
8
+ - fe341ff menu: set-position: simplify
9
+
10
+ 2025.09.04, v1.4.0
11
+
12
+ feature:
13
+ - 2749907 aleman: menu: select: jsx-operator
14
+ - 43294ec aleman: menu: jsx-operator
15
+
1
16
  2025.09.04, v1.3.2
2
17
 
3
18
  feature:
@@ -26,7 +26,7 @@ const DefaultMenu = {
26
26
  };
27
27
 
28
28
  export const fix = ({path, menu, icon, name = ''}) => {
29
- const {children} = path.parentPath.node;
29
+ const {children} = path.node;
30
30
 
31
31
  for (const [key, value] of entries(menu)) {
32
32
  const menuItem = createMenuItem();
@@ -45,13 +45,13 @@ export const fix = ({path, menu, icon, name = ''}) => {
45
45
  setSubmenu(menuItem);
46
46
  menuItem.children.push(createMenu());
47
47
 
48
- const openingElement = path.parentPath
48
+ const elementPath = path
49
49
  .get('children')
50
50
  .at(-1)
51
- .get('children.1.openingElement');
51
+ .get('children.1');
52
52
 
53
53
  fix({
54
- path: openingElement,
54
+ path: elementPath,
55
55
  icon,
56
56
  menu: value,
57
57
  name: key,
@@ -61,7 +61,7 @@ export const fix = ({path, menu, icon, name = ''}) => {
61
61
  };
62
62
 
63
63
  export const traverse = ({options, push}) => ({
64
- JSXOpeningElement(path) {
64
+ JSXElement(path) {
65
65
  const {
66
66
  name = 'menu',
67
67
  menu = DefaultMenu,
@@ -71,7 +71,7 @@ export const traverse = ({options, push}) => ({
71
71
  if (!checkDataName(path, name))
72
72
  return;
73
73
 
74
- if (path.parentPath.node.children.length)
74
+ if (path.node.children.length)
75
75
  return;
76
76
 
77
77
  push({
@@ -1,41 +1,33 @@
1
- import {operator} from 'putout';
2
1
  import {checkDataName} from '../check-data-name.js';
3
- import {getAttributePath} from '../jsx-operator.js';
2
+ import {
3
+ checkTagName,
4
+ containsClass,
5
+ removeClass,
6
+ } from '../jsx-operator.js';
4
7
 
5
- const {setLiteralValue} = operator;
8
+ const CLASS = 'menu-submenu-show';
6
9
 
7
10
  export const report = () => `Hide submenu`;
8
11
 
9
12
  export const fix = (path) => {
10
- const {value} = path.node;
11
- const newValue = value.value;
12
-
13
- setLiteralValue(value, newValue
14
- .replace('menu-submenu-show', '')
15
- .trim());
13
+ removeClass(path, CLASS);
16
14
  };
17
15
 
18
16
  export const traverse = ({push, options}) => ({
19
- JSXOpeningElement(path) {
17
+ JSXElement(path) {
20
18
  const {name, showSubmenu} = options;
21
-
22
- if (path.node.name.name !== 'li')
23
- return;
19
+ const {parentPath} = path;
24
20
 
25
21
  if (showSubmenu)
26
22
  return;
27
23
 
28
- const openingElementPath = path.parentPath.parentPath.get('openingElement');
24
+ if (!checkTagName(path, 'li'))
25
+ return;
29
26
 
30
- if (!checkDataName(openingElementPath, name))
27
+ if (!checkDataName(parentPath, name))
31
28
  return false;
32
29
 
33
- const attributePath = getAttributePath(path, 'className');
34
-
35
- if (!attributePath)
36
- return;
37
-
38
- if (attributePath.node.value.value.includes('menu-submenu-show'))
39
- push(attributePath);
30
+ if (containsClass(path, CLASS))
31
+ push(path);
40
32
  },
41
33
  });
@@ -1,8 +1,12 @@
1
- import {operator} from 'putout';
1
+ import {operator, types} from 'putout';
2
2
 
3
+ const {isJSXElement} = types;
3
4
  const {setLiteralValue} = operator;
4
5
 
5
6
  export function getAttributeValue(path, attributeName) {
7
+ if (isJSXElement(path))
8
+ path = path.get('openingElement');
9
+
6
10
  const {attributes} = path.node;
7
11
 
8
12
  for (const {name, value} of attributes) {
@@ -28,6 +32,9 @@ export function getAttributeNode(node, name) {
28
32
  }
29
33
 
30
34
  export function getAttributePath(path, name) {
35
+ if (isJSXElement(path))
36
+ path = path.get('openingElement');
37
+
31
38
  let result = null;
32
39
  const attributes = path.get('attributes');
33
40
 
@@ -41,16 +48,58 @@ export function getAttributePath(path, name) {
41
48
  return result;
42
49
  }
43
50
 
44
- export function appendAttributeValue(node, name, value) {
51
+ export function appendAttributeValue(path, name, value) {
52
+ const node = path.node || path;
45
53
  const attributeNode = getAttributeNode(node, name);
46
54
 
47
- if (attributeNode)
48
- setLiteralValue(attributeNode.value, `${attributeNode.value.value} ${value}`);
55
+ if (!attributeNode)
56
+ return;
57
+
58
+ if (attributeNode.value.value.includes(value))
59
+ return;
60
+
61
+ setLiteralValue(attributeNode.value, `${attributeNode.value.value} ${value}`);
49
62
  }
50
63
 
51
64
  export function setAttributeValue(node, name, value) {
65
+ if (!node)
66
+ return;
67
+
68
+ node = node.node || node;
69
+
52
70
  const attributeNode = getAttributeNode(node, name);
53
71
 
54
72
  if (attributeNode)
55
73
  setLiteralValue(attributeNode.value, value);
56
74
  }
75
+
76
+ export function addClass(path, name) {
77
+ appendAttributeValue(path, 'className', name);
78
+ }
79
+
80
+ export function removeClass(path, name) {
81
+ removeAttributeValue(path, 'className', name);
82
+ }
83
+
84
+ export function containsClass(path, className) {
85
+ const classNameValue = getAttributeValue(path, 'className');
86
+ return classNameValue.includes(className);
87
+ }
88
+
89
+ export const checkTagName = (path, name) => path.node.openingElement.name.name === name;
90
+
91
+ export function removeAttributeValue(path, name, attributeValue) {
92
+ if (!path)
93
+ return;
94
+
95
+ const node = path.node || path;
96
+ const classAttribute = getAttributeNode(node, name);
97
+
98
+ if (!classAttribute)
99
+ return;
100
+
101
+ const {value} = classAttribute.value;
102
+
103
+ if (value.includes(attributeValue))
104
+ setLiteralValue(classAttribute.value, value.replace(RegExp(`\\s?${attributeValue}`), ''));
105
+ }
@@ -1,7 +1,9 @@
1
- import {operator} from 'putout';
2
1
  import {checkDataName} from '../check-data-name.js';
3
-
4
- const {setLiteralValue} = operator;
2
+ import {
3
+ addClass,
4
+ containsClass,
5
+ removeClass,
6
+ } from '../jsx-operator.js';
5
7
 
6
8
  export const report = ({command}) => {
7
9
  const [first, ...rest] = command;
@@ -10,44 +12,27 @@ export const report = ({command}) => {
10
12
  };
11
13
 
12
14
  export const fix = ({path, command}) => {
13
- const {value} = path.node;
14
- let newValue;
15
-
16
- if (command === 'show')
17
- newValue = value.value.replace(/\s?menu-hidden/, '');
18
- else
19
- newValue = value.value + ' menu-hidden';
15
+ if (command === 'show') {
16
+ removeClass(path, 'menu-hidden');
17
+ return;
18
+ }
20
19
 
21
- setLiteralValue(value, newValue);
20
+ addClass(path, 'menu-hidden');
22
21
  };
23
22
 
24
23
  export const traverse = ({push, options}) => ({
25
- JSXOpeningElement(path) {
24
+ JSXElement(path) {
26
25
  const {name, command} = options;
27
- const attributes = path.get('attributes');
28
26
 
29
- const opengineElementPath = path.parentPath.get('openingElement');
30
-
31
- if (!checkDataName(opengineElementPath, name))
27
+ if (!checkDataName(path, name))
32
28
  return false;
33
29
 
34
- for (const attr of attributes) {
35
- const {name, value} = attr.node;
36
-
37
- if (name.name !== 'className')
38
- continue;
39
-
40
- if (command === 'show' && value.value.includes('menu-hidden'))
41
- push({
42
- command: 'show',
43
- path: attr,
44
- });
45
-
46
- if (command === 'hide' && !value.value.includes('menu-hidden'))
47
- push({
48
- command,
49
- path: attr,
50
- });
51
- }
30
+ const shown = !containsClass(path, 'menu-hidden');
31
+
32
+ if (command === 'show' && !shown || command === 'hide' && shown)
33
+ push({
34
+ command,
35
+ path,
36
+ });
52
37
  },
53
38
  });
@@ -0,0 +1,8 @@
1
+ <ul data-name="menu" class="menu menu-hidden">
2
+ <li data-name="menu-item" className='menu-item icon icon-view'>
3
+ <label data-menu-path="View">View</label>
4
+ </li>
5
+ <li data-name="menu-item" className='menu-item icon icon-edit menu-item-selected menu-submenu-show'>
6
+ <label data-menu-path="Edit">Edit</label>
7
+ </li>
8
+ </ul>;
@@ -0,0 +1,8 @@
1
+ <ul data-name="menu" class="menu menu-hidden">
2
+ <li data-name="menu-item" className="menu-item icon icon-view menu-item-selected">
3
+ <label data-menu-path="View">View</label>
4
+ </li>
5
+ <li data-name="menu-item" className="menu-item icon icon-edit">
6
+ <label data-menu-path="Edit">Edit</label>
7
+ </li>
8
+ </ul>;
@@ -1,5 +1,10 @@
1
1
  import {operator, types} from 'putout';
2
2
  import {checkDataName} from '../check-data-name.js';
3
+ import {
4
+ appendAttributeValue,
5
+ getAttributePath,
6
+ removeAttributeValue,
7
+ } from '../jsx-operator.js';
3
8
 
4
9
  const {isJSXElement} = types;
5
10
  const {setLiteralValue} = operator;
@@ -33,18 +38,15 @@ export const traverse = ({options, push}) => ({
33
38
  if (path.node.name.name !== 'li')
34
39
  return;
35
40
 
36
- if (!isJSXElement(path.parentPath.parentPath))
37
- return;
41
+ const parentParentPath = path.parentPath.parentPath;
38
42
 
39
- const openingElementPath = path.parentPath.parentPath.get('openingElement');
43
+ if (!isJSXElement(parentParentPath))
44
+ return;
40
45
 
41
- if (!checkDataName(openingElementPath, name))
46
+ if (!checkDataName(parentParentPath, name))
42
47
  return;
43
48
 
44
- const children = path.parentPath
45
- .parentPath
46
- .get('children')
47
- .filter(isJSXElement);
49
+ const children = parentParentPath.get('children').filter(isJSXElement);
48
50
 
49
51
  const prev = children[index - 1];
50
52
  const current = children[index];
@@ -53,75 +55,27 @@ export const traverse = ({options, push}) => ({
53
55
  if (!current)
54
56
  return;
55
57
 
56
- for (const attr of current.get('openingElement.attributes')) {
57
- const {name} = attr.node;
58
-
59
- if (name.name !== 'className')
60
- continue;
61
-
62
- push({
63
- path: attr,
64
- current,
65
- prev,
66
- next,
67
- showSubmenu,
68
- });
69
- break;
70
- }
58
+ const attributePath = getAttributePath(current, 'className');
59
+
60
+ push({
61
+ path: attributePath,
62
+ current,
63
+ prev,
64
+ next,
65
+ showSubmenu,
66
+ });
71
67
  },
72
68
  });
73
69
 
74
70
  function unselect(path) {
75
- if (!path)
76
- return;
77
-
78
- for (const attr of path.get('openingElement.attributes')) {
79
- const {name, value} = attr.node;
80
-
81
- if (name.name !== 'className')
82
- continue;
83
-
84
- if (value.value.includes('menu-item-selected'))
85
- setLiteralValue(value, value.value.replace(' menu-item-selected', ''));
86
- }
71
+ removeAttributeValue(path, 'className', 'menu-item-selected');
87
72
  }
88
73
 
89
74
  function addShowSubmenu(path, {showSubmenu}) {
90
- for (const attr of path.get('openingElement.attributes')) {
91
- const {name, value} = attr.node;
92
-
93
- if (name.name !== 'className')
94
- continue;
95
-
96
- const currentValue = value.value;
97
-
98
- if (showSubmenu && !currentValue.includes('menu-submenu-show')) {
99
- setLiteralValue(value, `${currentValue} menu-submenu-show`);
100
- break;
101
- }
102
-
103
- if (!showSubmenu && currentValue.includes('menu-submenu-show')) {
104
- setLiteralValue(value, currentValue.replace('menu-submenu-show', ''));
105
- break;
106
- }
107
- }
75
+ if (showSubmenu)
76
+ return appendAttributeValue(path, 'className', 'menu-submenu-show');
108
77
  }
109
78
 
110
79
  function removeShowSubmenu(path) {
111
- if (!path)
112
- return;
113
-
114
- for (const attr of path.get('openingElement.attributes')) {
115
- const {name, value} = attr.node;
116
-
117
- if (name.name !== 'className')
118
- continue;
119
-
120
- const currentValue = value.value;
121
-
122
- if (currentValue.includes('menu-submenu-show')) {
123
- setLiteralValue(value, currentValue.replace(' menu-submenu-show', ''));
124
- break;
125
- }
126
- }
80
+ removeAttributeValue(path, 'className', 'menu-submenu-show');
127
81
  }
@@ -0,0 +1,2 @@
1
+ <ul data-name="menu" className="menu menu-hidden" style="left: 0; top: 20px;">
2
+ </ul>;
@@ -1,5 +1,10 @@
1
- import {operator, types} from 'putout';
1
+ import {types} from 'putout';
2
2
  import {checkDataName} from '../check-data-name.js';
3
+ import {
4
+ getAttributePath,
5
+ getAttributeValue,
6
+ setAttributeValue,
7
+ } from '../jsx-operator.js';
3
8
 
4
9
  const {
5
10
  stringLiteral,
@@ -7,56 +12,51 @@ const {
7
12
  jsxAttribute,
8
13
  } = types;
9
14
 
10
- const {setLiteralValue} = operator;
11
-
12
15
  export const report = () => `Set position`;
13
16
 
14
17
  export const fix = ({path, attr, x, y}) => {
15
18
  const style = `left: ${x}px; top: ${y}px;`;
16
19
 
17
20
  if (attr) {
18
- setLiteralValue(attr.value, style);
19
-
21
+ setAttributeValue(path, 'style', style);
20
22
  return;
21
23
  }
22
24
 
23
- const {attributes} = path.node;
24
-
25
+ const {attributes} = path.node.openingElement;
25
26
  const attribute = jsxAttribute(jsxIdentifier('style'), stringLiteral(style));
26
27
 
27
28
  attributes.push(attribute);
28
29
  };
29
30
 
30
31
  export const traverse = ({options, push}) => ({
31
- JSXOpeningElement(path) {
32
+ JSXElement(path) {
32
33
  const {name = 'menu', position = {}} = options;
33
34
  const {x = 0, y = 20} = position;
34
35
 
35
- const openingElementPath = path.parentPath.get('openingElement');
36
-
37
- if (!checkDataName(openingElementPath, name))
36
+ if (!checkDataName(path, name))
38
37
  return;
39
38
 
40
- for (const attr of path.node.attributes) {
41
- if (attr.name.name !== 'style')
42
- continue;
43
-
44
- const [x1, y1] = parsePosition(attr.value.value);
45
-
46
- if (x === x1 && y === y1)
47
- return;
48
-
39
+ const styleAttributeValue = getAttributeValue(path, 'style');
40
+
41
+ if (!styleAttributeValue) {
49
42
  push({
50
43
  path,
51
- attr,
52
44
  x,
53
45
  y,
54
46
  });
55
47
  return;
56
48
  }
57
49
 
50
+ const [x1, y1] = parsePosition(styleAttributeValue);
51
+
52
+ if (x === x1 && y === y1)
53
+ return;
54
+
55
+ const styleAttributePath = getAttributePath(path, 'style');
56
+
58
57
  push({
59
58
  path,
59
+ attr: styleAttributePath,
60
60
  x,
61
61
  y,
62
62
  });
@@ -68,5 +68,8 @@ function parsePosition(str) {
68
68
  .split(/;|left|\s|:|top|px/)
69
69
  .filter(Boolean);
70
70
 
71
- return [x, y];
71
+ return [
72
+ Number(x),
73
+ Number(y),
74
+ ];
72
75
  }
@@ -0,0 +1,3 @@
1
+ <li data-menu-path="Upload" data-name="menu-item" className="menu-item icon icon-view">
2
+ <label data-menu-path="Upload">Upload</label>
3
+ </li>
@@ -0,0 +1,17 @@
1
+ // second selected
2
+ <ul data-name="menu" class="menu menu-hidden">
3
+ <li data-menu-path="Upload" data-name="menu-item" className="menu-item icon icon-view">
4
+ <label data-menu-path="Upload">Upload</label>
5
+ </li>
6
+ <li data-menu-path="New" data-name="menu-item" className="menu-item icon icon-edit menu-item-selected">
7
+ <label data-menu-path="New">New</label>
8
+ <ul data-name="world" class="menu menu-hidden">
9
+ <li data-menu-path="New.File" data-name="menu-item" className="menu-item icon icon-view">
10
+ <label data-menu-path="New.File">File</label>
11
+ </li>
12
+ <li data-menu-path="New.Directory" data-name="menu-item" className="menu-item icon icon-edit">
13
+ <label data-menu-path="New.Directory">Directory</label>
14
+ </li>
15
+ </ul>
16
+ </li>
17
+ </ul>;
@@ -1,5 +1,10 @@
1
1
  import {operator, types} from 'putout';
2
2
  import {checkDataName} from '../check-data-name.js';
3
+ import {
4
+ getAttributePath,
5
+ getAttributeValue,
6
+ removeAttributeValue,
7
+ } from '../jsx-operator.js';
3
8
 
4
9
  const {isJSXElement} = types;
5
10
  const {setLiteralValue} = operator;
@@ -29,6 +34,9 @@ export const traverse = ({options, push}) => ({
29
34
  if (path.node.name.name !== 'li')
30
35
  return;
31
36
 
37
+ if (!isJSXElement(path.parentPath.parentPath))
38
+ return;
39
+
32
40
  const {insideSubmenu = true, submenuIndex = 1} = options;
33
41
  const parentMenu = path.parentPath.parentPath.parentPath;
34
42
 
@@ -38,9 +46,6 @@ export const traverse = ({options, push}) => ({
38
46
  if (!isParentSelected(parentMenu))
39
47
  return;
40
48
 
41
- if (!isJSXElement(path.parentPath.parentPath))
42
- return;
43
-
44
49
  const openingElementPath = path.parentPath.parentPath.get('openingElement');
45
50
 
46
51
  if (!checkDataName(openingElementPath))
@@ -58,7 +63,8 @@ export const traverse = ({options, push}) => ({
58
63
  if (!current)
59
64
  return;
60
65
 
61
- const [currentClassPath] = getClassPath(current);
66
+ const currentOpeningElementPath = current.get('openingElement');
67
+ const currentClassPath = getAttributePath(currentOpeningElementPath, 'className');
62
68
 
63
69
  push({
64
70
  path: currentClassPath,
@@ -70,46 +76,15 @@ export const traverse = ({options, push}) => ({
70
76
  });
71
77
 
72
78
  function isParentSelected(path) {
73
- const attributes = path.get('openingElement.attributes');
79
+ const openingElement = path.get('openingElement');
80
+ const classAttributeValue = getAttributeValue(openingElement, 'className');
74
81
 
75
- for (const attr of attributes) {
76
- const {name, value} = attr.node;
77
-
78
- if (name.name === 'className')
79
- return value.value.includes('menu-item-selected');
80
- }
81
-
82
- return false;
82
+ return classAttributeValue.includes('menu-item-selected');
83
83
  }
84
84
 
85
85
  function unselect(path) {
86
86
  if (!path)
87
87
  return;
88
88
 
89
- for (const attr of path.get('openingElement.attributes')) {
90
- const {name, value} = attr.node;
91
-
92
- if (name.name !== 'className')
93
- continue;
94
-
95
- if (value.value.includes('menu-item-selected'))
96
- setLiteralValue(value, value.value.replace(' menu-item-selected', ''));
97
- }
98
- }
99
-
100
- function getClassPath(path) {
101
- if (!path)
102
- return [null, ''];
103
-
104
- for (const attr of path.get('openingElement.attributes')) {
105
- const {name, value} = attr.node;
106
-
107
- if (name.name !== 'className')
108
- continue;
109
-
110
- return [
111
- attr,
112
- value.value,
113
- ];
114
- }
89
+ removeAttributeValue(path, 'className', 'menu-item-selected');
115
90
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aleman",
3
- "version": "1.3.2",
3
+ "version": "1.4.1",
4
4
  "type": "module",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout-based framework for web",