Skip to content

Commit

Permalink
refactor(ns-json-schema-2019-09): use inheritance for visitors (#4687)
Browse files Browse the repository at this point in the history
  • Loading branch information
char0n authored Jan 25, 2025
1 parent a2f4958 commit 13efde8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 65 deletions.
6 changes: 2 additions & 4 deletions packages/apidom-ns-json-schema-2019-09/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ export type {

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 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 $defsVisitor,
$defsVisitorOptions,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,23 @@
import { Mixin } from 'ts-mixer';
import { always } from 'ramda';
import { ObjectElement, BooleanElement, isStringElement } from '@swagger-api/apidom-core';
import { ObjectElement, isStringElement } from '@swagger-api/apidom-core';
import {
FixedFieldsVisitor,
FixedFieldsVisitorOptions,
ParentSchemaAwareVisitor,
ParentSchemaAwareVisitorOptions,
FallbackVisitor,
FallbackVisitorOptions,
SpecPath,
JSONSchemaVisitor as JSONSchemaDraft7Visitor,
JSONSchemaVisitorOptions,
} from '@swagger-api/apidom-ns-json-schema-draft-7';

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 JSONSchemaDraft7Visitor {
declare public element: JSONSchemaElement;

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

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

// eslint-disable-next-line class-methods-use-this
Expand All @@ -45,7 +26,6 @@ class JSONSchemaVisitor extends Mixin(
}

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

Expand All @@ -61,25 +41,6 @@ class JSONSchemaVisitor extends Mixin(

return result;
}

BooleanElement(booleanElement: BooleanElement) {
const result = this.enter(booleanElement);
this.element.classes.push('boolean-json-schema');

return result;
}

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

handleSchemaIdentifier(objectElement: ObjectElement, identifierKeyword: string = '$id'): void {
return JSONSchemaDraft7Visitor.prototype.handleSchemaIdentifier.call(
this,
objectElement,
identifierKeyword,
);
}
}

export default JSONSchemaVisitor;
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 JSONSchemaDraft7LinkDescriptionVisitor,
LinkDescriptionVisitorOptions,
} from '@swagger-api/apidom-ns-json-schema-draft-7';

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 JSONSchemaDraft7LinkDescriptionVisitor {
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

0 comments on commit 13efde8

Please sign in to comment.