@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.
- package/.turbo/turbo-build.log +4 -0
- package/.turbo/turbo-lint.log +4 -0
- package/.turbo/turbo-test.log +50 -0
- package/CHANGELOG.md +12 -0
- package/dist/align/align.vtest.d.ts +2 -0
- package/dist/align/align.vtest.d.ts.map +1 -0
- package/dist/align/align.vtest.js +46 -0
- package/dist/align/align.vtest.js.map +1 -0
- package/dist/analyze-exports.sandbox-components.vtest.d.ts +2 -0
- package/dist/analyze-exports.sandbox-components.vtest.d.ts.map +1 -0
- package/dist/analyze-exports.sandbox-components.vtest.js +51 -0
- package/dist/analyze-exports.sandbox-components.vtest.js.map +1 -0
- package/dist/analyze-exports.vtest.d.ts +2 -0
- package/dist/analyze-exports.vtest.d.ts.map +1 -0
- package/dist/analyze-exports.vtest.js +160 -0
- package/dist/analyze-exports.vtest.js.map +1 -0
- package/dist/convert-to-spec.vtest.d.ts +2 -0
- package/dist/convert-to-spec.vtest.d.ts.map +1 -0
- package/dist/convert-to-spec.vtest.js +131 -0
- package/dist/convert-to-spec.vtest.js.map +1 -0
- package/dist/get-ts-config.vtest.d.ts +2 -0
- package/dist/get-ts-config.vtest.d.ts.map +1 -0
- package/dist/get-ts-config.vtest.js +9 -0
- package/dist/get-ts-config.vtest.js.map +1 -0
- package/dist/resolve.vtest.d.ts +2 -0
- package/dist/resolve.vtest.d.ts.map +1 -0
- package/dist/resolve.vtest.js +57 -0
- package/dist/resolve.vtest.js.map +1 -0
- package/dist/utils.vtest.d.ts +2 -0
- package/dist/utils.vtest.d.ts.map +1 -0
- package/dist/utils.vtest.js +37 -0
- package/dist/utils.vtest.js.map +1 -0
- package/package.json +10 -10
- package/src/align/align.vtest.ts +56 -0
- package/src/align/get-exports.bench.ts +28 -0
- package/src/align/resolve.bench.ts +20 -0
- package/src/align/utils.ts +14 -0
- package/src/analyze-exports.sandbox-components.vtest.ts +53 -0
- package/src/analyze-exports.ts +54 -0
- package/src/analyze-exports.vtest.ts +178 -0
- package/src/analyze-symbol.ts +213 -0
- package/src/analyze-type.ts +316 -0
- package/src/boot.ts +31 -0
- package/src/convert-to-spec.ts +196 -0
- package/src/convert-to-spec.vtest.ts +136 -0
- package/src/get-exports.ts +70 -0
- package/src/get-ts-config.ts +96 -0
- package/src/get-ts-config.vtest.ts +9 -0
- package/src/index.ts +5 -0
- package/src/resolve.ts +54 -0
- package/src/resolve.vtest.ts +69 -0
- package/src/test-fixtures/basics.ts +17 -0
- package/src/test-fixtures/functions.ts +50 -0
- package/src/test-fixtures/index.ts +2 -0
- package/src/types.ts +66 -0
- package/src/utils.ts +61 -0
- package/src/utils.vtest.ts +39 -0
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
|
+
});
|