Skip to content

Commit 2bb7f71

Browse files
committed
Fix up examples
1 parent 2f8be4f commit 2bb7f71

6 files changed

Lines changed: 156 additions & 7 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { fetchCSV } from "macro:fetchCSV";
2+
3+
export const Covid19 = () => {
4+
const rows = fetchCSV(
5+
"https://covid19.who.int/WHO-COVID-19-global-data.csv",
6+
{
7+
last: 100,
8+
columns: ["New_cases", "Date_reported", "Country"],
9+
}
10+
);
11+
12+
return (
13+
<div>
14+
<h2>Covid-19</h2>
15+
<h6>last {rows.length} updates from the WHO</h6>
16+
<div className="Table">
17+
<div className="Header">
18+
<div className="Heading">New Cases</div>
19+
<div className="Heading">Date</div>
20+
<div className="Heading">Country</div>
21+
</div>
22+
23+
{rows.map((row, index) => (
24+
<div className="Row" key={index}>
25+
<div className="Column">{row[0]}</div>
26+
<div className="Column">{row[1]}</div>
27+
<div className="Column">{row[2]}</div>
28+
</div>
29+
))}
30+
</div>
31+
</div>
32+
);
33+
};

examples/macros/components/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { IPAddresses } from "./example";
55
const Start = function () {
66
const root = document.createElement("div");
77
document.body.appendChild(root);
8+
89
ReactDOM.render(<IPAddresses />, root);
910
};
1011

examples/macros/fetchCSV.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
import Pappa from "papaparse";
2+
// Example usage:
3+
// const rows = fetchCSV(
4+
// "https://covid19.who.int/WHO-COVID-19-global-data.csv",
5+
// {
6+
// last: 100,
7+
// columns: ["New_cases", "Date_reported", "Country"],
8+
// }
9+
// );
210
export async function fetchCSV(callExpression) {
311
console.time("fetchCSV Total");
412
const [

examples/macros/matchInFile.tsx

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
// macro code
2-
export function matchInFile(callExpression) {
2+
export function matchInFile(callExpression: BunAST.CallExpression) {
33
const [filePathNode, matcherNode] = callExpression.arguments;
4-
const filePath: string = filePathNode.get();
5-
const matcher: RegExp = matcherNode.get();
4+
let filePath: string;
5+
filePath = filePathNode.get();
6+
7+
let matcher: RegExp;
8+
matcher = matcherNode.get();
69
const file: string = Bun.readFile(Bun.cwd + filePath);
710

811
return (
@@ -18,3 +21,81 @@ export function matchInFile(callExpression) {
1821
</array>
1922
);
2023
}
24+
25+
export declare namespace BunAST {
26+
export abstract class ASTNode {
27+
constructor(...args: any);
28+
}
29+
30+
export interface ASTElement<
31+
P = any,
32+
T extends string | JSXElementConstructor<any> =
33+
| string
34+
| JSXElementConstructor<any>
35+
> {
36+
type: T;
37+
props: P;
38+
key: Key | null;
39+
}
40+
41+
export abstract class Expression extends ASTNode {}
42+
43+
export abstract class CallExpression extends Expression {
44+
arguments: AnyExpression[];
45+
name: string;
46+
target: AnyExpression;
47+
}
48+
49+
export abstract class StringExpression extends Expression {
50+
get(): string;
51+
value: string;
52+
}
53+
54+
export interface StringExpressionElementProps {
55+
value: string;
56+
}
57+
58+
export type StringExpressionElement = ASTElement<
59+
StringExpressionElementProps,
60+
StringExpression
61+
>;
62+
63+
export abstract class RegExpExpression extends Expression {
64+
get(): RegExp;
65+
66+
flags: string;
67+
pattern: string;
68+
raw: string;
69+
}
70+
71+
export type AnyExpression =
72+
| CallExpression
73+
| StringExpression
74+
| RegExpExpression;
75+
}
76+
77+
declare global {
78+
namespace JSX {
79+
interface Element extends BunAST.ASTElement<any, BunAST.AnyExpression> {}
80+
interface ElementClass extends BunAST.Expression {}
81+
interface ElementAttributesProperty {
82+
props: {};
83+
}
84+
interface ElementChildrenAttribute {
85+
children: {};
86+
}
87+
88+
// // We can't recurse forever because `type` can't be self-referential;
89+
// // let's assume it's reasonable to do a single React.lazy() around a single React.memo() / vice-versa
90+
// type LibraryManagedAttributes<C, P> = C extends React.MemoExoticComponent<infer T> | React.LazyExoticComponent<infer T>
91+
// ? T extends React.MemoExoticComponent<infer U> | React.LazyExoticComponent<infer U>
92+
// ? ReactManagedAttributes<U, P>
93+
// : ReactManagedAttributes<T, P>
94+
// : ReactManagedAttributes<C, P>;
95+
96+
interface IntrinsicElements {
97+
// HTML
98+
string: BunAST.StringExpressionElement;
99+
}
100+
}
101+
}

examples/macros/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
"license": "MIT",
66
"dependencies": {
77
"moment": "^2.29.1",
8+
"papaparse": "^5.3.1",
89
"react": "^17.0.2",
910
"react-dom": "^17.0.2",
1011
"react-refresh": "^0.10.0"
12+
},
13+
"devDependencies": {
14+
"@types/react": "^17.0.24",
15+
"@types/react-dom": "^17.0.9"
1116
}
1217
}

examples/macros/styles.css

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,30 @@ body {
1818
font-family: monospace;
1919
}
2020

21-
.Lines {
21+
.Table {
22+
display: grid;
23+
width: fit-content;
24+
}
25+
26+
.Row,
27+
.Header {
28+
display: grid;
29+
grid-template-columns: 2fr 1fr 1fr;
30+
text-align: right;
31+
32+
column-gap: 2rem;
33+
}
34+
35+
.Heading {
36+
text-align: right;
37+
}
38+
39+
.Header {
40+
border-bottom: 1px solid rgb(0, 255, 0);
41+
margin-bottom: 20px;
42+
padding-bottom: 20px;
43+
}
44+
45+
.Heading:nth-of-type(2) {
2246
text-align: left;
23-
margin: 0 auto;
24-
max-width: fit-content;
25-
line-height: 1.5;
2647
}

0 commit comments

Comments
 (0)