Skip to content

Commit

Permalink
refactor(ns-json-schema-draft-6): use inheritance for visitors (#4678)
Browse files Browse the repository at this point in the history
  • Loading branch information
glowcloud authored Jan 25, 2025
1 parent d49fcd8 commit bc76b5e
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 127 deletions.
12 changes: 4 additions & 8 deletions packages/apidom-ns-json-schema-draft-4/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,8 @@ export type {
default as JSONReference$RefVisitor,
$RefVisitorOptions as JSONReference$RefVisitorOptions,
} from './refractor/visitors/json-schema/json-reference/$RefVisitor.ts';
export type {
default as LinkDescriptionVisitor,
LinkDescriptionVisitorOptions,
} from './refractor/visitors/json-schema/link-description/index.ts';
export { default as LinkDescriptionVisitor } from './refractor/visitors/json-schema/link-description/index.ts';
export type { LinkDescriptionVisitorOptions } from './refractor/visitors/json-schema/link-description/index.ts';
export type {
default as MediaVisitor,
MediaVisitorOptions,
Expand All @@ -90,10 +88,8 @@ export type {
} from './refractor/visitors/json-schema/EnumVisitor.ts';
export { default as JSONSchemaVisitor } from './refractor/visitors/json-schema/index.ts';
export type { JSONSchemaVisitorOptions } from './refractor/visitors/json-schema/index.ts';
export type {
default as ItemsVisitor,
ItemsVisitorOptions,
} from './refractor/visitors/json-schema/ItemsVisitor.ts';
export { default as ItemsVisitor } from './refractor/visitors/json-schema/ItemsVisitor.ts';
export type { ItemsVisitorOptions } from './refractor/visitors/json-schema/ItemsVisitor.ts';
export type {
default as SchemaOrReferenceVisitor,
SchemaOrReferenceVisitorOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class JSONSchemaVisitor extends Mixin(

constructor(options: JSONSchemaVisitorOptions) {
super(options);
this.element = new JSONSchemaElement();
this.specPath = always(['document', 'objects', 'JSONSchema']);
}

Expand All @@ -52,6 +51,7 @@ class JSONSchemaVisitor extends Mixin(
}

ObjectElement(objectElement: ObjectElement) {
this.element = new JSONSchemaElement();
this.handleDialectIdentifier(objectElement);
this.handleSchemaIdentifier(objectElement);

Expand Down
12 changes: 4 additions & 8 deletions packages/apidom-ns-json-schema-draft-6/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,16 @@ export type {
ItemsVisitor as JSONSchemaDraft4ItemsVisitor,
} from '@swagger-api/apidom-ns-json-schema-draft-4';

export type {
default as LinkDescriptionVisitor,
LinkDescriptionVisitorOptions,
} from './refractor/visitors/json-schema/link-description/index.ts';
export { default as LinkDescriptionVisitor } from './refractor/visitors/json-schema/link-description/index.ts';
export type { LinkDescriptionVisitorOptions } from './refractor/visitors/json-schema/link-description/index.ts';
export { default as JSONSchemaVisitor } from './refractor/visitors/json-schema/index.ts';
export type { JSONSchemaVisitorOptions } from './refractor/visitors/json-schema/index.ts';
export type {
default as ExamplesVisitor,
ExamplesVisitorOptions,
} from './refractor/visitors/json-schema/ExamplesVisitor.ts';
export type {
default as ItemsVisitor,
ItemsVisitorOptions,
} from './refractor/visitors/json-schema/ItemsVisitor.ts';
export { default as ItemsVisitor } from './refractor/visitors/json-schema/ItemsVisitor.ts';
export type { ItemsVisitorOptions } from './refractor/visitors/json-schema/ItemsVisitor.ts';

export { keyMap, getNodeType } from './traversal/visitor.ts';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,62 +1,15 @@
import { Mixin } from 'ts-mixer';
import { BooleanElement, BREAK } from '@swagger-api/apidom-core';
import {
ObjectElement,
ArrayElement,
BooleanElement,
Element,
BREAK,
} from '@swagger-api/apidom-core';
import {
SpecificationVisitor,
SpecificationVisitorOptions,
FallbackVisitor,
FallbackVisitorOptions,
ParentSchemaAwareVisitor,
ParentSchemaAwareVisitorOptions,
isJSONReferenceLikeElement,
ItemsVisitor as JSONSchemaDraft4ItemsVisitor,
ItemsVisitorOptions,
} from '@swagger-api/apidom-ns-json-schema-draft-4';

/**
* @public
*/
export interface ItemsVisitorOptions
extends SpecificationVisitorOptions,
ParentSchemaAwareVisitorOptions,
FallbackVisitorOptions {}
export type { ItemsVisitorOptions };

/**
* @public
*/
class ItemsVisitor extends Mixin(SpecificationVisitor, ParentSchemaAwareVisitor, FallbackVisitor) {
declare public element: ObjectElement | ArrayElement;

ObjectElement(objectElement: ObjectElement) {
const specPath = isJSONReferenceLikeElement(objectElement)
? ['document', 'objects', 'JSONReference']
: ['document', 'objects', 'JSONSchema'];
this.element = this.toRefractedElement(specPath, objectElement);

return BREAK;
}

ArrayElement(arrayElement: ArrayElement) {
this.element = new ArrayElement();
this.element.classes.push('json-schema-items');

arrayElement.forEach((item: Element): void => {
const specPath = isJSONReferenceLikeElement(item)
? ['document', 'objects', 'JSONReference']
: ['document', 'objects', 'JSONSchema'];
const element = this.toRefractedElement(specPath, item);

this.element.push(element);
});

this.copyMetaAndAttributes(arrayElement, this.element);

return BREAK;
}

class ItemsVisitor extends JSONSchemaDraft4ItemsVisitor {
BooleanElement(booleanElement: BooleanElement) {
this.element = this.toRefractedElement(['document', 'objects', 'JSONSchema'], booleanElement);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,20 @@
import { Mixin } from 'ts-mixer';
import { always } from 'ramda';
import { ObjectElement, BooleanElement } from '@swagger-api/apidom-core';
import {
FixedFieldsVisitor,
FixedFieldsVisitorOptions,
ParentSchemaAwareVisitor,
ParentSchemaAwareVisitorOptions,
FallbackVisitor,
FallbackVisitorOptions,
SpecPath,
JSONSchemaVisitor as JSONSchemaDraft4Visitor,
JSONSchemaVisitorOptions,
} from '@swagger-api/apidom-ns-json-schema-draft-4';

import JSONSchemaElement from '../../../elements/JSONSchema.ts';

/**
* @public
*/
export interface JSONSchemaVisitorOptions
extends FixedFieldsVisitorOptions,
ParentSchemaAwareVisitorOptions,
FallbackVisitorOptions {}
export type { JSONSchemaVisitorOptions };

/**
* @public
*/
class JSONSchemaVisitor extends Mixin(
FixedFieldsVisitor,
ParentSchemaAwareVisitor,
FallbackVisitor,
) {
class JSONSchemaVisitor extends JSONSchemaDraft4Visitor {
declare public element: JSONSchemaElement;

declare protected readonly specPath: SpecPath<['document', 'objects', 'JSONSchema']>;

constructor(options: JSONSchemaVisitorOptions) {
super(options);
this.specPath = always(['document', 'objects', 'JSONSchema']);
}

// eslint-disable-next-line class-methods-use-this
get defaultDialectIdentifier(): string {
return 'http://json-schema.org/draft-06/schema#';
Expand All @@ -62,10 +38,6 @@ class JSONSchemaVisitor extends Mixin(
return result;
}

handleDialectIdentifier(objectElement: ObjectElement): void {
return JSONSchemaDraft4Visitor.prototype.handleDialectIdentifier.call(this, objectElement);
}

handleSchemaIdentifier(objectElement: ObjectElement, identifierKeyword: string = '$id'): void {
return JSONSchemaDraft4Visitor.prototype.handleSchemaIdentifier.call(
this,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
import { Mixin } from 'ts-mixer';
import { always } from 'ramda';
import {
FixedFieldsVisitor,
FixedFieldsVisitorOptions,
FallbackVisitor,
FallbackVisitorOptions,
SpecPath,
LinkDescriptionVisitor as JSONSchemaDraft4LinkDescriptionVisitor,
LinkDescriptionVisitorOptions,
} from '@swagger-api/apidom-ns-json-schema-draft-4';

import LinkDescriptionElement from '../../../../elements/LinkDescription.ts';

/**
* @public
*/
export interface LinkDescriptionVisitorOptions
extends FixedFieldsVisitorOptions,
FallbackVisitorOptions {}
export type { LinkDescriptionVisitorOptions };

/**
* @public
*/
class LinkDescriptionVisitor extends Mixin(FixedFieldsVisitor, FallbackVisitor) {
class LinkDescriptionVisitor extends JSONSchemaDraft4LinkDescriptionVisitor {
declare public readonly element: LinkDescriptionElement;

declare protected readonly specPath: SpecPath<['document', 'objects', 'LinkDescription']>;

constructor(options: LinkDescriptionVisitorOptions) {
super(options);
this.element = new LinkDescriptionElement();
this.specPath = always(['document', 'objects', 'LinkDescription']);
}
}

Expand Down
12 changes: 2 additions & 10 deletions packages/apidom-ns-json-schema-draft-6/src/traversal/visitor.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { keyMap as keyMapBase, isElement, Element } from '@swagger-api/apidom-core';
import { keyMap as keyMapBase } from '@swagger-api/apidom-core';

/**
* @public
*/
export const getNodeType = <T extends Element>(element: T): string | undefined => {
if (!isElement(element)) {
return undefined;
}
return `${element.element.charAt(0).toUpperCase() + element.element.slice(1)}Element`;
};
export { getNodeType } from '@swagger-api/apidom-ns-json-schema-draft-4';

/**
* @public
Expand Down

0 comments on commit bc76b5e

Please sign in to comment.