@alloy-js/typescript 0.2.0 → 0.4.0
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/CHANGELOG.md +12 -0
- package/dist/src/components/PackageDirectory.d.ts.map +1 -1
- package/dist/src/components/PackageDirectory.js +2 -2
- package/dist/src/components/PackageDirectory.js.map +1 -1
- package/dist/src/components/SourceFile.d.ts.map +1 -1
- package/dist/src/components/SourceFile.js +2 -2
- package/dist/src/components/SourceFile.js.map +1 -1
- package/dist/src/components/ValueExpression.d.ts.map +1 -1
- package/dist/src/components/ValueExpression.js +3 -1
- package/dist/src/components/ValueExpression.js.map +1 -1
- package/dist/src/components/stc/index.d.ts +20 -8
- package/dist/src/components/stc/index.d.ts.map +1 -1
- package/dist/src/components/stc/index.js +5 -2
- package/dist/src/components/stc/index.js.map +1 -1
- package/dist/src/name-policy.d.ts.map +1 -1
- package/dist/src/name-policy.js +31 -2
- package/dist/src/name-policy.js.map +1 -1
- package/dist/test/value-expression.test.d.ts +2 -0
- package/dist/test/value-expression.test.d.ts.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -6
- package/src/components/PackageDirectory.tsx +3 -2
- package/src/components/SourceFile.tsx +2 -2
- package/src/components/ValueExpression.tsx +3 -1
- package/src/components/stc/index.ts +5 -2
- package/src/name-policy.ts +99 -2
- package/test/enum.test.tsx +2 -2
- package/test/name-policy.test.tsx +28 -0
- package/test/package-external.test.tsx +1 -0
- package/test/value-expression.test.tsx +39 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alloy-js/typescript",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"change-case": "^5.4.4",
|
|
20
20
|
"pathe": "^1.1.2",
|
|
21
|
-
"@alloy-js/core": "~0.
|
|
21
|
+
"@alloy-js/core": "~0.4.0"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@babel/cli": "^7.24.7",
|
|
@@ -28,12 +28,9 @@
|
|
|
28
28
|
"@rollup/plugin-typescript": "^11.1.6",
|
|
29
29
|
"concurrently": "^8.2.2",
|
|
30
30
|
"typescript": "^5.5.4",
|
|
31
|
-
"vitest": "^
|
|
31
|
+
"vitest": "^3.0.4",
|
|
32
32
|
"@alloy-js/babel-preset": "~0.1.1"
|
|
33
33
|
},
|
|
34
|
-
"overrides": {
|
|
35
|
-
"esbuild": "0.23"
|
|
36
|
-
},
|
|
37
34
|
"type": "module",
|
|
38
35
|
"scripts": {
|
|
39
36
|
"build-src": "babel src -d dist/src --extensions .ts,.tsx",
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Children,
|
|
3
3
|
ComponentContext,
|
|
4
|
-
|
|
4
|
+
createNamedContext,
|
|
5
5
|
Ref,
|
|
6
6
|
Scope,
|
|
7
7
|
shallowRef,
|
|
@@ -30,7 +30,8 @@ export interface PackageDirectoryProps extends PackageJsonFileProps {
|
|
|
30
30
|
path?: string;
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
export const PackageContext: ComponentContext<PackageContext> =
|
|
33
|
+
export const PackageContext: ComponentContext<PackageContext> =
|
|
34
|
+
createNamedContext("@alloy-js/typescript PackageDirectory");
|
|
34
35
|
|
|
35
36
|
export function usePackage() {
|
|
36
37
|
const ctx = useContext(PackageContext);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
ComponentContext,
|
|
3
3
|
SourceFile as CoreSourceFile,
|
|
4
|
-
|
|
4
|
+
createNamedContext,
|
|
5
5
|
Scope,
|
|
6
6
|
SourceDirectoryContext,
|
|
7
7
|
useBinder,
|
|
@@ -21,7 +21,7 @@ export interface SourceFileContext {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
export const SourceFileContext: ComponentContext<SourceFileContext> =
|
|
24
|
-
|
|
24
|
+
createNamedContext("@alloy-js/typescript SourceFile");
|
|
25
25
|
|
|
26
26
|
export function useSourceFile() {
|
|
27
27
|
return useContext(SourceFileContext)!;
|
|
@@ -14,8 +14,10 @@ export function ValueExpression(props: ValueExpressionProps) {
|
|
|
14
14
|
return "undefined";
|
|
15
15
|
} else if (typeof jsValue === "number" || typeof jsValue === "boolean") {
|
|
16
16
|
return String(jsValue);
|
|
17
|
+
} else if (typeof jsValue === "bigint") {
|
|
18
|
+
return `${jsValue}n`;
|
|
17
19
|
} else if (typeof jsValue === "string") {
|
|
18
|
-
return
|
|
20
|
+
return JSON.stringify(jsValue);
|
|
19
21
|
} else if (typeof jsValue === "object") {
|
|
20
22
|
if (jsValue === null) {
|
|
21
23
|
return "null";
|
|
@@ -4,10 +4,15 @@ import * as base from "../index.js";
|
|
|
4
4
|
export const ArrayExpression = stc(base.ArrayExpression);
|
|
5
5
|
export const BarrelFile = stc(base.BarrelFile);
|
|
6
6
|
export const Block = stc(base.Block);
|
|
7
|
+
export const ClassDeclaration = stc(base.ClassDeclaration);
|
|
8
|
+
export const ClassField = stc(base.ClassField);
|
|
9
|
+
export const ClassMember = stc(base.ClassMember);
|
|
10
|
+
export const ClassMethod = stc(base.ClassMethod);
|
|
7
11
|
export const Declaration = stc(base.Declaration);
|
|
8
12
|
export const EnumDeclaration = stc(base.EnumDeclaration);
|
|
9
13
|
export const EnumMember = stc(base.EnumMember);
|
|
10
14
|
export const ExportStatement = stc(base.ExportStatement);
|
|
15
|
+
export const FunctionCallExpression = stc(base.FunctionCallExpression);
|
|
11
16
|
export const FunctionDeclaration = stc(base.FunctionDeclaration);
|
|
12
17
|
export const ImportStatement = stc(base.ImportStatement);
|
|
13
18
|
export const ImportStatements = stc(base.ImportStatements);
|
|
@@ -24,5 +29,3 @@ export const TSConfigJson = stc(base.TSConfigJson);
|
|
|
24
29
|
export const TypeDeclaration = stc(base.TypeDeclaration);
|
|
25
30
|
export const ValueExpression = stc(base.ValueExpression);
|
|
26
31
|
export const VarDeclaration = stc(base.VarDeclaration);
|
|
27
|
-
export const ClassDeclaration = stc(base.ClassDeclaration);
|
|
28
|
-
export const ClassField = stc(base.ClassField);
|
package/src/name-policy.ts
CHANGED
|
@@ -16,21 +16,118 @@ export type TypeScriptElements =
|
|
|
16
16
|
| "interface-member"
|
|
17
17
|
| "type";
|
|
18
18
|
|
|
19
|
+
// Reserved words
|
|
20
|
+
const GLOBAL_RESERVED_WORDS = new Set([
|
|
21
|
+
"break",
|
|
22
|
+
"case",
|
|
23
|
+
"catch",
|
|
24
|
+
"class",
|
|
25
|
+
"const",
|
|
26
|
+
"continue",
|
|
27
|
+
"debugger",
|
|
28
|
+
"default",
|
|
29
|
+
"delete",
|
|
30
|
+
"do",
|
|
31
|
+
"else",
|
|
32
|
+
"enum",
|
|
33
|
+
"export",
|
|
34
|
+
"extends",
|
|
35
|
+
"false",
|
|
36
|
+
"finally",
|
|
37
|
+
"for",
|
|
38
|
+
"function",
|
|
39
|
+
"if",
|
|
40
|
+
"import",
|
|
41
|
+
"in",
|
|
42
|
+
"instanceof",
|
|
43
|
+
"new",
|
|
44
|
+
"null",
|
|
45
|
+
"return",
|
|
46
|
+
"super",
|
|
47
|
+
"switch",
|
|
48
|
+
"this",
|
|
49
|
+
"throw",
|
|
50
|
+
"true",
|
|
51
|
+
"try",
|
|
52
|
+
"typeof",
|
|
53
|
+
"var",
|
|
54
|
+
"void",
|
|
55
|
+
"while",
|
|
56
|
+
"with",
|
|
57
|
+
"as",
|
|
58
|
+
"implements",
|
|
59
|
+
"interface",
|
|
60
|
+
"let",
|
|
61
|
+
"package",
|
|
62
|
+
"private",
|
|
63
|
+
"protected",
|
|
64
|
+
"public",
|
|
65
|
+
"static",
|
|
66
|
+
"yield",
|
|
67
|
+
"await",
|
|
68
|
+
]);
|
|
69
|
+
|
|
70
|
+
const CONTEXT_SAFE_WORDS = new Set([
|
|
71
|
+
"delete",
|
|
72
|
+
"default",
|
|
73
|
+
"super",
|
|
74
|
+
"this",
|
|
75
|
+
"typeof",
|
|
76
|
+
"instanceof",
|
|
77
|
+
]);
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Ensures a valid TypeScript identifier for the given element kind.
|
|
81
|
+
* @param name - The name to validate.
|
|
82
|
+
* @param element - The TypeScript element kind.
|
|
83
|
+
* @returns A TypeScript-safe name.
|
|
84
|
+
*/
|
|
85
|
+
function ensureNonReservedName(
|
|
86
|
+
name: string,
|
|
87
|
+
element: TypeScriptElements,
|
|
88
|
+
): string {
|
|
89
|
+
const suffix = "_";
|
|
90
|
+
|
|
91
|
+
// Global reserved words always need handling
|
|
92
|
+
if (GLOBAL_RESERVED_WORDS.has(name)) {
|
|
93
|
+
return `${name}${suffix}`;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Context-safe reserved words for properties
|
|
97
|
+
if (
|
|
98
|
+
CONTEXT_SAFE_WORDS.has(name) &&
|
|
99
|
+
(element.includes("member") || element.includes("object-member"))
|
|
100
|
+
) {
|
|
101
|
+
return name; // Safe as properties
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return name;
|
|
105
|
+
}
|
|
106
|
+
|
|
19
107
|
export function createTSNamePolicy(): NamePolicy<TypeScriptElements> {
|
|
20
108
|
const caseOptions = {
|
|
21
109
|
prefixCharacters: "$_",
|
|
22
110
|
suffixCharacters: "$_",
|
|
23
111
|
};
|
|
112
|
+
|
|
24
113
|
return createNamePolicy((name, element) => {
|
|
114
|
+
let transformedName: string;
|
|
115
|
+
|
|
25
116
|
switch (element) {
|
|
26
117
|
case "class":
|
|
27
118
|
case "type":
|
|
28
119
|
case "interface":
|
|
29
120
|
case "enum":
|
|
30
|
-
|
|
121
|
+
case "enum-member":
|
|
122
|
+
transformedName = pascalCase(name, caseOptions);
|
|
123
|
+
break;
|
|
124
|
+
|
|
31
125
|
default:
|
|
32
|
-
|
|
126
|
+
transformedName = camelCase(name, caseOptions);
|
|
127
|
+
break;
|
|
33
128
|
}
|
|
129
|
+
|
|
130
|
+
return ensureNonReservedName(transformedName, element);
|
|
34
131
|
});
|
|
35
132
|
}
|
|
36
133
|
|
package/test/enum.test.tsx
CHANGED
|
@@ -60,3 +60,31 @@ it("keeps _ and $ prefix", () => {
|
|
|
60
60
|
$oneTwo;
|
|
61
61
|
`);
|
|
62
62
|
});
|
|
63
|
+
|
|
64
|
+
it("appends _ to reserved words", () => {
|
|
65
|
+
const ref1 = refkey({});
|
|
66
|
+
const ref2 = refkey({});
|
|
67
|
+
|
|
68
|
+
const namePolicy = createTSNamePolicy();
|
|
69
|
+
const res = render(
|
|
70
|
+
<Output namePolicy={namePolicy}>
|
|
71
|
+
<ts.SourceFile path="test.ts">
|
|
72
|
+
<ts.FunctionDeclaration name="default" refkey={ref1} parameters={{"await": "any"}} />
|
|
73
|
+
<ts.VarDeclaration name="super" refkey={ref2}>
|
|
74
|
+
"hello"
|
|
75
|
+
</ts.VarDeclaration>
|
|
76
|
+
<ts.Reference refkey={ref1} />;
|
|
77
|
+
<ts.Reference refkey={ref2} />;
|
|
78
|
+
</ts.SourceFile>
|
|
79
|
+
</Output>,
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
expect(res.contents[0].contents).toEqual(d`
|
|
83
|
+
function default_(await_: any) {
|
|
84
|
+
|
|
85
|
+
}
|
|
86
|
+
const super_ = "hello";
|
|
87
|
+
default_;
|
|
88
|
+
super_;
|
|
89
|
+
`);
|
|
90
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { d } from "@alloy-js/core/testing";
|
|
2
|
+
import { expect, it } from "vitest";
|
|
3
|
+
import { ValueExpression } from "../src/index.js";
|
|
4
|
+
import { toSourceText } from "./utils.jsx";
|
|
5
|
+
|
|
6
|
+
it.each([
|
|
7
|
+
[undefined, "undefined"],
|
|
8
|
+
[null, "null"],
|
|
9
|
+
[true, "true"],
|
|
10
|
+
[false, "false"],
|
|
11
|
+
[42, "42"],
|
|
12
|
+
[42n, "42n"],
|
|
13
|
+
["abc", `"abc"`],
|
|
14
|
+
["a\nb\rc\\", `"a\\nb\\rc\\\\"`],
|
|
15
|
+
[
|
|
16
|
+
[1, 2, 3],
|
|
17
|
+
d`
|
|
18
|
+
[
|
|
19
|
+
1,
|
|
20
|
+
2,
|
|
21
|
+
3
|
|
22
|
+
]
|
|
23
|
+
`,
|
|
24
|
+
],
|
|
25
|
+
[
|
|
26
|
+
{ a: 1, b: 2, c: 3 },
|
|
27
|
+
d`
|
|
28
|
+
{
|
|
29
|
+
a: 1,
|
|
30
|
+
b: 2,
|
|
31
|
+
c: 3
|
|
32
|
+
}
|
|
33
|
+
`,
|
|
34
|
+
],
|
|
35
|
+
])("works - %o => %s", (jsValue, expectedSource) => {
|
|
36
|
+
expect(toSourceText(<ValueExpression jsValue={jsValue} />)).toBe(
|
|
37
|
+
expectedSource,
|
|
38
|
+
);
|
|
39
|
+
});
|