@final-hill/decorator-contracts 2.0.0 → 3.1.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.
- package/LICENSE +660 -660
- package/README.md +1722 -879
- package/dist/AssertionError.d.mts +28 -1
- package/dist/Contracted.d.mts +72 -10
- package/dist/admissibility.d.mts +353 -4
- package/dist/assert.d.mts +41 -4
- package/dist/decorator.d.mts +66 -4
- package/dist/demands.d.mts +88 -10
- package/dist/ensures.d.mts +104 -8
- package/dist/feature-kind.d.mts +25 -0
- package/dist/iff.d.mts +9 -4
- package/dist/implies.d.mts +9 -2
- package/dist/index.d.mts +10 -3
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/inspect.d.mts +38 -0
- package/dist/invariant.d.mts +20 -2
- package/dist/metadata.d.mts +98 -0
- package/dist/name.d.mts +70 -0
- package/dist/rescue.d.mts +94 -12
- package/dist/serialization.d.mts +63 -0
- package/dist/types.d.mts +22 -1
- package/package.json +82 -82
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renders an instance for `util.inspect`.
|
|
3
|
+
*
|
|
4
|
+
* The name is the class's declared one when it has one, and the constructor's
|
|
5
|
+
* otherwise — the same rule every violation message follows, so a class that named
|
|
6
|
+
* itself is named the same way in a debugger as in a failure. The state is read
|
|
7
|
+
* from own descriptors, so no contract runs and no accessor can throw.
|
|
8
|
+
*
|
|
9
|
+
* @param depth - How many levels of nesting `util.inspect` will show; `null` means
|
|
10
|
+
* unlimited.
|
|
11
|
+
* @param options - The options in force for this inspection.
|
|
12
|
+
* @param inspect - Renders a value with those options, used to recurse.
|
|
13
|
+
* @returns The class name with the instance's own state.
|
|
14
|
+
* @internal
|
|
15
|
+
* Installed on `Contracted.prototype` by `installInspect`; exported so a test can
|
|
16
|
+
* call it directly with the arguments `util.inspect` would pass.
|
|
17
|
+
*/
|
|
18
|
+
export declare const inspectInstance: (this: object, depth: number | null, options: object, inspect: (value: unknown, options: object) => string) => string;
|
|
19
|
+
/**
|
|
20
|
+
* Installs the rendering on a class prototype.
|
|
21
|
+
*
|
|
22
|
+
* On the prototype rather than on each instance, so a class pays nothing per
|
|
23
|
+
* instance and a subclass may override it by declaring its own method under the same
|
|
24
|
+
* key.
|
|
25
|
+
*
|
|
26
|
+
* Defined rather than assigned so the property is fixed in place: a caller cannot
|
|
27
|
+
* swap the rendering without the class knowing. `enumerable: false` states the
|
|
28
|
+
* intent — `for...in` never enumerates a symbol key either way, so it changes what a
|
|
29
|
+
* loop sees by nothing, but it is the flag a prototype method would carry.
|
|
30
|
+
*
|
|
31
|
+
* Here rather than in `Contracted.mts` so the key stays module-private: the
|
|
32
|
+
* installation is the only thing that needs it, and every other consumer derives it
|
|
33
|
+
* from `Symbol.for`.
|
|
34
|
+
*
|
|
35
|
+
* @param prototype - The prototype to install on.
|
|
36
|
+
* @internal
|
|
37
|
+
*/
|
|
38
|
+
export declare const installInspect: (prototype: object) => void;
|
package/dist/invariant.d.mts
CHANGED
|
@@ -1,10 +1,28 @@
|
|
|
1
1
|
import { Contracted } from './Contracted.mjs';
|
|
2
2
|
import { type ArmOptions } from './admissibility.mjs';
|
|
3
3
|
import type { StandardSchemaV1 } from './standard-schema.mjs';
|
|
4
|
+
/**
|
|
5
|
+
* The fields an invariant predicate is called with.
|
|
6
|
+
*
|
|
7
|
+
* An invariant describes the whole instance, so `self` is the only field. See
|
|
8
|
+
* `DemandsSubject` for why it is not `Readonly<This>`, which would hide the
|
|
9
|
+
* `protected` state an invariant is written against.
|
|
10
|
+
*
|
|
11
|
+
* A predicate rarely needs to name this type. The decorator receives the class,
|
|
12
|
+
* so `This` is inferred and `@invariant(({ self }) => ...)` reads instance state
|
|
13
|
+
* without an annotation. It is exported for code that builds an arm separately
|
|
14
|
+
* from the decorator that registers it.
|
|
15
|
+
*/
|
|
16
|
+
export interface InvariantSubject<This> {
|
|
17
|
+
readonly self: This;
|
|
18
|
+
}
|
|
4
19
|
/**
|
|
5
20
|
* An invariant predicate: a property the instance always satisfies.
|
|
21
|
+
*
|
|
22
|
+
* It takes one object rather than a positional list, so every contract in the
|
|
23
|
+
* library presents the same shape to the predicate it calls.
|
|
6
24
|
*/
|
|
7
|
-
export type InvariantPredicate<Class extends typeof Contracted> = (
|
|
25
|
+
export type InvariantPredicate<Class extends typeof Contracted> = (subject: InvariantSubject<InstanceType<Class>>) => boolean;
|
|
8
26
|
/**
|
|
9
27
|
* An invariant arm: a predicate, or a schema the instance must satisfy.
|
|
10
28
|
*
|
|
@@ -23,5 +41,5 @@ export interface RegisteredInvariant {
|
|
|
23
41
|
arm: InvariantArm<typeof Contracted>;
|
|
24
42
|
options?: ArmOptions;
|
|
25
43
|
}
|
|
26
|
-
export declare function assertInvariants<C extends typeof Contracted, T extends InstanceType<C>>(Class: C, instance: T): void;
|
|
44
|
+
export declare function assertInvariants<C extends typeof Contracted, T extends InstanceType<C>>(Class: C, instance: T, enabled?: boolean): void;
|
|
27
45
|
export declare const invariant: InvariantDecorator;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { Contracted } from './Contracted.mjs';
|
|
2
|
+
import type { RegisteredDemands } from './demands.mjs';
|
|
3
|
+
import type { RegisteredEnsures } from './ensures.mjs';
|
|
4
|
+
import type { RegisteredInvariant } from './invariant.mjs';
|
|
5
|
+
import { type RescueDeclaration } from './rescue.mjs';
|
|
6
|
+
/**
|
|
7
|
+
* The contracts a class declares, as {@link getContractMetadata} reports them.
|
|
8
|
+
*
|
|
9
|
+
* This is the shape stored on `Contracted[Symbol.metadata]`, but read through the
|
|
10
|
+
* accessor rather than the symbol so that a consumer receives copies and cannot
|
|
11
|
+
* corrupt the contracts a class is checked against.
|
|
12
|
+
*
|
|
13
|
+
* Every list is already the **effective** set: a subclass's record contains its
|
|
14
|
+
* own declarations and the inherited ones, because each decorator registers by
|
|
15
|
+
* spreading the record it inherited. A consumer therefore never has to walk the
|
|
16
|
+
* prototype chain itself.
|
|
17
|
+
*/
|
|
18
|
+
export interface ContractMetadata {
|
|
19
|
+
/**
|
|
20
|
+
* The class-level invariant arms.
|
|
21
|
+
*
|
|
22
|
+
* Unlike the feature contracts these are not keyed by feature: an invariant
|
|
23
|
+
* describes the whole instance, so every arm applies to every call.
|
|
24
|
+
*/
|
|
25
|
+
invariants: readonly RegisteredInvariant[];
|
|
26
|
+
/** Precondition arms, keyed by feature name. */
|
|
27
|
+
demands: Readonly<Record<PropertyKey, readonly RegisteredDemands[]>>;
|
|
28
|
+
/** Postcondition arms, keyed by feature name. */
|
|
29
|
+
ensures: Readonly<Record<PropertyKey, readonly RegisteredEnsures[]>>;
|
|
30
|
+
/**
|
|
31
|
+
* The rescue declaration, keyed by feature name.
|
|
32
|
+
*
|
|
33
|
+
* A map rather than a list because a feature has one handler: a second
|
|
34
|
+
* `@rescue` on the same feature replaces the first.
|
|
35
|
+
*/
|
|
36
|
+
rescue: Readonly<Record<PropertyKey, RescueDeclaration<Contracted>>>;
|
|
37
|
+
/**
|
|
38
|
+
* The class-level rescue declaration, which applies to every feature that
|
|
39
|
+
* declares none of its own.
|
|
40
|
+
*
|
|
41
|
+
* Separate from {@link rescue} because the two forms are keyed differently: a
|
|
42
|
+
* feature declaration is stored under the feature's name, and a class
|
|
43
|
+
* declaration exists once for the whole class. `undefined` when neither this
|
|
44
|
+
* class nor an ancestor declared one.
|
|
45
|
+
*/
|
|
46
|
+
classRescue: RescueDeclaration<Contracted> | undefined;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The contracts that apply to one feature, in the order they are evaluated.
|
|
50
|
+
*
|
|
51
|
+
* Each list is the effective composition for the feature: inherited arms first,
|
|
52
|
+
* then the class's own, and within one class in **reverse source order**, because
|
|
53
|
+
* decorators evaluate bottom-up. That order is the one the contract runtime uses,
|
|
54
|
+
* so an assertion built from this list describes what actually happens.
|
|
55
|
+
*/
|
|
56
|
+
export interface FeatureContracts {
|
|
57
|
+
/** The feature's precondition arms, in evaluation order. */
|
|
58
|
+
demands: readonly RegisteredDemands[];
|
|
59
|
+
/** The feature's postcondition arms, in evaluation order. */
|
|
60
|
+
ensures: readonly RegisteredEnsures[];
|
|
61
|
+
/**
|
|
62
|
+
* The feature's effective rescue declaration, absent when neither it nor its
|
|
63
|
+
* class declares one.
|
|
64
|
+
*/
|
|
65
|
+
rescue: RescueDeclaration<Contracted> | undefined;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Reports the contracts declared on a class.
|
|
69
|
+
*
|
|
70
|
+
* Use this instead of reading `Contracted[Symbol.metadata]`. The symbol is a
|
|
71
|
+
* supported read surface, but the record it holds is the one the runtime consults,
|
|
72
|
+
* so a consumer who reaches it directly can edit the contracts their own class is
|
|
73
|
+
* checked against. This returns copies.
|
|
74
|
+
*
|
|
75
|
+
* A class that declares nothing — including `Contracted` itself, and any subclass
|
|
76
|
+
* that adds no decorator — has no record at all, and reports an empty one.
|
|
77
|
+
*
|
|
78
|
+
* @param Class - The class to inspect.
|
|
79
|
+
* @returns The class's effective contracts. Stable across calls in the sense that
|
|
80
|
+
* the declared arms do not change after the class is defined.
|
|
81
|
+
*/
|
|
82
|
+
export declare function getContractMetadata<Class extends typeof Contracted>(Class: Class): ContractMetadata;
|
|
83
|
+
/**
|
|
84
|
+
* Reports the contracts that apply to one feature, in the order they are evaluated.
|
|
85
|
+
*
|
|
86
|
+
* This is what an architectural test usually wants: "every public verb declares a
|
|
87
|
+
* precondition" is a question about one feature, and the answer depends on the
|
|
88
|
+
* inherited arms as well as the class's own. Because each decorator registers by
|
|
89
|
+
* spreading the record it inherited, the effective list is already assembled — this
|
|
90
|
+
* reads it without walking the prototype chain.
|
|
91
|
+
*
|
|
92
|
+
* @param Class - The class to inspect.
|
|
93
|
+
* @param feature - The feature's name.
|
|
94
|
+
* @returns The feature's arms in evaluation order, or empty lists when it declares
|
|
95
|
+
* none. `rescue` is the declaration the runtime would actually apply: the
|
|
96
|
+
* feature's own when it declares one, otherwise the class-level declaration.
|
|
97
|
+
*/
|
|
98
|
+
export declare function getFeatureContracts<Class extends typeof Contracted>(Class: Class, feature: PropertyKey): FeatureContracts;
|
package/dist/name.d.mts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The name a class reports in a contract violation, and how it is resolved.
|
|
3
|
+
*
|
|
4
|
+
* `Class.name` is whatever the consumer's build emitted. The standard decorator
|
|
5
|
+
* lowering wraps a decorated class in an immediately invoked arrow that returns a
|
|
6
|
+
* **class expression**, which a compressor is free to leave anonymous, so the
|
|
7
|
+
* reported name degrades with the build: correct when the bundle is unminified, a
|
|
8
|
+
* short identifier when it is not, and empty when the compressor dropped the name
|
|
9
|
+
* outright.
|
|
10
|
+
*
|
|
11
|
+
* There is no earlier moment with a better name. The class object is created once,
|
|
12
|
+
* and the name a class decorator observes is the same name the violation would
|
|
13
|
+
* report later, so capturing it at decoration time cannot improve it. Only a
|
|
14
|
+
* declaration by the consumer is independent of the build.
|
|
15
|
+
*
|
|
16
|
+
* Declaring one makes the name stable no matter how the bundle is minified, which
|
|
17
|
+
* is what a production `className`, message, or `onViolation` factory needs:
|
|
18
|
+
*
|
|
19
|
+
* ```typescript
|
|
20
|
+
* import { Contracted, contractName } from '@final-hill/decorator-contracts';
|
|
21
|
+
*
|
|
22
|
+
* class Cart extends Contracted {
|
|
23
|
+
* static [contractName] = 'Cart';
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* A name is declared rather than inherited. `checkedMode` and `encapsulationMode`
|
|
28
|
+
* inherit because they answer how a class *behaves*, and a subclass that says
|
|
29
|
+
* nothing about behaviour should behave like its base. A name answers who the class
|
|
30
|
+
* *is*, so an inherited one would label every subclass's violations with the base's
|
|
31
|
+
* name.
|
|
32
|
+
*
|
|
33
|
+
* Set it **through the class**: it is read from the constructor, so a write to an
|
|
34
|
+
* instance has no effect. Unlike the enforcement switches there is nothing to
|
|
35
|
+
* refuse — a name that does not take effect removes no guarantee.
|
|
36
|
+
*
|
|
37
|
+
* Nothing here imports `Contracted`, so every contract module and the proxy can use
|
|
38
|
+
* it without an import cycle.
|
|
39
|
+
*/
|
|
40
|
+
export declare const contractName: unique symbol;
|
|
41
|
+
/**
|
|
42
|
+
* The name to report for `Class` in a contract violation.
|
|
43
|
+
*
|
|
44
|
+
* An own {@link contractName} declaration wins, so a class that declares one is
|
|
45
|
+
* named stably whatever the build did to its class name. Otherwise `Class.name` is
|
|
46
|
+
* reported, which is what the consumer's build emitted.
|
|
47
|
+
*
|
|
48
|
+
* **Own, not inherited**, for the reason {@link contractName} gives. The fallback
|
|
49
|
+
* is also what makes ownership meaningful: a subclass that declares no name reports
|
|
50
|
+
* itself rather than the base whose name it inherited.
|
|
51
|
+
*
|
|
52
|
+
* The declared value is used only when it is a non-empty string, so a declaration
|
|
53
|
+
* that resolves to `undefined` or `''` falls back to `Class.name` rather than
|
|
54
|
+
* producing an empty label. An empty name would render as
|
|
55
|
+
* `@demands failed for .withdraw`, which is the failure this symbol exists to
|
|
56
|
+
* prevent and must not be reachable through a mistyped declaration.
|
|
57
|
+
*
|
|
58
|
+
* A declaration that throws is not a name either. The lookup is wrapped for that
|
|
59
|
+
* reason: a contract failure is the outcome the caller is waiting for, and a
|
|
60
|
+
* declaration is read on the way to reporting it, so a throwing accessor must not
|
|
61
|
+
* replace the failure with its own error. The constructor name is what is left.
|
|
62
|
+
*
|
|
63
|
+
* @param Class - The class to name, passed as the constructor itself (`Cart`).
|
|
64
|
+
* `instance.constructor` is the same object and may be passed instead, but a
|
|
65
|
+
* caller that already holds the class needs no instance to name it.
|
|
66
|
+
* @internal
|
|
67
|
+
* Exported only because the contract modules and the proxy live in their own files
|
|
68
|
+
* and import it. Read {@link contractName} instead; it is the published surface.
|
|
69
|
+
*/
|
|
70
|
+
export declare const classNameOf: (Class: unknown) => string;
|
package/dist/rescue.d.mts
CHANGED
|
@@ -1,14 +1,41 @@
|
|
|
1
1
|
import { Contracted } from './Contracted.mjs';
|
|
2
2
|
import type { FeatureDecoratorContext } from './types.mjs';
|
|
3
|
+
/**
|
|
4
|
+
* The fields a rescue handler is called with.
|
|
5
|
+
*
|
|
6
|
+
* `self` is the **raw** instance. Restoring corrupted state is what a handler is
|
|
7
|
+
* for, so unlike a predicate it must be able to assign — `read-only.mts` excludes
|
|
8
|
+
* `@rescue` for that reason.
|
|
9
|
+
*
|
|
10
|
+
* `retry` is an ordinary function field, called as `retry(...)`. It is not a
|
|
11
|
+
* method of the subject, so lifting it off (`subject.retry(...)`) is unnecessary
|
|
12
|
+
* and would give it a `this` it does not want. The handler itself carries no
|
|
13
|
+
* `this: This` annotation for the same reason: the `this` context of a bare call
|
|
14
|
+
* is `void`, so a declared `this` makes every consumer-written handler fail to
|
|
15
|
+
* type-check. The subject carries the instance instead.
|
|
16
|
+
*
|
|
17
|
+
* `feature` names the feature whose invocation failed. A per-feature declaration
|
|
18
|
+
* already knows it, because the handler was written next to that feature; a
|
|
19
|
+
* class-level declaration serves every feature, so without this field a handler
|
|
20
|
+
* that translates or logs per feature has nothing to key on. The value is the
|
|
21
|
+
* property key the lifecycle was running for, so a `delete` reports the deleted
|
|
22
|
+
* property and an accessor reports the accessor's name.
|
|
23
|
+
*/
|
|
24
|
+
export interface RescueSubject<This> {
|
|
25
|
+
readonly self: This;
|
|
26
|
+
readonly error: unknown;
|
|
27
|
+
readonly args: any[];
|
|
28
|
+
readonly retry: (...args: any[]) => any;
|
|
29
|
+
readonly feature: PropertyKey;
|
|
30
|
+
}
|
|
3
31
|
/**
|
|
4
32
|
* A handler for `@rescue`.
|
|
5
33
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* `void`.
|
|
34
|
+
* It takes one object rather than a positional list, matching the other
|
|
35
|
+
* contracts. See {@link RescueSubject} for the fields and for why `retry` is a
|
|
36
|
+
* bare callable.
|
|
10
37
|
*/
|
|
11
|
-
export type RescueHandler<This extends Contracted> = (
|
|
38
|
+
export type RescueHandler<This extends Contracted> = (subject: RescueSubject<This>) => void;
|
|
12
39
|
export interface RescueOptions {
|
|
13
40
|
/**
|
|
14
41
|
* The maximum number of times `retry` may restart the lifecycle for a single
|
|
@@ -24,21 +51,76 @@ export interface RescueDeclaration<This extends Contracted> {
|
|
|
24
51
|
maxRetries?: number;
|
|
25
52
|
}
|
|
26
53
|
/**
|
|
27
|
-
* Declares a rescue handler for a feature.
|
|
54
|
+
* Declares a rescue handler for a feature, or for every feature of a class.
|
|
28
55
|
*
|
|
29
56
|
* `retry` is explicit: it passes only the arguments it is given and never
|
|
30
|
-
* replays the original arguments. Forward the `args`
|
|
31
|
-
*
|
|
57
|
+
* replays the original arguments. Forward the `args` field to retry with the
|
|
58
|
+
* same arguments (`retry(...args)`); calling `retry()` retries with none.
|
|
32
59
|
*
|
|
33
60
|
* Options are declaration level because they describe the feature rather than an
|
|
34
61
|
* individual failure. `maxRetries` bounds how many times `retry` may restart the
|
|
35
|
-
* lifecycle for one invocation; it defaults to unbounded
|
|
62
|
+
* lifecycle for one invocation; it defaults to unbounded, and it is counted per
|
|
63
|
+
* invocation of the feature the handler served rather than across the class.
|
|
64
|
+
*
|
|
65
|
+
* On a class the declaration applies to every feature that declares none of its
|
|
66
|
+
* own. Precedence is feature-level first, then the nearest class-level
|
|
67
|
+
* declaration walking up the constructor chain, so a subclass may override a
|
|
68
|
+
* class-level handler its base declared without restating it per feature.
|
|
69
|
+
*
|
|
70
|
+
* The returned decorator takes one call signature whose context is the **union**
|
|
71
|
+
* of the two positions, rather than two overloads. An overload pair cannot be
|
|
72
|
+
* selected for a decorator expression: TypeScript resolves the class position
|
|
73
|
+
* against the first signature and does not fall through, so a member-first pair
|
|
74
|
+
* is rejected on a class and a class-first pair is rejected on a member. A union
|
|
75
|
+
* is accepted in both positions and needs no ordering.
|
|
76
|
+
*
|
|
77
|
+
* `This` reaches both positions, and each supplies it differently: the member
|
|
78
|
+
* context carries it through the decorated element's own signature, and the
|
|
79
|
+
* class context through the constructor being decorated. Only the class context
|
|
80
|
+
* is named by this type; the member context is left to `FeatureDecoratorContext`,
|
|
81
|
+
* which already threads the instance through its three kind variants.
|
|
82
|
+
*
|
|
83
|
+
* The decorated element itself is typed `unknown` in both positions. Its type is
|
|
84
|
+
* the feature's signature, which no handler reads, and naming it would make it a
|
|
85
|
+
* second inference site for `This` — `typeof A` where the handler said `A` — and
|
|
86
|
+
* a decorator expression has no way to unify the two.
|
|
36
87
|
*/
|
|
37
|
-
export type RescueDecorator = <This extends Contracted>(handler: RescueHandler<This>, options?: RescueOptions) => (value: unknown, context: FeatureDecoratorContext) => void;
|
|
88
|
+
export type RescueDecorator = <This extends Contracted>(handler: RescueHandler<This>, options?: RescueOptions) => <Args extends any[]>(value: unknown, context: FeatureDecoratorContext<This, Args> | ClassDecoratorContext<abstract new (...args: any[]) => This>) => void;
|
|
38
89
|
export declare const rescue: RescueDecorator;
|
|
39
90
|
/**
|
|
40
|
-
*
|
|
41
|
-
*
|
|
91
|
+
* The `@rescue` declaration a metadata record holds for `feature`.
|
|
92
|
+
*
|
|
93
|
+
* A feature-level declaration wins over a class-level one, which is what lets a
|
|
94
|
+
* class serve a uniform handler and still let one feature state its own. A
|
|
95
|
+
* feature's own entry is an own property of the map, copied into a subclass's
|
|
96
|
+
* record by the spread a decorator performs; a class-level declaration is one
|
|
97
|
+
* property, reached through the metadata prototype chain when this class did not
|
|
98
|
+
* declare its own.
|
|
99
|
+
*
|
|
100
|
+
* A `_`-prefixed feature takes neither form: the decorator refuses one wherever it
|
|
101
|
+
* is written, because `@rescue` is the only contract a handler applies, and a
|
|
102
|
+
* lifecycle that starts for a `_` name would otherwise inherit a class-level
|
|
103
|
+
* handler for it. Leaving the name out of both answers keeps the two forms
|
|
104
|
+
* consistent, so a consumer never has a feature they can cover but not override.
|
|
105
|
+
*
|
|
106
|
+
* @internal
|
|
107
|
+
* Exported because `metadata.mts` reports the same effective declaration this
|
|
108
|
+
* module applies, so both have to answer the question one way. It is not
|
|
109
|
+
* re-exported from the package entry: it returns the live declaration, and a
|
|
110
|
+
* consumer has to read a copy through `getContractMetadata`.
|
|
111
|
+
*/
|
|
112
|
+
export declare const rescueDeclarationOf: (metadata: {
|
|
113
|
+
rescue?: Readonly<Record<PropertyKey, RescueDeclaration<Contracted>>>;
|
|
114
|
+
classRescue?: RescueDeclaration<Contracted>;
|
|
115
|
+
} | undefined, feature: PropertyKey) => RescueDeclaration<Contracted> | undefined;
|
|
116
|
+
/**
|
|
117
|
+
* Resolves the `@rescue` declaration for `propertyKey`, walking the constructor
|
|
118
|
+
* chain so that a declaration on an ancestor is inherited.
|
|
119
|
+
*
|
|
120
|
+
* The walk covers the case where a class inherited its base's metadata record by
|
|
121
|
+
* `Symbol.metadata` lookup rather than owning one, so the record it sees is the
|
|
122
|
+
* one its ancestor wrote; the first class whose record resolves a declaration
|
|
123
|
+
* supplies it.
|
|
42
124
|
*/
|
|
43
125
|
export declare function getRescueDeclaration(instance: Contracted, propertyKey: PropertyKey): RescueDeclaration<Contracted> | undefined;
|
|
44
126
|
/**
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Opt-in JSON serialization for a contracted class.
|
|
3
|
+
*
|
|
4
|
+
* https://github.com/final-hill/decorator-contracts/issues/341
|
|
5
|
+
*
|
|
6
|
+
* A contracted instance serializes its own data properties, which are the
|
|
7
|
+
* `_`-prefixed fields the library's convention marks private:
|
|
8
|
+
*
|
|
9
|
+
* ```text
|
|
10
|
+
* JSON.stringify(instance) // {"_balance":5,"_orgId":"o"}
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* That is a leak, but the fix cannot be a default `toJSON`. Which fields are
|
|
14
|
+
* "public" is a decision about the consumer's domain, and the `_` prefix is a
|
|
15
|
+
* convention the library enforces for assignment rather than a declaration of what
|
|
16
|
+
* may be published. A default would also change what `JSON.stringify` emits for
|
|
17
|
+
* every existing class, which is the kind of silent break a library cannot make.
|
|
18
|
+
*
|
|
19
|
+
* So a class opts in, naming the getters it wants published:
|
|
20
|
+
*
|
|
21
|
+
* ```typescript
|
|
22
|
+
* import { Contracted, serializable } from '@final-hill/decorator-contracts';
|
|
23
|
+
*
|
|
24
|
+
* @serializable('balance', 'orgId')
|
|
25
|
+
* class Account extends Contracted {
|
|
26
|
+
* protected _balance = 0;
|
|
27
|
+
* protected _orgId = 'o';
|
|
28
|
+
*
|
|
29
|
+
* get balance() { return this._balance; }
|
|
30
|
+
* get orgId() { return this._orgId; }
|
|
31
|
+
* }
|
|
32
|
+
*
|
|
33
|
+
* JSON.stringify(Account.new()); // {"balance":0,"orgId":"o"}
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* Naming getters rather than fields is deliberate. A field name is storage; a
|
|
37
|
+
* getter is the class's own statement of what is observable, and it is already the
|
|
38
|
+
* only way an external caller reads state the encapsulation rule protects. The
|
|
39
|
+
* published shape is therefore the shape a caller can already see, and it cannot
|
|
40
|
+
* drift from the class's public surface without the class saying so.
|
|
41
|
+
*/
|
|
42
|
+
/**
|
|
43
|
+
* Publishes the named getters through `JSON.stringify`.
|
|
44
|
+
*
|
|
45
|
+
* Every name is checked at decoration time, where the author is looking at the
|
|
46
|
+
* declaration, rather than at serialization time where the diagnostic would arrive
|
|
47
|
+
* in a logger.
|
|
48
|
+
*
|
|
49
|
+
* The method is installed on the class's **own** prototype, so a subclass that
|
|
50
|
+
* declares its own `@serializable` publishes its own shape rather than the base's.
|
|
51
|
+
* A subclass that declares nothing inherits the base's publication, which is the
|
|
52
|
+
* substitution rule: the subclass *is* a `Base`, so the shape a `Base` publishes is
|
|
53
|
+
* the shape it publishes. Contrast `contractName`, which is declared rather than
|
|
54
|
+
* inherited because a name answers who a class is, while a serialized shape answers
|
|
55
|
+
* what a class is.
|
|
56
|
+
*
|
|
57
|
+
* @param features - The getter names to publish, in the order they are emitted.
|
|
58
|
+
* @returns A class decorator.
|
|
59
|
+
* @throws {TypeError} If the target is not a `Contracted` class, if no feature is
|
|
60
|
+
* named, if a named feature is not a getter, or if the class declares its
|
|
61
|
+
* serialization twice.
|
|
62
|
+
*/
|
|
63
|
+
export declare const serializable: (...features: string[]) => (Class: unknown, context: DecoratorContext) => void;
|
package/dist/types.d.mts
CHANGED
|
@@ -4,4 +4,25 @@ declare global {
|
|
|
4
4
|
}
|
|
5
5
|
}
|
|
6
6
|
export type Constructor<T> = new (...args: any[]) => T;
|
|
7
|
-
|
|
7
|
+
/**
|
|
8
|
+
* The context a feature contract decorator is applied with.
|
|
9
|
+
*
|
|
10
|
+
* The type parameters are what make a contract predicate inferable. A standard
|
|
11
|
+
* member decorator's context carries the class it is declared on (`This`) and the
|
|
12
|
+
* decorated member's own signature, so stating them here connects the decorator's
|
|
13
|
+
* argument to the member it is applied to. Leaving them out — as the
|
|
14
|
+
* unparameterized `ClassMemberDecoratorContext` does — gives the compiler nothing
|
|
15
|
+
* to infer from, and `self` degrades to the constraint.
|
|
16
|
+
*
|
|
17
|
+
* `Result` is deliberately **not** a parameter here. A method's context carries a
|
|
18
|
+
* call signature (`() => number`) while a getter's carries the property type
|
|
19
|
+
* (`number`), and both would be inference sites for one `Result`, so the compiler
|
|
20
|
+
* resolves it to their intersection — `number & (() => number)`. An `@ensures`
|
|
21
|
+
* predicate therefore keeps `result` at `unknown` and names it with an explicit
|
|
22
|
+
* annotation when it needs more, which is what `EnsuresSubject`'s third parameter
|
|
23
|
+
* is for.
|
|
24
|
+
*
|
|
25
|
+
* `ClassAccessorDecoratorContext` is excluded because the library does not support
|
|
26
|
+
* the `accessor` keyword.
|
|
27
|
+
*/
|
|
28
|
+
export type FeatureDecoratorContext<This, Args extends any[] = any[]> = ClassMethodDecoratorContext<This, (this: This, ...args: Args) => unknown> | ClassGetterDecoratorContext<This, unknown> | ClassSetterDecoratorContext<This, unknown>;
|
package/package.json
CHANGED
|
@@ -1,82 +1,82 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@final-hill/decorator-contracts",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Code Contracts for TypeScript and ECMAScript classes",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"exports": {
|
|
7
|
-
".": {
|
|
8
|
-
"types": "./dist/index.d.mts",
|
|
9
|
-
"import": "./dist/index.mjs"
|
|
10
|
-
}
|
|
11
|
-
},
|
|
12
|
-
"types": "./dist/index.d.mts",
|
|
13
|
-
"main": "./dist/index.mjs",
|
|
14
|
-
"scripts": {
|
|
15
|
-
"prebuild": "npm run lint",
|
|
16
|
-
"build": "webpack",
|
|
17
|
-
"lint": "eslint src
|
|
18
|
-
"lint:fix": "eslint src
|
|
19
|
-
"pretest": "npm run build && npm run typecheck",
|
|
20
|
-
"test": "globstar -- node --import tsx --test ./src/tests/*.test.mts",
|
|
21
|
-
"pretest:coverage": "npm run build && node -e \"require('node:fs').mkdirSync('coverage',{recursive:true})\"",
|
|
22
|
-
"test:coverage": "globstar -- node --import tsx --test --experimental-test-coverage --test-reporter=spec --test-reporter-destination=stdout --test-reporter=lcov --test-reporter-destination=coverage/lcov.info ./src/tests/*.test.mts",
|
|
23
|
-
"typecheck": "tsc --noEmit -p tsconfig.json && tsc -p tsconfig.test.json"
|
|
24
|
-
},
|
|
25
|
-
"homepage": "https://github.com/final-hill/decorator-contracts#readme",
|
|
26
|
-
"repository": {
|
|
27
|
-
"type": "git",
|
|
28
|
-
"url": "git+https://github.com/final-hill/decorator-contracts.git"
|
|
29
|
-
},
|
|
30
|
-
"bugs": {
|
|
31
|
-
"url": "https://github.com/final-hill/decorator-contracts/issues"
|
|
32
|
-
},
|
|
33
|
-
"keywords": [
|
|
34
|
-
"assertion",
|
|
35
|
-
"biconditional",
|
|
36
|
-
"behavioral-subtyping",
|
|
37
|
-
"code-contracts",
|
|
38
|
-
"decorators",
|
|
39
|
-
"demands",
|
|
40
|
-
"design-by-contract",
|
|
41
|
-
"encapsulation",
|
|
42
|
-
"exception-handling",
|
|
43
|
-
"if-and-only-if",
|
|
44
|
-
"invariant",
|
|
45
|
-
"liskov-substitution-principle",
|
|
46
|
-
"LSP",
|
|
47
|
-
"material-implication",
|
|
48
|
-
"organized-panic",
|
|
49
|
-
"precondition",
|
|
50
|
-
"postcondition",
|
|
51
|
-
"rescue",
|
|
52
|
-
"requires",
|
|
53
|
-
"SOLID"
|
|
54
|
-
],
|
|
55
|
-
"engines": {
|
|
56
|
-
"node": ">=26"
|
|
57
|
-
},
|
|
58
|
-
"author": "Michael L Haufe <michael.haufe@final-hill.com> (https://final-hill.com)",
|
|
59
|
-
"license": "AGPL-3.0-only",
|
|
60
|
-
"devDependencies": {
|
|
61
|
-
"@eslint/js": "^10.0.1",
|
|
62
|
-
"@eslint/json": "^2.1.1",
|
|
63
|
-
"@eslint/markdown": "^9.0.0",
|
|
64
|
-
"@types/node": "^26.6.6",
|
|
65
|
-
"eslint": "^10.15.0",
|
|
66
|
-
"eslint-webpack-plugin": "^6.0.0",
|
|
67
|
-
"globals": "^17.14.0",
|
|
68
|
-
"globstar": "^1.0.0",
|
|
69
|
-
"ts-loader": "^9.6.2",
|
|
70
|
-
"tsx": "^4.23.15",
|
|
71
|
-
"typescript": "^6.0.3",
|
|
72
|
-
"typescript-eslint": "^8.73.0",
|
|
73
|
-
"webpack": "^5.113.0",
|
|
74
|
-
"webpack-cli": "^7.3.1"
|
|
75
|
-
},
|
|
76
|
-
"files": [
|
|
77
|
-
"dist"
|
|
78
|
-
],
|
|
79
|
-
"overrides": {
|
|
80
|
-
"katex": "^0.19.1"
|
|
81
|
-
}
|
|
82
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@final-hill/decorator-contracts",
|
|
3
|
+
"version": "3.1.0",
|
|
4
|
+
"description": "Code Contracts for TypeScript and ECMAScript classes",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.mts",
|
|
9
|
+
"import": "./dist/index.mjs"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"types": "./dist/index.d.mts",
|
|
13
|
+
"main": "./dist/index.mjs",
|
|
14
|
+
"scripts": {
|
|
15
|
+
"prebuild": "npm run lint",
|
|
16
|
+
"build": "webpack",
|
|
17
|
+
"lint": "eslint src '**/*.md' eslint.config.js webpack.config.mjs --ignore-pattern node_modules --ignore-pattern dist --max-warnings 0",
|
|
18
|
+
"lint:fix": "eslint src '**/*.md' eslint.config.js webpack.config.mjs --ignore-pattern node_modules --ignore-pattern dist --fix",
|
|
19
|
+
"pretest": "npm run build && npm run typecheck",
|
|
20
|
+
"test": "globstar -- node --import tsx --test ./src/tests/*.test.mts",
|
|
21
|
+
"pretest:coverage": "npm run build && node -e \"require('node:fs').mkdirSync('coverage',{recursive:true})\"",
|
|
22
|
+
"test:coverage": "globstar -- node --import tsx --test --experimental-test-coverage --test-reporter=spec --test-reporter-destination=stdout --test-reporter=lcov --test-reporter-destination=coverage/lcov.info ./src/tests/*.test.mts",
|
|
23
|
+
"typecheck": "tsc --noEmit -p tsconfig.json && tsc -p tsconfig.test.json"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/final-hill/decorator-contracts#readme",
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/final-hill/decorator-contracts.git"
|
|
29
|
+
},
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/final-hill/decorator-contracts/issues"
|
|
32
|
+
},
|
|
33
|
+
"keywords": [
|
|
34
|
+
"assertion",
|
|
35
|
+
"biconditional",
|
|
36
|
+
"behavioral-subtyping",
|
|
37
|
+
"code-contracts",
|
|
38
|
+
"decorators",
|
|
39
|
+
"demands",
|
|
40
|
+
"design-by-contract",
|
|
41
|
+
"encapsulation",
|
|
42
|
+
"exception-handling",
|
|
43
|
+
"if-and-only-if",
|
|
44
|
+
"invariant",
|
|
45
|
+
"liskov-substitution-principle",
|
|
46
|
+
"LSP",
|
|
47
|
+
"material-implication",
|
|
48
|
+
"organized-panic",
|
|
49
|
+
"precondition",
|
|
50
|
+
"postcondition",
|
|
51
|
+
"rescue",
|
|
52
|
+
"requires",
|
|
53
|
+
"SOLID"
|
|
54
|
+
],
|
|
55
|
+
"engines": {
|
|
56
|
+
"node": ">=26"
|
|
57
|
+
},
|
|
58
|
+
"author": "Michael L Haufe <michael.haufe@final-hill.com> (https://final-hill.com)",
|
|
59
|
+
"license": "AGPL-3.0-only",
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"@eslint/js": "^10.0.1",
|
|
62
|
+
"@eslint/json": "^2.1.1",
|
|
63
|
+
"@eslint/markdown": "^9.0.0",
|
|
64
|
+
"@types/node": "^26.6.6",
|
|
65
|
+
"eslint": "^10.15.0",
|
|
66
|
+
"eslint-webpack-plugin": "^6.0.0",
|
|
67
|
+
"globals": "^17.14.0",
|
|
68
|
+
"globstar": "^1.0.0",
|
|
69
|
+
"ts-loader": "^9.6.2",
|
|
70
|
+
"tsx": "^4.23.15",
|
|
71
|
+
"typescript": "^6.0.3",
|
|
72
|
+
"typescript-eslint": "^8.73.0",
|
|
73
|
+
"webpack": "^5.113.0",
|
|
74
|
+
"webpack-cli": "^7.3.1"
|
|
75
|
+
},
|
|
76
|
+
"files": [
|
|
77
|
+
"dist"
|
|
78
|
+
],
|
|
79
|
+
"overrides": {
|
|
80
|
+
"katex": "^0.19.1"
|
|
81
|
+
}
|
|
82
|
+
}
|