Skip to content

Commit ebc413a

Browse files
committed
Fix errors from running on Base UI codebase
1 parent 9d712f1 commit ebc413a

File tree

8 files changed

+100
-88
lines changed

8 files changed

+100
-88
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
},
2929
"dependencies": {
3030
"lodash": "^4.17.21",
31-
"typescript": "^5.7.3"
31+
"typescript": "^5.7.3",
32+
"yaml": "^2.7.0"
3233
},
3334
"devDependencies": {
3435
"@types/lodash": "^4.17.15",

pnpm-lock.yaml

Lines changed: 22 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/cli.ts

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import * as fs from 'node:fs';
22
import yargs from 'yargs';
33
import { hideBin } from 'yargs/helpers';
44
import * as rae from '../src';
5+
import * as inspector from 'node:inspector';
6+
import * as yaml from 'yaml';
7+
8+
const isDebug = inspector.url() !== undefined;
59

610
interface RunOptions {
711
files?: string[];
@@ -22,14 +26,17 @@ function run(options: RunOptions) {
2226
let errorCounter = 0;
2327

2428
for (const file of files) {
25-
console.log(`Processing ${file}`);
26-
console.group();
29+
if (!isDebug) {
30+
console.log(`Processing ${file}`);
31+
console.group();
32+
}
33+
2734
try {
2835
const ast = rae.parseFromProgram(file, program);
2936

3037
const componentsApi = ast.body.filter(rae.isComponentNode).map((component) => {
3138
return {
32-
name: component.name,
39+
...component,
3340
props: component.props
3441
.map((prop) => {
3542
return {
@@ -42,41 +49,49 @@ function run(options: RunOptions) {
4249
options.includeExternal ||
4350
!Array.from(prop.filenames).some((filename) => filename.includes('/node_modules/')),
4451
),
52+
nodeType: undefined,
4553
};
4654
});
4755

48-
const hooksApi = ast.body.filter(rae.isHookNode).map((hook) => {
49-
return {
50-
name: hook.name,
51-
parameters: hook.callSignatures[0].parameters.map((parameter) => {
52-
return {
53-
...parameter,
54-
};
55-
}),
56-
/*.filter(
57-
(prop) =>
58-
options.includeExternal ||
59-
!Array.from(prop.filenames).some((filename) => filename.includes('/node_modules/')),
60-
),*/
61-
};
62-
});
56+
const hooksApi = ast.body
57+
.filter(rae.isHookNode)
58+
.map((hook) => {
59+
return {
60+
...hook,
61+
parameters: hook.callSignatures[0].parameters.map((parameter) => {
62+
return {
63+
...parameter,
64+
};
65+
}),
66+
nodeType: undefined,
67+
};
68+
})
69+
.filter((hook) => options.includeExternal || !hook.fileName?.includes('/node_modules/'));
6370

6471
components.push(...componentsApi);
6572
hooks.push(...hooksApi);
6673
} catch (e) {
6774
console.error(`⛔ Error processing ${file}: ${e.message}`);
6875
++errorCounter;
6976
} finally {
70-
console.groupEnd();
77+
if (!isDebug) {
78+
console.groupEnd();
79+
}
7180
}
7281
}
7382

74-
const outputJSON = JSON.stringify({ components, hooks }, null, 2);
7583
if (options.out) {
76-
fs.writeFileSync(options.out, outputJSON);
84+
if (options.out.endsWith('.yaml') || options.out.endsWith('.yml')) {
85+
const outputYAML = yaml.stringify({ components, hooks });
86+
fs.writeFileSync(options.out, outputYAML);
87+
} else {
88+
const outputJSON = JSON.stringify({ components, hooks }, null, 2);
89+
fs.writeFileSync(options.out, outputJSON);
90+
}
91+
7792
console.log(`Output written to ${options.out}`);
7893
} else {
79-
console.log(outputJSON);
94+
console.log(yaml.stringify({ components, hooks }));
8095
}
8196

8297
console.log(`\nProcessed ${files.length} files.`);

src/parser.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -264,25 +264,6 @@ export function parseFromProgram(
264264
}
265265
}
266266

267-
function isTypeJSXElementLike(type: ts.Type): boolean {
268-
if (type.isUnion()) {
269-
return type.types.every(
270-
(subType) => subType.flags & ts.TypeFlags.Null || isTypeJSXElementLike(subType),
271-
);
272-
} else if (type.symbol) {
273-
const name = checker.getFullyQualifiedName(type.symbol);
274-
return (
275-
name === 'global.JSX.Element' ||
276-
name === 'React.ReactElement' ||
277-
name === 'React.JSX.Element' ||
278-
name.endsWith('@types/react/jsx-runtime".JSX.Element') || // when `"jsx": "react-jsx"` in tsconfig
279-
name.endsWith('@types/react/jsx-dev-runtime".JSX.Element') // when `"jsx": "react-jsxdev"` in tsconfig
280-
);
281-
}
282-
283-
return false;
284-
}
285-
286267
function parseFunctionComponent(
287268
node: ts.VariableDeclaration | ts.FunctionDeclaration,
288269
documentationNode: ts.Node,
@@ -299,10 +280,6 @@ export function parseFromProgram(
299280

300281
const type = checker.getTypeOfSymbolAtLocation(symbol, node);
301282
type.getCallSignatures().forEach((signature) => {
302-
if (!isTypeJSXElementLike(signature.getReturnType())) {
303-
return;
304-
}
305-
306283
const propsType = checker.getTypeOfSymbolAtLocation(
307284
signature.parameters[0],
308285
signature.parameters[0].valueDeclaration!,

src/types/nodes/component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface ComponentNode {
88
nodeType: typeof typeString;
99
name: string;
1010
props: MemberNode[];
11-
propsFilename?: string;
11+
fileName?: string;
1212
description?: string;
1313
visibility?: Documentation['visibility'];
1414
}
@@ -17,15 +17,15 @@ export function componentNode(
1717
name: string,
1818
props: MemberNode[],
1919
documentation: Documentation | undefined,
20-
propsFilename: string | undefined,
20+
fileName: string | undefined,
2121
): ComponentNode {
2222
return {
2323
nodeType: typeString,
2424
name: name,
2525
props: props || [],
2626
description: documentation?.description,
2727
visibility: documentation?.visibility,
28-
propsFilename,
28+
fileName,
2929
};
3030
}
3131

src/types/nodes/hook.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ export interface HookNode extends Omit<FunctionNode, 'nodeType'> {
88
nodeType: typeof typeString;
99
name: string;
1010
documentation: Documentation | undefined;
11-
parametersFilename?: string;
11+
fileName: string | undefined;
1212
}
1313

1414
export function hookNode(
1515
name: string,
1616
callSignatures: CallSignature[],
1717
documentation: Documentation | undefined,
18-
parametersFilename: string | undefined,
18+
fileName: string | undefined,
1919
): HookNode {
2020
return {
2121
nodeType: typeString,
2222
name: name,
2323
callSignatures,
2424
documentation,
25-
parametersFilename,
25+
fileName,
2626
};
2727
}
2828

test/callbacks/output.json

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,24 @@
1010
"name": "onChange",
1111
"type": {
1212
"nodeType": "function",
13-
"parameters": [
13+
"callSignatures": [
1414
{
15-
"nodeType": "parameter",
16-
"name": "event",
17-
"type": {
18-
"nodeType": "reference",
19-
"typeName": "React.ChangeEvent"
15+
"parameters": [
16+
{
17+
"nodeType": "parameter",
18+
"name": "event",
19+
"type": {
20+
"nodeType": "reference",
21+
"typeName": "React.ChangeEvent"
22+
}
23+
}
24+
],
25+
"returnValueType": {
26+
"nodeType": "intrinsic",
27+
"type": "void"
2028
}
2129
}
22-
],
23-
"returnValueType": {
24-
"nodeType": "intrinsic",
25-
"type": "void"
26-
}
30+
]
2731
},
2832
"optional": false,
2933
"filenames": {}
@@ -33,20 +37,24 @@
3337
"name": "onClosing",
3438
"type": {
3539
"nodeType": "function",
36-
"parameters": [
40+
"callSignatures": [
3741
{
38-
"nodeType": "parameter",
39-
"name": "animated",
40-
"type": {
42+
"parameters": [
43+
{
44+
"nodeType": "parameter",
45+
"name": "animated",
46+
"type": {
47+
"nodeType": "intrinsic",
48+
"type": "boolean"
49+
}
50+
}
51+
],
52+
"returnValueType": {
4153
"nodeType": "intrinsic",
4254
"type": "boolean"
4355
}
4456
}
45-
],
46-
"returnValueType": {
47-
"nodeType": "intrinsic",
48-
"type": "boolean"
49-
}
57+
]
5058
},
5159
"optional": false,
5260
"filenames": {}

0 commit comments

Comments
 (0)