@knapsack/spec-utils 4.78.13--canary.5646.9581069.0 → 4.78.13

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 (57) hide show
  1. package/.turbo/turbo-build.log +4 -0
  2. package/.turbo/turbo-lint.log +4 -0
  3. package/.turbo/turbo-test.log +50 -0
  4. package/CHANGELOG.md +12 -0
  5. package/dist/align/align.vtest.d.ts +2 -0
  6. package/dist/align/align.vtest.d.ts.map +1 -0
  7. package/dist/align/align.vtest.js +46 -0
  8. package/dist/align/align.vtest.js.map +1 -0
  9. package/dist/analyze-exports.sandbox-components.vtest.d.ts +2 -0
  10. package/dist/analyze-exports.sandbox-components.vtest.d.ts.map +1 -0
  11. package/dist/analyze-exports.sandbox-components.vtest.js +51 -0
  12. package/dist/analyze-exports.sandbox-components.vtest.js.map +1 -0
  13. package/dist/analyze-exports.vtest.d.ts +2 -0
  14. package/dist/analyze-exports.vtest.d.ts.map +1 -0
  15. package/dist/analyze-exports.vtest.js +160 -0
  16. package/dist/analyze-exports.vtest.js.map +1 -0
  17. package/dist/convert-to-spec.vtest.d.ts +2 -0
  18. package/dist/convert-to-spec.vtest.d.ts.map +1 -0
  19. package/dist/convert-to-spec.vtest.js +131 -0
  20. package/dist/convert-to-spec.vtest.js.map +1 -0
  21. package/dist/get-ts-config.vtest.d.ts +2 -0
  22. package/dist/get-ts-config.vtest.d.ts.map +1 -0
  23. package/dist/get-ts-config.vtest.js +9 -0
  24. package/dist/get-ts-config.vtest.js.map +1 -0
  25. package/dist/resolve.vtest.d.ts +2 -0
  26. package/dist/resolve.vtest.d.ts.map +1 -0
  27. package/dist/resolve.vtest.js +57 -0
  28. package/dist/resolve.vtest.js.map +1 -0
  29. package/dist/utils.vtest.d.ts +2 -0
  30. package/dist/utils.vtest.d.ts.map +1 -0
  31. package/dist/utils.vtest.js +37 -0
  32. package/dist/utils.vtest.js.map +1 -0
  33. package/package.json +10 -10
  34. package/src/align/align.vtest.ts +56 -0
  35. package/src/align/get-exports.bench.ts +28 -0
  36. package/src/align/resolve.bench.ts +20 -0
  37. package/src/align/utils.ts +14 -0
  38. package/src/analyze-exports.sandbox-components.vtest.ts +53 -0
  39. package/src/analyze-exports.ts +54 -0
  40. package/src/analyze-exports.vtest.ts +178 -0
  41. package/src/analyze-symbol.ts +213 -0
  42. package/src/analyze-type.ts +316 -0
  43. package/src/boot.ts +31 -0
  44. package/src/convert-to-spec.ts +196 -0
  45. package/src/convert-to-spec.vtest.ts +136 -0
  46. package/src/get-exports.ts +70 -0
  47. package/src/get-ts-config.ts +96 -0
  48. package/src/get-ts-config.vtest.ts +9 -0
  49. package/src/index.ts +5 -0
  50. package/src/resolve.ts +54 -0
  51. package/src/resolve.vtest.ts +69 -0
  52. package/src/test-fixtures/basics.ts +17 -0
  53. package/src/test-fixtures/functions.ts +50 -0
  54. package/src/test-fixtures/index.ts +2 -0
  55. package/src/types.ts +66 -0
  56. package/src/utils.ts +61 -0
  57. package/src/utils.vtest.ts +39 -0
@@ -0,0 +1,2 @@
1
+ export * from './basics.js';
2
+ export * from './functions.js';
package/src/types.ts ADDED
@@ -0,0 +1,66 @@
1
+ import ts from 'typescript';
2
+
3
+ export type TypeInfoCommon = {
4
+ tsRawType: string;
5
+ isOptional?: boolean;
6
+ tsMetadata: {
7
+ typeFlags: Array<keyof typeof ts.TypeFlags>;
8
+ type: ts.Type;
9
+ };
10
+ };
11
+ export type TypeInfo =
12
+ | ({
13
+ type: 'string' | 'number' | 'boolean';
14
+ } & TypeInfoCommon)
15
+ | ({
16
+ type: 'stringLiteral';
17
+ value: string;
18
+ } & TypeInfoCommon)
19
+ | ({
20
+ type: 'numberLiteral';
21
+ value: number;
22
+ } & TypeInfoCommon)
23
+ | ({
24
+ type: 'union';
25
+ items: TypeInfo[];
26
+ } & TypeInfoCommon)
27
+ | ({
28
+ type: 'array';
29
+ items: TypeInfo;
30
+ } & TypeInfoCommon)
31
+ | ({
32
+ type: 'object';
33
+ properties: Record<string, SymbolInfo>;
34
+ } & TypeInfoCommon)
35
+ | ({
36
+ type: 'function';
37
+ parameters: SymbolInfo[];
38
+ returnType?: TypeInfo;
39
+ typeParameters?: TypeInfo[];
40
+ } & TypeInfoCommon)
41
+ | ({
42
+ type: 'class';
43
+ properties: Record<string, SymbolInfo>;
44
+ constructorParameters: SymbolInfo[];
45
+ constructorReturnType: TypeInfo;
46
+ prototype: SymbolInfo;
47
+ } & TypeInfoCommon)
48
+ | ({ type: 'unknown' } & TypeInfoCommon)
49
+ | ({ type: 'any' } & TypeInfoCommon)
50
+ | ({
51
+ type: 'misc';
52
+ } & TypeInfoCommon);
53
+
54
+ export type SymbolInfo = {
55
+ name: string;
56
+ jsDoc?: {
57
+ description?: string;
58
+ jsDocTags: Array<{ name: string; text?: string }>;
59
+ };
60
+ typeInfo: TypeInfo;
61
+ tsMetadata: {
62
+ symbol: ts.Symbol;
63
+ type: ts.Type;
64
+ symbolFlags: Array<keyof typeof ts.SymbolFlags>;
65
+ };
66
+ };
package/src/utils.ts ADDED
@@ -0,0 +1,61 @@
1
+ import { TypeInfo } from './types.js';
2
+
3
+ /**
4
+ * Extracts all set enum flags from a given value.
5
+ *
6
+ * @returns An array of enum keys (names) that are set in the given flags value
7
+ *
8
+ * @example
9
+ * enum Permissions {
10
+ * Read = 1,
11
+ * Write = 2,
12
+ * Execute = 4
13
+ * }
14
+ * const flags = Permissions.Read | Permissions.Write;
15
+ * const setFlags = getSetFlags({ flags, enumObject: Permissions });
16
+ * console.log(setFlags); // Output: ["Read", "Write"]
17
+ */
18
+ export function getSetFlags<T extends Record<string, unknown>>({
19
+ flags,
20
+ enumObject,
21
+ }: {
22
+ /**
23
+ * The flags value to check
24
+ */
25
+ flags: number;
26
+ /**
27
+ * The enum object to check against
28
+ */
29
+ enumObject: T;
30
+ }): Array<keyof T> {
31
+ /**
32
+ * HEADS UP: this uses "bitwise" which is not well understood
33
+ * So if you see `&` and think it should be `&&` you're wrong
34
+ * Crazy stuff goes on
35
+ * In TypeScript, enums are represented as numbers, for example see `ts.SymbolFlags` or `ts.TypeFlags`
36
+ * In a `symbol.flags` it is a `number` that is created from multiple numbers each representing a flag.
37
+ * This function is used to extract the flags from the number.
38
+ * @see https://patrickdesjardins.com/blog/how-to-set-and-read-bitwise-enum-values-in-typescript
39
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_AND
40
+ */
41
+ return (Object.entries(enumObject) as Array<[keyof T, number]>)
42
+ .filter(([, value]) => {
43
+ // Only consider power of 2 values (single flags)
44
+ // eslint-disable-next-line no-bitwise
45
+ if ((value & (value - 1)) !== 0) return false;
46
+ // Check if the flag is set
47
+ // eslint-disable-next-line no-bitwise
48
+ return (flags & value) !== 0;
49
+ })
50
+ .map(([key]) => key);
51
+ }
52
+
53
+ export function assertTypeInfo<T extends TypeInfo['type']>(
54
+ typeInfo: TypeInfo,
55
+ type: T,
56
+ ): asserts typeInfo is Extract<TypeInfo, { type: T }> {
57
+ if (typeInfo.type !== type) {
58
+ const msg = `Expected type to be "${type}", got "${typeInfo.type}"`;
59
+ throw new Error(msg);
60
+ }
61
+ }
@@ -0,0 +1,39 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import ts from 'typescript';
3
+ import { getSetFlags } from './utils.js';
4
+
5
+ describe('utils tests', () => {
6
+ it('getSetFlags single flag', () => {
7
+ const single = getSetFlags({
8
+ enumObject: ts.SymbolFlags,
9
+ flags: ts.SymbolFlags.Property,
10
+ });
11
+ expect(single).toEqual(['Property'] satisfies typeof single);
12
+ });
13
+
14
+ it('getSetFlags multiple flags', () => {
15
+ const multiple = getSetFlags({
16
+ enumObject: ts.SymbolFlags,
17
+ flags:
18
+ // eslint-disable-next-line no-bitwise
19
+ ts.SymbolFlags.FunctionScopedVariable |
20
+ ts.SymbolFlags.BlockScopedVariable,
21
+ });
22
+ expect(multiple).toEqual([
23
+ 'FunctionScopedVariable',
24
+ 'BlockScopedVariable',
25
+ ] satisfies typeof multiple);
26
+ });
27
+
28
+ it('single flag with multiple flags extracted', () => {
29
+ // an "Accessor" is both a "GetAccessor" and a "SetAccessor"
30
+ const single = getSetFlags({
31
+ enumObject: ts.SymbolFlags,
32
+ flags: ts.SymbolFlags.Accessor,
33
+ });
34
+ expect(single).toEqual([
35
+ 'GetAccessor',
36
+ 'SetAccessor',
37
+ ] satisfies typeof single);
38
+ });
39
+ });