@yearofthedan/warp 0.3.0 → 0.4.1
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/README.md +20 -22
- package/dist/app/check.d.ts +3 -1
- package/dist/app/check.js +3 -2
- package/dist/app/configLines.d.ts +5 -0
- package/dist/app/configLines.js +24 -0
- package/dist/app/load.d.ts +2 -2
- package/dist/app/load.js +32 -44
- package/dist/app/unclassified.js +1 -1
- package/dist/builtins/advice.js +9 -9
- package/dist/builtins/dependency-direction/dependencyDirection.js +7 -2
- package/dist/builtins/module-externals/moduleExternals.js +3 -2
- package/dist/builtins/unused-declaration/empty.js +11 -10
- package/dist/builtins/unused-declaration/grants.js +39 -11
- package/dist/builtins/unused-declaration/surfaces.js +16 -6
- package/dist/cli.js +37 -17
- package/dist/config/loadConfig.d.ts +8 -2
- package/dist/config/loadConfig.js +107 -14
- package/dist/git/cache.js +5 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/model/archetype/data.d.ts +14 -23
- package/dist/model/archetype/data.js +1 -1
- package/dist/model/archetype/fixes.d.ts +5 -7
- package/dist/model/archetype/fixes.js +132 -90
- package/dist/model/archetype/language.d.ts +16 -0
- package/dist/model/archetype/language.js +432 -0
- package/dist/model/archetype/reach.d.ts +8 -3
- package/dist/model/archetype/reach.js +122 -56
- package/dist/model/archetype/references.d.ts +11 -5
- package/dist/model/archetype/references.js +59 -118
- package/dist/model/archetype/requirements.d.ts +3 -3
- package/dist/model/archetype/requirements.js +75 -58
- package/dist/model/archetype/resolve.d.ts +1 -0
- package/dist/model/archetype/resolve.js +26 -56
- package/dist/model/archetype/rulesets.d.ts +2 -1
- package/dist/model/archetype/rulesets.js +74 -43
- package/dist/model/archetype/settings.d.ts +1 -0
- package/dist/model/archetype/settings.js +5 -0
- package/dist/reporters/model.js +4 -3
- package/dist/rules/registry.js +2 -2
- package/package.json +4 -2
- package/dist/model/archetype/builder.d.ts +0 -183
- package/dist/model/archetype/builder.js +0 -78
- package/dist/model/archetype/draft.d.ts +0 -30
- package/dist/model/archetype/draft.js +0 -264
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
import type { IdentifyData } from './data.ts';
|
|
2
|
-
declare const referenceBrand: unique symbol;
|
|
3
|
-
declare const packageBrand: unique symbol;
|
|
4
|
-
declare const shapeBrand: unique symbol;
|
|
5
|
-
declare const selectorBrand: unique symbol;
|
|
6
|
-
declare const selfBrand: unique symbol;
|
|
7
|
-
declare const exposesType: unique symbol;
|
|
8
|
-
declare const usingType: unique symbol;
|
|
9
|
-
declare const modulesType: unique symbol;
|
|
10
|
-
declare const allBrand: unique symbol;
|
|
11
|
-
declare const handleBrand: unique symbol;
|
|
12
|
-
declare const rulesetBrand: unique symbol;
|
|
13
|
-
export interface Reference {
|
|
14
|
-
readonly [referenceBrand]: true;
|
|
15
|
-
}
|
|
16
|
-
export interface Package {
|
|
17
|
-
readonly [packageBrand]: true;
|
|
18
|
-
}
|
|
19
|
-
export interface Shape {
|
|
20
|
-
readonly [shapeBrand]: 'dir' | 'files';
|
|
21
|
-
}
|
|
22
|
-
export interface Selector<K extends 'dir' | 'files' | 'mirror' = 'dir' | 'files'> {
|
|
23
|
-
readonly [selectorBrand]: K;
|
|
24
|
-
}
|
|
25
|
-
export interface Self {
|
|
26
|
-
readonly [selfBrand]: true;
|
|
27
|
-
}
|
|
28
|
-
export interface ModuleHandle {
|
|
29
|
-
readonly [handleBrand]: true;
|
|
30
|
-
}
|
|
31
|
-
export type RuleSettings = 'off' | Record<string, boolean | object>;
|
|
32
|
-
export interface All {
|
|
33
|
-
readonly [allBrand]: true;
|
|
34
|
-
}
|
|
35
|
-
export interface Archetype<N extends string | undefined = string, E = unknown, P = unknown, H = unknown> {
|
|
36
|
-
readonly name: N;
|
|
37
|
-
readonly [exposesType]?: E;
|
|
38
|
-
readonly [usingType]?: (s: P) => void;
|
|
39
|
-
readonly [modulesType]?: H;
|
|
40
|
-
}
|
|
41
|
-
export type AnyArchetype = Archetype<any, any, any, any>;
|
|
42
|
-
export type SurfaceRefs<E> = Reference & {
|
|
43
|
-
readonly [K in keyof E]: Reference;
|
|
44
|
-
};
|
|
45
|
-
type AnySurface = Reference & {
|
|
46
|
-
readonly [surface: string]: Reference;
|
|
47
|
-
};
|
|
48
|
-
type RequiredRef<A> = A extends Archetype<string, infer E> ? SurfaceRefs<E> : Reference;
|
|
49
|
-
export interface UsingContext {
|
|
50
|
-
readonly required: Readonly<Record<string, AnySurface>>;
|
|
51
|
-
}
|
|
52
|
-
export type Using<P> = readonly (Reference | Package)[] | ((m: P, context: UsingContext) => readonly (Reference | Package)[]);
|
|
53
|
-
type Surfaces = Record<string, Reference | readonly Reference[]>;
|
|
54
|
-
export type Exposed = All | Selector<'files'>;
|
|
55
|
-
type NewName<K extends string, C> = K extends keyof C ? `module '${K}' is already declared` : K;
|
|
56
|
-
type With<C, K extends string, T> = C & {
|
|
57
|
-
readonly [k in K]: T;
|
|
58
|
-
};
|
|
59
|
-
type HandleOf<N, H> = N extends string ? ModuleHandle : {} extends H ? ModuleHandle : ModuleHandle & {
|
|
60
|
-
readonly modules: H;
|
|
61
|
-
};
|
|
62
|
-
export interface Modules<C, H> {
|
|
63
|
-
module<const K extends string>(name: NewName<K, C>, selector: Selector<'files'>, archetype?: Archetype<undefined, {}, C, {}>): Modules<With<C, K, Reference>, With<H, K, ModuleHandle>>;
|
|
64
|
-
module<const K extends string>(name: NewName<K, C>, selector: Selector<'dir'>, archetype: Self): Modules<With<C, K, AnySurface>, With<H, K, ModuleHandle>>;
|
|
65
|
-
module<const K extends string, N extends string | undefined = undefined, E = {}, H2 = {}>(name: NewName<K, C>, selector: Selector<'dir'>, archetype?: Archetype<N, E, C, H2>): Modules<With<C, K, SurfaceRefs<E>>, With<H, K, HandleOf<N, H2>>>;
|
|
66
|
-
testModule<const K extends string>(name: NewName<K, C>, selector: Selector<'files' | 'dir' | 'mirror'>, archetype?: Archetype<undefined, {}, C, {}>): Modules<With<C, K, Reference>, With<H, K, ModuleHandle>>;
|
|
67
|
-
}
|
|
68
|
-
export type TestUsing<C, R> = readonly (Reference | Package)[] | ((m: C, context: {
|
|
69
|
-
readonly required: R;
|
|
70
|
-
}) => readonly (Reference | Package)[]);
|
|
71
|
-
export interface ContainsContext<R> {
|
|
72
|
-
required: R;
|
|
73
|
-
self: Self;
|
|
74
|
-
}
|
|
75
|
-
export type Identify = IdentifyData;
|
|
76
|
-
export type ArchetypeBuilder<N extends string | undefined, C, H, R, E, P = unknown> = ArchetypeMethods<N, C, H, R, E, P> & {
|
|
77
|
-
readonly modules: H;
|
|
78
|
-
};
|
|
79
|
-
export interface ArchetypeMethods<N extends string | undefined, C, H, R, E, P> extends Archetype<N, E, P, H> {
|
|
80
|
-
identify(identify: Identify): ArchetypeBuilder<N, C, H, R, E, P>;
|
|
81
|
-
requires<const R2 extends Record<string, AnyArchetype | Shape>>(requires: R2): ArchetypeBuilder<N, C, H, {
|
|
82
|
-
readonly [K in keyof R2]: RequiredRef<R2[K]>;
|
|
83
|
-
}, E, P>;
|
|
84
|
-
contains<C2, H2>(modules: (m: Modules<{}, {}>, context: ContainsContext<R>) => Modules<C2, H2>): ArchetypeBuilder<N, C2, H2, R, E, P>;
|
|
85
|
-
exposes<const E2 extends Surfaces>(exposes: (s: C) => E2): ArchetypeBuilder<N, C, H, R, E2, P>;
|
|
86
|
-
exposes(exposes: Exposed): ArchetypeBuilder<N, C, H, R, {}, P>;
|
|
87
|
-
entry(entry: Selector<'files'>): ArchetypeBuilder<N, C, H, R, {}, P>;
|
|
88
|
-
provides(provides: (s: C) => Record<string, Reference>): ArchetypeBuilder<N, C, H, R, E, P>;
|
|
89
|
-
description(description: string): ArchetypeBuilder<N, C, H, R, E, P>;
|
|
90
|
-
testUsing(using: TestUsing<C, R>): ArchetypeBuilder<N, C, H, R, E, P>;
|
|
91
|
-
using<P2 = any>(using: Using<P2>): ArchetypeBuilder<N, C, H, R, E, P2>;
|
|
92
|
-
}
|
|
93
|
-
export interface Ruleset {
|
|
94
|
-
readonly [rulesetBrand]: true;
|
|
95
|
-
rules(settings: RuleSettings): Ruleset;
|
|
96
|
-
on(target: Archetype<string, any, any, any> | ModuleHandle, settings: RuleSettings): Ruleset;
|
|
97
|
-
extends(base: Ruleset): Ruleset;
|
|
98
|
-
}
|
|
99
|
-
export interface WarpConfig {
|
|
100
|
-
root: Archetype<string, any, any, any>;
|
|
101
|
-
tsconfig?: string | readonly string[];
|
|
102
|
-
plugins?: Record<string, unknown>;
|
|
103
|
-
rulesets?: Record<string, Ruleset>;
|
|
104
|
-
}
|
|
105
|
-
export type RecordedRef = {
|
|
106
|
-
kind: 'module';
|
|
107
|
-
module: string;
|
|
108
|
-
surface?: string;
|
|
109
|
-
} | {
|
|
110
|
-
kind: 'required';
|
|
111
|
-
name: string;
|
|
112
|
-
surface?: string;
|
|
113
|
-
} | {
|
|
114
|
-
kind: 'package';
|
|
115
|
-
pattern: string;
|
|
116
|
-
};
|
|
117
|
-
export interface RecordedSelector {
|
|
118
|
-
kind: 'dir' | 'files' | 'mirror';
|
|
119
|
-
patterns: readonly unknown[];
|
|
120
|
-
}
|
|
121
|
-
export interface RecordedModule {
|
|
122
|
-
name: unknown;
|
|
123
|
-
selector: unknown;
|
|
124
|
-
archetype?: unknown;
|
|
125
|
-
extra: readonly unknown[];
|
|
126
|
-
test?: true;
|
|
127
|
-
}
|
|
128
|
-
export interface RecordedDefinition {
|
|
129
|
-
name: unknown;
|
|
130
|
-
identify?: unknown;
|
|
131
|
-
requires: unknown;
|
|
132
|
-
modules: unknown;
|
|
133
|
-
exposes?: unknown;
|
|
134
|
-
entry?: unknown;
|
|
135
|
-
provides?: unknown;
|
|
136
|
-
description?: unknown;
|
|
137
|
-
testUsing?: unknown;
|
|
138
|
-
}
|
|
139
|
-
export interface RecordedArchetype {
|
|
140
|
-
definition: RecordedDefinition;
|
|
141
|
-
using?: unknown;
|
|
142
|
-
}
|
|
143
|
-
export interface RecordedRuleset {
|
|
144
|
-
base?: unknown;
|
|
145
|
-
entries: readonly {
|
|
146
|
-
target?: unknown;
|
|
147
|
-
settings: unknown;
|
|
148
|
-
}[];
|
|
149
|
-
}
|
|
150
|
-
interface Recorded {
|
|
151
|
-
ref: RecordedRef;
|
|
152
|
-
self: true;
|
|
153
|
-
all: true;
|
|
154
|
-
shape: 'dir' | 'files';
|
|
155
|
-
selector: RecordedSelector;
|
|
156
|
-
modules: readonly RecordedModule[];
|
|
157
|
-
archetype: RecordedArchetype;
|
|
158
|
-
handle: {
|
|
159
|
-
archetype: unknown;
|
|
160
|
-
module: unknown;
|
|
161
|
-
};
|
|
162
|
-
ruleset: RecordedRuleset;
|
|
163
|
-
}
|
|
164
|
-
export declare const recordedAs: <T extends keyof Recorded>(value: unknown, type: T) => Recorded[T] | undefined;
|
|
165
|
-
export declare const moduleRefs: () => unknown;
|
|
166
|
-
export declare const requiredRefs: () => unknown;
|
|
167
|
-
export declare const self: Self;
|
|
168
|
-
export declare const inlineName: (holder: string, module: string) => string;
|
|
169
|
-
export declare function archetype(): ArchetypeBuilder<undefined, {}, {}, {}, {}>;
|
|
170
|
-
export declare function archetype<const N extends string>(name: N): ArchetypeBuilder<N, {}, {}, {}, {}>;
|
|
171
|
-
export declare const using: <P = any>(using: Using<P>) => ArchetypeBuilder<undefined, {}, {}, {}, {}, P>;
|
|
172
|
-
export declare const exposes: (exposes: Exposed) => ArchetypeBuilder<undefined, {}, {}, {}, {}>;
|
|
173
|
-
export declare const contains: <C2, H2>(modules: (m: Modules<{}, {}>, context: ContainsContext<{}>) => Modules<C2, H2>) => ArchetypeBuilder<undefined, C2, H2, {}, {}>;
|
|
174
|
-
export declare const all: () => All;
|
|
175
|
-
export declare function dir(): Shape;
|
|
176
|
-
export declare function dir(...paths: [string, ...string[]]): Selector<'dir'>;
|
|
177
|
-
export declare function files(): Shape;
|
|
178
|
-
export declare function files(...patterns: [string, ...string[]]): Selector<'files'>;
|
|
179
|
-
export declare const mirror: (from: string, to: string) => Selector<'mirror'>;
|
|
180
|
-
export declare const pkg: (pattern: string) => Package;
|
|
181
|
-
export declare const defineConfig: <const C extends WarpConfig>(config: C) => C;
|
|
182
|
-
export declare const ruleset: () => Ruleset;
|
|
183
|
-
export {};
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { ConfigError } from '../configError.js';
|
|
2
|
-
const recorded = Symbol('warp.recorded');
|
|
3
|
-
const record = (type, data) => ({ [recorded]: { type, data } });
|
|
4
|
-
export const recordedAs = (value, type) => {
|
|
5
|
-
const entry = value?.[recorded];
|
|
6
|
-
return entry?.type === type ? entry.data : undefined;
|
|
7
|
-
};
|
|
8
|
-
const ref = (data) => Object.freeze(record('ref', data));
|
|
9
|
-
const surfaces = (base) => new Proxy(ref(base), {
|
|
10
|
-
get: (target, key) => (typeof key === 'string' ? ref({ ...base, surface: key }) : Reflect.get(target, key)),
|
|
11
|
-
});
|
|
12
|
-
const names = (make) => new Proxy({}, { get: (_, key) => surfaces(make(String(key))) });
|
|
13
|
-
export const moduleRefs = () => names((module) => ({ kind: 'module', module }));
|
|
14
|
-
export const requiredRefs = () => names((name) => ({ kind: 'required', name }));
|
|
15
|
-
export const self = Object.freeze(record('self', true));
|
|
16
|
-
const modules = (entries) => ({
|
|
17
|
-
...record('modules', entries),
|
|
18
|
-
module: (name, selector, archetype, ...extra) => modules([...entries, { name, selector, archetype, extra }]),
|
|
19
|
-
testModule: (name, selector, archetype, ...extra) => modules([...entries, { name, selector, archetype, extra, test: true }]),
|
|
20
|
-
});
|
|
21
|
-
export const inlineName = (holder, module) => `${holder}.${module}`;
|
|
22
|
-
const handles = (holder, entries) => Object.fromEntries((Array.isArray(entries) ? entries : [])
|
|
23
|
-
.filter((entry) => typeof entry.name === 'string')
|
|
24
|
-
.map((entry) => {
|
|
25
|
-
const handle = record('handle', { archetype: holder, module: entry.name });
|
|
26
|
-
const anonymous = recordedAs(entry.archetype, 'archetype')?.definition;
|
|
27
|
-
const nested = anonymous && anonymous.name === undefined && !entry.test
|
|
28
|
-
? { modules: handles(inlineName(String(holder), entry.name), anonymous.modules) }
|
|
29
|
-
: {};
|
|
30
|
-
return [entry.name, Object.freeze({ ...handle, ...nested })];
|
|
31
|
-
}));
|
|
32
|
-
const builder = (state) => {
|
|
33
|
-
const { definition } = state;
|
|
34
|
-
const define = (field) => (value) => builder({ ...state, definition: { ...definition, [field]: value } });
|
|
35
|
-
return {
|
|
36
|
-
modules: handles(definition.name, definition.modules),
|
|
37
|
-
name: definition.name,
|
|
38
|
-
...record('archetype', state),
|
|
39
|
-
identify: define('identify'),
|
|
40
|
-
requires: define('requires'),
|
|
41
|
-
contains: (contains) => define('modules')(recordedAs(contains(modules([]), { required: requiredRefs(), self }), 'modules')),
|
|
42
|
-
exposes: define('exposes'),
|
|
43
|
-
entry: define('entry'),
|
|
44
|
-
provides: define('provides'),
|
|
45
|
-
description: define('description'),
|
|
46
|
-
testUsing: define('testUsing'),
|
|
47
|
-
using: (using) => builder({ ...state, using }),
|
|
48
|
-
};
|
|
49
|
-
};
|
|
50
|
-
const start = (name) => builder({ definition: { name, requires: {}, modules: [] } });
|
|
51
|
-
export function archetype(name) {
|
|
52
|
-
return start(name);
|
|
53
|
-
}
|
|
54
|
-
export const using = (using) => start(undefined).using(using);
|
|
55
|
-
export const exposes = (exposes) => start(undefined).exposes(exposes);
|
|
56
|
-
export const contains = (modules) => start(undefined).contains(modules);
|
|
57
|
-
export const all = () => Object.freeze(record('all', true));
|
|
58
|
-
const selector = (kind, patterns) => Object.freeze(patterns.length === 0 ? record('shape', kind) : record('selector', { kind, patterns }));
|
|
59
|
-
export function dir(...paths) {
|
|
60
|
-
return selector('dir', paths);
|
|
61
|
-
}
|
|
62
|
-
export function files(...patterns) {
|
|
63
|
-
return selector('files', patterns);
|
|
64
|
-
}
|
|
65
|
-
export const mirror = (from, to) => Object.freeze(record('selector', { kind: 'mirror', patterns: [from, to] }));
|
|
66
|
-
export const pkg = (pattern) => ref({ kind: 'package', pattern });
|
|
67
|
-
export const defineConfig = (config) => config;
|
|
68
|
-
const rulesetOf = (state) => ({
|
|
69
|
-
...record('ruleset', state),
|
|
70
|
-
rules: (settings) => rulesetOf({ ...state, entries: [...state.entries, { settings }] }),
|
|
71
|
-
on: (target, settings) => rulesetOf({ ...state, entries: [...state.entries, { target, settings }] }),
|
|
72
|
-
extends: (base) => {
|
|
73
|
-
if (state.base !== undefined)
|
|
74
|
-
throw new ConfigError('ruleset().extends: a ruleset extends one base');
|
|
75
|
-
return rulesetOf({ ...state, base });
|
|
76
|
-
},
|
|
77
|
-
});
|
|
78
|
-
export const ruleset = () => rulesetOf({ entries: [] });
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { type RecordedDefinition } from './builder.ts';
|
|
2
|
-
import type { ArchetypeData, ModuleData } from './data.ts';
|
|
3
|
-
import { type Fail } from './shared.ts';
|
|
4
|
-
export interface DraftModule {
|
|
5
|
-
data: ModuleData;
|
|
6
|
-
using: unknown[];
|
|
7
|
-
}
|
|
8
|
-
export type DraftExposes = {
|
|
9
|
-
kind: 'all';
|
|
10
|
-
} | {
|
|
11
|
-
kind: 'files';
|
|
12
|
-
patterns: string[];
|
|
13
|
-
entry?: true;
|
|
14
|
-
} | {
|
|
15
|
-
kind: 'surfaces';
|
|
16
|
-
surfaces: Record<string, unknown[]>;
|
|
17
|
-
};
|
|
18
|
-
export interface Draft {
|
|
19
|
-
data: ArchetypeData;
|
|
20
|
-
modules: DraftModule[];
|
|
21
|
-
exposes?: DraftExposes;
|
|
22
|
-
provides: Record<string, unknown>;
|
|
23
|
-
testUsing: unknown[];
|
|
24
|
-
}
|
|
25
|
-
export interface Collected extends RecordedDefinition {
|
|
26
|
-
inline?: true;
|
|
27
|
-
using?: unknown;
|
|
28
|
-
}
|
|
29
|
-
export declare const collect: (root: RecordedDefinition, fail: Fail) => Map<string, Collected>;
|
|
30
|
-
export declare const draft: (name: string, state: Collected, fail: Fail) => Draft;
|
|
@@ -1,264 +0,0 @@
|
|
|
1
|
-
import { inlineName, moduleRefs, requiredRefs, recordedAs, self, } from './builder.js';
|
|
2
|
-
import { isName, isRecord } from './shared.js';
|
|
3
|
-
export const collect = (root, fail) => {
|
|
4
|
-
const byName = new Map();
|
|
5
|
-
const pending = [root];
|
|
6
|
-
for (let definition = pending.shift(); definition; definition = pending.shift()) {
|
|
7
|
-
const known = byName.get(definition.name);
|
|
8
|
-
if (!isName(definition.name))
|
|
9
|
-
fail(`archetype(${JSON.stringify(definition.name)}): a name is a non-empty string`);
|
|
10
|
-
else if (known && known !== definition)
|
|
11
|
-
fail(`two archetypes are named ${definition.name}: give each its own name`);
|
|
12
|
-
else if (!known) {
|
|
13
|
-
byName.set(definition.name, definition);
|
|
14
|
-
pending.push(...reached(definition));
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
return byName;
|
|
18
|
-
};
|
|
19
|
-
const named = (value) => {
|
|
20
|
-
const definition = recordedAs(value, 'archetype')?.definition;
|
|
21
|
-
return definition && definition.name !== undefined ? [definition] : [];
|
|
22
|
-
};
|
|
23
|
-
const reached = (definition) => [
|
|
24
|
-
...entriesOf(definition.modules).flatMap((entry) => (entry.test ? [] : named(entry.archetype))),
|
|
25
|
-
...Object.values(isRecord(definition.requires) ? definition.requires : {}).flatMap(named),
|
|
26
|
-
...inlineArchetypes(definition),
|
|
27
|
-
];
|
|
28
|
-
const anonymousOf = (entry) => {
|
|
29
|
-
const definition = recordedAs(entry.archetype, 'archetype')?.definition;
|
|
30
|
-
return definition?.name === undefined ? definition : undefined;
|
|
31
|
-
};
|
|
32
|
-
const inlineArchetypes = (holder) => isName(holder.name)
|
|
33
|
-
? entriesOf(holder.modules).flatMap((entry) => isInline(entry) && isName(entry.name)
|
|
34
|
-
? [
|
|
35
|
-
{
|
|
36
|
-
...anonymousOf(entry),
|
|
37
|
-
name: inlineName(holder.name, entry.name),
|
|
38
|
-
inline: true,
|
|
39
|
-
requires: holder.requires,
|
|
40
|
-
modules: anonymousOf(entry)?.modules ?? [],
|
|
41
|
-
description: undefined,
|
|
42
|
-
},
|
|
43
|
-
]
|
|
44
|
-
: [])
|
|
45
|
-
: [];
|
|
46
|
-
const isInline = (entry) => !entry.test &&
|
|
47
|
-
(entry.archetype === undefined || anonymousOf(entry) !== undefined) &&
|
|
48
|
-
recordedAs(entry.selector, 'selector')?.kind === 'dir';
|
|
49
|
-
const entriesOf = (modules) => (Array.isArray(modules) ? modules : []);
|
|
50
|
-
const callback = (value) => (typeof value === 'function' ? value(moduleRefs()) : undefined);
|
|
51
|
-
export const draft = (name, state, fail) => {
|
|
52
|
-
checkFields(name, state, fail);
|
|
53
|
-
const identify = identifyData(name, state.identify, fail);
|
|
54
|
-
const requires = requirements(name, state.requires, fail);
|
|
55
|
-
const modules = draftModules(name, state.modules, fail);
|
|
56
|
-
const exposes = surfaceOf(name, state, fail);
|
|
57
|
-
const provides = state.provides === undefined ? {} : callback(state.provides);
|
|
58
|
-
if (!isRecord(provides))
|
|
59
|
-
fail(`${name}.provides: expected a callback returning modules by name, (m) => ({ strings: m.strings })`);
|
|
60
|
-
const testUsing = usingOf(`${name}.testUsing`, state.testUsing, fail);
|
|
61
|
-
return {
|
|
62
|
-
data: {
|
|
63
|
-
name,
|
|
64
|
-
...(state.inline ? { inline: true } : {}),
|
|
65
|
-
description: state.description,
|
|
66
|
-
identify,
|
|
67
|
-
requires,
|
|
68
|
-
modules: modules.map((module) => module.data),
|
|
69
|
-
provides: {},
|
|
70
|
-
},
|
|
71
|
-
modules,
|
|
72
|
-
...(exposes ? { exposes } : {}),
|
|
73
|
-
provides: isRecord(provides) ? provides : {},
|
|
74
|
-
testUsing,
|
|
75
|
-
};
|
|
76
|
-
};
|
|
77
|
-
const surfaceOf = (name, state, fail) => {
|
|
78
|
-
if (state.entry === undefined)
|
|
79
|
-
return exposesOf(`${name}.exposes`, state.exposes, true, fail);
|
|
80
|
-
if (state.exposes !== undefined)
|
|
81
|
-
fail(`${name}: an entry is the module's whole surface; give it .entry(...) or .exposes(...)`);
|
|
82
|
-
const selector = recordedAs(state.entry, 'selector');
|
|
83
|
-
if (selector?.kind === 'files' && selector.patterns.every(isName))
|
|
84
|
-
return { kind: 'files', patterns: selector.patterns, entry: true };
|
|
85
|
-
fail(`${name}.entry: expected the module's entry files, files('index.ts')`);
|
|
86
|
-
return undefined;
|
|
87
|
-
};
|
|
88
|
-
const exposesOf = (where, value, surfaces, fail) => {
|
|
89
|
-
if (value === undefined)
|
|
90
|
-
return undefined;
|
|
91
|
-
if (recordedAs(value, 'all'))
|
|
92
|
-
return { kind: 'all' };
|
|
93
|
-
const selector = recordedAs(value, 'selector');
|
|
94
|
-
if (selector?.kind === 'files' && selector.patterns.every(isName))
|
|
95
|
-
return { kind: 'files', patterns: selector.patterns };
|
|
96
|
-
const returned = surfaces ? callback(value) : undefined;
|
|
97
|
-
if (isRecord(returned))
|
|
98
|
-
return {
|
|
99
|
-
kind: 'surfaces',
|
|
100
|
-
surfaces: Object.fromEntries(Object.entries(returned).map(([surface, refs]) => [surface, [refs].flat()])),
|
|
101
|
-
};
|
|
102
|
-
const shapes = surfaces ? `all(), files('a.ts', ...) or (m) => ({ render: m.dom })` : `all() or files('a.ts', ...)`;
|
|
103
|
-
fail(`${where}: expected ${shapes}`);
|
|
104
|
-
return undefined;
|
|
105
|
-
};
|
|
106
|
-
const checkFields = (name, state, fail) => {
|
|
107
|
-
if (state.description !== undefined && typeof state.description !== 'string')
|
|
108
|
-
fail(`${name}.description: expected a string`);
|
|
109
|
-
if (!Array.isArray(state.modules))
|
|
110
|
-
fail(`${name}.contains: expected the modules chain, (m) => m.module(...)`);
|
|
111
|
-
};
|
|
112
|
-
const draftModules = (archetype, recorded, fail) => {
|
|
113
|
-
const modules = [];
|
|
114
|
-
for (const entry of entriesOf(recorded)) {
|
|
115
|
-
const module = draftModule(archetype, entry, fail);
|
|
116
|
-
if (modules.some((other) => other.data.name === module.data.name))
|
|
117
|
-
fail(`${archetype}: module '${module.data.name}' is already declared`);
|
|
118
|
-
else
|
|
119
|
-
modules.push(module);
|
|
120
|
-
}
|
|
121
|
-
return modules;
|
|
122
|
-
};
|
|
123
|
-
const draftModule = (archetype, entry, fail) => {
|
|
124
|
-
const { name } = entry;
|
|
125
|
-
if (!isName(name) || name.includes('.'))
|
|
126
|
-
fail(`${archetype}: a module's name is a non-empty string without dots, not ${JSON.stringify(name)}`);
|
|
127
|
-
const where = `${archetype}.${String(name)}`;
|
|
128
|
-
if (entry.test)
|
|
129
|
-
return draftTestModule(archetype, entry, where, fail);
|
|
130
|
-
const { kind, patterns } = selectorOf(where, entry.selector, fail);
|
|
131
|
-
if (entry.extra.length > 0)
|
|
132
|
-
fail(`${where}: .module() takes a name, a selector and an archetype`);
|
|
133
|
-
const { named, anonymous, using } = archetypeOf(where, archetype, entry.archetype, fail);
|
|
134
|
-
const description = anonymous?.description;
|
|
135
|
-
const common = { name: name, using: [], ...(description === undefined ? {} : { description }) };
|
|
136
|
-
if (anonymous)
|
|
137
|
-
checkAnonymous(where, kind, anonymous, fail);
|
|
138
|
-
if (kind === 'dir')
|
|
139
|
-
return {
|
|
140
|
-
data: { kind, ...common, paths: patterns, archetype: named ?? inlineName(archetype, name) },
|
|
141
|
-
using,
|
|
142
|
-
};
|
|
143
|
-
if (named !== undefined)
|
|
144
|
-
fail(`${where}: an archetype makes a folder module; use dir(...), not files(...)`);
|
|
145
|
-
const exposes = exposesOf(`${where}.exposes`, anonymous?.exposes, false, fail);
|
|
146
|
-
return { data: { kind, ...common, patterns, ...(exposes ? { exposes } : {}) }, using };
|
|
147
|
-
};
|
|
148
|
-
const draftTestModule = (holder, entry, where, fail) => {
|
|
149
|
-
const locator = locatorOf(where, entry.selector, fail);
|
|
150
|
-
if (entry.extra.length > 0)
|
|
151
|
-
fail(`${where}: .testModule() takes a name, a selector and an archetype`);
|
|
152
|
-
const { named, anonymous, using } = archetypeOf(where, holder, entry.archetype, fail);
|
|
153
|
-
if (named !== undefined)
|
|
154
|
-
fail(`${where}: a test module's archetype is its own: using(...), exposes(...) or description(...)`);
|
|
155
|
-
if (anonymous)
|
|
156
|
-
checkAnonymous(where, 'test', anonymous, fail);
|
|
157
|
-
const exposes = exposesOf(`${where}.exposes`, anonymous?.exposes, false, fail);
|
|
158
|
-
const description = anonymous?.description;
|
|
159
|
-
return {
|
|
160
|
-
data: {
|
|
161
|
-
kind: 'test',
|
|
162
|
-
name: entry.name,
|
|
163
|
-
locator,
|
|
164
|
-
using: [],
|
|
165
|
-
...(exposes ? { exposes } : {}),
|
|
166
|
-
...(description === undefined ? {} : { description }),
|
|
167
|
-
},
|
|
168
|
-
using,
|
|
169
|
-
};
|
|
170
|
-
};
|
|
171
|
-
const locatorOf = (where, value, fail) => {
|
|
172
|
-
const selector = recordedAs(value, 'selector');
|
|
173
|
-
if (selector?.kind !== 'mirror')
|
|
174
|
-
return selectorOf(where, value, fail, 'test');
|
|
175
|
-
const [from, to] = selector.patterns;
|
|
176
|
-
if (!isMirrorPath(from) || !isMirrorPath(to))
|
|
177
|
-
fail(`${where}: mirror() takes two folder paths without placeholders, mirror('src', 'test')`);
|
|
178
|
-
return { kind: 'mirror', from: String(from), to: String(to) };
|
|
179
|
-
};
|
|
180
|
-
const isMirrorPath = (path) => isName(path) && !path.includes('{') && isDirPath(path);
|
|
181
|
-
const archetypeOf = (where, holder, value, fail) => {
|
|
182
|
-
if (value === undefined)
|
|
183
|
-
return { using: [] };
|
|
184
|
-
if (value === self)
|
|
185
|
-
return { named: holder, using: [] };
|
|
186
|
-
const recorded = recordedAs(value, 'archetype');
|
|
187
|
-
if (!recorded) {
|
|
188
|
-
fail(`${where}: expected an archetype, such as component, using(...) or exposes(all())`);
|
|
189
|
-
return { using: [] };
|
|
190
|
-
}
|
|
191
|
-
const using = usingOf(`${where}.using`, recorded.using, fail);
|
|
192
|
-
const { definition } = recorded;
|
|
193
|
-
return definition.name === undefined ? { anonymous: definition, using } : { named: definition.name, using };
|
|
194
|
-
};
|
|
195
|
-
const checkAnonymous = (where, kind, definition, fail) => {
|
|
196
|
-
if (definition.description !== undefined && typeof definition.description !== 'string')
|
|
197
|
-
fail(`${where}.description: expected a string`);
|
|
198
|
-
if (isRecord(definition.requires) && Object.keys(definition.requires).length > 0)
|
|
199
|
-
fail(`${where}.requires: a module's own archetype sees what its holder requires; name the archetype to require more`);
|
|
200
|
-
if (kind === 'dir')
|
|
201
|
-
return;
|
|
202
|
-
const hasModules = Array.isArray(definition.modules) && definition.modules.length > 0;
|
|
203
|
-
const folderOnly = [
|
|
204
|
-
...(hasModules ? ['contains'] : []),
|
|
205
|
-
...['identify', 'provides', 'entry', 'testUsing'].filter((field) => definition[field] !== undefined),
|
|
206
|
-
];
|
|
207
|
-
for (const field of folderOnly)
|
|
208
|
-
fail(kind === 'test'
|
|
209
|
-
? `${where}.${field}: a test module holds test code; its archetype takes using(...), exposes(...) and description(...)`
|
|
210
|
-
: `${where}.${field}: a files module has no modules; use dir(...) for ${field}`);
|
|
211
|
-
};
|
|
212
|
-
const selectorOf = (where, value, fail, of = 'module') => {
|
|
213
|
-
const selector = recordedAs(value, 'selector');
|
|
214
|
-
if (!selector || selector.kind === 'mirror') {
|
|
215
|
-
const shapes = of === 'test' ? "files('*.test.ts'), dir('e2e') or mirror('src', 'test')" : "files('*.ts') or dir('src/ui')";
|
|
216
|
-
fail(`${where}: expected a selector, ${shapes}`);
|
|
217
|
-
return { kind: 'files', patterns: [] };
|
|
218
|
-
}
|
|
219
|
-
const patterns = selector.patterns;
|
|
220
|
-
if (!patterns.every(isName))
|
|
221
|
-
fail(`${where}: ${selector.kind}() takes non-empty strings`);
|
|
222
|
-
if (selector.kind === 'dir')
|
|
223
|
-
checkDirPaths(where, patterns, fail);
|
|
224
|
-
return { kind: selector.kind, patterns };
|
|
225
|
-
};
|
|
226
|
-
const usingOf = (where, value, fail) => {
|
|
227
|
-
const using = typeof value === 'function' ? value(moduleRefs(), { required: requiredRefs() }) : (value ?? []);
|
|
228
|
-
if (Array.isArray(using))
|
|
229
|
-
return using;
|
|
230
|
-
fail(`${where}: expected a list of references, or a callback returning one`);
|
|
231
|
-
return [];
|
|
232
|
-
};
|
|
233
|
-
const checkDirPaths = (where, paths, fail) => {
|
|
234
|
-
for (const path of paths.filter((path) => isName(path) && !isDirPath(path)))
|
|
235
|
-
fail(`${where}: a dir path is folder names and {placeholders}, a whole segment each, not '${String(path)}'`);
|
|
236
|
-
};
|
|
237
|
-
const isDirPath = (path) => path.split('/').every((segment) => segment !== '.' && segment !== '..' && /^(\{\w+\}|[^{}*]+)$/.test(segment));
|
|
238
|
-
const requirements = (archetype, value, fail) => {
|
|
239
|
-
if (!isRecord(value)) {
|
|
240
|
-
fail(`${archetype}.requires: expected requirements by name, { testRunner, strings: dir() }`);
|
|
241
|
-
return {};
|
|
242
|
-
}
|
|
243
|
-
const result = {};
|
|
244
|
-
for (const [name, requirement] of Object.entries(value)) {
|
|
245
|
-
const shape = recordedAs(requirement, 'shape');
|
|
246
|
-
const required = recordedAs(requirement, 'archetype')?.definition.name;
|
|
247
|
-
if (shape)
|
|
248
|
-
result[name] = { kind: shape };
|
|
249
|
-
else if (isName(required))
|
|
250
|
-
result[name] = { kind: 'archetype', archetype: required };
|
|
251
|
-
else
|
|
252
|
-
fail(`${archetype}.requires.${name}: expected an archetype, dir() or files()`);
|
|
253
|
-
}
|
|
254
|
-
return result;
|
|
255
|
-
};
|
|
256
|
-
const identifyData = (archetype, value, fail) => {
|
|
257
|
-
const keys = isRecord(value) ? Object.keys(value) : [];
|
|
258
|
-
if (value === undefined || (keys.length === 1 && isName(value.marker)))
|
|
259
|
-
return value;
|
|
260
|
-
if (keys.length === 1 && typeof value.match === 'function')
|
|
261
|
-
return value;
|
|
262
|
-
fail(`${archetype}.identify: expected { marker: '{name}.dom.ts' } or { match: (dir, chain) => ... }`);
|
|
263
|
-
return undefined;
|
|
264
|
-
};
|