compmark-vue 0.1.1 → 0.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/dist/cli.mjs CHANGED
@@ -17,27 +17,92 @@ function parseSFC(source, filename) {
17
17
  const scriptSource = descriptor.scriptSetup?.content ?? compiled.content;
18
18
  for (const stmt of ast) {
19
19
  const calls = extractDefineCalls(stmt);
20
- for (const { callee, args, leadingComments } of calls) if (callee === "defineProps" && args[0]?.type === "ObjectExpression") doc.props = extractProps(args[0], scriptSource);
20
+ for (const { callee, args, leadingComments, typeParams, defaultsArg } of calls) if (callee === "defineProps" && args[0]?.type === "ObjectExpression") doc.props = extractProps(args[0], scriptSource);
21
+ else if (callee === "defineProps" && typeParams?.params[0]?.type === "TSTypeLiteral") doc.props = extractTypeProps(typeParams.params[0], defaultsArg, scriptSource);
21
22
  else if (callee === "defineEmits" && args[0]?.type === "ArrayExpression") doc.emits = extractEmits(args[0], leadingComments);
22
23
  }
23
24
  return doc;
24
25
  }
26
+ function processCallExpression(callExpr, leadingComments) {
27
+ if (callExpr.callee.type === "Identifier" && callExpr.callee.name === "withDefaults" && callExpr.arguments[0]?.type === "CallExpression" && callExpr.arguments[0].callee.type === "Identifier" && callExpr.arguments[0].callee.name === "defineProps") {
28
+ const innerCall = callExpr.arguments[0];
29
+ return {
30
+ callee: "defineProps",
31
+ args: innerCall.arguments,
32
+ leadingComments,
33
+ typeParams: innerCall.typeParameters,
34
+ defaultsArg: callExpr.arguments[1]
35
+ };
36
+ }
37
+ return {
38
+ callee: callExpr.callee.type === "Identifier" ? callExpr.callee.name : "",
39
+ args: callExpr.arguments,
40
+ leadingComments,
41
+ typeParams: callExpr.typeParameters
42
+ };
43
+ }
25
44
  function extractDefineCalls(stmt) {
26
45
  const calls = [];
27
- if (stmt.type === "ExpressionStatement" && stmt.expression.type === "CallExpression" && stmt.expression.callee.type === "Identifier") calls.push({
28
- callee: stmt.expression.callee.name,
29
- args: stmt.expression.arguments,
30
- leadingComments: stmt.leadingComments ?? []
31
- });
46
+ const comments = stmt.leadingComments ?? [];
47
+ if (stmt.type === "ExpressionStatement" && stmt.expression.type === "CallExpression" && stmt.expression.callee.type === "Identifier") calls.push(processCallExpression(stmt.expression, comments));
32
48
  if (stmt.type === "VariableDeclaration") {
33
- for (const decl of stmt.declarations) if (decl.init?.type === "CallExpression" && decl.init.callee.type === "Identifier") calls.push({
34
- callee: decl.init.callee.name,
35
- args: decl.init.arguments,
36
- leadingComments: stmt.leadingComments ?? []
37
- });
49
+ for (const decl of stmt.declarations) if (decl.init?.type === "CallExpression" && decl.init.callee.type === "Identifier") calls.push(processCallExpression(decl.init, comments));
38
50
  }
39
51
  return calls;
40
52
  }
53
+ function extractTypeProps(typeLiteral, defaultsArg, source) {
54
+ const defaults = defaultsArg?.type === "ObjectExpression" ? extractDefaultsMap(defaultsArg, source) : /* @__PURE__ */ new Map();
55
+ const props = [];
56
+ for (const member of typeLiteral.members) {
57
+ if (member.type !== "TSPropertySignature") continue;
58
+ const name = member.key.type === "Identifier" ? member.key.name : member.key.type === "StringLiteral" ? member.key.value : "";
59
+ if (!name) continue;
60
+ const type = member.typeAnnotation?.typeAnnotation ? resolveTypeString(member.typeAnnotation.typeAnnotation) : "unknown";
61
+ const description = extractJSDoc(member.leadingComments ?? []);
62
+ props.push({
63
+ name,
64
+ type,
65
+ required: !member.optional,
66
+ default: defaults.get(name),
67
+ description
68
+ });
69
+ }
70
+ return props;
71
+ }
72
+ function resolveTypeString(node) {
73
+ switch (node.type) {
74
+ case "TSStringKeyword": return "string";
75
+ case "TSNumberKeyword": return "number";
76
+ case "TSBooleanKeyword": return "boolean";
77
+ case "TSObjectKeyword": return "object";
78
+ case "TSAnyKeyword": return "any";
79
+ case "TSUnionType": return node.types.map((t) => resolveTypeString(t)).join(" | ");
80
+ case "TSIntersectionType": return node.types.map((t) => resolveTypeString(t)).join(" & ");
81
+ case "TSLiteralType":
82
+ if (node.literal.type === "StringLiteral") return `'${node.literal.value}'`;
83
+ if (node.literal.type === "NumericLiteral") return String(node.literal.value);
84
+ if (node.literal.type === "BooleanLiteral") return String(node.literal.value);
85
+ return "unknown";
86
+ case "TSArrayType": return `${resolveTypeString(node.elementType)}[]`;
87
+ case "TSTypeReference": {
88
+ const name = node.typeName?.type === "Identifier" ? node.typeName.name : "unknown";
89
+ if (node.typeParameters?.params?.length) return `${name}<${node.typeParameters.params.map((p) => resolveTypeString(p)).join(", ")}>`;
90
+ return name;
91
+ }
92
+ case "TSFunctionType": return "Function";
93
+ default: return "unknown";
94
+ }
95
+ }
96
+ function extractDefaultsMap(obj, source) {
97
+ const map = /* @__PURE__ */ new Map();
98
+ for (const prop of obj.properties) {
99
+ if (prop.type !== "ObjectProperty") continue;
100
+ const name = prop.key.type === "Identifier" ? prop.key.name : prop.key.type === "StringLiteral" ? prop.key.value : "";
101
+ if (!name) continue;
102
+ map.set(name, stringifyDefault(prop.value, source));
103
+ }
104
+ return map;
105
+ }
41
106
  function extractProps(obj, source) {
42
107
  const props = [];
43
108
  for (const prop of obj.properties) {
package/dist/index.mjs CHANGED
@@ -16,27 +16,92 @@ function parseSFC(source, filename) {
16
16
  const scriptSource = descriptor.scriptSetup?.content ?? compiled.content;
17
17
  for (const stmt of ast) {
18
18
  const calls = extractDefineCalls(stmt);
19
- for (const { callee, args, leadingComments } of calls) if (callee === "defineProps" && args[0]?.type === "ObjectExpression") doc.props = extractProps(args[0], scriptSource);
19
+ for (const { callee, args, leadingComments, typeParams, defaultsArg } of calls) if (callee === "defineProps" && args[0]?.type === "ObjectExpression") doc.props = extractProps(args[0], scriptSource);
20
+ else if (callee === "defineProps" && typeParams?.params[0]?.type === "TSTypeLiteral") doc.props = extractTypeProps(typeParams.params[0], defaultsArg, scriptSource);
20
21
  else if (callee === "defineEmits" && args[0]?.type === "ArrayExpression") doc.emits = extractEmits(args[0], leadingComments);
21
22
  }
22
23
  return doc;
23
24
  }
25
+ function processCallExpression(callExpr, leadingComments) {
26
+ if (callExpr.callee.type === "Identifier" && callExpr.callee.name === "withDefaults" && callExpr.arguments[0]?.type === "CallExpression" && callExpr.arguments[0].callee.type === "Identifier" && callExpr.arguments[0].callee.name === "defineProps") {
27
+ const innerCall = callExpr.arguments[0];
28
+ return {
29
+ callee: "defineProps",
30
+ args: innerCall.arguments,
31
+ leadingComments,
32
+ typeParams: innerCall.typeParameters,
33
+ defaultsArg: callExpr.arguments[1]
34
+ };
35
+ }
36
+ return {
37
+ callee: callExpr.callee.type === "Identifier" ? callExpr.callee.name : "",
38
+ args: callExpr.arguments,
39
+ leadingComments,
40
+ typeParams: callExpr.typeParameters
41
+ };
42
+ }
24
43
  function extractDefineCalls(stmt) {
25
44
  const calls = [];
26
- if (stmt.type === "ExpressionStatement" && stmt.expression.type === "CallExpression" && stmt.expression.callee.type === "Identifier") calls.push({
27
- callee: stmt.expression.callee.name,
28
- args: stmt.expression.arguments,
29
- leadingComments: stmt.leadingComments ?? []
30
- });
45
+ const comments = stmt.leadingComments ?? [];
46
+ if (stmt.type === "ExpressionStatement" && stmt.expression.type === "CallExpression" && stmt.expression.callee.type === "Identifier") calls.push(processCallExpression(stmt.expression, comments));
31
47
  if (stmt.type === "VariableDeclaration") {
32
- for (const decl of stmt.declarations) if (decl.init?.type === "CallExpression" && decl.init.callee.type === "Identifier") calls.push({
33
- callee: decl.init.callee.name,
34
- args: decl.init.arguments,
35
- leadingComments: stmt.leadingComments ?? []
36
- });
48
+ for (const decl of stmt.declarations) if (decl.init?.type === "CallExpression" && decl.init.callee.type === "Identifier") calls.push(processCallExpression(decl.init, comments));
37
49
  }
38
50
  return calls;
39
51
  }
52
+ function extractTypeProps(typeLiteral, defaultsArg, source) {
53
+ const defaults = defaultsArg?.type === "ObjectExpression" ? extractDefaultsMap(defaultsArg, source) : /* @__PURE__ */ new Map();
54
+ const props = [];
55
+ for (const member of typeLiteral.members) {
56
+ if (member.type !== "TSPropertySignature") continue;
57
+ const name = member.key.type === "Identifier" ? member.key.name : member.key.type === "StringLiteral" ? member.key.value : "";
58
+ if (!name) continue;
59
+ const type = member.typeAnnotation?.typeAnnotation ? resolveTypeString(member.typeAnnotation.typeAnnotation) : "unknown";
60
+ const description = extractJSDoc(member.leadingComments ?? []);
61
+ props.push({
62
+ name,
63
+ type,
64
+ required: !member.optional,
65
+ default: defaults.get(name),
66
+ description
67
+ });
68
+ }
69
+ return props;
70
+ }
71
+ function resolveTypeString(node) {
72
+ switch (node.type) {
73
+ case "TSStringKeyword": return "string";
74
+ case "TSNumberKeyword": return "number";
75
+ case "TSBooleanKeyword": return "boolean";
76
+ case "TSObjectKeyword": return "object";
77
+ case "TSAnyKeyword": return "any";
78
+ case "TSUnionType": return node.types.map((t) => resolveTypeString(t)).join(" | ");
79
+ case "TSIntersectionType": return node.types.map((t) => resolveTypeString(t)).join(" & ");
80
+ case "TSLiteralType":
81
+ if (node.literal.type === "StringLiteral") return `'${node.literal.value}'`;
82
+ if (node.literal.type === "NumericLiteral") return String(node.literal.value);
83
+ if (node.literal.type === "BooleanLiteral") return String(node.literal.value);
84
+ return "unknown";
85
+ case "TSArrayType": return `${resolveTypeString(node.elementType)}[]`;
86
+ case "TSTypeReference": {
87
+ const name = node.typeName?.type === "Identifier" ? node.typeName.name : "unknown";
88
+ if (node.typeParameters?.params?.length) return `${name}<${node.typeParameters.params.map((p) => resolveTypeString(p)).join(", ")}>`;
89
+ return name;
90
+ }
91
+ case "TSFunctionType": return "Function";
92
+ default: return "unknown";
93
+ }
94
+ }
95
+ function extractDefaultsMap(obj, source) {
96
+ const map = /* @__PURE__ */ new Map();
97
+ for (const prop of obj.properties) {
98
+ if (prop.type !== "ObjectProperty") continue;
99
+ const name = prop.key.type === "Identifier" ? prop.key.name : prop.key.type === "StringLiteral" ? prop.key.value : "";
100
+ if (!name) continue;
101
+ map.set(name, stringifyDefault(prop.value, source));
102
+ }
103
+ return map;
104
+ }
40
105
  function extractProps(obj, source) {
41
106
  const props = [];
42
107
  for (const prop of obj.properties) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "compmark-vue",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Auto-generate Markdown documentation from Vue 3 SFCs",
5
5
  "license": "MIT",
6
6
  "repository": "noopurphalak/compmark-vue",