-
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.
feat(ast): see edits by the following merged visitors (#3412)
This change is specific to function which allows visitors to run in parallel.
- Loading branch information
Showing
4 changed files
with
211 additions
and
63 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import sinon from 'sinon'; | ||
import { assert } from 'chai'; | ||
|
||
import { visit, mergeAllVisitors } from '../../src'; | ||
|
||
describe('visitor', function () { | ||
context('given structure with cycle', function () { | ||
specify('should skip over a sub-tree to avoid recursion', function () { | ||
const visitor = { | ||
enter() {}, | ||
}; | ||
const structure = { | ||
type: 'object', | ||
children: [ | ||
{ type: 'number', value: 1 }, | ||
{ type: 'string', value: 'test' }, | ||
{ type: 'object', children: [] }, | ||
], | ||
}; | ||
// @ts-ignore | ||
structure.children[2].children.push(structure); | ||
|
||
sinon.spy(visitor, 'enter'); | ||
// @ts-ignore | ||
visit(structure, visitor, { keyMap: { object: ['children'] } }); | ||
|
||
// @ts-ignore | ||
assert.strictEqual(visitor.enter.callCount, 4); | ||
}); | ||
}); | ||
|
||
context('mergeAll', function () { | ||
context('given exposeEdits=true', function () { | ||
specify('should see edited node', function () { | ||
const visitor1 = { | ||
string: { | ||
enter() { | ||
return { type: 'boolean', value: true }; | ||
}, | ||
}, | ||
}; | ||
const visitor2 = { | ||
boolean: { | ||
enter(node: any) { | ||
node.value = false; // eslint-disable-line no-param-reassign | ||
}, | ||
}, | ||
}; | ||
const structure = { | ||
type: 'object', | ||
children: [ | ||
{ type: 'number', value: 1 }, | ||
{ type: 'string', value: 'test' }, | ||
{ type: 'object', children: [] }, | ||
], | ||
}; | ||
const mergedVisitor = mergeAllVisitors([visitor1, visitor2], { exposeEdits: true }); | ||
// @ts-ignore | ||
const newStructure = visit(structure, mergedVisitor, { keyMap: { object: ['children'] } }); | ||
|
||
assert.deepEqual(newStructure, { | ||
type: 'object', | ||
children: [ | ||
{ type: 'number', value: 1 }, | ||
{ type: 'boolean', value: false }, | ||
{ type: 'object', children: [] }, | ||
], | ||
}); | ||
}); | ||
}); | ||
|
||
context('given exposeEditor=false', function () { | ||
specify('should not see edited node', function () { | ||
const visitor1 = { | ||
string: { | ||
enter() { | ||
return { type: 'boolean', value: true }; | ||
}, | ||
}, | ||
}; | ||
const visitor2 = { | ||
boolean: { | ||
enter(node: any) { | ||
node.value = false; // eslint-disable-line no-param-reassign | ||
}, | ||
}, | ||
}; | ||
const structure = { | ||
type: 'object', | ||
children: [ | ||
{ type: 'number', value: 1 }, | ||
{ type: 'string', value: 'test' }, | ||
{ type: 'object', children: [] }, | ||
], | ||
}; | ||
const mergedVisitor = mergeAllVisitors([visitor1, visitor2]); | ||
// @ts-ignore | ||
const newStructure = visit(structure, mergedVisitor, { keyMap: { object: ['children'] } }); | ||
|
||
assert.deepEqual(newStructure, { | ||
type: 'object', | ||
children: [ | ||
{ type: 'number', value: 1 }, | ||
{ type: 'boolean', value: true }, | ||
{ type: 'object', children: [] }, | ||
], | ||
}); | ||
}); | ||
}); | ||
|
||
specify('should see edited node in leave hook', function () { | ||
const visitor1 = { | ||
string: { | ||
enter() { | ||
return { type: 'foo', value: 'bar' }; | ||
}, | ||
}, | ||
}; | ||
const visitor2 = { | ||
foo: { | ||
leave(node: any) { | ||
node.value = 'foo'; // eslint-disable-line no-param-reassign | ||
}, | ||
}, | ||
}; | ||
const structure = { | ||
type: 'object', | ||
children: [ | ||
{ type: 'number', value: 1 }, | ||
{ type: 'string', value: 2 }, | ||
{ type: 'object', children: [] }, | ||
], | ||
}; | ||
const mergedVisitor = mergeAllVisitors([visitor1, visitor2]); | ||
// @ts-ignore | ||
const newStructure = visit(structure, mergedVisitor, { keyMap: { object: ['children'] } }); | ||
|
||
assert.deepEqual(newStructure, { | ||
type: 'object', | ||
children: [ | ||
{ type: 'number', value: 1 }, | ||
{ type: 'foo', value: 'foo' }, | ||
{ type: 'object', children: [] }, | ||
], | ||
}); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
33 changes: 20 additions & 13 deletions
33
packages/apidom-core/src/refractor/plugins/utils/index.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,29 +1,36 @@ | ||
import { Element } from 'minim'; | ||
import { propOr } from 'ramda'; | ||
import { mergeDeepRight, propOr } from 'ramda'; | ||
import { invokeArgs } from 'ramda-adjunct'; | ||
|
||
import createToolbox from '../../toolbox'; | ||
import { getNodeType, mergeAllVisitors, visit } from '../../../traversal/visitor'; | ||
|
||
const defaultDispatchPluginsOptions = { | ||
toolboxCreator: createToolbox, | ||
visitorOptions: { | ||
nodeTypeGetter: getNodeType, | ||
exposeEdits: true, | ||
}, | ||
}; | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export const dispatchPlugins = <T extends Element>(element: T, plugins: any[], options = {}): T => { | ||
export const dispatchPlugins = <T extends Element>( | ||
element: T, | ||
plugins: ((toolbox: any) => object)[], | ||
options = {}, | ||
): T => { | ||
if (plugins.length === 0) return element; | ||
|
||
const toolboxCreator = propOr(createToolbox, 'toolboxCreator', options) as typeof createToolbox; | ||
const visitorOptions = propOr({}, 'visitorOptions', options); | ||
const nodeTypeGetter = propOr( | ||
getNodeType, | ||
'nodeTypeGetter', | ||
visitorOptions, | ||
) as typeof getNodeType; | ||
const mergedOptions = mergeDeepRight(defaultDispatchPluginsOptions, options); | ||
const { toolboxCreator, visitorOptions } = mergedOptions; | ||
const toolbox = toolboxCreator(); | ||
const pluginsSpecs = plugins.map((plugin: any) => plugin(toolbox)); | ||
const pluginsVisitor = mergeAllVisitors(pluginsSpecs.map(propOr({}, 'visitor')), { | ||
nodeTypeGetter, | ||
const pluginsSpecs = plugins.map((plugin) => plugin(toolbox)); | ||
const mergedPluginsVisitor = mergeAllVisitors(pluginsSpecs.map(propOr({}, 'visitor')), { | ||
...visitorOptions, | ||
}); | ||
|
||
pluginsSpecs.forEach(invokeArgs(['pre'], [])); | ||
const newElement = visit(element, pluginsVisitor, visitorOptions as any); | ||
const newElement = visit(element, mergedPluginsVisitor, visitorOptions as any); | ||
pluginsSpecs.forEach(invokeArgs(['post'], [])); | ||
return newElement as T; | ||
}; |