jsc-typescript-ast-mcp 1.1.1 → 1.1.2

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 { 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)) {
@@ -173,6 +173,10 @@ const buildNodeFromJSX = (node, project, options, depth) => {
173
173
  }
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();
@@ -44,3 +44,62 @@ export const extractPropsFromNode = (node) => {
44
44
  }
45
45
  return props;
46
46
  };
47
+ const getExpressionKind = (expression) => {
48
+ if (Node.isIdentifier(expression)) {
49
+ return 'identifier';
50
+ }
51
+ if (Node.isPropertyAccessExpression(expression)) {
52
+ return 'member-expression';
53
+ }
54
+ if (Node.isCallExpression(expression)) {
55
+ return 'call-expression';
56
+ }
57
+ if (Node.isArrowFunction(expression)) {
58
+ return 'arrow-function';
59
+ }
60
+ if (Node.isFunctionExpression(expression)) {
61
+ return 'function-expression';
62
+ }
63
+ return 'expression';
64
+ };
65
+ export const extractOnClickInfoFromNode = (node) => {
66
+ const attributes = Node.isJsxElement(node)
67
+ ? node.getOpeningElement().getAttributes()
68
+ : node.getAttributes();
69
+ const onClickAttribute = attributes.find((attribute) => Node.isJsxAttribute(attribute) &&
70
+ attribute.getNameNode().getText() === 'onClick');
71
+ if (!onClickAttribute || !Node.isJsxAttribute(onClickAttribute)) {
72
+ return undefined;
73
+ }
74
+ const initializer = onClickAttribute.getInitializer();
75
+ if (!initializer) {
76
+ return {
77
+ attribute: 'onClick',
78
+ expression: 'true',
79
+ kind: 'boolean-shorthand',
80
+ };
81
+ }
82
+ if (Node.isStringLiteral(initializer)) {
83
+ return {
84
+ attribute: 'onClick',
85
+ expression: initializer.getLiteralText(),
86
+ kind: 'string-literal',
87
+ };
88
+ }
89
+ if (Node.isJsxExpression(initializer)) {
90
+ const expression = initializer.getExpression();
91
+ if (!expression) {
92
+ return undefined;
93
+ }
94
+ return {
95
+ attribute: 'onClick',
96
+ expression: expression.getText(),
97
+ kind: getExpressionKind(expression),
98
+ };
99
+ }
100
+ return {
101
+ attribute: 'onClick',
102
+ expression: initializer.getText(),
103
+ kind: 'expression',
104
+ };
105
+ };
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.2",
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",