@alloy-js/typescript 0.6.0 → 0.7.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.
Files changed (65) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/src/components/ArrayExpression.d.ts.map +1 -1
  3. package/dist/src/components/ArrayExpression.js +1 -1
  4. package/dist/src/components/ArrayExpression.js.map +1 -1
  5. package/dist/src/components/Declaration.d.ts +4 -0
  6. package/dist/src/components/Declaration.d.ts.map +1 -1
  7. package/dist/src/components/Declaration.js +3 -2
  8. package/dist/src/components/Declaration.js.map +1 -1
  9. package/dist/src/components/EnumDeclaration.d.ts.map +1 -1
  10. package/dist/src/components/EnumDeclaration.js +3 -2
  11. package/dist/src/components/EnumDeclaration.js.map +1 -1
  12. package/dist/src/components/ExportStatement.d.ts.map +1 -1
  13. package/dist/src/components/ExportStatement.js +3 -1
  14. package/dist/src/components/ExportStatement.js.map +1 -1
  15. package/dist/src/components/FunctionDeclaration.d.ts +2 -1
  16. package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
  17. package/dist/src/components/FunctionDeclaration.js +2 -1
  18. package/dist/src/components/FunctionDeclaration.js.map +1 -1
  19. package/dist/src/components/MemberChainExpression.d.ts +2 -2
  20. package/dist/src/components/MemberChainExpression.d.ts.map +1 -1
  21. package/dist/src/components/MemberChainExpression.js +43 -7
  22. package/dist/src/components/MemberChainExpression.js.map +1 -1
  23. package/dist/src/components/PackageDirectory.d.ts +1 -3
  24. package/dist/src/components/PackageDirectory.d.ts.map +1 -1
  25. package/dist/src/components/PackageDirectory.js +9 -16
  26. package/dist/src/components/PackageDirectory.js.map +1 -1
  27. package/dist/src/components/PackageJson.d.ts +39 -0
  28. package/dist/src/components/PackageJson.d.ts.map +1 -1
  29. package/dist/src/components/PackageJson.js +30 -12
  30. package/dist/src/components/PackageJson.js.map +1 -1
  31. package/dist/src/components/VarDeclaration.d.ts.map +1 -1
  32. package/dist/src/components/VarDeclaration.js +3 -2
  33. package/dist/src/components/VarDeclaration.js.map +1 -1
  34. package/dist/src/symbols/reference.d.ts.map +1 -1
  35. package/dist/src/symbols/reference.js +5 -2
  36. package/dist/src/symbols/reference.js.map +1 -1
  37. package/dist/src/symbols/ts-output-symbol.d.ts +2 -1
  38. package/dist/src/symbols/ts-output-symbol.d.ts.map +1 -1
  39. package/dist/src/symbols/ts-output-symbol.js +4 -1
  40. package/dist/src/symbols/ts-output-symbol.js.map +1 -1
  41. package/dist/src/symbols/ts-package-scope.d.ts +0 -15
  42. package/dist/src/symbols/ts-package-scope.d.ts.map +1 -1
  43. package/dist/src/symbols/ts-package-scope.js +0 -8
  44. package/dist/src/symbols/ts-package-scope.js.map +1 -1
  45. package/dist/tsconfig.tsbuildinfo +1 -1
  46. package/package.json +2 -2
  47. package/src/components/ArrayExpression.tsx +5 -1
  48. package/src/components/Declaration.tsx +7 -2
  49. package/src/components/EnumDeclaration.tsx +2 -2
  50. package/src/components/ExportStatement.tsx +3 -1
  51. package/src/components/FunctionDeclaration.tsx +4 -2
  52. package/src/components/MemberChainExpression.tsx +37 -5
  53. package/src/components/PackageDirectory.tsx +12 -22
  54. package/src/components/PackageJson.tsx +86 -27
  55. package/src/components/VarDeclaration.tsx +2 -2
  56. package/src/symbols/reference.ts +7 -4
  57. package/src/symbols/ts-output-symbol.ts +6 -2
  58. package/src/symbols/ts-package-scope.ts +0 -28
  59. package/temp/api.json +375 -304
  60. package/test/array.test.tsx +12 -0
  61. package/test/imports.test.tsx +25 -10
  62. package/test/interface.test.tsx +3 -3
  63. package/test/member-chain.test.tsx +77 -0
  64. package/test/package-external.test.tsx +98 -3
  65. package/test/package.test.tsx +56 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alloy-js/typescript",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "",
5
5
  "exports": {
6
6
  ".": {
@@ -16,7 +16,7 @@
16
16
  "dependencies": {
17
17
  "change-case": "^5.4.4",
18
18
  "pathe": "^1.1.2",
19
- "@alloy-js/core": "~0.6.0"
19
+ "@alloy-js/core": "~0.7.0"
20
20
  },
21
21
  "devDependencies": {
22
22
  "@babel/cli": "^7.24.7",
@@ -16,7 +16,11 @@ export function ArrayExpression(props: ArrayExpressionProps) {
16
16
  </For>
17
17
  {props.children && (
18
18
  <>
19
- ,<br />
19
+ {props.jsValue && props.jsValue.length > 0 && (
20
+ <>
21
+ ,<br />
22
+ </>
23
+ )}
20
24
  {props.children}
21
25
  </>
22
26
  )}
@@ -3,7 +3,6 @@ import {
3
3
  Declaration as CoreDeclaration,
4
4
  MemberScope,
5
5
  OutputSymbolFlags,
6
- refkey,
7
6
  Refkey,
8
7
  } from "@alloy-js/core";
9
8
  import { TypeScriptElements, useTSNamePolicy } from "../name-policy.js";
@@ -29,6 +28,11 @@ export interface BaseDeclarationProps {
29
28
  */
30
29
  refkey?: Refkey;
31
30
 
31
+ /**
32
+ * Multiple unique keys for this declaration.
33
+ */
34
+ refkeys?: Refkey[];
35
+
32
36
  /**
33
37
  * Whether to export this declaration from the module.
34
38
  */
@@ -84,7 +88,8 @@ export function Declaration(props: DeclarationProps) {
84
88
 
85
89
  const sym = createTSSymbol({
86
90
  name: namePolicy.getName(props.name, props.nameKind),
87
- refkey: props.refkey ?? refkey(props.name),
91
+ refkey: props.refkey,
92
+ refkeys: props.refkeys,
88
93
  export: props.export,
89
94
  default: props.default,
90
95
  flags: props.flags,
@@ -6,7 +6,6 @@ import {
6
6
  MemberScope,
7
7
  Name,
8
8
  OutputSymbolFlags,
9
- refkey,
10
9
  useBinder,
11
10
  } from "@alloy-js/core";
12
11
  import { useTSNamePolicy } from "../name-policy.js";
@@ -31,7 +30,8 @@ export function EnumDeclaration(props: EnumDeclarationProps) {
31
30
  binder,
32
31
  scope,
33
32
  name: name,
34
- refkey: props.refkey ?? refkey(name),
33
+ refkey: props.refkey,
34
+ refkeys: props.refkeys,
35
35
  default: props.default,
36
36
  export: props.export,
37
37
  flags: OutputSymbolFlags.StaticMemberContainer,
@@ -12,7 +12,9 @@ export function ExportStatement(props: ExportStatementProps) {
12
12
  if (props.star) {
13
13
  const module = useSourceFile();
14
14
  for (const symbol of props.from!.symbols.values()) {
15
- module.scope.exportedSymbols.set(symbol.refkey, symbol as TSOutputSymbol);
15
+ for (const refkey of symbol.refkeys) {
16
+ module.scope.exportedSymbols.set(refkey, symbol as TSOutputSymbol);
17
+ }
16
18
  }
17
19
  return (
18
20
  <>
@@ -24,7 +24,8 @@ import { BaseDeclarationProps, Declaration } from "./Declaration.js";
24
24
 
25
25
  export interface ParameterDescriptor {
26
26
  type?: Children;
27
- refkey: Refkey;
27
+ refkey?: Refkey;
28
+ refkeys?: Refkey[];
28
29
  optional?: boolean;
29
30
  }
30
31
 
@@ -117,7 +118,7 @@ function normalizeAndDeclareParameters(
117
118
  tsFlags: flags,
118
119
  });
119
120
 
120
- return { refkey: symbol.refkey, symbol };
121
+ return { refkeys: symbol.refkeys, symbol };
121
122
  });
122
123
  }
123
124
 
@@ -136,6 +137,7 @@ function normalizeAndDeclareParameters(
136
137
  flags & TSSymbolFlags.TypeSymbol ? "type" : "parameter",
137
138
  ),
138
139
  refkey: descriptor.refkey,
140
+ refkeys: descriptor.refkeys,
139
141
  tsFlags: flags,
140
142
  });
141
143
  descriptor.symbol = symbol;
@@ -8,8 +8,8 @@ export interface MemberChainExpressionProps {
8
8
 
9
9
  /**
10
10
  * Create a member chain expression, which is a member expression comprised of
11
- * either children or {@link FunctionCallExpression} components. It is formatted
12
- * such that when a linebreak is required, the FunctionCallExpression
11
+ * either children, {@link FunctionCallExpression} components, or nested member
12
+ * chain expression components. Other component types are ignored.
13
13
  */
14
14
  export function MemberChainExpression(props: MemberChainExpressionProps) {
15
15
  // chunks are constructed by consuming as many non-call expressions as
@@ -17,7 +17,7 @@ export function MemberChainExpression(props: MemberChainExpressionProps) {
17
17
  // any subsequent call expressions
18
18
 
19
19
  const chunks = computed(() => {
20
- const children = childrenArray(() => props.children);
20
+ const children = flattenCallChains(childrenArray(() => props.children));
21
21
  const chunks: Children[][] = [];
22
22
 
23
23
  let currentChunk: Children[] = [];
@@ -37,13 +37,29 @@ export function MemberChainExpression(props: MemberChainExpressionProps) {
37
37
  }
38
38
  }
39
39
 
40
+ if (currentChunk.length > 0) {
41
+ chunks.push(currentChunk);
42
+ }
43
+
40
44
  return [chunks[0], chunks.slice(1)];
41
45
  });
46
+ const groupId = Symbol();
42
47
 
43
48
  return (
44
49
  <group>
45
- <List joiner="." children={chunks.value[0]} />
46
- <Show when={chunks.value[1].length > 0}>
50
+ <group id={groupId}>
51
+ <List joiner="." children={chunks.value[0]} />
52
+ </group>
53
+ <Show when={chunks.value[1].length === 1}>
54
+ <For each={chunks.value[1]} softline>
55
+ {(chunk) => (
56
+ <>
57
+ .<List joiner="." children={chunk} />
58
+ </>
59
+ )}
60
+ </For>
61
+ </Show>
62
+ <Show when={chunks.value[1].length > 1}>
47
63
  <indent>
48
64
  <For each={chunks.value[1]} softline>
49
65
  {(chunk) => (
@@ -57,3 +73,19 @@ export function MemberChainExpression(props: MemberChainExpressionProps) {
57
73
  </group>
58
74
  );
59
75
  }
76
+
77
+ function flattenCallChains(children: Children[]): Children[] {
78
+ const flatChildren = [];
79
+ for (const child of children) {
80
+ if (
81
+ isComponentCreator(child) &&
82
+ child.component === MemberChainExpression
83
+ ) {
84
+ flatChildren.push(...flattenCallChains(child.props.children));
85
+ } else {
86
+ flatChildren.push(child);
87
+ }
88
+ }
89
+
90
+ return flatChildren;
91
+ }
@@ -7,6 +7,7 @@ import {
7
7
  shallowRef,
8
8
  SourceDirectory,
9
9
  SourceDirectoryContext,
10
+ splitProps,
10
11
  useBinder,
11
12
  useContext,
12
13
  useScope,
@@ -14,17 +15,10 @@ import {
14
15
  import { join } from "pathe";
15
16
  import { createTSPackageScope, TSPackageScope } from "../symbols/index.js";
16
17
  import { modulePath } from "../utils.js";
17
- import {
18
- ExportPath,
19
- PackageExports,
20
- PackageJsonFile,
21
- PackageJsonFileProps,
22
- } from "./PackageJson.js";
18
+ import { PackageJsonFile, PackageJsonFileProps } from "./PackageJson.js";
23
19
  import { TSConfigJson } from "./TsConfigJson.js";
24
20
 
25
21
  export interface PackageDirectoryProps extends PackageJsonFileProps {
26
- dependencies?: Record<string, string>;
27
- exports?: PackageExports | ExportPath;
28
22
  tsConfig?: { outDir?: string };
29
23
  children?: Children;
30
24
  path?: string;
@@ -54,27 +48,23 @@ export function PackageDirectory(props: PackageDirectoryProps) {
54
48
  props.path ?? ".",
55
49
  );
56
50
 
57
- if (props.exports) {
58
- if (typeof props.exports === "string") {
59
- packageContext.scope.addRawExport(".", props.exports);
60
- } else {
61
- for (const ex of Object.entries(props.exports)) {
62
- packageContext.scope.addRawExport(ex[0], ex[1]);
63
- }
64
- }
51
+ const devDeps = props.devDependencies ?? {};
52
+ if (!devDeps["typescript"]) {
53
+ devDeps["typescript"] = "^5.5.2";
65
54
  }
66
55
 
67
- if (props.dependencies) {
68
- for (const dep of Object.entries(props.dependencies)) {
69
- packageContext.scope.addRawDependency(dep[0], dep[1]);
70
- }
71
- }
56
+ const [_, pkgJsonProps] = splitProps(props, [
57
+ "tsConfig",
58
+ "children",
59
+ "path",
60
+ "devDependencies",
61
+ ]);
72
62
 
73
63
  return (
74
64
  <SourceDirectory path={props.path ?? "."}>
75
65
  <PackageContext.Provider value={packageContext}>
76
66
  <Scope value={packageContext.scope}>
77
- <PackageJsonFile {...props} />
67
+ <PackageJsonFile {...pkgJsonProps} devDependencies={devDeps} />
78
68
  <TSConfigJson {...props.tsConfig} />
79
69
  {props.children}
80
70
  </Scope>
@@ -3,10 +3,47 @@ import { modulePath } from "../utils.js";
3
3
  import { usePackage } from "./PackageDirectory.js";
4
4
 
5
5
  export interface PackageJsonFileProps {
6
+ /** The name of the package */
6
7
  name: string;
8
+
9
+ /** The version of the package */
7
10
  version: string;
11
+
12
+ /** The description of the package */
13
+ description?: string;
14
+
15
+ /** The license of the package */
16
+ license?: string;
17
+
18
+ /** The author of the package */
19
+ author?: string;
20
+
21
+ /** The homepage of the package */
22
+ homepage?: string;
23
+
24
+ /** The repository of the package */
25
+ repository?: string | { type: string; url: string };
26
+
27
+ /** The keywords of the package */
28
+ keywords?: string[];
29
+
30
+ /** Whether this is a commonjs or module. Defaults to module. */
8
31
  type?: "module" | "commonjs";
32
+
33
+ /**
34
+ * The hard-coded dependencies of the package. References to external packages
35
+ * will add to the dependency list automatically. Only list dependencies here
36
+ * that are not referenced via refkey.
37
+ */
38
+ dependencies?: Record<string, string>;
39
+
40
+ /** The hard-coded exports of the package. */
41
+ exports?: PackageExports | ExportPath;
42
+
43
+ /** The dev dependencies of the package. */
9
44
  devDependencies?: Record<string, string>;
45
+
46
+ /** The scripts entries of the package. */
10
47
  scripts?: Record<string, string>;
11
48
  }
12
49
 
@@ -20,46 +57,68 @@ export interface PackageExports {
20
57
  [key: string]: ExportPath;
21
58
  }
22
59
 
60
+ /**
61
+ * A file containing a package.json that defines a package. When placed inside a
62
+ * {@link PackageDirectory}, it will contain dependencies and exports for
63
+ * symbols defined inside the package. Additional dependencies and exports can
64
+ * be provided.
65
+ *
66
+ * @example
67
+ * ```tsx
68
+ * <PackageJsonFile name="my-package" version="1.0.0" />
69
+ * ```
70
+ */
23
71
  export function PackageJsonFile(props: PackageJsonFileProps) {
24
72
  const pkg = usePackage();
25
- if (!pkg) {
26
- throw new Error(
27
- "Package json component needs to be inside a PackageDirectory",
28
- );
29
- }
30
73
 
31
74
  const jsonContent = memo(() => {
32
75
  const pkgJson = {
33
76
  name: props.name,
34
77
  version: props.version,
78
+ author: props.author,
79
+ description: props.description,
80
+ license: props.license,
81
+ homepage: props.homepage,
35
82
  type: props.type ?? "module",
36
- dependencies: Object.fromEntries([
37
- ...pkg.scope.rawDependencies.entries(),
38
- ...Array.from(pkg.scope.dependencies).map((dependency) => [
39
- dependency.name,
40
- dependency.version,
41
- ]),
42
- ]),
43
- devDependencies: {
44
- ...props.devDependencies,
45
- typescript: "^5.5.2",
46
- },
83
+ dependencies:
84
+ props.dependencies || (pkg && pkg.scope.dependencies.size > 0) ?
85
+ Object.fromEntries([
86
+ ...Object.entries(props.dependencies ?? {}),
87
+ ...(pkg ?
88
+ Array.from(pkg.scope.dependencies).map((dependency) => [
89
+ dependency.name,
90
+ dependency.version,
91
+ ])
92
+ : []),
93
+ ])
94
+ : undefined,
95
+ devDependencies: props.devDependencies,
47
96
  scripts: props.scripts,
48
97
  exports: undefined as any,
49
98
  };
50
99
 
51
- const exportsEntries: [string, ExportPath][] = [];
52
- for (const [publicPath, module] of pkg.scope.exportedSymbols) {
53
- exportsEntries.push([
54
- publicPath,
55
- modulePath(pkg.outFileMapper.value(module.name)),
56
- ]);
57
- }
100
+ if (typeof props.exports === "string") {
101
+ pkgJson.exports = props.exports;
102
+ } else {
103
+ const exportsEntries: [string, ExportPath][] = [];
104
+ for (const entry of Object.entries(props.exports ?? {})) {
105
+ exportsEntries.push(entry);
106
+ }
107
+
108
+ if (pkg) {
109
+ for (const [publicPath, module] of pkg.scope.exportedSymbols) {
110
+ exportsEntries.push([
111
+ publicPath,
112
+ modulePath(pkg.outFileMapper.value(module.name)),
113
+ ]);
114
+ }
115
+ }
58
116
 
59
- pkgJson.exports =
60
- exportsEntries.length === 0 ?
61
- undefined
62
- : Object.fromEntries(exportsEntries);
117
+ pkgJson.exports =
118
+ exportsEntries.length === 0 ?
119
+ undefined
120
+ : Object.fromEntries(exportsEntries);
121
+ }
63
122
 
64
123
  return JSON.stringify(pkgJson, null, 2);
65
124
  });
@@ -4,7 +4,6 @@ import {
4
4
  Declaration as CoreDeclaration,
5
5
  createAssignmentContext,
6
6
  Name,
7
- refkey,
8
7
  } from "@alloy-js/core";
9
8
  import { useTSNamePolicy } from "../name-policy.js";
10
9
  import { createTSSymbol } from "../symbols/index.js";
@@ -27,7 +26,8 @@ export function VarDeclaration(props: VarDeclarationProps) {
27
26
  const name = useTSNamePolicy().getName(props.name, "variable");
28
27
  const sym = createTSSymbol({
29
28
  name: name,
30
- refkey: props.refkey ?? refkey(name),
29
+ refkey: props.refkey,
30
+ refkeys: props.refkeys,
31
31
  default: props.default,
32
32
  export: props.export,
33
33
  });
@@ -53,10 +53,13 @@ export function ref(refkey: Refkey): () => string {
53
53
 
54
54
  // find public dependency
55
55
  for (const module of sourcePackage.exportedSymbols.values()) {
56
- if (module.exportedSymbols.has(importSymbol.refkey)) {
57
- localSymbol = untrack(() =>
58
- sourceFile!.scope.addImport(importSymbol, module),
59
- );
56
+ for (const refkey of importSymbol.refkeys) {
57
+ if (module.exportedSymbols.has(refkey)) {
58
+ localSymbol = untrack(() =>
59
+ sourceFile!.scope.addImport(importSymbol, module),
60
+ );
61
+ break;
62
+ }
60
63
  }
61
64
  }
62
65
 
@@ -26,7 +26,8 @@ export interface TSOutputSymbol extends OutputSymbol {
26
26
 
27
27
  export interface createTsSymbolOptions {
28
28
  name: string;
29
- refkey: Refkey;
29
+ refkey?: Refkey;
30
+ refkeys?: Refkey[];
30
31
  binder?: Binder;
31
32
  scope?: TSOutputScope;
32
33
  export?: boolean;
@@ -49,6 +50,7 @@ export function createTSSymbol(options: createTsSymbolOptions): TSOutputSymbol {
49
50
  name: options.name,
50
51
  scope,
51
52
  refkey: options.refkey,
53
+ refkeys: options.refkeys,
52
54
  export: !!options.export,
53
55
  default: !!options.default,
54
56
  flags: options.flags ?? OutputSymbolFlags.None,
@@ -56,7 +58,9 @@ export function createTSSymbol(options: createTsSymbolOptions): TSOutputSymbol {
56
58
  });
57
59
 
58
60
  if (options.export && scope.kind === "module") {
59
- scope.exportedSymbols.set(sym.refkey, sym);
61
+ for (const refkey of sym.refkeys) {
62
+ scope.exportedSymbols.set(refkey, sym);
63
+ }
60
64
  }
61
65
 
62
66
  return sym;
@@ -1,8 +1,4 @@
1
1
  import { Binder, OutputScope, Refkey } from "@alloy-js/core";
2
- import {
3
- ExportConditions,
4
- PackageExports,
5
- } from "../components/PackageJson.jsx";
6
2
  import { modulePath } from "../utils.js";
7
3
  import { TSModuleScope } from "./ts-module-scope.js";
8
4
 
@@ -31,20 +27,6 @@ export interface TSPackageScope extends OutputScope {
31
27
  */
32
28
  dependencies: Set<TSPackageScope>;
33
29
 
34
- /**
35
- * Fixed dependencies of this package. Only use this for packages in which you
36
- * do not intend to reference via the `Reference` component. Symbols from raw
37
- * dependencies cannot be auto imported.
38
- */
39
- rawDependencies: Map<string, string>;
40
-
41
- /**
42
- * Fixed exports of this package. Only use this for exports which you want to
43
- * add manually to the package. Otherwise use the `export` prop on
44
- * `SourceFile` to add an export.
45
- */
46
- rawExports: PackageExports;
47
-
48
30
  /**
49
31
  * All of the modules contained within the package. These modules may or may not
50
32
  * be exported.
@@ -57,9 +39,7 @@ export interface TSPackageScope extends OutputScope {
57
39
  builtin?: boolean;
58
40
 
59
41
  addExport(publicPath: string, localModule: TSModuleScope): void;
60
- addRawExport(publicPath: string, exportPath: string | ExportConditions): void;
61
42
  addDependency(pkg: TSPackageScope): void;
62
- addRawDependency(packageName: string, version: string): void;
63
43
  addModule(module: TSModuleScope): void;
64
44
  findExportedSymbol(refkey: Refkey): [string, TSModuleScope] | null;
65
45
  }
@@ -78,8 +58,6 @@ export function createTSPackageScope(
78
58
  parent,
79
59
  exportedSymbols: new Map(),
80
60
  dependencies: new Set(),
81
- rawDependencies: new Map(),
82
- rawExports: {},
83
61
  modules: new Set(),
84
62
  version,
85
63
  path: path,
@@ -90,12 +68,6 @@ export function createTSPackageScope(
90
68
  addExport(publicPath, module) {
91
69
  this.exportedSymbols.set(modulePath(publicPath), module);
92
70
  },
93
- addRawDependency(packageName, version) {
94
- this.rawDependencies.set(packageName, version);
95
- },
96
- addRawExport(localPath, exportPath) {
97
- this.rawExports[localPath] = exportPath;
98
- },
99
71
  addModule(module) {
100
72
  this.modules.add(module);
101
73
  },