FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[refactor] replace JSX factory with JSX runtime by Copilot · Pull Request #5 · TechQuery/CommanderJSX · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .json  (2) .md  (1) .ts  (2) .tsx  (2) dotfile  (1) All 5 file types selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
6 changes: 5 additions & 1 deletion .gitignore
This file contains hidden or 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
Expand Up @@ -2,4 +2,8 @@ package-lock.json
yarn.lock
node_modules/
dist/
docs/
docs/
jsx-runtime.js
jsx-runtime.d.ts
jsx-dev-runtime.js
jsx-dev-runtime.d.ts
6 changes: 3 additions & 3 deletions ReadMe.md
This file contains hidden or 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
Expand Up @@ -12,7 +12,7 @@
`index.tsx`

```JavaScript
import { Command, createCommand } from 'commander-jsx';
import { Command } from 'commander-jsx';

Command.execute(
<Command
Expand Down Expand Up @@ -51,8 +51,8 @@ Command.execute(
"compilerOptions": {
"module": "CommonJS",
"moduleResolution": "Node",
"jsx": "react",
"jsxFactory": "createCommand",
"jsx": "react-jsx",
"jsxImportSource": "commander-jsx",
"target": "ES2017",
"outDir": "dist/"
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
This file contains hidden or 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
Expand Up @@ -56,7 +56,8 @@
"scripts": {
"prepare": "husky install",
"test": "lint-staged && jest",
"build": "rm -rf dist/ docs/ && tsc && typedoc source/",
"parcel": "tsc && cp dist/jsx-runtime.* . && cp jsx-runtime.js jsx-dev-runtime.js && cp jsx-runtime.d.ts jsx-dev-runtime.d.ts",
"build": "rm -rf dist/ docs/ && npm run parcel && typedoc source/",
"start": "typedoc source/ && open-cli docs/index.html",
"prepublishOnly": "npm test && npm run build"
}
Expand Down
18 changes: 0 additions & 18 deletions source/creator.ts
This file contains hidden or 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,21 +1,3 @@
import { Command, CommandMeta } from './Command';

declare global {
namespace JSX {
interface IntrinsicElements<T> {
[key: string]: Command<T>;
}
}
}

export function createCommand<T>(
component: { new (meta: CommandMeta<T>): Command<T> },
props: CommandMeta<T>,
...children: Command<T>[]
) {
return new component({ ...props, children });
}

export function createTable(list: string[][]) {
const counts = list.reduce((counts, row) => {
for (const [index, { length }] of row.entries())
Expand Down
45 changes: 45 additions & 0 deletions source/jsx-runtime.ts
This file contains hidden or 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 @@
import { Command, CommandMeta } from './Command';

declare global {
namespace JSX {
interface ElementType<T = any> {
new (meta: CommandMeta<T>): Command<T>;
}
interface Element extends Command<any> {}
interface IntrinsicAttributes {
children?: Command<any> | Command<any>[];
}
}
}

/**
* JSX runtime for CommanderJSX
* @see {@link https://github.com/reactjs/rfcs/blob/createlement-rfc/text/0000-create-element-changes.md}
* @see {@link https://babeljs.io/docs/babel-plugin-transform-react-jsx}
*/
export function jsx<T>(
type: { new (meta: CommandMeta<T>): Command<T> },
{
children,
...props
}: CommandMeta<T> & {
children?: Command<T> | Command<T>[];
}
): Command<T> {
const childArray = children
? Array.isArray(children)
? children
: [children]
: [];

return new type({ ...props, children: childArray } as CommandMeta<T>);
}

export const jsxs = jsx;
export const jsxDEV = jsx;

/**
* Fragment support (not typically used in CommanderJSX, but required by JSX runtime)
*/
export const Fragment = ({ children }: { children?: Command<any>[] }) =>
Array.isArray(children) ? children : children ? [children] : [];
2 changes: 1 addition & 1 deletion test/Command.spec.tsx
This file contains hidden or 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,6 +1,6 @@
import { currentModulePath, packageOf } from '@tech_query/node-toolkit';

import { createCommand, Command } from '../source';
import { Command } from '../source';

const log = (console.log = jest.fn()),
CMP = currentModulePath();
Expand Down
2 changes: 1 addition & 1 deletion test/creator.spec.tsx
This file contains hidden or 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,4 +1,4 @@
import { createCommand, Command, createTable } from '../source';
import { Command, createTable } from '../source';

describe('Creating utility', () => {
it('should create a Command tree with JSX', () => {
Expand Down
10 changes: 7 additions & 3 deletions tsconfig.json
This file contains hidden or 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
Expand Up @@ -3,14 +3,18 @@
"module": "CommonJS",
"moduleResolution": "Node",
"esModuleInterop": true,
"jsx": "react",
"jsxFactory": "createCommand",
"jsx": "react-jsx",
"jsxImportSource": "commander-jsx",
"strict": true,
"target": "ES2017",
"lib": ["ES2022", "DOM"],
"declaration": true,
"importHelpers": true,
"outDir": "dist/"
"outDir": "dist/",
"paths": {
"commander-jsx/jsx-runtime": ["./source/jsx-runtime"],
"commander-jsx/jsx-dev-runtime": ["./source/jsx-runtime"]
}
},
"include": ["source/*.ts"],
"typedocOptions": {
Expand Down

Back | FazBrowse Home | New Git URL