Skip to content

Commit

Permalink
refactor(ns-json-schema-draft-7): use inheritance for visitors (#4686)
Browse files Browse the repository at this point in the history
  • Loading branch information
char0n authored Jan 25, 2025
1 parent bc76b5e commit a2f4958
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class JSONSchemaVisitor extends Mixin(

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

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ObjectElement, BooleanElement } from '@swagger-api/apidom-core';
import {
FixedFieldsVisitor,
JSONSchemaVisitor as JSONSchemaDraft4Visitor,
JSONSchemaVisitorOptions,
} from '@swagger-api/apidom-ns-json-schema-draft-4';
Expand All @@ -15,22 +14,16 @@ export type { JSONSchemaVisitorOptions };
class JSONSchemaVisitor extends JSONSchemaDraft4Visitor {
declare public element: JSONSchemaElement;

constructor(options: JSONSchemaVisitorOptions) {
super(options);
this.element = new JSONSchemaElement();
}

// eslint-disable-next-line class-methods-use-this
get defaultDialectIdentifier(): string {
return 'http://json-schema.org/draft-06/schema#';
}

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

// for further processing consider this Schema Element as parent for all embedded Schema Elements
this.parent = this.element;

return FixedFieldsVisitor.prototype.ObjectElement.call(this, objectElement);
}

BooleanElement(booleanElement: BooleanElement) {
const result = this.enter(booleanElement);
this.element.classes.push('boolean-json-schema');
Expand All @@ -39,11 +32,7 @@ class JSONSchemaVisitor extends JSONSchemaDraft4Visitor {
}

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

Expand Down
7 changes: 3 additions & 4 deletions packages/apidom-ns-json-schema-draft-7/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@ 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 { keyMap, getNodeType } from './traversal/visitor.ts';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,78 +1,27 @@
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 JSONSchemaDraft6Visitor,
JSONSchemaVisitorOptions,
} from '@swagger-api/apidom-ns-json-schema-draft-6';

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 JSONSchemaDraft6Visitor {
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
get defaultDialectIdentifier(): string {
return 'http://json-schema.org/draft-07/schema#';
}

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

// for further processing consider this Schema Element as parent for all embedded Schema Elements
this.parent = this.element;

return FixedFieldsVisitor.prototype.ObjectElement.call(this, objectElement);
}

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

return result;
}

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

handleSchemaIdentifier(objectElement: ObjectElement, identifierKeyword: string = '$id'): void {
return JSONSchemaDraft6Visitor.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 JSONSchemaDraft6LinkDescriptionVisitor,
LinkDescriptionVisitorOptions,
} from '@swagger-api/apidom-ns-json-schema-draft-6';

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 JSONSchemaDraft6LinkDescriptionVisitor {
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-7/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-6';

/**
* @public
Expand Down

0 comments on commit a2f4958

Please sign in to comment.