Skip to content

Commit

Permalink
extract default exports of code examples
Browse files Browse the repository at this point in the history
  • Loading branch information
deder-mw committed Dec 6, 2023
1 parent 205ea2f commit d47f1c1
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 55 deletions.
60 changes: 20 additions & 40 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions .yarn/patches/acorn-npm-8.11.2-a470f49bb6.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
diff --git a/dist/acorn.d.mts b/dist/acorn.d.mts
index 6ad58121195c96dab6b6c704ae0c0a31fefc2778..386631c03128490856cf493d968f187def49e2bb 100644
--- a/dist/acorn.d.mts
+++ b/dist/acorn.d.mts
@@ -519,6 +519,9 @@ export type Declaration =
| FunctionDeclaration
| VariableDeclaration
| ClassDeclaration
+export interface JSXElement extends Node {
+ type: "JSXElement"
+}

export type Expression =
| Identifier
@@ -547,6 +550,7 @@ export type Expression =
| ChainExpression
| ImportExpression
| ParenthesizedExpression
+| JSXElement

export type Pattern =
| Identifier
diff --git a/dist/acorn.d.ts b/dist/acorn.d.ts
index 6ad58121195c96dab6b6c704ae0c0a31fefc2778..b2321be5483ddcd8826086b9a22d228238f5e3bd 100644
--- a/dist/acorn.d.ts
+++ b/dist/acorn.d.ts
@@ -520,6 +520,10 @@ export type Declaration =
| VariableDeclaration
| ClassDeclaration

+export interface JSXElement extends Node {
+ type: "JSXElement"
+}
+
export type Expression =
| Identifier
| Literal
@@ -547,6 +551,7 @@ export type Expression =
| ChainExpression
| ImportExpression
| ParenthesizedExpression
+| JSXElement

export type Pattern =
| Identifier
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@
"typescript": "^5.3.2"
},
"resolutions": {
"@vitejs/plugin-react": "^4.2.0"
"@vitejs/plugin-react": "^4.2.0",
"acorn@npm:^8.9.0": "patch:acorn@npm%3A8.11.2#~/.yarn/patches/acorn-npm-8.11.2-a470f49bb6.patch",
"acorn@npm:^8.4.1": "patch:acorn@npm%3A8.11.2#~/.yarn/patches/acorn-npm-8.11.2-a470f49bb6.patch",
"acorn@npm:^8.7.1": "patch:acorn@npm%3A8.11.2#~/.yarn/patches/acorn-npm-8.11.2-a470f49bb6.patch",
"acorn@npm:^7.4.1": "patch:acorn@npm%3A8.11.2#~/.yarn/patches/acorn-npm-8.11.2-a470f49bb6.patch",
"acorn@npm:^8.8.2": "patch:acorn@npm%3A8.11.2#~/.yarn/patches/acorn-npm-8.11.2-a470f49bb6.patch",
"acorn@npm:^8.11.2": "patch:acorn@npm%3A8.11.2#~/.yarn/patches/acorn-npm-8.11.2-a470f49bb6.patch",
"acorn@npm:^8.0.0": "patch:acorn@npm%3A8.11.2#~/.yarn/patches/acorn-npm-8.11.2-a470f49bb6.patch"
},
"dependenciesMeta": {
"@types/react": {
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@types/react": "^18.2.42",
"@types/react-dom": "^18.2.17",
"@yarnpkg/pnpify": "^4.0.1",
"acorn": "^8.11.2",
"acorn": "patch:acorn@npm%3A8.11.2#~/.yarn/patches/acorn-npm-8.11.2-a470f49bb6.patch",
"acorn-jsx": "^5.3.2",
"eslint": "^8.55.0",
"eslint-config-next": "14.0.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/src/app/button/_examples/Default.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { Button } from "@mittwald/flow-components/Button";

<Button foo={true} />;
export default <Button foo={false} />;
2 changes: 1 addition & 1 deletion packages/docs/src/app/button/_examples/Simple.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import Button from "@mittwald/flow-components/Button";

<Button foo={true} />;
export default <Button foo={true} />;
5 changes: 4 additions & 1 deletion packages/docs/src/app/button/page.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import defaultExampleCode from "./_examples/Simple";
import defaultExampleCode from "./_examples/Default";
import simpleExampleCode from "./_examples/Simple";

# Hello

<LiveCodeEditor code={defaultExampleCode} />

<LiveCodeEditor code={simpleExampleCode} />
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import React, { FC } from "react";
import { LiveEditor, LiveError, LivePreview, LiveProvider } from "react-live";
import { extractEditorScope } from "@/components/LiveCodeEditor/lib/extractEditorScope";
import { stripImports } from "@/components/LiveCodeEditor/lib/stripImports";
import { LiveCodeEditorProps } from "@/components/LiveCodeEditor/types";
import extractDefaultExport from "@/lib/extractDefaultExport";

// Waiting for https://github.com/FormidableLabs/react-live/issues/339
const error = console.error;
Expand All @@ -22,7 +22,11 @@ const LiveCodeEditor: FC<LiveCodeEditorProps> = (props) => {
const scope = extractEditorScope(code);

return (
<LiveProvider code={code} scope={scope} transformCode={stripImports}>
<LiveProvider
code={code}
scope={scope}
transformCode={extractDefaultExport}
>
<LiveEditor />
<LiveError />
<LivePreview />
Expand Down
73 changes: 73 additions & 0 deletions packages/docs/src/lib/extractDefaultExport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import * as acorn from "acorn";
import acornJsx from "acorn-jsx";
import {
AnonymousFunctionDeclaration,
ExportDefaultDeclaration,
Expression,
ExpressionStatement,
FunctionDeclaration,
ReturnStatement,
} from "acorn";

export default function extractDefaultExport(code: string): string {
const JSXParser = acorn.Parser.extend(acornJsx());

const tree = JSXParser.parse(code, {
ecmaVersion: 14,
sourceType: "module",
});

const defaultExport = tree.body.find(
(it): it is ExportDefaultDeclaration =>
it.type === "ExportDefaultDeclaration",
);

if (
!defaultExport &&
tree.body.some((it) => it.type === "ExpressionStatement")
) {
return extractCode(
code,
tree.body.find(
(it): it is ExpressionStatement => it.type === "ExpressionStatement",
)!.expression as Expression,
);
}

if (
defaultExport!.declaration.type === "ArrowFunctionExpression" ||
defaultExport!.declaration.type === "JSXElement" ||
defaultExport!.declaration.type === "FunctionDeclaration"
) {
return extractCode(code, defaultExport!.declaration);
}

console.log("after", defaultExport);

return "<p>Error parsing example.</p>";
}

function extractCode(
code: string,
expression: Expression | FunctionDeclaration | AnonymousFunctionDeclaration,
) {
if (expression.type === "JSXElement") {
return code.slice(expression.start, expression.end);
}
if (expression.type === "ArrowFunctionExpression") {
return code.slice(expression.start, expression.end);
}
if (expression.type === "FunctionDeclaration") {
const returnStatement = expression.body.body.find(
(it): it is ReturnStatement => it.type === "ReturnStatement",
);
if (!returnStatement || !returnStatement.argument) {
return "<p>Error parsing example.</p>";
}
return code.slice(
returnStatement.argument.start,
returnStatement.argument.end,
);
}
return "<p>Error parsing example.</p>";
}
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3059,7 +3059,7 @@ __metadata:
"@types/react": "npm:^18.2.42"
"@types/react-dom": "npm:^18.2.17"
"@yarnpkg/pnpify": "npm:^4.0.1"
acorn: "npm:^8.11.2"
acorn: "patch:acorn@npm%3A8.11.2#~/.yarn/patches/acorn-npm-8.11.2-a470f49bb6.patch"
acorn-jsx: "npm:^5.3.2"
eslint: "npm:^8.55.0"
eslint-config-next: "npm:14.0.3"
Expand Down Expand Up @@ -7166,21 +7166,21 @@ __metadata:
languageName: node
linkType: hard

"acorn@npm:^7.4.1":
version: 7.4.1
resolution: "acorn@npm:7.4.1"
"acorn@npm:8.11.2":
version: 8.11.2
resolution: "acorn@npm:8.11.2"
bin:
acorn: bin/acorn
checksum: bd0b2c2b0f334bbee48828ff897c12bd2eb5898d03bf556dcc8942022cec795ac5bb5b6b585e2de687db6231faf07e096b59a361231dd8c9344d5df5f7f0e526
checksum: a3ed76c761b75ec54b1ec3068fb7f113a182e95aea7f322f65098c2958d232e3d211cb6dac35ff9c647024b63714bc528a26d54a925d1fef2c25585b4c8e4017
languageName: node
linkType: hard

"acorn@npm:^8.0.0, acorn@npm:^8.11.2, acorn@npm:^8.4.1, acorn@npm:^8.7.1, acorn@npm:^8.8.2, acorn@npm:^8.9.0":
"acorn@patch:acorn@npm%3A8.11.2#~/.yarn/patches/acorn-npm-8.11.2-a470f49bb6.patch":
version: 8.11.2
resolution: "acorn@npm:8.11.2"
resolution: "acorn@patch:acorn@npm%3A8.11.2#~/.yarn/patches/acorn-npm-8.11.2-a470f49bb6.patch::version=8.11.2&hash=eac067"
bin:
acorn: bin/acorn
checksum: a3ed76c761b75ec54b1ec3068fb7f113a182e95aea7f322f65098c2958d232e3d211cb6dac35ff9c647024b63714bc528a26d54a925d1fef2c25585b4c8e4017
checksum: becd7f0f6c5b4aa0b4e5c3e168eb1910b32710f50e8a212ea1e7c2bef1bbfa8b2a668717d3a8646d11ad99dc8d9ad7503491fcf4bd6fcb93546f2efc4d30d3ea
languageName: node
linkType: hard

Expand Down

0 comments on commit d47f1c1

Please sign in to comment.