-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto-generated ECS Docs. Needs work, but it's better than nothing.
- Loading branch information
1 parent
6a45da9
commit d4d268a
Showing
6 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// @ts-expect-error | ||
import { docs } from "virtual:ecs-component-docs"; | ||
import { Fragment } from "react"; | ||
|
||
export function ECSComponentDocs() { | ||
console.log(docs); | ||
|
||
return Object.values(docs) | ||
.sort((a: any, b: any) => (a.component > b.component ? 1 : -1)) | ||
.map((doc) => { | ||
const typedDoc: { | ||
component: string; | ||
comment: string; | ||
properties: { name: string; comment: string }[]; | ||
} = doc as any; | ||
return ( | ||
<Fragment key={typedDoc.component}> | ||
<h3> | ||
<code>{typedDoc.component}</code> | ||
</h3> | ||
<p>{typedDoc.comment}</p> | ||
<ul> | ||
{typedDoc.properties.map((p) => ( | ||
<li key={p.name}> | ||
<code> | ||
<strong>{p.name}</strong> | ||
</code> | ||
: {p.comment} | ||
</li> | ||
))} | ||
</ul> | ||
</Fragment> | ||
); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// @ts-nocheck | ||
import * as ts from "typescript"; | ||
|
||
const virtualModuleId = "virtual:ecs-component-docs"; | ||
const resolvedVirtualModuleId = `\0${virtualModuleId}`; | ||
const docs: Record< | ||
string, | ||
{ | ||
component: string; | ||
comment: string; | ||
properties: { name: string; comment: string }[]; | ||
} | ||
> = {}; | ||
const componentDocsRegex = /.*app\/ecs-components\/.*\.ts$/; | ||
export function componentDocs() { | ||
return { | ||
name: "component-docs", | ||
resolveId(id: string) { | ||
if (id === virtualModuleId) { | ||
return resolvedVirtualModuleId; | ||
} | ||
}, | ||
load(id: string) { | ||
if (id === resolvedVirtualModuleId) { | ||
return `export const docs = ${JSON.stringify(docs)}`; | ||
} | ||
}, | ||
transform(src: string, id: string) { | ||
if (componentDocsRegex.test(id)) { | ||
const sourceFile = ts.createSourceFile( | ||
id, | ||
src, | ||
ts.ScriptTarget.Latest, | ||
true, | ||
); | ||
let currentExportKeyword = ""; | ||
function visit(node: ts.Node) { | ||
if (ts.SyntaxKind[node.kind] === "ExportKeyword") { | ||
const keyword = | ||
node.parent.declarationList.declarations[0].name.getText(); | ||
console.log(keyword); | ||
if (keyword) { | ||
currentExportKeyword = keyword; | ||
docs[currentExportKeyword] = { | ||
component: currentExportKeyword, | ||
comment: node.jsDoc?.[0]?.comment || "", | ||
properties: [], | ||
}; | ||
} | ||
} | ||
if (ts.SyntaxKind[node.kind] === "PropertyAssignment") { | ||
const name = node.name.getText() || ""; | ||
if (!docs[currentExportKeyword]) { | ||
docs[currentExportKeyword] = { | ||
component: currentExportKeyword, | ||
comment: "", | ||
properties: [], | ||
}; | ||
} | ||
if (name) { | ||
docs[currentExportKeyword].properties.push({ | ||
name, | ||
comment: node.jsDoc?.[0]?.comment || "", | ||
}); | ||
} | ||
} | ||
ts.forEachChild(node, visit); | ||
} | ||
|
||
visit(sourceFile); | ||
|
||
return null; | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters