diff options
author | 2021-09-27 21:21:51 -0700 | |
---|---|---|
committer | 2021-09-27 21:21:51 -0700 | |
commit | 2bb7f71b9c88b680008564cdf56186360228c317 (patch) | |
tree | 2c876e18189c7291c142f5c093f60b151a5d9528 | |
parent | 2f8be4f13f6be8577afbb4c65db2faadf9459751 (diff) | |
download | bun-2bb7f71b9c88b680008564cdf56186360228c317.tar.gz bun-2bb7f71b9c88b680008564cdf56186360228c317.tar.zst bun-2bb7f71b9c88b680008564cdf56186360228c317.zip |
Fix up examplesbun-v0.0.26
-rw-r--r-- | examples/macros/components/covid19.tsx | 33 | ||||
-rw-r--r-- | examples/macros/components/index.tsx | 1 | ||||
-rw-r--r-- | examples/macros/fetchCSV.tsx | 8 | ||||
-rw-r--r-- | examples/macros/matchInFile.tsx | 87 | ||||
-rw-r--r-- | examples/macros/package.json | 5 | ||||
-rw-r--r-- | examples/macros/styles.css | 29 |
6 files changed, 156 insertions, 7 deletions
diff --git a/examples/macros/components/covid19.tsx b/examples/macros/components/covid19.tsx new file mode 100644 index 000000000..52fee2ff3 --- /dev/null +++ b/examples/macros/components/covid19.tsx @@ -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> + ); +}; diff --git a/examples/macros/components/index.tsx b/examples/macros/components/index.tsx index 6c3e39be7..20e58b7b7 100644 --- a/examples/macros/components/index.tsx +++ b/examples/macros/components/index.tsx @@ -5,6 +5,7 @@ import { IPAddresses } from "./example"; const Start = function () { const root = document.createElement("div"); document.body.appendChild(root); + ReactDOM.render(<IPAddresses />, root); }; diff --git a/examples/macros/fetchCSV.tsx b/examples/macros/fetchCSV.tsx index 0709fd500..b06b1e03e 100644 --- a/examples/macros/fetchCSV.tsx +++ b/examples/macros/fetchCSV.tsx @@ -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 [ diff --git a/examples/macros/matchInFile.tsx b/examples/macros/matchInFile.tsx index a73f7ee8b..1f5c20fb3 100644 --- a/examples/macros/matchInFile.tsx +++ b/examples/macros/matchInFile.tsx @@ -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 ( @@ -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; + } + } +} diff --git a/examples/macros/package.json b/examples/macros/package.json index de4e26453..dff2c648e 100644 --- a/examples/macros/package.json +++ b/examples/macros/package.json @@ -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" } } diff --git a/examples/macros/styles.css b/examples/macros/styles.css index 87f0ef1a8..a8109a820 100644 --- a/examples/macros/styles.css +++ b/examples/macros/styles.css @@ -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; } |