Skip to content

Commit ba2069c

Browse files
committed
refactor(@angular-devkit/core): stop using PartiallyOrderedSet internally
CoreSchemaRegistry was the only internal consumer of the deprecated PartiallyOrderedSet class. Replacing its _pre and _post fields with plain arrays and a small addTransform helper removes that dependency while preserving insertion order, deduplication, and support for the deprecated deps parameter. To avoid importing partially-ordered-set.ts for its error types as well, DependencyNotFoundException and CircularDependencyFoundException have been moved to exception.ts so they remain exported from the package root after the deprecated file is eventually removed.
1 parent d899c51 commit ba2069c

4 files changed

Lines changed: 146 additions & 21 deletions

File tree

‎packages/angular_devkit/core/src/exception.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,13 @@ export class PathIsFileException extends BaseException {
3939
super(`Path "${path}" is a file.`);
4040
}
4141
}
42+
export class DependencyNotFoundException extends BaseException {
43+
constructor() {
44+
super('One of the dependencies is not part of the set.');
45+
}
46+
}
47+
export class CircularDependencyFoundException extends BaseException {
48+
constructor() {
49+
super('Circular dependencies found.');
50+
}
51+
}

‎packages/angular_devkit/core/src/json/schema/registry.ts‎

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ import * as http from 'node:http';
1212
import * as https from 'node:https';
1313
import * as Url from 'node:url';
1414
import { Observable, from, isObservable, lastValueFrom } from 'rxjs';
15-
import { BaseException } from '../../exception';
16-
import { PartiallyOrderedSet } from '../../utils';
15+
import {
16+
BaseException,
17+
CircularDependencyFoundException,
18+
DependencyNotFoundException,
19+
} from '../../exception';
1720
import { JsonArray, JsonObject, JsonValue, isJsonObject } from '../utils';
1821
import {
1922
JsonPointer,
@@ -93,12 +96,46 @@ interface SchemaInfo {
9396
promptDefinitions: Array<PromptDefinition>;
9497
}
9598

99+
/**
100+
* Adds a transform to an ordered list of transforms.
101+
*
102+
* Transforms run in insertion order and each transform is only present once. Dependencies
103+
* must already be in the list, which guarantees that they run before the added transform.
104+
*/
105+
function addTransform(
106+
transforms: JsonVisitor[],
107+
visitor: JsonVisitor,
108+
deps: JsonVisitor[] = [],
109+
): void {
110+
const existingIndex = transforms.indexOf(visitor);
111+
const position = existingIndex === -1 ? transforms.length : existingIndex;
112+
113+
for (const dep of deps) {
114+
const depIndex = transforms.indexOf(dep);
115+
if (depIndex === -1) {
116+
throw new DependencyNotFoundException();
117+
}
118+
if (depIndex === position) {
119+
throw new CircularDependencyFoundException();
120+
}
121+
if (depIndex > position) {
122+
throw new Error(
123+
'The transform is already registered and runs before one of the provided dependencies.',
124+
);
125+
}
126+
}
127+
128+
if (existingIndex === -1) {
129+
transforms.push(visitor);
130+
}
131+
}
132+
96133
export class CoreSchemaRegistry implements SchemaRegistry {
97134
private _ajv: Ajv;
98135
private _uriCache = new Map<string, JsonObject>();
99136
private _uriHandlers = new Set<UriHandler>();
100-
private _pre = new PartiallyOrderedSet<JsonVisitor>();
101-
private _post = new PartiallyOrderedSet<JsonVisitor>();
137+
private _pre: JsonVisitor[] = [];
138+
private _post: JsonVisitor[] = [];
102139

103140
private _currentCompilationSchemaInfo?: SchemaInfo;
104141

@@ -181,7 +218,7 @@ export class CoreSchemaRegistry implements SchemaRegistry {
181218
* @param {JsonVisitor[]} deps [Deprecated] A list of other visitors to run before. Add transforms in the desired execution order instead.
182219
*/
183220
addPreTransform(visitor: JsonVisitor, deps?: JsonVisitor[]): void {
184-
this._pre.add(visitor, deps);
221+
addTransform(this._pre, visitor, deps);
185222
}
186223

187224
/**
@@ -192,7 +229,7 @@ export class CoreSchemaRegistry implements SchemaRegistry {
192229
* @param {JsonVisitor[]} deps [Deprecated] A list of other visitors to run before. Add transforms in the desired execution order instead.
193230
*/
194231
addPostTransform(visitor: JsonVisitor, deps?: JsonVisitor[]): void {
195-
this._post.add(visitor, deps);
232+
addTransform(this._post, visitor, deps);
196233
}
197234

198235
protected _resolver(
@@ -326,7 +363,7 @@ export class CoreSchemaRegistry implements SchemaRegistry {
326363

327364
// Apply pre-validation transforms
328365
if (validationOptions.applyPreTransforms) {
329-
for (const visitor of this._pre.values()) {
366+
for (const visitor of this._pre) {
330367
data = await lastValueFrom(
331368
visitJson(data, visitor, schema, this._resolver.bind(this), validator),
332369
);
@@ -378,7 +415,7 @@ export class CoreSchemaRegistry implements SchemaRegistry {
378415

379416
// Apply post-validation transforms
380417
if (validationOptions.applyPostTransforms) {
381-
for (const visitor of this._post.values()) {
418+
for (const visitor of this._post) {
382419
data = await lastValueFrom(
383420
visitJson(data, visitor, schema, this._resolver.bind(this), validator),
384421
);

‎packages/angular_devkit/core/src/json/schema/registry_spec.ts‎

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
*/
88

99
/* eslint-disable @typescript-eslint/no-explicit-any */
10+
import { CircularDependencyFoundException, DependencyNotFoundException } from '../../exception';
1011
import { JsonValue } from '../utils';
11-
import { SchemaFormat } from './interface';
12+
import { JsonVisitor, SchemaFormat } from './interface';
1213
import { CoreSchemaRegistry, SchemaValidationException } from './registry';
1314
import { JsonSchema } from './schema';
1415
import { addUndefinedDefaults } from './transforms';
@@ -454,4 +455,92 @@ describe('CoreSchemaRegistry', () => {
454455
expect(messages).toEqual(['Data path "/outputPath" must be string.']);
455456
});
456457
});
458+
459+
describe('transform ordering', () => {
460+
function createVisitor(name: string, calls: string[]): JsonVisitor {
461+
return (value) => {
462+
calls.push(name);
463+
464+
return value;
465+
};
466+
}
467+
468+
async function run(registry: CoreSchemaRegistry): Promise<void> {
469+
const validator = await registry.compile({ type: 'string' });
470+
const result = await validator('value');
471+
expect(result.success).toBe(true);
472+
}
473+
474+
it('runs transforms in the order they were added', async () => {
475+
const calls: string[] = [];
476+
const registry = new CoreSchemaRegistry();
477+
registry.addPreTransform(createVisitor('pre1', calls));
478+
registry.addPreTransform(createVisitor('pre2', calls));
479+
registry.addPostTransform(createVisitor('post1', calls));
480+
registry.addPostTransform(createVisitor('post2', calls));
481+
482+
await run(registry);
483+
484+
expect(calls).toEqual(['pre1', 'pre2', 'post1', 'post2']);
485+
});
486+
487+
it('only runs a transform once when it is added multiple times', async () => {
488+
const calls: string[] = [];
489+
const registry = new CoreSchemaRegistry();
490+
const first = createVisitor('first', calls);
491+
registry.addPostTransform(first);
492+
registry.addPostTransform(createVisitor('second', calls));
493+
registry.addPostTransform(first);
494+
495+
await run(registry);
496+
497+
expect(calls).toEqual(['first', 'second']);
498+
});
499+
500+
it('runs a transform after its dependencies', async () => {
501+
const calls: string[] = [];
502+
const registry = new CoreSchemaRegistry();
503+
const first = createVisitor('first', calls);
504+
const second = createVisitor('second', calls);
505+
registry.addPostTransform(first);
506+
registry.addPostTransform(second, [first]);
507+
// Adding again with dependencies that already run earlier is a no-op.
508+
registry.addPostTransform(second, [first]);
509+
510+
await run(registry);
511+
512+
expect(calls).toEqual(['first', 'second']);
513+
});
514+
515+
it('throws when a dependency has not been added', () => {
516+
const registry = new CoreSchemaRegistry();
517+
const missing = createVisitor('missing', []);
518+
519+
expect(() => registry.addPreTransform(createVisitor('visitor', []), [missing])).toThrowError(
520+
DependencyNotFoundException,
521+
);
522+
});
523+
524+
it('throws when a transform depends on itself', () => {
525+
const registry = new CoreSchemaRegistry();
526+
const visitor = createVisitor('visitor', []);
527+
registry.addPreTransform(visitor);
528+
529+
expect(() => registry.addPreTransform(visitor, [visitor])).toThrowError(
530+
CircularDependencyFoundException,
531+
);
532+
});
533+
534+
it('throws when an existing transform is given a dependency that runs after it', () => {
535+
const registry = new CoreSchemaRegistry();
536+
const first = createVisitor('first', []);
537+
const second = createVisitor('second', []);
538+
registry.addPreTransform(first);
539+
registry.addPreTransform(second);
540+
541+
expect(() => registry.addPreTransform(first, [second])).toThrowError(
542+
/already registered and runs before/,
543+
);
544+
});
545+
});
457546
});

‎packages/angular_devkit/core/src/utils/partially-ordered-set.ts‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import { BaseException } from '../exception';
10-
11-
export class DependencyNotFoundException extends BaseException {
12-
constructor() {
13-
super('One of the dependencies is not part of the set.');
14-
}
15-
}
16-
export class CircularDependencyFoundException extends BaseException {
17-
constructor() {
18-
super('Circular dependencies found.');
19-
}
20-
}
9+
import { CircularDependencyFoundException, DependencyNotFoundException } from '../exception';
2110

2211
/**
2312
* @deprecated Use standard arrays and ensure correct insertion order instead.

0 commit comments

Comments
 (0)