-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ns-workflows-1): change from stamps to TypeScript classes (#…
…3720) Co-authored-by: Vladimír Gorej <[email protected]> Refs #3481
- Loading branch information
Showing
45 changed files
with
811 additions
and
711 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 10 additions & 10 deletions
20
packages/apidom-ns-workflows-1/src/refractor/visitors/FallbackVisitor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
import stampit from 'stampit'; | ||
import { Element, BREAK, cloneDeep } from '@swagger-api/apidom-core'; | ||
|
||
import Visitor from './Visitor'; | ||
import Visitor, { VisitorOptions } from './Visitor'; | ||
|
||
/** | ||
* This visitor is responsible for falling back to current traversed element. | ||
* Given WorkflowsSpecificationVisitor expects ObjectElement to be traversed. If | ||
* different Element is provided FallBackVisitor is responsible to assigning | ||
* this Element as current element. | ||
*/ | ||
const FallbackVisitor = stampit(Visitor, { | ||
methods: { | ||
enter(element: Element) { | ||
this.element = cloneDeep(element); | ||
return BREAK; | ||
}, | ||
}, | ||
}); | ||
|
||
export type { VisitorOptions as FallbackVisitorOptions }; | ||
|
||
class FallbackVisitor extends Visitor { | ||
enter(element: Element) { | ||
this.element = cloneDeep(element); | ||
return BREAK; | ||
} | ||
} | ||
|
||
export default FallbackVisitor; |
19 changes: 9 additions & 10 deletions
19
packages/apidom-ns-workflows-1/src/refractor/visitors/SpecificationExtensionVisitor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
import stampit from 'stampit'; | ||
import { MemberElement, BREAK, cloneDeep } from '@swagger-api/apidom-core'; | ||
|
||
import SpecificationVisitor from './SpecificationVisitor'; | ||
|
||
const SpecificationExtensionVisitor = stampit(SpecificationVisitor, { | ||
methods: { | ||
MemberElement(memberElement: MemberElement) { | ||
this.element = cloneDeep(memberElement); | ||
this.element.classes.push('specification-extension'); | ||
class SpecificationExtensionVisitor extends SpecificationVisitor { | ||
public declare element: MemberElement; | ||
|
||
return BREAK; | ||
}, | ||
}, | ||
}); | ||
MemberElement(memberElement: MemberElement) { | ||
this.element = cloneDeep(memberElement); | ||
this.element.classes.push('specification-extension'); | ||
|
||
return BREAK; | ||
} | ||
} | ||
|
||
export default SpecificationExtensionVisitor; |
114 changes: 57 additions & 57 deletions
114
packages/apidom-ns-workflows-1/src/refractor/visitors/SpecificationVisitor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,77 @@ | ||
import stampit from 'stampit'; | ||
import { pathSatisfies, path, pick } from 'ramda'; | ||
import { isFunction, isUndefined } from 'ramda-adjunct'; | ||
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 Type for every visitor that does | ||
* internal look-ups to retrieve other child visitors. | ||
*/ | ||
const SpecificationVisitor = stampit(Visitor, { | ||
props: { | ||
specObj: null, | ||
passingOptionsNames: ['specObj'], | ||
}, | ||
// @ts-ignore | ||
init({ specObj = this.specObj }) { | ||
export interface SpecificationVisitorOptions extends VisitorOptions { | ||
readonly specObj: typeof specification; | ||
} | ||
|
||
class SpecificationVisitor extends Visitor { | ||
protected readonly specObj!: typeof specification; | ||
|
||
protected readonly passingOptionsNames = ['specObj']; | ||
|
||
constructor({ specObj, ...rest }: SpecificationVisitorOptions) { | ||
super({ ...rest }); | ||
this.specObj = specObj; | ||
}, | ||
methods: { | ||
retrievePassingOptions() { | ||
return pick(this.passingOptionsNames, this); | ||
}, | ||
} | ||
|
||
retrievePassingOptions() { | ||
return pick(this.passingOptionsNames as (keyof this)[], this); | ||
} | ||
|
||
retrieveFixedFields(specPath) { | ||
const fixedFields = path(['visitors', ...specPath, 'fixedFields'], this.specObj); | ||
if (typeof fixedFields === 'object' && fixedFields !== null) { | ||
return Object.keys(fixedFields); | ||
} | ||
return []; | ||
}, | ||
retrieveFixedFields(specPath: string[]) { | ||
const fixedFields = path(['visitors', ...specPath, 'fixedFields'], this.specObj); | ||
if (typeof fixedFields === 'object' && fixedFields !== null) { | ||
return Object.keys(fixedFields); | ||
} | ||
return []; | ||
} | ||
|
||
retrieveVisitor(specPath) { | ||
if (pathSatisfies(isFunction, ['visitors', ...specPath], this.specObj)) { | ||
return path(['visitors', ...specPath], this.specObj); | ||
} | ||
retrieveVisitor(specPath: string[]) { | ||
if (pathSatisfies(isFunction, ['visitors', ...specPath], this.specObj)) { | ||
return path(['visitors', ...specPath], this.specObj); | ||
} | ||
|
||
return path(['visitors', ...specPath, '$visitor'], this.specObj); | ||
}, | ||
return path(['visitors', ...specPath, '$visitor'], this.specObj); | ||
} | ||
|
||
retrieveVisitorInstance(specPath, options = {}) { | ||
const passingOpts = this.retrievePassingOptions(); | ||
retrieveVisitorInstance(specPath: string[], options = {}): Visitor { | ||
const passingOpts = this.retrievePassingOptions(); | ||
const VisitorClz = this.retrieveVisitor(specPath) as typeof Visitor; | ||
const visitorOpts = { ...passingOpts, ...options }; | ||
|
||
return this.retrieveVisitor(specPath)({ ...passingOpts, ...options }); | ||
}, | ||
return new VisitorClz(visitorOpts); | ||
} | ||
|
||
toRefractedElement(specPath: string[], element, options = {}) { | ||
/** | ||
* 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 | ||
* 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. | ||
*/ | ||
const visitor = this.retrieveVisitorInstance(specPath, options); | ||
const visitorPrototype = Object.getPrototypeOf(visitor); | ||
toRefractedElement(specPath: string[], element: any, options = {}) { | ||
/** | ||
* 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 | ||
* 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. | ||
*/ | ||
const visitor = this.retrieveVisitorInstance(specPath, options); | ||
|
||
if (isUndefined(this.fallbackVisitorPrototype)) { | ||
this.fallbackVisitorPrototype = Object.getPrototypeOf( | ||
this.retrieveVisitorInstance(['value']), | ||
); | ||
} | ||
if (this.fallbackVisitorPrototype === visitorPrototype) { | ||
return cloneDeep(element); | ||
} | ||
if (visitor instanceof FallbackVisitor && visitor?.constructor === FallbackVisitor) { | ||
return cloneDeep(element); | ||
} | ||
|
||
// standard processing continues | ||
visit(element, visitor, { keyMap, ...options, nodeTypeGetter: getNodeType }); | ||
return visitor.element; | ||
}, | ||
}, | ||
}); | ||
// @ts-ignore | ||
visit(element, visitor, { keyMap, ...options, nodeTypeGetter: getNodeType }); | ||
return visitor.element; | ||
} | ||
} | ||
|
||
export default SpecificationVisitor; |
33 changes: 18 additions & 15 deletions
33
packages/apidom-ns-workflows-1/src/refractor/visitors/Visitor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,21 @@ | ||
import stampit from 'stampit'; | ||
import { hasElementSourceMap } from '@swagger-api/apidom-core'; | ||
import { hasElementSourceMap, Element } from '@swagger-api/apidom-core'; | ||
|
||
const Visitor = stampit({ | ||
props: { | ||
element: null, | ||
}, | ||
methods: { | ||
copyMetaAndAttributes(from, to) { | ||
// copy sourcemaps | ||
if (hasElementSourceMap(from)) { | ||
to.meta.set('sourceMap', from.meta.get('sourceMap')); | ||
} | ||
}, | ||
}, | ||
}); | ||
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 | ||
if (hasElementSourceMap(from)) { | ||
to.meta.set('sourceMap', from.meta.get('sourceMap')); | ||
} | ||
} | ||
} | ||
|
||
export default Visitor; |
Oops, something went wrong.