jsc-typescript-ast-mcp 1.1.1 → 1.1.3

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
@@ -97,7 +97,7 @@ Analyze React component structures in your codebase.
97
97
  - `entryFilePath`: Path to the entry file
98
98
  - `maxDepth`: Maximum depth of the component tree (default: 3)
99
99
  - `data-id` (optional): Attribute name to capture into `TreeNode.dataId` (example: `data-attribute-id`)
100
- - **Output**: JSON representation of the component tree structure
100
+ - **Output**: JSON representation of the component tree structure, including optional element metadata such as `props`, `dataId`, and `onClick`
101
101
 
102
102
  ## License
103
103
 
@@ -2,7 +2,7 @@ import { Node, SyntaxKind, } from 'ts-morph';
2
2
  import { analyzeComponent } from './analyzeComponent.js';
3
3
  import { getComponent } from './component.js';
4
4
  import { getTagName } from './nameHelper.js';
5
- import { extractPropsFromNode } from './props.js';
5
+ import { extractAttributeValueFromNode, extractOnClickInfoFromNode, extractPropsFromNode, } from './props.js';
6
6
  import { handleTernary } from './ternary.js';
7
7
  export const extractFromExpression = (expression, project, options, depth) => {
8
8
  if (Node.isConditionalExpression(expression)) {
@@ -166,13 +166,17 @@ const buildNodeFromJSX = (node, project, options, depth) => {
166
166
  const props = extractPropsFromNode(node);
167
167
  if (props) {
168
168
  treeNode.props = props;
169
- if (options.dataIdAttribute && options.dataIdAttribute in props) {
170
- const dataIdValue = props[options.dataIdAttribute];
171
- if (dataIdValue !== undefined && dataIdValue !== null) {
172
- treeNode.dataId = String(dataIdValue);
173
- }
169
+ }
170
+ if (options.dataIdAttribute) {
171
+ const dataIdValue = extractAttributeValueFromNode(node, options.dataIdAttribute);
172
+ if (dataIdValue !== undefined && dataIdValue !== null) {
173
+ treeNode.dataId = String(dataIdValue);
174
174
  }
175
175
  }
176
+ const onClick = extractOnClickInfoFromNode(node);
177
+ if (onClick) {
178
+ treeNode.onClick = onClick;
179
+ }
176
180
  // Recurse into children
177
181
  if (Node.isJsxElement(node)) {
178
182
  const children = node.getJsxChildren();
@@ -1,4 +1,217 @@
1
1
  import { Node, } from 'ts-morph';
2
+ const STANDARD_HTML_ATTRIBUTES = new Set([
3
+ 'accept',
4
+ 'acceptcharset',
5
+ 'action',
6
+ 'allow',
7
+ 'allowfullscreen',
8
+ 'alt',
9
+ 'as',
10
+ 'async',
11
+ 'autocapitalize',
12
+ 'autocomplete',
13
+ 'autocorrect',
14
+ 'autofocus',
15
+ 'autoplay',
16
+ 'capture',
17
+ 'cellpadding',
18
+ 'cellspacing',
19
+ 'charset',
20
+ 'challenge',
21
+ 'checked',
22
+ 'cite',
23
+ 'class',
24
+ 'classname',
25
+ 'cols',
26
+ 'colspan',
27
+ 'content',
28
+ 'contenteditable',
29
+ 'controls',
30
+ 'coords',
31
+ 'crossorigin',
32
+ 'data',
33
+ 'datetime',
34
+ 'decoding',
35
+ 'default',
36
+ 'defer',
37
+ 'dir',
38
+ 'dirname',
39
+ 'disabled',
40
+ 'download',
41
+ 'draggable',
42
+ 'enctype',
43
+ 'fetchpriority',
44
+ 'for',
45
+ 'form',
46
+ 'formaction',
47
+ 'formenctype',
48
+ 'formmethod',
49
+ 'formnovalidate',
50
+ 'formtarget',
51
+ 'frameborder',
52
+ 'headers',
53
+ 'height',
54
+ 'hidden',
55
+ 'high',
56
+ 'href',
57
+ 'hreflang',
58
+ 'htmlfor',
59
+ 'httpequiv',
60
+ 'id',
61
+ 'inputmode',
62
+ 'integrity',
63
+ 'is',
64
+ 'key',
65
+ 'kind',
66
+ 'label',
67
+ 'lang',
68
+ 'list',
69
+ 'loading',
70
+ 'loop',
71
+ 'low',
72
+ 'max',
73
+ 'maxlength',
74
+ 'media',
75
+ 'method',
76
+ 'min',
77
+ 'minlength',
78
+ 'multiple',
79
+ 'muted',
80
+ 'name',
81
+ 'nomodule',
82
+ 'nonce',
83
+ 'novalidate',
84
+ 'open',
85
+ 'optimum',
86
+ 'pattern',
87
+ 'placeholder',
88
+ 'playsinline',
89
+ 'poster',
90
+ 'preload',
91
+ 'readonly',
92
+ 'referrerpolicy',
93
+ 'rel',
94
+ 'required',
95
+ 'reversed',
96
+ 'role',
97
+ 'rows',
98
+ 'rowspan',
99
+ 'sandbox',
100
+ 'scope',
101
+ 'selected',
102
+ 'shape',
103
+ 'size',
104
+ 'sizes',
105
+ 'slot',
106
+ 'span',
107
+ 'spellcheck',
108
+ 'src',
109
+ 'srcdoc',
110
+ 'srclang',
111
+ 'srcset',
112
+ 'start',
113
+ 'step',
114
+ 'style',
115
+ 'tabindex',
116
+ 'target',
117
+ 'title',
118
+ 'translate',
119
+ 'type',
120
+ 'usemap',
121
+ 'value',
122
+ 'width',
123
+ 'wrap',
124
+ // Common DOM event handler props
125
+ 'onabort',
126
+ 'onanimationend',
127
+ 'onanimationiteration',
128
+ 'onanimationstart',
129
+ 'onauxclick',
130
+ 'onbeforeinput',
131
+ 'onblur',
132
+ 'oncanplay',
133
+ 'oncanplaythrough',
134
+ 'onchange',
135
+ 'onclick',
136
+ 'oncompositionend',
137
+ 'oncompositionstart',
138
+ 'oncompositionupdate',
139
+ 'oncontextmenu',
140
+ 'oncopy',
141
+ 'oncut',
142
+ 'ondblclick',
143
+ 'ondrag',
144
+ 'ondragend',
145
+ 'ondragenter',
146
+ 'ondragexit',
147
+ 'ondragleave',
148
+ 'ondragover',
149
+ 'ondragstart',
150
+ 'ondrop',
151
+ 'ondurationchange',
152
+ 'onemptied',
153
+ 'onended',
154
+ 'onerror',
155
+ 'onfocus',
156
+ 'onfocusin',
157
+ 'onfocusout',
158
+ 'onfullscreenchange',
159
+ 'onfullscreenerror',
160
+ 'ongotpointercapture',
161
+ 'oninput',
162
+ 'oninvalid',
163
+ 'onkeydown',
164
+ 'onkeypress',
165
+ 'onkeyup',
166
+ 'onload',
167
+ 'onloadeddata',
168
+ 'onloadedmetadata',
169
+ 'onloadstart',
170
+ 'onlostpointercapture',
171
+ 'onmouseenter',
172
+ 'onmouseleave',
173
+ 'onmousemove',
174
+ 'onmouseout',
175
+ 'onmouseover',
176
+ 'onmouseup',
177
+ 'onpaste',
178
+ 'onpause',
179
+ 'onplay',
180
+ 'onplaying',
181
+ 'onpointercancel',
182
+ 'onpointerdown',
183
+ 'onpointerenter',
184
+ 'onpointerleave',
185
+ 'onpointermove',
186
+ 'onpointerout',
187
+ 'onpointerover',
188
+ 'onpointerup',
189
+ 'onprogress',
190
+ 'onratechange',
191
+ 'onreset',
192
+ 'onresize',
193
+ 'onscroll',
194
+ 'onseeked',
195
+ 'onseeking',
196
+ 'onselect',
197
+ 'onstalled',
198
+ 'onsubmit',
199
+ 'onsuspend',
200
+ 'ontimeupdate',
201
+ 'ontoggle',
202
+ 'ontransitionend',
203
+ 'onvolumechange',
204
+ 'onwaiting',
205
+ 'onwheel',
206
+ ]);
207
+ const isStandardHtmlAttribute = (name) => {
208
+ const normalizedName = name.toLowerCase();
209
+ if (normalizedName.startsWith('aria-') ||
210
+ normalizedName.startsWith('data-')) {
211
+ return true;
212
+ }
213
+ return STANDARD_HTML_ATTRIBUTES.has(normalizedName);
214
+ };
2
215
  const extractPropValue = (attribute) => {
3
216
  const initializer = attribute.getInitializer();
4
217
  if (!initializer) {
@@ -32,7 +245,11 @@ export const extractPropsFromNode = (node) => {
32
245
  const props = {};
33
246
  for (const attribute of attributes) {
34
247
  if (Node.isJsxAttribute(attribute)) {
35
- props[attribute.getNameNode().getText()] = extractPropValue(attribute);
248
+ const attributeName = attribute.getNameNode().getText();
249
+ if (isStandardHtmlAttribute(attributeName)) {
250
+ continue;
251
+ }
252
+ props[attributeName] = extractPropValue(attribute);
36
253
  continue;
37
254
  }
38
255
  if (Node.isJsxSpreadAttribute(attribute)) {
@@ -44,3 +261,73 @@ export const extractPropsFromNode = (node) => {
44
261
  }
45
262
  return props;
46
263
  };
264
+ export const extractAttributeValueFromNode = (node, attributeName) => {
265
+ const attributes = Node.isJsxElement(node)
266
+ ? node.getOpeningElement().getAttributes()
267
+ : node.getAttributes();
268
+ const matchedAttribute = attributes.find((attribute) => Node.isJsxAttribute(attribute) &&
269
+ attribute.getNameNode().getText() === attributeName);
270
+ if (!matchedAttribute || !Node.isJsxAttribute(matchedAttribute)) {
271
+ return undefined;
272
+ }
273
+ return extractPropValue(matchedAttribute);
274
+ };
275
+ const getExpressionKind = (expression) => {
276
+ if (Node.isIdentifier(expression)) {
277
+ return 'identifier';
278
+ }
279
+ if (Node.isPropertyAccessExpression(expression)) {
280
+ return 'member-expression';
281
+ }
282
+ if (Node.isCallExpression(expression)) {
283
+ return 'call-expression';
284
+ }
285
+ if (Node.isArrowFunction(expression)) {
286
+ return 'arrow-function';
287
+ }
288
+ if (Node.isFunctionExpression(expression)) {
289
+ return 'function-expression';
290
+ }
291
+ return 'expression';
292
+ };
293
+ export const extractOnClickInfoFromNode = (node) => {
294
+ const attributes = Node.isJsxElement(node)
295
+ ? node.getOpeningElement().getAttributes()
296
+ : node.getAttributes();
297
+ const onClickAttribute = attributes.find((attribute) => Node.isJsxAttribute(attribute) &&
298
+ attribute.getNameNode().getText() === 'onClick');
299
+ if (!onClickAttribute || !Node.isJsxAttribute(onClickAttribute)) {
300
+ return undefined;
301
+ }
302
+ const initializer = onClickAttribute.getInitializer();
303
+ if (!initializer) {
304
+ return {
305
+ attribute: 'onClick',
306
+ expression: 'true',
307
+ kind: 'boolean-shorthand',
308
+ };
309
+ }
310
+ if (Node.isStringLiteral(initializer)) {
311
+ return {
312
+ attribute: 'onClick',
313
+ expression: initializer.getLiteralText(),
314
+ kind: 'string-literal',
315
+ };
316
+ }
317
+ if (Node.isJsxExpression(initializer)) {
318
+ const expression = initializer.getExpression();
319
+ if (!expression) {
320
+ return undefined;
321
+ }
322
+ return {
323
+ attribute: 'onClick',
324
+ expression: expression.getText(),
325
+ kind: getExpressionKind(expression),
326
+ };
327
+ }
328
+ return {
329
+ attribute: 'onClick',
330
+ expression: initializer.getText(),
331
+ kind: 'expression',
332
+ };
333
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsc-typescript-ast-mcp",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "mcpName": "io.github.jscoobyced/jsc-typescript-ast-mcp",
5
5
  "description": "A Model Context Protocol (MCP) server that provides an abstract syntax tree (AST) representation of TypeScript code using the ts-morph library. It allows clients to analyze and manipulate TypeScript code structures, making it easier for AI models to understand and work with TypeScript projects. You can create a JSON representation of your React components, and use it for various purposes such as documentation, code analysis, or even generating new code based on existing components.",
6
6
  "main": "dist/index.js",