Skip to content

Commit

Permalink
refactor(output, scribbler)!: use the jsx-runtime transform (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrazauskas committed May 16, 2024
1 parent 839f313 commit b210cca
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 78 deletions.
12 changes: 11 additions & 1 deletion cspell.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,15 @@
"ignorePaths": ["**/__snapshots__/**/*"],
"language": "en-US",
"useGitignore": true,
"words": ["codacy", "biomejs", "lcovonly", "packagephobia", "tsserverlibrary", "tstyche", "typecheck", "typetests"]
"words": [
"biomejs",
"codacy",
"jsxs",
"lcovonly",
"packagephobia",
"tsserverlibrary",
"tstyche",
"typecheck",
"typetests"
]
}
3 changes: 1 addition & 2 deletions source/output/CodeSpanText.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { DiagnosticOrigin } from "#diagnostic";
import { Path } from "#path";
// biome-ignore lint/correctness/noUnusedImports: TODO false positive
import { Color, Line, Scribbler, Text } from "#scribbler";
import { Color, Line, Text } from "#scribbler";

export class CodeSpanText implements JSX.ElementClass {
constructor(readonly props: DiagnosticOrigin) {}
Expand Down
3 changes: 1 addition & 2 deletions source/output/addsPackageStepText.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// biome-ignore lint/correctness/noUnusedImports: TODO false positive
import { Color, Line, Scribbler, Text } from "#scribbler";
import { Color, Line, Text } from "#scribbler";

export function addsPackageStepText(compilerVersion: string, installationPath: string): JSX.Element {
return (
Expand Down
3 changes: 1 addition & 2 deletions source/output/describeNameText.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// biome-ignore lint/correctness/noUnusedImports: TODO false positive
import { Line, Scribbler } from "#scribbler";
import { Line } from "#scribbler";

export function describeNameText(name: string, indent = 0): JSX.Element {
return <Line indent={indent + 1}>{name}</Line>;
Expand Down
3 changes: 1 addition & 2 deletions source/output/diagnosticText.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { type Diagnostic, DiagnosticCategory } from "#diagnostic";
// biome-ignore lint/correctness/noUnusedImports: TODO false positive
import { Color, Line, Scribbler, Text } from "#scribbler";
import { Color, Line, Text } from "#scribbler";
import { CodeSpanText } from "./CodeSpanText.js";

class DiagnosticText implements JSX.ElementClass {
Expand Down
3 changes: 1 addition & 2 deletions source/output/fileStatusText.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { TestFile } from "#file";
import { Path } from "#path";
import { type FileResultStatus, ResultStatus } from "#result";
// biome-ignore lint/correctness/noUnusedImports: TODO false positive
import { Color, Line, Scribbler, Text } from "#scribbler";
import { Color, Line, Text } from "#scribbler";

class FileNameText implements JSX.ElementClass {
constructor(readonly props: { filePath: string }) {}
Expand Down
3 changes: 1 addition & 2 deletions source/output/fileViewText.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// biome-ignore lint/correctness/noUnusedImports: TODO false positive
import { Line, Scribbler, Text } from "#scribbler";
import { Line, Text } from "#scribbler";

export function fileViewText(lines: Array<JSX.Element>, addEmptyFinalLine: boolean): JSX.Element {
return (
Expand Down
3 changes: 1 addition & 2 deletions source/output/formattedText.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// biome-ignore lint/correctness/noUnusedImports: TODO false positive
import { Line, Scribbler } from "#scribbler";
import { Line } from "#scribbler";

export class JsonText implements JSX.ElementClass {
constructor(readonly props: { input: Array<string> | Record<string, unknown> }) {}
Expand Down
5 changes: 2 additions & 3 deletions source/output/helpText.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { OptionBrand, type OptionDefinition } from "#config";
// biome-ignore lint/correctness/noUnusedImports: TODO false positive
import { Color, Line, Scribbler, Text } from "#scribbler";
import { Color, Line, Text } from "#scribbler";

const usageExamples: Array<[commandText: string, descriptionText: string]> = [
["tstyche", "Run all tests."],
Expand Down Expand Up @@ -96,7 +95,7 @@ class CommandLineOptionNameText implements JSX.ElementClass {
class CommandLineOptionHintText implements JSX.ElementClass {
constructor(readonly props: { definition: OptionDefinition }) {}

render(): JSX.Element | null {
render(): JSX.Element {
if (this.props.definition.brand === OptionBrand.List) {
return (
<Text>
Expand Down
3 changes: 1 addition & 2 deletions source/output/summaryText.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { ResultCount } from "#result";
// biome-ignore lint/correctness/noUnusedImports: TODO false positive
import { Color, Line, Scribbler, Text } from "#scribbler";
import { Color, Line, Text } from "#scribbler";

class RowText implements JSX.ElementClass {
constructor(readonly props: { label: string; text: JSX.Element }) {}
Expand Down
3 changes: 1 addition & 2 deletions source/output/testNameText.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// biome-ignore lint/correctness/noUnusedImports: TODO false positive
import { Color, Line, Scribbler, Text } from "#scribbler";
import { Color, Line, Text } from "#scribbler";

class StatusText implements JSX.ElementClass {
constructor(readonly props: { status: "fail" | "pass" | "skip" | "todo" }) {}
Expand Down
3 changes: 1 addition & 2 deletions source/output/usesCompilerStepText.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Path } from "#path";
// biome-ignore lint/correctness/noUnusedImports: TODO false positive
import { Color, Line, Scribbler, Text } from "#scribbler";
import { Color, Line, Text } from "#scribbler";

class ProjectNameText implements JSX.ElementClass {
constructor(readonly props: { filePath: string }) {}
Expand Down
3 changes: 1 addition & 2 deletions source/output/watchModeUsageText.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// biome-ignore lint/correctness/noUnusedImports: TODO false positive
import { Color, Line, Scribbler, Text } from "#scribbler";
import { Color, Line, Text } from "#scribbler";

export function watchModeUsageText(): JSX.Element {
const usageText = Object.entries({ a: "run all tests", x: "exit" }).map(([key, action]) => {
Expand Down
2 changes: 0 additions & 2 deletions source/scribbler/Line.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// biome-ignore lint/correctness/noUnusedImports: TODO false positive
import { Scribbler } from "./Scribbler.js";
import { Text } from "./Text.js";
import type { Color } from "./enums.js";

Expand Down
81 changes: 36 additions & 45 deletions source/scribbler/Scribbler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ declare global {
namespace JSX {
interface Element {
$$typeof: symbol;
children: ElementChildren;
props: Record<string, unknown> | null;
props: Record<string, unknown>;
type: ComponentConstructor | string;
}
interface ElementAttributesProperty {
props: Record<string, unknown> | null;
props: Record<string, unknown>;
}
interface ElementClass {
render: () => JSX.Element | null;
render: () => JSX.Element;
}
interface ElementChildrenAttribute {
children: ElementChildren;
Expand Down Expand Up @@ -54,19 +53,6 @@ export class Scribbler {
this.#noColor = options?.noColor ?? false;
}

static createElement(
type: ComponentConstructor | string,
props: Record<string, unknown> | null,
...children: Array<ElementChildren>
): JSX.Element {
return {
$$typeof: Symbol.for("tstyche:scribbler"),
children: children.length > 1 ? children : children[0],
props,
type,
};
}

#escapeSequence(attributes: string | Array<string>): string {
return ["\u001B[", Array.isArray(attributes) ? attributes.join(";") : attributes, "m"].join("");
}
Expand All @@ -75,43 +61,38 @@ export class Scribbler {
return lines.replace(this.#notEmptyLineRegex, this.#indentStep.repeat(level));
}

render(element: JSX.Element | null): string {
if (element != null) {
if (typeof element.type === "function") {
const instance = new element.type({
...element.props,
children: element.children,
});

return this.render(instance.render());
}
render(element: JSX.Element): string {
if (typeof element.type === "function") {
const instance = new element.type({ ...element.props });

if (element.type === "ansi" && !this.#noColor) {
const flags =
typeof element.props?.["escapes"] === "string" || Array.isArray(element.props?.["escapes"])
? element.props["escapes"]
: undefined;
return this.render(instance.render());
}

if (flags != null) {
return this.#escapeSequence(flags);
}
}
if (element.type === "ansi" && !this.#noColor) {
const flags =
typeof element.props?.["escapes"] === "string" || Array.isArray(element.props?.["escapes"])
? element.props["escapes"]
: undefined;

if (element.type === "newLine") {
return this.#newLine;
if (flags != null) {
return this.#escapeSequence(flags);
}
}

if (element.type === "text") {
const indentLevel = typeof element.props?.["indent"] === "number" ? element.props["indent"] : 0;
if (element.type === "newLine") {
return this.#newLine;
}

let text = this.#visitElementChildren(element.children);
if (element.type === "text") {
const indentLevel = typeof element.props?.["indent"] === "number" ? element.props["indent"] : 0;

if (indentLevel > 0) {
text = this.#indentEachLine(text, indentLevel);
}
let text = this.#visitElementChildren(element.props["children"] as ElementChildren);

return text;
if (indentLevel > 0) {
text = this.#indentEachLine(text, indentLevel);
}

return text;
}

return "";
Expand Down Expand Up @@ -151,3 +132,13 @@ export class Scribbler {
return "";
}
}

export function jsx(type: ComponentConstructor | string, props: Record<string, unknown>): JSX.Element {
return {
$$typeof: Symbol.for("tstyche:scribbler"),
props,
type,
};
}

export { jsx as jsxs };
2 changes: 0 additions & 2 deletions source/scribbler/Text.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// biome-ignore lint/correctness/noUnusedImports: TODO false positive
import { Scribbler } from "./Scribbler.js";
import { Color } from "./enums.js";

interface TextProps {
Expand Down
7 changes: 4 additions & 3 deletions source/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"module": "node16",
"moduleResolution": "node16",
"paths": {
"#*": ["./*/index.ts"]
"#*": ["./*/index.ts"],
"#scribbler/jsx-runtime": ["./scribbler/Scribbler.ts"]
},

"jsx": "react",
"jsxFactory": "Scribbler.createElement",
"jsx": "react-jsx",
"jsxImportSource": "#scribbler",

"skipLibCheck": false
},
Expand Down

0 comments on commit b210cca

Please sign in to comment.