aleman 1.4.0 → 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,12 @@
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
+
1
10
  2025.09.04, v1.4.0
2
11
 
3
12
  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
 
@@ -55,12 +62,32 @@ export function appendAttributeValue(path, name, value) {
55
62
  }
56
63
 
57
64
  export function setAttributeValue(node, name, value) {
65
+ if (!node)
66
+ return;
67
+
68
+ node = node.node || node;
69
+
58
70
  const attributeNode = getAttributeNode(node, name);
59
71
 
60
72
  if (attributeNode)
61
73
  setLiteralValue(attributeNode.value, value);
62
74
  }
63
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
+
64
91
  export function removeAttributeValue(path, name, attributeValue) {
65
92
  if (!path)
66
93
  return;
@@ -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>;
@@ -38,18 +38,15 @@ export const traverse = ({options, push}) => ({
38
38
  if (path.node.name.name !== 'li')
39
39
  return;
40
40
 
41
- if (!isJSXElement(path.parentPath.parentPath))
42
- return;
41
+ const parentParentPath = path.parentPath.parentPath;
43
42
 
44
- const openingElementPath = path.parentPath.parentPath.get('openingElement');
43
+ if (!isJSXElement(parentParentPath))
44
+ return;
45
45
 
46
- if (!checkDataName(openingElementPath, name))
46
+ if (!checkDataName(parentParentPath, name))
47
47
  return;
48
48
 
49
- const children = path.parentPath
50
- .parentPath
51
- .get('children')
52
- .filter(isJSXElement);
49
+ const children = parentParentPath.get('children').filter(isJSXElement);
53
50
 
54
51
  const prev = children[index - 1];
55
52
  const current = children[index];
@@ -58,8 +55,7 @@ export const traverse = ({options, push}) => ({
58
55
  if (!current)
59
56
  return;
60
57
 
61
- const currentOpeningElement = current.get('openingElement');
62
- const attributePath = getAttributePath(currentOpeningElement, 'className');
58
+ const attributePath = getAttributePath(current, 'className');
63
59
 
64
60
  push({
65
61
  path: attributePath,
@@ -78,8 +74,6 @@ function unselect(path) {
78
74
  function addShowSubmenu(path, {showSubmenu}) {
79
75
  if (showSubmenu)
80
76
  return appendAttributeValue(path, 'className', 'menu-submenu-show');
81
-
82
- removeAttributeValue(path, 'className', 'menu-submenu-show');
83
77
  }
84
78
 
85
79
  function removeShowSubmenu(path) {
@@ -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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aleman",
3
- "version": "1.4.0",
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",