66 */
77
88import 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[]];
3132export type AttenuationDefinition =
3233 | FullAttenuationDefinition
3334 | ImplicitAttenuationDefinition ;
35+
36+ /**
37+ * Information about the attenuator implementation
38+ */
3439export 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 */
7193export 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 */
113135export 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+ */
143167export 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 > ;
0 commit comments