Skip to content

Commit 8422d54

Browse files
committed
fix(module-source,compartment-mapper): fix SourceMapHook & other types
This fixes the type of `SourceMapHook` and the implementation to align with reality. - Introduces some `.ts` sources for types; fixes `tsconfig.json` - Fixes exports of said types - Fixes shim types - Adds a `typedoc.json` for proper entry points - Adds type arguments for `ParserImplementation`, `ParseFn`, etc. for external parsers using custom policies.
1 parent 0309977 commit 8422d54

5 files changed

Lines changed: 130 additions & 49 deletions

File tree

packages/compartment-mapper/src/types/compartment-map-schema.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type {
1414
} from '../policy-format.js';
1515
import type { CanonicalName } from './canonical-name.js';
1616
import type { FileUrlString } from './external.js';
17-
import type { SomePackagePolicy } from './policy-schema.js';
17+
import type { PackagePolicy, SomePackagePolicy } from './policy-schema.js';
1818
import type { PatternDescriptor } from './pattern-replacement.js';
1919
import type { LiteralUnion } from './typescript.js';
2020

@@ -122,23 +122,24 @@ export interface PackageCompartmentDescriptor
122122
* one for a given library or application `package.json`.
123123
*/
124124
export interface CompartmentDescriptor<
125-
T extends ModuleConfiguration = ModuleConfiguration,
126-
U extends string = string,
125+
TModuleConfiguration extends ModuleConfiguration = ModuleConfiguration,
126+
TCompartmentName extends string = string,
127+
TPackagePolicy extends SomePackagePolicy = SomePackagePolicy,
127128
> {
128-
label: CanonicalName<U>;
129+
label: CanonicalName<TCompartmentName>;
129130
/**
130131
* the name of the originating package suitable for constructing a sourceURL
131132
* prefix that will match it to files in a developer workspace.
132133
*/
133134
name: string;
134-
modules: Record<string, T>;
135+
modules: Record<string, TModuleConfiguration>;
135136
scopes?: Record<string, ScopeDescriptor>;
136137
/** language for extension */
137138
parsers?: LanguageForExtension;
138139
/** language for module specifier */
139140
types?: LanguageForModuleSpecifier;
140141
/** policy specific to compartment */
141-
policy?: SomePackagePolicy;
142+
policy?: TPackagePolicy;
142143

143144
location: string;
144145
/**
@@ -154,9 +155,32 @@ export interface CompartmentDescriptor<
154155
retained?: true;
155156
}
156157

158+
/**
159+
* Any {@link CompartmentDescriptor}
160+
*/
161+
export type SomeCompartmentDescriptor = CompartmentDescriptor<any, any, any>;
162+
163+
/**
164+
* Any {@link CompartmentDescriptor} with a non-nullish
165+
* {@link CompartmentDescriptor.policy} property
166+
*/
167+
export type SomeCompartmentDescriptorWithPolicy =
168+
CompartmentDescriptorWithPolicy<any, any, any>;
169+
170+
/**
171+
* A {@link CompartmentDescriptor} with a non-nullish
172+
* {@link CompartmentDescriptor.policy} property
173+
*/
157174
export type CompartmentDescriptorWithPolicy<
158-
T extends ModuleConfiguration = ModuleConfiguration,
159-
> = Omit<CompartmentDescriptor<T>, 'policy'> & { policy: SomePackagePolicy };
175+
TModuleConfiguration extends ModuleConfiguration = ModuleConfiguration,
176+
TCompartmentName extends string = string,
177+
TPackagePolicy extends SomePackagePolicy = SomePackagePolicy,
178+
> = Omit<
179+
CompartmentDescriptor<TModuleConfiguration, TCompartmentName, TPackagePolicy>,
180+
'policy'
181+
> & {
182+
policy: TPackagePolicy;
183+
};
160184

161185
/**
162186
* A compartment descriptor digested by `digestCompartmentMap()`

packages/compartment-mapper/src/types/external.ts

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type {
1919
} from '../policy-format.js';
2020
import type { CanonicalName } from './canonical-name.js';
2121
import type {
22+
SomeCompartmentDescriptor,
2223
CompartmentDescriptor,
2324
CompartmentMapDescriptor,
2425
DigestedCompartmentMapDescriptor,
@@ -735,37 +736,51 @@ interface BaseParserImplementation {
735736
heuristicImports: boolean;
736737
}
737738

738-
export interface ParserImplementation extends BaseParserImplementation {
739-
parse: ParseFn;
739+
export interface ParserImplementation<
740+
TCompartmentDescriptor extends
741+
SomeCompartmentDescriptor = SomeCompartmentDescriptor,
742+
> extends BaseParserImplementation {
743+
parse: ParseFn<TCompartmentDescriptor>;
740744
synchronous: true;
741745
}
742746

743-
export interface AsyncParserImplementation extends BaseParserImplementation {
744-
parse: AsyncParseFn;
747+
export interface AsyncParserImplementation<
748+
TCompartmentDescriptor extends
749+
SomeCompartmentDescriptor = SomeCompartmentDescriptor,
750+
> extends BaseParserImplementation {
751+
parse: AsyncParseFn<TCompartmentDescriptor>;
745752
synchronous: false;
746753
}
747754

748755
/**
749756
* Options bag for a {@link ParseFn} or {@link AsyncParseFn}.
757+
*
758+
* @template TCompartmentDescriptor The compartment descriptor to use for the parse
750759
*/
751-
export type ParseOptions = Partial<{
760+
export type ParseOptions<
761+
TCompartmentDescriptor extends
762+
SomeCompartmentDescriptor = SomeCompartmentDescriptor,
763+
> = Partial<{
752764
sourceMap: string | undefined;
753765
sourceMapHook: ParseSourceMapHook | undefined;
754766
sourceMapUrl: string | undefined;
755767
readPowers: ReadFn | ReadPowers | undefined;
756-
compartmentDescriptor: CompartmentDescriptor | undefined;
768+
compartmentDescriptor: TCompartmentDescriptor | undefined;
757769
}> &
758770
ArchiveOnlyOption;
759771

760772
/**
761773
* Arguments for a {@link ParseFn} or {@link AsyncParseFn}.
762774
*/
763-
export type ParseArguments = [
775+
export type ParseArguments<
776+
TCompartmentDescriptor extends
777+
SomeCompartmentDescriptor = SomeCompartmentDescriptor,
778+
> = [
764779
bytes: Uint8Array,
765780
specifier: string,
766781
moduleLocation: string,
767782
packageLocation: string,
768-
options?: ParseOptions,
783+
options?: ParseOptions<TCompartmentDescriptor>,
769784
];
770785

771786
/**
@@ -788,17 +803,17 @@ export type ParseResult = {
788803
* Because {@link ParseResult} contains {@link FinalStaticModuleType} from
789804
* `ses`, those types would want to be moved out of `ses` with it.
790805
*/
791-
export type ParseFn = { isSyncParser?: true } & ((
792-
...args: ParseArguments
793-
) => ParseResult);
806+
export interface ParseFn<
807+
TCompartmentDescriptor extends
808+
SomeCompartmentDescriptor = SomeCompartmentDescriptor,
809+
> {
810+
isSyncParser?: true;
811+
(...args: ParseArguments<TCompartmentDescriptor>): ParseResult;
812+
}
794813

795814
/**
796815
* An asynchronous module parsing function.
797816
*/
798-
export type AsyncParseFn = { isSyncParser?: false } & ((
799-
...args: ParseArguments
800-
) => Promise<ParseResult>);
801-
802817
/**
803818
* Mapping of `Language` to synchronous {@link ParserImplementation}s only.
804819
*
@@ -808,6 +823,13 @@ export type SyncParserForLanguage = Record<
808823
Language | string,
809824
ParserImplementation
810825
>;
826+
export interface AsyncParseFn<
827+
TCompartmentDescriptor extends
828+
SomeCompartmentDescriptor = SomeCompartmentDescriptor,
829+
> {
830+
isSyncParser?: false;
831+
(...args: ParseArguments<TCompartmentDescriptor>): Promise<ParseResult>;
832+
}
811833

812834
/**
813835
* Mapping of `Language` to {@link ParserImplementation

packages/compartment-mapper/src/types/policy-schema.ts

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import type { WILDCARD_POLICY_VALUE } from '../policy-format.js';
9+
import type { IsAny } from './typescript.js';
910

1011
/* eslint-disable no-use-before-define */
1112

@@ -31,9 +32,16 @@ export type ImplicitAttenuationDefinition = [any, ...any[]];
3132
export type AttenuationDefinition =
3233
| FullAttenuationDefinition
3334
| ImplicitAttenuationDefinition;
35+
36+
/**
37+
* Information about the attenuator implementation
38+
*/
3439
export type UnifiedAttenuationDefinition = {
40+
/** Name of the attenuator (for error messages) */
3541
displayName: string;
42+
/** The module specifier of the implementation */
3643
specifier: string | null;
44+
/** Parameters to pass to the attenuator at invocation */
3745
params?: any[] | undefined;
3846
};
3947

@@ -52,10 +60,24 @@ export type PropertyPolicy = Record<string, boolean>;
5260
* A type representing a policy item, which can be a {@link WildcardPolicy
5361
* wildcard policy}, a property policy, `undefined`, or defined by an
5462
* attenuator
63+
*
64+
* @remarks
65+
* The void-vs-custom `T` branch was originally `[T] extends [void] ? … : …`, but
66+
* the type `any` also makes that test succeed, so `PolicyItem<any>` used to
67+
* reduce to the same as `void` and
68+
* `PackagePolicy<any, any, any, any> = AnyPackagePolicy` was not a supertype of
69+
* policies with extra string literals (for example, LavaMoat's {@code "root"} on
70+
* package imports). A separate branch for a wide
71+
* `any` type parameter yields
72+
* `PolicyItem<any> = WildcardPolicy | PropertyPolicy | any` so
73+
* `AnyPackagePolicy` correctly accepts all package policy item shapes.
5574
*/
56-
export type PolicyItem<T = void> = [T] extends [void]
57-
? WildcardPolicy | PropertyPolicy
58-
: WildcardPolicy | PropertyPolicy | T;
75+
export type PolicyItem<T = void> =
76+
IsAny<T> extends true
77+
? WildcardPolicy | PropertyPolicy | T
78+
: [T] extends [void]
79+
? WildcardPolicy | PropertyPolicy
80+
: WildcardPolicy | PropertyPolicy | T;
5981

6082
/**
6183
* An object representing a nested attenuation definition.
@@ -69,10 +91,10 @@ export type NestedAttenuationDefinition = Record<
6991
* An object representing a base package policy.
7092
*/
7193
export type PackagePolicy<
72-
PackagePolicyItem = void,
73-
GlobalsPolicyItem = void,
74-
BuiltinsPolicyItem = void,
75-
ExtraOptions = unknown,
94+
PackagePolicyExtra = void,
95+
GlobalsPolicyExtra = void,
96+
BuiltinsPolicyExtra = void,
97+
Options = unknown,
7698
> = {
7799
/**
78100
* The default attenuator, if any.
@@ -81,17 +103,17 @@ export type PackagePolicy<
81103
/**
82104
* The policy item for packages.
83105
*/
84-
packages?: PolicyItem<PackagePolicyItem> | undefined;
106+
packages?: PolicyItem<PackagePolicyExtra> | undefined;
85107
/**
86108
* The policy item or full attenuation definition for globals.
87109
*/
88-
globals?: AttenuationDefinition | PolicyItem<GlobalsPolicyItem> | undefined;
110+
globals?: AttenuationDefinition | PolicyItem<GlobalsPolicyExtra> | undefined;
89111
/**
90112
* The policy item or nested attenuation definition for builtins.
91113
*/
92114
builtins?:
93115
| NestedAttenuationDefinition
94-
| PolicyItem<BuiltinsPolicyItem>
116+
| PolicyItem<BuiltinsPolicyExtra>
95117
| undefined;
96118
/**
97119
* Whether to disable global freeze.
@@ -104,43 +126,47 @@ export type PackagePolicy<
104126
/**
105127
* Any additional user-defined options can be added to the policy here
106128
*/
107-
options?: ExtraOptions | undefined;
129+
options?: Options | undefined;
108130
};
109131

110132
/**
111133
* An object representing a base policy.
112134
*/
113135
export type Policy<
114-
PackagePolicyItem = void,
115-
GlobalsPolicyItem = void,
116-
BuiltinsPolicyItem = void,
117-
ExtraOptions = unknown,
136+
PackagePolicyExtra = void,
137+
GlobalsPolicyExtra = void,
138+
BuiltinsPolicyExtra = void,
139+
Options = unknown,
118140
> = {
119141
/** The package policies for the resources. */
120142
resources: Record<
121143
string,
122144
PackagePolicy<
123-
PackagePolicyItem,
124-
GlobalsPolicyItem,
125-
BuiltinsPolicyItem,
126-
ExtraOptions
145+
PackagePolicyExtra,
146+
GlobalsPolicyExtra,
147+
BuiltinsPolicyExtra,
148+
Options
127149
>
128150
>;
129151
/** The default attenuator. */
130152
defaultAttenuator?: string | undefined;
131153
/** The package policy for the entry. */
132154
entry?:
133155
| PackagePolicy<
134-
PackagePolicyItem,
135-
GlobalsPolicyItem,
136-
BuiltinsPolicyItem,
137-
ExtraOptions
156+
PackagePolicyExtra,
157+
GlobalsPolicyExtra,
158+
BuiltinsPolicyExtra,
159+
Options
138160
>
139161
| undefined;
140162
};
141163

142-
/** Any {@link Policy} */
164+
/**
165+
* Any {@link Policy}
166+
*/
143167
export type SomePolicy = Policy<any, any, any, any>;
144168

145-
/** Any {@link PackagePolicy} */
146-
export type SomePackagePolicy = PackagePolicy<void, void, void, unknown>;
169+
/**
170+
* Any {@link PackagePolicy}
171+
*/
172+
export type SomePackagePolicy = PackagePolicy<any, any, any, any>;

packages/compartment-mapper/src/types/typescript.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,11 @@ export type UnionToIntersection<U> = (
7777
* Makes a nicer tooltip for `T` in IDEs (most of the time).
7878
*/
7979
export type Simplify<T> = { [K in keyof T]: T[K] } & {};
80+
81+
/**
82+
* `true` when the type parameter is a wide `any`
83+
*
84+
* `0 extends 1 & T` is true for `T = any` and false for `void` and specific literals
85+
* @see {@link https://github.com/microsoft/TypeScript/issues/30029}
86+
*/
87+
export type IsAny<T> = 0 extends 1 & T ? true : false;

packages/module-source/src/external.types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export type * from './types/visitor-passes.ts';
12
export type {
23
SourceMapObject,
34
SourceMapHook,

0 commit comments

Comments
 (0)