react-lib-tools 0.0.18 → 0.0.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -8,6 +8,7 @@ import { syntaxHighlight } from "../syntax-highlight.ts";
|
|
|
8
8
|
import { getPropTypeText } from "./getPropTypeText.ts";
|
|
9
9
|
import { parseDescription } from "./parseDescription.ts";
|
|
10
10
|
import { propsToTable } from "./propsToTable.ts";
|
|
11
|
+
import { getDefaultValueType } from "./getDefaultValueType.ts";
|
|
11
12
|
|
|
12
13
|
const TOKEN_TO_REPLACE = "TOKEN_TO_REPLACE";
|
|
13
14
|
|
|
@@ -61,11 +62,13 @@ export async function compileComponent({
|
|
|
61
62
|
let formattedValue = prop.defaultValue.value;
|
|
62
63
|
|
|
63
64
|
// Wrap strings in quotes
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
switch (getDefaultValueType(prop.defaultValue.value)) {
|
|
66
|
+
case "string": {
|
|
67
|
+
// But don't double quote, e.g.
|
|
68
|
+
// tagName?: keyof IntrinsicElements = ""div" as TagName"
|
|
69
|
+
if (!prop.defaultValue.value.includes('" as ')) {
|
|
70
|
+
formattedValue = `"${prop.defaultValue.value}"`;
|
|
71
|
+
}
|
|
69
72
|
}
|
|
70
73
|
}
|
|
71
74
|
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import { getDefaultValueType } from "./getDefaultValueType";
|
|
3
|
+
|
|
4
|
+
describe("getDefaultValueType", () => {
|
|
5
|
+
test.each(["[]", "[1,2]", '["a","b","c"]'])(
|
|
6
|
+
"should identify array value: %s",
|
|
7
|
+
(value) => {
|
|
8
|
+
expect(getDefaultValueType(value)).toBe("array");
|
|
9
|
+
}
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
test.each(["true", "false"])("should identify boolean value: %s", (value) => {
|
|
13
|
+
expect(getDefaultValueType(value)).toBe("boolean");
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test("should detect null values", () => {
|
|
17
|
+
expect(getDefaultValueType("null")).toBe("null");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test.each(["0", "1", "1.0", "-1", "-1.0"])(
|
|
21
|
+
"should identify numeric value: %s",
|
|
22
|
+
(value) => {
|
|
23
|
+
expect(getDefaultValueType(value)).toBe("number");
|
|
24
|
+
}
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
test.each(["{}", '{foo:"a",bar:true, baz:123}'])(
|
|
28
|
+
"should identify object value: %s",
|
|
29
|
+
(value) => {
|
|
30
|
+
expect(getDefaultValueType(value)).toBe("object");
|
|
31
|
+
}
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
test.each(["", " ", "foo bar baz"])(
|
|
35
|
+
"should identify string value: %o",
|
|
36
|
+
(value) => {
|
|
37
|
+
expect(getDefaultValueType(value)).toBe("string");
|
|
38
|
+
}
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
test("should detect undefined values", () => {
|
|
42
|
+
expect(getDefaultValueType("undefined")).toBe("undefined");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test.each(["1a", "-.!/"])(
|
|
46
|
+
"should identify invalid value: %o as string",
|
|
47
|
+
(value) => {
|
|
48
|
+
expect(getDefaultValueType(value)).toBe("string");
|
|
49
|
+
}
|
|
50
|
+
);
|
|
51
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export type Type =
|
|
2
|
+
| "array"
|
|
3
|
+
| "boolean"
|
|
4
|
+
| "null"
|
|
5
|
+
| "number"
|
|
6
|
+
| "object"
|
|
7
|
+
| "string"
|
|
8
|
+
| "undefined";
|
|
9
|
+
|
|
10
|
+
export function getDefaultValueType(value: string): Type {
|
|
11
|
+
switch (value) {
|
|
12
|
+
case "false":
|
|
13
|
+
case "true": {
|
|
14
|
+
return "boolean";
|
|
15
|
+
}
|
|
16
|
+
case "null": {
|
|
17
|
+
return "null";
|
|
18
|
+
}
|
|
19
|
+
case "undefined": {
|
|
20
|
+
return "undefined";
|
|
21
|
+
}
|
|
22
|
+
default: {
|
|
23
|
+
if (value.trim() === "") {
|
|
24
|
+
return "string";
|
|
25
|
+
} else if (value.startsWith("{") && value.endsWith("}")) {
|
|
26
|
+
return "object";
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
const parsed = JSON.parse(value);
|
|
33
|
+
switch (typeof parsed) {
|
|
34
|
+
case "number": {
|
|
35
|
+
return "number";
|
|
36
|
+
}
|
|
37
|
+
default: {
|
|
38
|
+
if (Array.isArray(parsed)) {
|
|
39
|
+
return "array";
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return "object";
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
} catch {
|
|
46
|
+
// No-op
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return "string";
|
|
50
|
+
}
|
|
@@ -8,7 +8,9 @@ export async function parseDescription(rawText: string) {
|
|
|
8
8
|
// Paper over differences between "@ts-ast-parser/core" and "react-docgen-typescript"
|
|
9
9
|
let text = rawText;
|
|
10
10
|
|
|
11
|
-
text = text.
|
|
11
|
+
text = text.replaceAll("@deprecated", "❌ This prop has been deprecated.");
|
|
12
|
+
text = text.replace(/@param ([\w]+)/g, "- `$1`");
|
|
13
|
+
text = text.replaceAll("@return", "\n");
|
|
12
14
|
|
|
13
15
|
Object.keys(INTENT_FLAGS).forEach((flag) => {
|
|
14
16
|
text = text
|
package/styles.css
CHANGED
|
@@ -102,6 +102,9 @@ code {
|
|
|
102
102
|
color: rgba(255, 255, 255, 0.8);
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
+
.tok-bool {
|
|
106
|
+
color: var(--color-teal-300);
|
|
107
|
+
}
|
|
105
108
|
.tok-comment {
|
|
106
109
|
color: var(--color-slate-500);
|
|
107
110
|
}
|
|
@@ -115,6 +118,7 @@ code {
|
|
|
115
118
|
.tok-meta {
|
|
116
119
|
}
|
|
117
120
|
.tok-number {
|
|
121
|
+
color: var(--color-teal-300);
|
|
118
122
|
}
|
|
119
123
|
.tok-operator {
|
|
120
124
|
color: var(--color-slate-400);
|