Skip to content

Commit cb25eb5

Browse files
committed
comments comments comments
1 parent 0df33aa commit cb25eb5

27 files changed

+1472
-893
lines changed

src/generators/jsx-ast/constants.mjs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { JSX_IMPORTS } from '../web/constants.mjs';
2+
23
/**
34
* UI classes for Node.js API stability levels
45
*
@@ -11,6 +12,9 @@ export const STABILITY_LEVELS = [
1112
'info', // (3) Legacy
1213
];
1314

15+
// How deep should the Table of Contents go?
16+
export const TOC_MAX_HEADING_DEPTH = 3;
17+
1418
// 'Stability: '.length + ' - '.length
1519
export const STABILITY_PREFIX_LENGTH = 14;
1620

@@ -84,6 +88,48 @@ export const AST_NODE_TYPES = {
8488
* @see https://github.com/estree/estree/blob/master/es5.md#expressionstatement
8589
*/
8690
EXPRESSION_STATEMENT: 'ExpressionStatement',
91+
92+
/**
93+
* Literal value (string, number, boolean, null)
94+
*
95+
* @see https://github.com/estree/estree/blob/master/es5.md#literal
96+
*/
97+
LITERAL: 'Literal',
98+
99+
/**
100+
* Identifier (e.g., variable name)
101+
*
102+
* @see https://github.com/estree/estree/blob/master/es5.md#identifier
103+
*/
104+
IDENTIFIER: 'Identifier',
105+
106+
/**
107+
* Array expression (e.g., [1, 2, 3])
108+
*
109+
* @see https://github.com/estree/estree/blob/master/es5.md#arrayexpression
110+
*/
111+
ARRAY_EXPRESSION: 'ArrayExpression',
112+
113+
/**
114+
* Object expression (e.g., { key: value })
115+
*
116+
* @see https://github.com/estree/estree/blob/master/es5.md#objectexpression
117+
*/
118+
OBJECT_EXPRESSION: 'ObjectExpression',
119+
120+
/**
121+
* Property within an object
122+
*
123+
* @see https://github.com/estree/estree/blob/master/es5.md#property
124+
*/
125+
PROPERTY: 'Property',
126+
127+
/**
128+
* JSX fragment
129+
*
130+
* @see https://github.com/estree/estree/blob/master/es2020.md#jsxfragment
131+
*/
132+
JSX_FRAGMENT: 'JSXFragment',
87133
},
88134
};
89135

src/generators/jsx-ast/index.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export default {
6363

6464
// Process each head node and build content
6565
const results = [];
66+
6667
for (const entry of headNodes) {
6768
const sideBarProps = buildSideBarProps(
6869
entry,

src/generators/jsx-ast/utils/__tests__/ast.test.mjs

Lines changed: 133 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,138 @@ import assert from 'node:assert/strict';
22
import { describe, it } from 'node:test';
33

44
import { AST_NODE_TYPES } from '../../constants.mjs';
5-
import { createJSXElement } from '../ast.mjs';
6-
7-
describe('AST utilities', () => {
8-
it('should build correct JSX tree with createJSXElement', () => {
9-
const el = createJSXElement('TestComponent', {
10-
inline: false,
11-
children: 'Some content',
12-
dataAttr: { test: true },
13-
});
14-
15-
assert.equal(el.type, AST_NODE_TYPES.MDX.JSX_BLOCK_ELEMENT);
16-
assert.equal(el.name, 'TestComponent');
17-
assert.ok(Array.isArray(el.children));
18-
assert.ok(el.attributes.some(attr => attr.name === 'dataAttr'));
5+
import { toESTree, createJSXElement, createAttributeNode } from '../ast.mjs';
6+
7+
describe('toESTree', () => {
8+
it('preserves existing JSX fragment nodes', () => {
9+
const fragment = { type: AST_NODE_TYPES.ESTREE.JSX_FRAGMENT };
10+
assert.equal(toESTree(fragment), fragment);
11+
});
12+
13+
it('handles undefined', () => {
14+
const result = toESTree(undefined);
15+
assert.equal(result.type, AST_NODE_TYPES.ESTREE.IDENTIFIER);
16+
assert.equal(result.name, 'undefined');
17+
});
18+
19+
it('handles null', () => {
20+
const result = toESTree(null);
21+
assert.equal(result.type, AST_NODE_TYPES.ESTREE.LITERAL);
22+
assert.equal(result.value, null);
23+
});
24+
25+
it('handles string values', () => {
26+
const result = toESTree('test');
27+
assert.equal(result.type, AST_NODE_TYPES.ESTREE.LITERAL);
28+
assert.equal(result.value, 'test');
29+
});
30+
31+
it('handles number values', () => {
32+
const result = toESTree(42);
33+
assert.equal(result.type, AST_NODE_TYPES.ESTREE.LITERAL);
34+
assert.equal(result.value, 42);
35+
});
36+
37+
it('handles boolean values', () => {
38+
const result = toESTree(true);
39+
assert.equal(result.type, AST_NODE_TYPES.ESTREE.LITERAL);
40+
assert.equal(result.value, true);
41+
});
42+
43+
it('handles arrays', () => {
44+
const result = toESTree([1, 2]);
45+
assert.equal(result.type, AST_NODE_TYPES.ESTREE.ARRAY_EXPRESSION);
46+
assert.equal(result.elements.length, 2);
47+
assert.equal(result.elements[0].value, 1);
48+
assert.equal(result.elements[1].value, 2);
49+
});
50+
51+
it('handles objects', () => {
52+
const result = toESTree({ a: 1 });
53+
assert.equal(result.type, AST_NODE_TYPES.ESTREE.OBJECT_EXPRESSION);
54+
assert.equal(result.properties.length, 1);
55+
assert.equal(result.properties[0].key.name, 'a');
56+
assert.equal(result.properties[0].value.value, 1);
57+
});
58+
});
59+
60+
describe('createJSXElement', () => {
61+
it('creates inline element by default', () => {
62+
const result = createJSXElement('div');
63+
assert.equal(result.type, AST_NODE_TYPES.MDX.JSX_INLINE_ELEMENT);
64+
assert.equal(result.name, 'div');
65+
});
66+
67+
it('creates block element when specified', () => {
68+
const result = createJSXElement('div', { inline: false });
69+
assert.equal(result.type, AST_NODE_TYPES.MDX.JSX_BLOCK_ELEMENT);
70+
});
71+
72+
it('handles string children', () => {
73+
const result = createJSXElement('div', { children: 'text' });
74+
assert.equal(result.children.length, 1);
75+
assert.equal(result.children[0].value, 'text');
76+
});
77+
78+
it('handles array children', () => {
79+
const children = [{ type: 'text', value: 'test' }];
80+
const result = createJSXElement('div', { children });
81+
assert.deepEqual(result.children, children);
82+
});
83+
84+
it('handles attributes', () => {
85+
const result = createJSXElement('div', { id: 'test' });
86+
assert.equal(result.attributes.length, 1);
87+
assert.equal(result.attributes[0].name, 'id');
88+
assert.equal(result.attributes[0].value, 'test');
89+
});
90+
});
91+
92+
describe('createAttributeNode', () => {
93+
it('handles string values', () => {
94+
const result = createAttributeNode('id', 'test');
95+
assert.equal(result.name, 'id');
96+
assert.equal(result.value, 'test');
97+
});
98+
99+
it('handles number values', () => {
100+
const result = createAttributeNode('count', 5);
101+
assert.equal(result.value, '5');
102+
});
103+
104+
it('handles boolean values', () => {
105+
const result = createAttributeNode('enabled', true);
106+
assert.equal(result.value, 'true');
107+
});
108+
109+
it('handles null values', () => {
110+
const result = createAttributeNode('data', null);
111+
assert.equal(result.value, null);
112+
});
113+
114+
it('handles object values', () => {
115+
const result = createAttributeNode('data', { a: 1 });
116+
assert.equal(
117+
result.value.type,
118+
AST_NODE_TYPES.MDX.JSX_ATTRIBUTE_EXPRESSION
119+
);
120+
const estree = result.value.data.estree;
121+
assert.equal(
122+
estree.body[0].expression.type,
123+
AST_NODE_TYPES.ESTREE.OBJECT_EXPRESSION
124+
);
125+
});
126+
127+
it('handles array values', () => {
128+
const result = createAttributeNode('items', [1, 2]);
129+
assert.equal(
130+
result.value.type,
131+
AST_NODE_TYPES.MDX.JSX_ATTRIBUTE_EXPRESSION
132+
);
133+
const estree = result.value.data.estree;
134+
assert.equal(
135+
estree.body[0].expression.type,
136+
AST_NODE_TYPES.ESTREE.ARRAY_EXPRESSION
137+
);
19138
});
20139
});

0 commit comments

Comments
 (0)