diff --git a/mon-entreprise/source/components/utils/markdown.tsx b/mon-entreprise/source/components/utils/markdown.tsx index ba37cd263..0ebd24f85 100644 --- a/mon-entreprise/source/components/utils/markdown.tsx +++ b/mon-entreprise/source/components/utils/markdown.tsx @@ -133,7 +133,7 @@ export const MarkdownWithAnchorLinks = ({ ) const flatMapChildren = (children: React.ReactNode): Array => { - return React.Children.toArray(children).flatMap(child => + return React.Children.toArray(children).flatMap((child) => typeof child !== 'object' || !('props' in child) ? child : child.props?.value ?? flatMapChildren(child.props?.children) diff --git a/publicodes/source/AST/index.ts b/publicodes/source/AST/index.ts index 1cc7e4a28..b3b253595 100644 --- a/publicodes/source/AST/index.ts +++ b/publicodes/source/AST/index.ts @@ -5,7 +5,6 @@ import { ReplacementNode } from '../replacement' import { RuleNode } from '../rule' import { ASTNode, NodeKind, TraverseFunction } from './types' - type TransformASTFunction = (n: ASTNode) => ASTNode /** This function creates a transormation of the AST from on a simpler @@ -22,26 +21,26 @@ type TransformASTFunction = (n: ASTNode) => ASTNode by using the function passed as second argument. The returned value will be the transformed version of the node. */ - export function updateAST( - fn: ( - node: ASTNode, - updateFn: TransformASTFunction - ) => ASTNode | undefined | false - ): TransformASTFunction { - function traverseFn(node: ASTNode) { - const updatedNode = fn(node, traverseFn) - if (updatedNode === false) { - return node - } - if (updatedNode === undefined) { - return traverseASTNode(traverseFn, node) - } - return updatedNode +export function updateAST( + fn: ( + node: ASTNode, + updateFn: TransformASTFunction + ) => ASTNode | undefined | false +): TransformASTFunction { + function traverseFn(node: ASTNode) { + const updatedNode = fn(node, traverseFn) + if (updatedNode === false) { + return node } - return traverseFn + if (updatedNode === undefined) { + return traverseASTNode(traverseFn, node) + } + return updatedNode } - - /** + return traverseFn +} + +/** This function allows to construct a specific value while exploring the AST with a simple reducing function as argument. @@ -57,22 +56,21 @@ type TransformASTFunction = (n: ASTNode) => ASTNode by using the function passed as second argument. The returned value will be the reduced version of the node */ - - export function reduceAST( - fn: (acc: T, n: ASTNode, reduceFn: (n: ASTNode) => T) => T | undefined, - start: T, - node: ASTNode - ): T { - function traverseFn(acc: T, node: ASTNode): T { - const result = fn(acc, node, traverseFn.bind(null, start)) - if (result === undefined) { - return gatherNodes(node).reduce(traverseFn, acc) - } - return result - } - return traverseFn(start, node) - } +export function reduceAST( + fn: (acc: T, n: ASTNode, reduceFn: (n: ASTNode) => T) => T | undefined, + start: T, + node: ASTNode +): T { + function traverseFn(acc: T, node: ASTNode): T { + const result = fn(acc, node, traverseFn.bind(null, start)) + if (result === undefined) { + return gatherNodes(node).reduce(traverseFn, acc) + } + return result + } + return traverseFn(start, node) +} function gatherNodes(node: ASTNode): ASTNode[] { const nodes: ASTNode[] = [] diff --git a/publicodes/source/index.ts b/publicodes/source/index.ts index bc04ae842..b4f4bcffd 100644 --- a/publicodes/source/index.ts +++ b/publicodes/source/index.ts @@ -8,7 +8,7 @@ import parsePublicodes, { disambiguateReference } from './parsePublicodes' import { getReplacements, inlineReplacements, - ReplacementNode + ReplacementNode, } from './replacement' import { Rule, RuleNode } from './rule' import * as utils from './ruleUtils'