Skip to content

Commit

Permalink
Fix up examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner committed Sep 28, 2021
1 parent 2f8be4f commit 2bb7f71
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 7 deletions.
33 changes: 33 additions & 0 deletions examples/macros/components/covid19.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { fetchCSV } from "macro:fetchCSV";

export const Covid19 = () => {
const rows = fetchCSV(
"https://covid19.who.int/WHO-COVID-19-global-data.csv",
{
last: 100,
columns: ["New_cases", "Date_reported", "Country"],
}
);

return (
<div>
<h2>Covid-19</h2>
<h6>last {rows.length} updates from the WHO</h6>
<div className="Table">
<div className="Header">
<div className="Heading">New Cases</div>
<div className="Heading">Date</div>
<div className="Heading">Country</div>
</div>

{rows.map((row, index) => (
<div className="Row" key={index}>
<div className="Column">{row[0]}</div>
<div className="Column">{row[1]}</div>
<div className="Column">{row[2]}</div>
</div>
))}
</div>
</div>
);
};
1 change: 1 addition & 0 deletions examples/macros/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IPAddresses } from "./example";
const Start = function () {
const root = document.createElement("div");
document.body.appendChild(root);

ReactDOM.render(<IPAddresses />, root);
};

Expand Down
8 changes: 8 additions & 0 deletions examples/macros/fetchCSV.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import Pappa from "papaparse";
// Example usage:
// const rows = fetchCSV(
// "https://covid19.who.int/WHO-COVID-19-global-data.csv",
// {
// last: 100,
// columns: ["New_cases", "Date_reported", "Country"],
// }
// );
export async function fetchCSV(callExpression) {
console.time("fetchCSV Total");
const [
Expand Down
87 changes: 84 additions & 3 deletions examples/macros/matchInFile.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// macro code
export function matchInFile(callExpression) {
export function matchInFile(callExpression: BunAST.CallExpression) {
const [filePathNode, matcherNode] = callExpression.arguments;
const filePath: string = filePathNode.get();
const matcher: RegExp = matcherNode.get();
let filePath: string;
filePath = filePathNode.get();

let matcher: RegExp;
matcher = matcherNode.get();
const file: string = Bun.readFile(Bun.cwd + filePath);

return (
Expand All @@ -18,3 +21,81 @@ export function matchInFile(callExpression) {
</array>
);
}

export declare namespace BunAST {
export abstract class ASTNode {
constructor(...args: any);
}

export interface ASTElement<
P = any,
T extends string | JSXElementConstructor<any> =
| string
| JSXElementConstructor<any>
> {
type: T;
props: P;
key: Key | null;
}

export abstract class Expression extends ASTNode {}

export abstract class CallExpression extends Expression {
arguments: AnyExpression[];
name: string;
target: AnyExpression;
}

export abstract class StringExpression extends Expression {
get(): string;
value: string;
}

export interface StringExpressionElementProps {
value: string;
}

export type StringExpressionElement = ASTElement<
StringExpressionElementProps,
StringExpression
>;

export abstract class RegExpExpression extends Expression {
get(): RegExp;

flags: string;
pattern: string;
raw: string;
}

export type AnyExpression =
| CallExpression
| StringExpression
| RegExpExpression;
}

declare global {
namespace JSX {
interface Element extends BunAST.ASTElement<any, BunAST.AnyExpression> {}
interface ElementClass extends BunAST.Expression {}
interface ElementAttributesProperty {
props: {};
}
interface ElementChildrenAttribute {
children: {};
}

// // We can't recurse forever because `type` can't be self-referential;
// // let's assume it's reasonable to do a single React.lazy() around a single React.memo() / vice-versa
// type LibraryManagedAttributes<C, P> = C extends React.MemoExoticComponent<infer T> | React.LazyExoticComponent<infer T>
// ? T extends React.MemoExoticComponent<infer U> | React.LazyExoticComponent<infer U>
// ? ReactManagedAttributes<U, P>
// : ReactManagedAttributes<T, P>
// : ReactManagedAttributes<C, P>;

interface IntrinsicElements {
// HTML
string: BunAST.StringExpressionElement;
}
}
}
5 changes: 5 additions & 0 deletions examples/macros/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
"license": "MIT",
"dependencies": {
"moment": "^2.29.1",
"papaparse": "^5.3.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-refresh": "^0.10.0"
},
"devDependencies": {
"@types/react": "^17.0.24",
"@types/react-dom": "^17.0.9"
}
}
29 changes: 25 additions & 4 deletions examples/macros/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,30 @@ body {
font-family: monospace;
}

.Lines {
.Table {
display: grid;
width: fit-content;
}

.Row,
.Header {
display: grid;
grid-template-columns: 2fr 1fr 1fr;
text-align: right;

column-gap: 2rem;
}

.Heading {
text-align: right;
}

.Header {
border-bottom: 1px solid rgb(0, 255, 0);
margin-bottom: 20px;
padding-bottom: 20px;
}

.Heading:nth-of-type(2) {
text-align: left;
margin: 0 auto;
max-width: fit-content;
line-height: 1.5;
}

0 comments on commit 2bb7f71

Please sign in to comment.