Skip to content

Commit

Permalink
refactor(ns-ads): enhance usage of TypeScript (#3727)
Browse files Browse the repository at this point in the history
Refs #3481
  • Loading branch information
char0n authored Jan 24, 2024
1 parent b9c08f5 commit dd369a0
Show file tree
Hide file tree
Showing 19 changed files with 114 additions and 49 deletions.
21 changes: 18 additions & 3 deletions babel.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ module.exports = {
exclude: ['transform-function-name'],
},
],
'@babel/preset-typescript',
[
'@babel/preset-typescript',
{
allowDeclareFields: true,
}
],
],
plugins: [
[
Expand Down Expand Up @@ -53,7 +58,12 @@ module.exports = {
exclude: ['transform-function-name'], // this is here because of https://github.com/babel/babel/discussions/12874
},
],
'@babel/preset-typescript',
[
'@babel/preset-typescript',
{
allowDeclareFields: true,
}
],
],
plugins: [
[
Expand Down Expand Up @@ -84,7 +94,12 @@ module.exports = {
exclude: ['transform-function-name'], // this is here because of https://github.com/babel/babel/discussions/12874
},
],
'@babel/preset-typescript',
[
'@babel/preset-typescript',
{
allowDeclareFields: true,
}
],
],
plugins: [
[
Expand Down
9 changes: 4 additions & 5 deletions packages/apidom-ns-api-design-systems/src/refractor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { keyMap, getNodeType } from '../traversal/visitor';
import createToolbox from './toolbox';

const refract = <T extends Element>(
value: any,
value: unknown,
{ specPath = ['visitors', 'document', 'objects', 'Main', '$visitor'], plugins = [] } = {},
): T => {
const element = baseRefract(value);
Expand All @@ -25,10 +25,9 @@ const refract = <T extends Element>(
* Though we allow consumers to define their onw plugins on already transformed ApiDOM.
*/
const RootVisitorClass = path(specPath, resolvedSpec) as typeof VisitorClass;
const rootVisitor = new RootVisitorClass();
const rootVisitor = new RootVisitorClass({ specObj: resolvedSpec });

// @ts-ignore
visit(element, rootVisitor, { state: { specObj: resolvedSpec } });
visit(element, rootVisitor);

/**
* Running plugins visitors means extra single traversal === performance hit.
Expand All @@ -41,7 +40,7 @@ const refract = <T extends Element>(

export const createRefractor =
(specPath: string[]) =>
(value: any, options = {}) =>
(value: unknown, options = {}) =>
refract(value, { ...options, specPath });

export default refract;
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@ import { isFunction } from 'ramda-adjunct';
import { visit, cloneDeep } from '@swagger-api/apidom-core';

import { keyMap, getNodeType } from '../../traversal/visitor';
import Visitor from './Visitor';
import Visitor, { VisitorOptions } from './Visitor';
import FallbackVisitor from './FallbackVisitor';
import type specification from '../specification';

/**
* This is a base class for every visitor that does
* internal look-ups to retrieve other child visitors.
*/
export interface SpecificationVisitorOptions extends VisitorOptions {
readonly specObj: typeof specification;
}

class SpecificationVisitor extends Visitor {
public readonly specObj!: typeof specification;
protected readonly specObj!: typeof specification;

public readonly passingOptionsNames = ['specObj'];
protected readonly passingOptionsNames = ['specObj'];

constructor(options = {}) {
super();
Object.assign(this, options);
constructor({ specObj, ...rest }: SpecificationVisitorOptions) {
super({ ...rest });
this.specObj = specObj;
}

retrievePassingOptions() {
Expand All @@ -45,16 +49,15 @@ class SpecificationVisitor extends Visitor {
const VisitorClz = this.retrieveVisitor(specPath) as typeof Visitor;
const visitorOpts = { ...passingOpts, ...options };

// @ts-ignore
return new VisitorClz(visitorOpts as any);
return new VisitorClz(visitorOpts);
}

toRefractedElement(specPath: string[], element: any, options = {}) {
/**
* This is `Visitor shortcut`: mechanism for short circuiting the traversal and replacing
* This is `Visitor shortcut`: mechanism for short-circuiting the traversal and replacing
* it by basic node cloning.
*
* Visiting the element is equivalent to cloning it if the prototype of a visitor
* Visiting the element is equivalent to cloning it if the prototype of a visitor
* is the same as the prototype of FallbackVisitor. If that's the case, we can avoid
* bootstrapping the traversal cycle for fields that don't require any special visiting.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { hasElementSourceMap, Element } from '@swagger-api/apidom-core';

export interface VisitorOptions {}

class Visitor {
public element!: Element;

constructor(options: VisitorOptions = {}) {
Object.assign(this, options);
}

// eslint-disable-next-line class-methods-use-this
public copyMetaAndAttributes(from: Element, to: Element) {
// copy sourcemaps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { Mixin } from 'ts-mixer';

import InfoElement from '../../../../elements/Info';
import FallbackVisitor from '../../FallbackVisitor';
import FixedFieldsVisitor from '../../generics/FixedFieldsVisitor';
import FixedFieldsVisitor, { FixedFieldsVisitorOptions } from '../../generics/FixedFieldsVisitor';

class InfoVisitor extends Mixin(FixedFieldsVisitor, FallbackVisitor) {
public readonly element: InfoElement;
public declare readonly element: InfoElement;

constructor(options = {}) {
constructor(options: FixedFieldsVisitorOptions) {
super(options);
this.specPath = always(['document', 'objects', 'Info']);
this.element = new InfoElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import FallbackVisitor from '../../FallbackVisitor';
import SpecificationVisitor from '../../SpecificationVisitor';

class PrinciplesVisitor extends Mixin(SpecificationVisitor, FallbackVisitor) {
public readonly element: ArrayElement;
public declare readonly element: ArrayElement;

constructor(options = {}) {
super(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import FallbackVisitor from '../../FallbackVisitor';
import SpecificationVisitor from '../../SpecificationVisitor';

class ScenariosVisitor extends Mixin(SpecificationVisitor, FallbackVisitor) {
public readonly element: ArrayElement;
public declare readonly element: ArrayElement;

constructor(options = {}) {
super(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ArrayElement, Element, BREAK } from '@swagger-api/apidom-core';
import FallbackVisitor from '../../FallbackVisitor';
import SpecificationVisitor from '../../SpecificationVisitor';

class StandardsVisitor extends Mixin(FallbackVisitor, SpecificationVisitor) {
class StandardsVisitor extends Mixin(SpecificationVisitor, FallbackVisitor) {
public readonly element: ArrayElement;

constructor(options = {}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { always } from 'ramda';

import MainElement from '../../../../elements/Main';
import FallbackVisitor from '../../FallbackVisitor';
import FixedFieldsVisitor from '../../generics/FixedFieldsVisitor';
import FixedFieldsVisitor, { FixedFieldsVisitorOptions } from '../../generics/FixedFieldsVisitor';

class MainVisitor extends Mixin(FixedFieldsVisitor, FallbackVisitor) {
public readonly element: MainElement;
public declare readonly element: MainElement;

constructor(options = {}) {
constructor(options: FixedFieldsVisitorOptions) {
super(options);
this.specPath = always(['document', 'objects', 'Main']);
this.element = new MainElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { Mixin } from 'ts-mixer';

import PrincipleElement from '../../../../elements/Principle';
import FallbackVisitor from '../../FallbackVisitor';
import FixedFieldsVisitor from '../../generics/FixedFieldsVisitor';
import FixedFieldsVisitor, { FixedFieldsVisitorOptions } from '../../generics/FixedFieldsVisitor';

class PrincipleVisitor extends Mixin(FixedFieldsVisitor, FallbackVisitor) {
public readonly element: PrincipleElement;
public declare readonly element: PrincipleElement;

constructor(options = {}) {
constructor(options: FixedFieldsVisitorOptions) {
super(options);
this.specPath = always(['document', 'objects', 'Principle']);
this.element = new PrincipleElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import SpecificationVisitor from '../../SpecificationVisitor';
import RequirementLevelElement from '../../../../elements/RequirementLevel';

class RequirementLevelVisitor extends Mixin(SpecificationVisitor, FallbackVisitor) {
public declare element: RequirementLevelElement;

StringElement(stringElement: StringElement) {
const requirementLevelElement = new RequirementLevelElement(toValue(stringElement));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { Mixin } from 'ts-mixer';

import RequirementElement from '../../../../elements/Requirement';
import FallbackVisitor from '../../FallbackVisitor';
import FixedFieldsVisitor from '../../generics/FixedFieldsVisitor';
import FixedFieldsVisitor, { FixedFieldsVisitorOptions } from '../../generics/FixedFieldsVisitor';

class RequirementVisitor extends Mixin(FixedFieldsVisitor, FallbackVisitor) {
public readonly element: RequirementElement;
public declare readonly element: RequirementElement;

constructor(options = {}) {
constructor(options: FixedFieldsVisitorOptions) {
super(options);
this.specPath = always(['document', 'objects', 'Requirement']);
this.element = new RequirementElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { Mixin } from 'ts-mixer';
import { ArrayElement, Element, BREAK } from '@swagger-api/apidom-core';

import FallbackVisitor from '../../FallbackVisitor';
import SpecificationVisitor from '../../SpecificationVisitor';
import SpecificationVisitor, { SpecificationVisitorOptions } from '../../SpecificationVisitor';

class ThenVisitor extends Mixin(SpecificationVisitor, FallbackVisitor) {
public readonly element: ArrayElement;
public declare readonly element: ArrayElement;

constructor(options = {}) {
constructor(options: SpecificationVisitorOptions) {
super(options);
this.element = new ArrayElement();
this.element.classes.push('scenario-then');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { Mixin } from 'ts-mixer';

import ScenarioElement from '../../../../elements/Scenario';
import FallbackVisitor from '../../FallbackVisitor';
import FixedFieldsVisitor from '../../generics/FixedFieldsVisitor';
import FixedFieldsVisitor, { FixedFieldsVisitorOptions } from '../../generics/FixedFieldsVisitor';

class ScenarioVisitor extends Mixin(FixedFieldsVisitor, FallbackVisitor) {
public readonly element: ScenarioElement;
public declare readonly element: ScenarioElement;

constructor(options = {}) {
constructor(options: FixedFieldsVisitorOptions) {
super(options);
this.specPath = always(['document', 'objects', 'Scenario']);
this.element = new ScenarioElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { Mixin } from 'ts-mixer';
import { ArrayElement, BREAK, Element } from '@swagger-api/apidom-core';

import FallbackVisitor from '../../FallbackVisitor';
import SpecificationVisitor from '../../SpecificationVisitor';
import SpecificationVisitor, { SpecificationVisitorOptions } from '../../SpecificationVisitor';
import StandardIdentifierElement from '../../../../elements/StandardIdentifier';

class StandardIdentifierVisitor extends Mixin(SpecificationVisitor, FallbackVisitor) {
public readonly element: StandardIdentifierElement;
public declare readonly element: StandardIdentifierElement;

constructor(options = {}) {
constructor(options: SpecificationVisitorOptions) {
super(options);
this.element = new StandardIdentifierElement();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { Mixin } from 'ts-mixer';

import StandardElement from '../../../../elements/Standard';
import FallbackVisitor from '../../FallbackVisitor';
import FixedFieldsVisitor from '../../generics/FixedFieldsVisitor';
import FixedFieldsVisitor, { FixedFieldsVisitorOptions } from '../../generics/FixedFieldsVisitor';

class StandardVisitor extends Mixin(FixedFieldsVisitor, FallbackVisitor) {
public readonly element: StandardElement;
public declare readonly element: StandardElement;

constructor(options = {}) {
constructor(options: FixedFieldsVisitorOptions) {
super(options);
this.specPath = always(['document', 'objects', 'Standard']);
this.element = new StandardElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,25 @@ import {
toValue,
} from '@swagger-api/apidom-core';

import SpecificationVisitor from '../SpecificationVisitor';
import SpecificationVisitor, { SpecificationVisitorOptions } from '../SpecificationVisitor';

export interface FixedFieldsVisitorOptions extends SpecificationVisitorOptions {
readonly specPath: (element: Element) => string[];
readonly ignoredFields?: string[];
}

class FixedFieldsVisitor extends SpecificationVisitor {
public specPath!: (element: Element) => string[];
protected specPath: (element: Element) => string[];

public ignoredFields: string[] = [];
protected ignoredFields: string[];

constructor({ specPath, ignoredFields, ...rest }: FixedFieldsVisitorOptions) {
super({ ...rest });
this.specPath = specPath;
this.ignoredFields = ignoredFields || [];
}

public ObjectElement(objectElement: ObjectElement) {
ObjectElement(objectElement: ObjectElement) {
const specPath = this.specPath(objectElement);
const fields = this.retrieveFixedFields(specPath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ TreeCursorSyntaxNode {
column: 1,
row: 0,
},
fieldName: undefined,
hasError: false,
isMissing: false,
isNamed: false,
Expand Down Expand Up @@ -49,6 +50,7 @@ TreeCursorSyntaxNode {
column: 3,
row: 0,
},
fieldName: undefined,
hasError: false,
isMissing: false,
isNamed: false,
Expand Down Expand Up @@ -86,6 +88,7 @@ TreeCursorSyntaxNode {
column: 6,
row: 0,
},
fieldName: undefined,
hasError: false,
isMissing: false,
isNamed: false,
Expand Down Expand Up @@ -121,6 +124,7 @@ TreeCursorSyntaxNode {
column: 6,
row: 0,
},
fieldName: undefined,
hasError: false,
isMissing: false,
isNamed: true,
Expand Down
Loading

0 comments on commit dd369a0

Please sign in to comment.