-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract default exports of code examples
- Loading branch information
Showing
10 changed files
with
167 additions
and
55 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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 |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { Button } from "@mittwald/flow-components/Button"; | ||
|
||
<Button foo={true} />; | ||
export default <Button foo={false} />; |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import Button from "@mittwald/flow-components/Button"; | ||
|
||
<Button foo={true} />; | ||
export default <Button foo={true} />; |
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 |
---|---|---|
@@ -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} /> |
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,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>"; | ||
} |
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