Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: solid-primitives-parser #276

Merged
merged 29 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e550600
feat: solid-primitives-parser
bigmistqke Dec 31, 2022
c70d34b
add primitives to list
bigmistqke Jan 2, 2023
86aca9c
createParser to scope $TOKEN, spread meta over token
bigmistqke Jan 3, 2023
bd305e8
tokenize-callback, remove Token-type helper
bigmistqke Jan 4, 2023
a5434d3
warning behind DEV, id to warning, optional 2nd param for component-c…
bigmistqke Jan 4, 2023
8e40c76
parser -> jsx-parser
bigmistqke Jan 4, 2023
5e555e1
tokenize -> createToken
bigmistqke Jan 4, 2023
0f3f90c
add --dev tag to package.json
bigmistqke Jan 4, 2023
9d9a420
rename folder parser -> jsx-parser
bigmistqke Jan 4, 2023
c4ffe61
remove useless map in childrenTokens
bigmistqke Jan 4, 2023
e35fac8
add generic accepted TokenTypes to createJSXParser
bigmistqke Jan 4, 2023
513c3d4
childrenTokens: remove typecast, change returnType
bigmistqke Jan 4, 2023
8a1927b
childrenTokens: memo callback and resolveChild
bigmistqke Jan 4, 2023
52e6468
childrenTokens: support arrays, For, Index
bigmistqke Jan 5, 2023
bd1cfc8
createToken: pass owner to token-callback
bigmistqke Jan 6, 2023
b4857d2
remove owner from tokenProperties
bigmistqke Jan 6, 2023
0bbcd9b
createToken: cache to prevent token remounts
bigmistqke Jan 6, 2023
163903f
createToken: tokenProperties rename tokenCallback
bigmistqke Jan 6, 2023
11148d4
createToken: remove cache
bigmistqke Jan 6, 2023
2176c48
isToken: function to check if value is a token
bigmistqke Jan 7, 2023
3667566
update example, simplify types
bigmistqke Jan 23, 2023
4203f05
write documentation
bigmistqke Jan 23, 2023
b9a6133
update readme solid-primitives
bigmistqke Jan 23, 2023
6334e65
Merge branch 'main' into feat/solid-primitives-parser
thetarnav Jan 26, 2023
2db7a2f
Update lockfile
thetarnav Jan 26, 2023
a0f8f3b
Cleanup jsx-parser
thetarnav Jan 26, 2023
42a1453
Make createJSXParser parameters into options object
thetarnav Jan 26, 2023
dd5e8ad
Improve resolving children, add jsdoc
thetarnav Jan 26, 2023
508826c
jsx-parser: Add basic tests
thetarnav Jan 26, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/jsx-parser/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Solid Primitives Working Group

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
27 changes: 27 additions & 0 deletions packages/jsx-parser/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<p>
<img width="100%" src="" alt="Solid Primitives parser">
</p>

# @solid-primitives/parser

## Installation

```bash
npm install @solid-primitives/parser
# or
yarn add @solid-primitives/parser
# or
pnpm add @solid-primitives/parser
```

## How to use it

## TODO

## Demo

TODO

## Changelog

See [CHANGELOG.md](./CHANGELOG.md)
42 changes: 42 additions & 0 deletions packages/jsx-parser/dev/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<title>Solid App</title>
<style>
html {
font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
}

body {
padding: 0;
margin: 0;
}

a,
button {
cursor: pointer;
}
button[disabled] {
cursor: not-allowed;
}
button[aria-current] {
font-weight: bold;
background: #222;
color: #fff;
}
* {
margin: 0;
}
</style>
</head>

<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>

<script src="/index.tsx" type="module"></script>
</body>
</html>
93 changes: 93 additions & 0 deletions packages/jsx-parser/dev/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { Component, createMemo, For, JSXElement } from "solid-js";
import { render } from "solid-js/web";
import "uno.css";
import { createJSXParser } from "../src";

const { createToken, childrenTokens } = createJSXParser("calculator");

type Meta = { callback: (props: Props) => JSXElement };
type Props = {
value: number;
children?: JSXElement | JSXElement[];
};

type CustomToken = TokenValue | TokenAdd | TokenSubtract;

const Calculator = (props: {
children: JSXElement | JSXElement[] | CustomToken | CustomToken[];
}) => {
const tokens = childrenTokens(() => props.children);

const calculation = () => {
let result = 0;
tokens().forEach(token => {
console.info("token is ", token);
if (token.id === "Value") {
result = token.props.value;
} else if (token.id === "Add") {
result += token.props.value;
} else if (token.id === "Subtract") {
result -= token.props.value;
}
console.info("result is", result);
});
return result;
};

return (
<div>
<For each={tokens()}>{token => token.callback(token.props)}</For> = {calculation()}
</div>
);
};

type MetaValue = {
id: "Value";
} & Meta;
type TokenValue = MetaValue & { props: Props };

const Value = createToken<Props, TokenValue>(
props => ({
props,
id: "Value",
callback: props => <>{props.value}</>
}),
props => <>value outside of a calculator: {props.value}</>
);

type MetaAdd = {
id: "Add";
} & Meta;
type TokenAdd = MetaAdd & { props: Props };

const Add = createToken<Props, TokenAdd>(props => ({
props,
id: "Add",
callback: (props: Props) => <> + {props.value}</>
}));

type MetaSubtract = {
id: "Subtract";
} & Meta;
type TokenSubtract = MetaSubtract & { props: Props };

const Subtract = createToken<Props, TokenSubtract>(props => ({
props,
id: "Subtract",
callback: (props: Props) => <> - {props.value}</>
}));

const App: Component = () => {
return (
<>
<Value value={2} />
<Calculator>
<Value value={1} />
<Add value={4} />
<Subtract value={2} />
</Calculator>
</>
);
};

render(() => <App />, document.getElementById("root")!);
2 changes: 2 additions & 0 deletions packages/jsx-parser/dev/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { viteConfig } from "../../../configs/vite.config";
export default viteConfig;
72 changes: 72 additions & 0 deletions packages/jsx-parser/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"name": "@solid-primitives/jsx-parser",
"version": "0.0.101",
"description": "A primitive to tokenize your solid-components to enable custom parsing.",
"author": "Vincent Van Dijck <vandijckv@gmail.com>",
"contributors": [],
"license": "MIT",
"homepage": "/~https://github.com/solidjs-community/solid-primitives/tree/main/packages/parser#readme",
"repository": {
"type": "git",
"url": "git+/~https://github.com/solidjs-community/solid-primitives.git"
},
"bugs": {
"url": "/~https://github.com/solidjs-community/solid-primitives/issues"
},
"primitive": {
"name": "jsx-parser",
"stage": 0,
"list": [
"createJSXParser"
],
"category": "Utilities"
},
"keywords": [
"solid",
"primitives",
"jsx-parser"
],
"private": false,
"sideEffects": false,
"files": [
"dist"
],
"type": "module",
"main": "./dist/server.cjs",
"module": "./dist/server.js",
"browser": {
"./dist/server.cjs": "./dist/index.cjs",
"./dist/server.js": "./dist/index.js"
},
"types": "./dist/index.d.ts",
"exports": {
"worker": {
"import": "./dist/server.js",
"require": "./dist/server.cjs"
},
"browser": {
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"deno": {
"import": "./dist/server.js",
"require": "./dist/server.cjs"
},
"node": {
"import": "./dist/server.js",
"require": "./dist/server.cjs"
},
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"scripts": {
"dev": "vite serve dev",
"build": "jiti ../../scripts/build.ts --ssr --dev",
"test": "vitest -c ../../configs/vitest.config.ts",
"test:ssr": "pnpm run test --mode ssr"
},
"dependencies": {},
"peerDependencies": {
"solid-js": "^1.6.0"
}
}
39 changes: 39 additions & 0 deletions packages/jsx-parser/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { JSXElement, createMemo } from "solid-js";

export function createJSXParser(id: string = "solid-parser") {
const $TOKEN = Symbol(id);

function createToken<
TProps extends { [key: string]: any },
TToken extends { [key: string]: any } & { id: string }
>(
tokenProperties: (props: TProps) => TToken,
component?: (props: TProps) => JSXElement
): (props: TToken["props"]) => JSXElement {
return (props: TProps) => {
return Object.assign(
component
? () => component(props)
: () => {
process.env.DEV &&
console.info(`tokens can only be rendered inside a Parser with id '${id}'`);
return "";
},
{
[$TOKEN]: true,
...tokenProperties(props)
}
);
};
}

function childrenTokens<T>(children: () => JSXElement | JSXElement[] | T | T[]) {
return createMemo(() =>
children
? (([] as any[]).concat(children()).filter(child => child && $TOKEN in child) as T[])
: []
);
}

return { createToken, childrenTokens, $TOKEN };
}
Empty file.
Empty file.