diff --git a/publicodes/source/AST/graph.ts b/publicodes/source/AST/graph.ts index daeb51898..d0305d3f8 100644 --- a/publicodes/source/AST/graph.ts +++ b/publicodes/source/AST/graph.ts @@ -72,7 +72,7 @@ export function cyclesInDependenciesGraph(rawRules: RawRules): GraphCycles { * Tarjan method, which is **not necessarily the smallest cycle**. However, the * smallest cycle would be the most legibe one… */ -export function cyclicDependencies( +export function cyclicDependencies( rawRules: RawRules ): GraphCyclesWithDependencies { const parsedRules = parsePublicodes(rawRules) diff --git a/publicodes/source/AST/index.ts b/publicodes/source/AST/index.ts index b3b253595..b3751a2c3 100644 --- a/publicodes/source/AST/index.ts +++ b/publicodes/source/AST/index.ts @@ -21,7 +21,7 @@ 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( +export function transformAST( fn: ( node: ASTNode, updateFn: TransformASTFunction diff --git a/publicodes/source/evaluation.tsx b/publicodes/source/evaluation.tsx index bc8e1f9c5..78568cffe 100644 --- a/publicodes/source/evaluation.tsx +++ b/publicodes/source/evaluation.tsx @@ -9,7 +9,7 @@ import { reduce, } from 'ramda' import React from 'react' -import Engine, { evaluationFunction } from '.' +import Engine, { EvaluationFunction } from '.' import { ASTNode, ConstantNode, @@ -77,7 +77,7 @@ function convertNodesToSameUnit(nodes, contextRule, mecanismName) { export const evaluateArray: ( reducer: Parameters[0], start: Parameters[1] -) => evaluationFunction = (reducer, start) => +) => EvaluationFunction = (reducer, start) => function (node: any) { const evaluate = this.evaluateNode.bind(this) const evaluatedNodes = convertNodesToSameUnit( @@ -207,5 +207,5 @@ export function evaluateObject( temporalValue, temporalExplanation, } - } as evaluationFunction + } as EvaluationFunction } diff --git a/publicodes/source/evaluationFunctions.tsx b/publicodes/source/evaluationFunctions.tsx index 3778e377d..97f469c3c 100644 --- a/publicodes/source/evaluationFunctions.tsx +++ b/publicodes/source/evaluationFunctions.tsx @@ -1,4 +1,4 @@ -import { evaluationFunction } from '.' +import { EvaluationFunction } from '.' import { ASTNode } from './AST/types' export let evaluationFunctions = { @@ -7,7 +7,7 @@ export let evaluationFunctions = { export function registerEvaluationFunction< NodeName extends ASTNode['nodeKind'] ->(nodeKind: NodeName, evaluationFunction: evaluationFunction) { +>(nodeKind: NodeName, evaluationFunction: EvaluationFunction) { evaluationFunctions ??= {} if (evaluationFunctions[nodeKind]) { throw Error( diff --git a/publicodes/source/index.ts b/publicodes/source/index.ts index b4f4bcffd..71a89d8c5 100644 --- a/publicodes/source/index.ts +++ b/publicodes/source/index.ts @@ -47,7 +47,7 @@ export { parsePublicodes } export { utils } export { Rule } -export type evaluationFunction = ( +export type EvaluationFunction = ( this: Engine, node: ASTNode & { nodeKind: Kind } ) => ASTNode & { nodeKind: Kind } & EvaluatedNode diff --git a/publicodes/source/mecanisms/applicable.tsx b/publicodes/source/mecanisms/applicable.tsx index 03cb837a6..58e787a61 100644 --- a/publicodes/source/mecanisms/applicable.tsx +++ b/publicodes/source/mecanisms/applicable.tsx @@ -1,5 +1,5 @@ import React from 'react' -import { evaluationFunction } from '..' +import { EvaluationFunction } from '..' import parse from '../parse' import { InfixMecanism, Mecanism } from '../components/mecanisms/common' import { bonus, makeJsx, mergeMissing } from '../evaluation' @@ -26,7 +26,7 @@ function MecanismApplicable({ explanation }) { ) } -const evaluate: evaluationFunction<'applicable si'> = function (node) { +const evaluate: EvaluationFunction<'applicable si'> = function (node) { const explanation = { ...node.explanation } const condition = this.evaluateNode(explanation.condition) let valeur = explanation.valeur diff --git a/publicodes/source/mecanisms/arrondi.tsx b/publicodes/source/mecanisms/arrondi.tsx index c508293e5..e52014bd9 100644 --- a/publicodes/source/mecanisms/arrondi.tsx +++ b/publicodes/source/mecanisms/arrondi.tsx @@ -1,5 +1,5 @@ import React from 'react' -import { evaluationFunction } from '..' +import { EvaluationFunction } from '..' import { InfixMecanism } from '../components/mecanisms/common' import { makeJsx, mergeAllMissing } from '../evaluation' import { registerEvaluationFunction } from '../evaluationFunctions' @@ -30,7 +30,7 @@ function roundWithPrecision(n: number, fractionDigits: number) { return +n.toFixed(fractionDigits) } -const evaluate: evaluationFunction<'arrondi'> = function (node) { +const evaluate: EvaluationFunction<'arrondi'> = function (node) { const valeur = this.evaluateNode(node.explanation.valeur) const nodeValue = valeur.nodeValue let arrondi = node.explanation.arrondi @@ -56,7 +56,7 @@ const evaluate: evaluationFunction<'arrondi'> = function (node) { } } -export default function Arrondi(v, context) { +export default function parseArrondi(v, context) { const explanation = { valeur: parse(v.valeur, context), arrondi: parse(v.arrondi, context), @@ -64,10 +64,10 @@ export default function Arrondi(v, context) { return { jsx: MecanismArrondi, explanation, - nodeKind: Arrondi.nom, + nodeKind: parseArrondi.nom, } } -Arrondi.nom = 'arrondi' as const +parseArrondi.nom = 'arrondi' as const -registerEvaluationFunction(Arrondi.nom, evaluate) +registerEvaluationFunction(parseArrondi.nom, evaluate) diff --git a/publicodes/source/mecanisms/barème.ts b/publicodes/source/mecanisms/barème.ts index ef67d0c54..0c065a249 100644 --- a/publicodes/source/mecanisms/barème.ts +++ b/publicodes/source/mecanisms/barème.ts @@ -1,4 +1,4 @@ -import { evaluationFunction } from '..' +import { EvaluationFunction } from '..' import Barème from '../components/mecanisms/Barème' import { evaluationError } from '../error' import parse from '../parse' @@ -84,7 +84,7 @@ function evaluateBarème(tranches, assiette, evaluate, cache) { } }) } -const evaluate: evaluationFunction<'barème'> = function (node) { +const evaluate: EvaluationFunction<'barème'> = function (node) { const evaluateNode = this.evaluateNode.bind(this) const assiette = this.evaluateNode(node.explanation.assiette) const multiplicateur = this.evaluateNode(node.explanation.multiplicateur) diff --git a/publicodes/source/mecanisms/condition-allof.tsx b/publicodes/source/mecanisms/condition-allof.tsx index a87a57078..33eefd25d 100644 --- a/publicodes/source/mecanisms/condition-allof.tsx +++ b/publicodes/source/mecanisms/condition-allof.tsx @@ -1,6 +1,6 @@ import { is, map } from 'ramda' import React from 'react' -import { evaluationFunction } from '..' +import { EvaluationFunction } from '..' import { Mecanism } from '../components/mecanisms/common' import { ASTNode } from '../AST/types' import { makeJsx, mergeAllMissing } from '../evaluation' @@ -13,7 +13,7 @@ export type TouteCesConditionsNode = { jsx: any } -const evaluate: evaluationFunction<'toutes ces conditions'> = function (node) { +const evaluate: EvaluationFunction<'toutes ces conditions'> = function (node) { const [nodeValue, explanation] = node.explanation.reduce< [boolean | null, Array] >( diff --git a/publicodes/source/mecanisms/condition-oneof.tsx b/publicodes/source/mecanisms/condition-oneof.tsx index 579973d0b..332db66a7 100644 --- a/publicodes/source/mecanisms/condition-oneof.tsx +++ b/publicodes/source/mecanisms/condition-oneof.tsx @@ -1,6 +1,6 @@ import { is, isEmpty, map, max, mergeWith, reduce } from 'ramda' import React from 'react' -import { evaluationFunction } from '..' +import { EvaluationFunction } from '..' import { Mecanism } from '../components/mecanisms/common' import { ASTNode, EvaluatedNode, Evaluation } from '../AST/types' import { @@ -19,7 +19,7 @@ export type UneDeCesConditionsNode = { jsx: any } -const evaluate: evaluationFunction<'une de ces conditions'> = function (node) { +const evaluate: EvaluationFunction<'une de ces conditions'> = function (node) { type Calculations = { explanation: Array nodeValue: Evaluation diff --git a/publicodes/source/mecanisms/durée.tsx b/publicodes/source/mecanisms/durée.tsx index efcfacd79..8bd1397c8 100644 --- a/publicodes/source/mecanisms/durée.tsx +++ b/publicodes/source/mecanisms/durée.tsx @@ -1,5 +1,5 @@ import React from 'react' -import { evaluationFunction } from '..' +import { EvaluationFunction } from '..' import { ASTNode, Unit } from '../AST/types' import { Mecanism } from '../components/mecanisms/common' import { convertToDate, convertToString } from '../date' @@ -43,7 +43,7 @@ const objectShape = { depuis: defaultNode(todayString), "jusqu'à": defaultNode(todayString), } -const evaluate: evaluationFunction<'durée'> = function (node) { +const evaluate: EvaluationFunction<'durée'> = function (node) { const from = this.evaluateNode(node.explanation.depuis) const to = this.evaluateNode(node.explanation["jusqu'à"]) let nodeValue diff --git a/publicodes/source/mecanisms/grille.ts b/publicodes/source/mecanisms/grille.ts index c24a86c08..d23713f5e 100644 --- a/publicodes/source/mecanisms/grille.ts +++ b/publicodes/source/mecanisms/grille.ts @@ -1,5 +1,5 @@ import { lensPath, over } from 'ramda' -import { evaluationFunction } from '..' +import { EvaluationFunction } from '..' import grille from '../components/mecanisms/Grille' import { defaultNode, mergeAllMissing } from '../evaluation' import { registerEvaluationFunction } from '../evaluationFunctions' @@ -57,7 +57,7 @@ const evaluateGrille = (tranches, evaluate) => } }) -const evaluate: evaluationFunction<'grille'> = function (node) { +const evaluate: EvaluationFunction<'grille'> = function (node) { const evaluate = this.evaluateNode.bind(this) const assiette = this.evaluateNode(node.explanation.assiette) const multiplicateur = this.evaluateNode(node.explanation.multiplicateur) diff --git a/publicodes/source/mecanisms/inversion.ts b/publicodes/source/mecanisms/inversion.ts index fa62c1c38..1c1d4553b 100644 --- a/publicodes/source/mecanisms/inversion.ts +++ b/publicodes/source/mecanisms/inversion.ts @@ -1,5 +1,5 @@ import parse from '../parse' -import { evaluationFunction } from '..' +import { EvaluationFunction } from '..' import { ASTNode, ConstantNode, Unit } from '../AST/types' import InversionNumérique from '../components/mecanisms/InversionNumérique' import { mergeMissing } from '../evaluation' @@ -31,7 +31,7 @@ export type InversionNode = { // equal to its situation value, mathematically we search for the zero of the // function x → f(x) - goal. The iteration logic between each test is // implemented in the `uniroot` file. -export const evaluateInversion: evaluationFunction<'inversion'> = function ( +export const evaluateInversion: EvaluationFunction<'inversion'> = function ( node ) { const inversionGoal = node.explanation.inversionCandidates.find( diff --git a/publicodes/source/mecanisms/nonApplicable.tsx b/publicodes/source/mecanisms/nonApplicable.tsx index 48d794d8a..364738bd3 100644 --- a/publicodes/source/mecanisms/nonApplicable.tsx +++ b/publicodes/source/mecanisms/nonApplicable.tsx @@ -1,5 +1,5 @@ import React from 'react' -import { evaluationFunction } from '..' +import { EvaluationFunction } from '..' import { InfixMecanism, Mecanism } from '../components/mecanisms/common' import { ASTNode, EvaluatedNode } from '../AST/types' import { bonus, makeJsx, mergeMissing } from '../evaluation' @@ -27,7 +27,7 @@ function MecanismNonApplicable({ explanation }) { ) } -const evaluate: evaluationFunction<'non applicable si'> = function (node) { +const evaluate: EvaluationFunction<'non applicable si'> = function (node) { const condition = this.evaluateNode(node.explanation.condition) let valeur = node.explanation.valeur if (condition.nodeValue === false || condition.nodeValue === null) { diff --git a/publicodes/source/mecanisms/operation.tsx b/publicodes/source/mecanisms/operation.tsx index dbe4c119a..5314afa48 100644 --- a/publicodes/source/mecanisms/operation.tsx +++ b/publicodes/source/mecanisms/operation.tsx @@ -1,6 +1,6 @@ import { equals, fromPairs, map } from 'ramda' import React from 'react' -import { evaluationFunction } from '..' +import { EvaluationFunction } from '..' import { Operation } from '../components/mecanisms/common' import { ASTNode } from '../AST/types' import { convertToDate } from '../date' @@ -55,7 +55,7 @@ const parseOperation = (k, symbol) => (v, context) => { } as OperationNode } -const evaluate: evaluationFunction<'operation'> = function (node) { +const evaluate: EvaluationFunction<'operation'> = function (node) { const explanation = node.explanation.map((node) => this.evaluateNode(node) ) as [EvaluatedNode, EvaluatedNode] diff --git a/publicodes/source/mecanisms/parDéfaut.tsx b/publicodes/source/mecanisms/parDéfaut.tsx index ebd99593d..8f5107f2c 100644 --- a/publicodes/source/mecanisms/parDéfaut.tsx +++ b/publicodes/source/mecanisms/parDéfaut.tsx @@ -1,5 +1,5 @@ import React from 'react' -import { evaluationFunction } from '..' +import { EvaluationFunction } from '..' import { InfixMecanism } from '../components/mecanisms/common' import { ASTNode } from '../AST/types' import { bonus, makeJsx, mergeMissing } from '../evaluation' @@ -29,7 +29,7 @@ function ParDéfautComponent({ explanation }) { ) } -const evaluate: evaluationFunction<'par défaut'> = function (node) { +const evaluate: EvaluationFunction<'par défaut'> = function (node) { const explanation: { parDéfaut: EvaluatedNode | ASTNode valeur: EvaluatedNode | ASTNode diff --git a/publicodes/source/mecanisms/plafond.tsx b/publicodes/source/mecanisms/plafond.tsx index 5202e989f..001842531 100644 --- a/publicodes/source/mecanisms/plafond.tsx +++ b/publicodes/source/mecanisms/plafond.tsx @@ -1,14 +1,12 @@ import React from 'react' -import { evaluationFunction } from '..' +import { EvaluationFunction } from '..' +import { ASTNode } from '../AST/types' import { InfixMecanism } from '../components/mecanisms/common' import { typeWarning } from '../error' -import parse from '../parse' - import { makeJsx, mergeAllMissing } from '../evaluation' import { registerEvaluationFunction } from '../evaluationFunctions' import { convertNodeToUnit } from '../nodeUnits' -import { ASTNode } from '../AST/types' -import { EvaluatedNode } from '../AST/types' +import parse from '../parse' function MecanismPlafond({ explanation }) { return ( @@ -34,7 +32,7 @@ export type PlafondNode = { jsx: any nodeKind: 'plafond' } -const evaluate: evaluationFunction<'plafond'> = function (node) { +const evaluate: EvaluationFunction<'plafond'> = function (node) { const valeur = this.evaluateNode(node.explanation.valeur) let nodeValue = valeur.nodeValue diff --git a/publicodes/source/mecanisms/plancher.tsx b/publicodes/source/mecanisms/plancher.tsx index 11ff54c0a..82189731c 100644 --- a/publicodes/source/mecanisms/plancher.tsx +++ b/publicodes/source/mecanisms/plancher.tsx @@ -1,5 +1,5 @@ import React from 'react' -import { evaluationFunction } from '..' +import { EvaluationFunction } from '..' import { InfixMecanism } from '../components/mecanisms/common' import { ASTNode } from '../AST/types' import { typeWarning } from '../error' @@ -33,7 +33,7 @@ export type PlancherNode = { jsx: any nodeKind: 'plancher' } -const evaluate: evaluationFunction<'plancher'> = function (node) { +const evaluate: EvaluationFunction<'plancher'> = function (node) { const valeur = this.evaluateNode(node.explanation.valeur) let nodeValue = valeur.nodeValue let plancher = node.explanation.plancher diff --git a/publicodes/source/mecanisms/product.tsx b/publicodes/source/mecanisms/product.tsx index e6c3f97bd..befee2b33 100644 --- a/publicodes/source/mecanisms/product.tsx +++ b/publicodes/source/mecanisms/product.tsx @@ -1,4 +1,4 @@ -import { evaluationFunction } from '..' +import { EvaluationFunction } from '..' import Product from '../components/mecanisms/Product' import { ASTNode } from '../AST/types' import { typeWarning } from '../error' @@ -35,7 +35,7 @@ export const mecanismProduct = (v, context) => { } as ProductNode } -const productEffect: evaluationFunction = function ({ +const productEffect: EvaluationFunction = function ({ assiette, taux, facteur, diff --git a/publicodes/source/mecanisms/recalcul.ts b/publicodes/source/mecanisms/recalcul.ts index 33f9a202d..8cd1fa664 100644 --- a/publicodes/source/mecanisms/recalcul.ts +++ b/publicodes/source/mecanisms/recalcul.ts @@ -1,4 +1,4 @@ -import { evaluationFunction } from '..' +import { EvaluationFunction } from '..' import { ASTNode } from '../AST/types' import Recalcul from '../components/mecanisms/Recalcul' import { defaultNode } from '../evaluation' @@ -18,7 +18,7 @@ export type RecalculNode = { nodeKind: 'recalcul' } -const evaluateRecalcul: evaluationFunction<'recalcul'> = function (node) { +const evaluateRecalcul: EvaluationFunction<'recalcul'> = function (node) { if (this.cache._meta.inRecalcul) { return (defaultNode(false) as any) as RecalculNode & EvaluatedNode } diff --git a/publicodes/source/mecanisms/synchronisation.tsx b/publicodes/source/mecanisms/synchronisation.tsx index 93f62bf16..edd86c688 100644 --- a/publicodes/source/mecanisms/synchronisation.tsx +++ b/publicodes/source/mecanisms/synchronisation.tsx @@ -1,6 +1,6 @@ import { path } from 'ramda' import React from 'react' -import { evaluationFunction } from '..' +import { EvaluationFunction } from '..' import { RuleLinkWithContext } from '../components/RuleLink' import { ASTNode } from '../AST/types' import { registerEvaluationFunction } from '../evaluationFunctions' @@ -15,7 +15,7 @@ export type SynchronisationNode = { nodeKind: 'synchronisation' } -const evaluate: evaluationFunction<'synchronisation'> = function (node: any) { +const evaluate: EvaluationFunction<'synchronisation'> = function (node: any) { const data = this.evaluateNode(node.explanation.data) const valuePath = node.explanation.chemin.split(' . ') const nodeValue = diff --git a/publicodes/source/mecanisms/tauxProgressif.ts b/publicodes/source/mecanisms/tauxProgressif.ts index bf1e44d4e..f4cb42f6c 100644 --- a/publicodes/source/mecanisms/tauxProgressif.ts +++ b/publicodes/source/mecanisms/tauxProgressif.ts @@ -1,4 +1,4 @@ -import { evaluationFunction } from '..' +import { EvaluationFunction } from '..' import tauxProgressif from '../components/mecanisms/TauxProgressif' import { defaultNode, mergeAllMissing } from '../evaluation' import { registerEvaluationFunction } from '../evaluationFunctions' @@ -36,7 +36,7 @@ export default function parseTauxProgressif(v, context): TauxProgressifNode { } } -const evaluate: evaluationFunction<'taux progressif'> = function (node) { +const evaluate: EvaluationFunction<'taux progressif'> = function (node) { const evaluate = this.evaluateNode.bind(this) const assiette = this.evaluateNode(node.explanation.assiette) const multiplicateur = this.evaluateNode(node.explanation.multiplicateur) diff --git a/publicodes/source/mecanisms/variableTemporelle.ts b/publicodes/source/mecanisms/variableTemporelle.ts index 5610fb75c..91218c652 100644 --- a/publicodes/source/mecanisms/variableTemporelle.ts +++ b/publicodes/source/mecanisms/variableTemporelle.ts @@ -1,4 +1,4 @@ -import { evaluationFunction } from '..' +import { EvaluationFunction } from '..' import { ASTNode } from '../AST/types' import { registerEvaluationFunction } from '../evaluationFunctions' import parse from '../parse' @@ -22,7 +22,7 @@ export type VariableTemporelleNode = { nodeKind: 'variable temporelle' } -const evaluate: evaluationFunction<'variable temporelle'> = function ( +const evaluate: EvaluationFunction<'variable temporelle'> = function ( node: any ) { const start = diff --git a/publicodes/source/mecanisms/variations.ts b/publicodes/source/mecanisms/variations.ts index 0f3520a6c..e9451feb9 100644 --- a/publicodes/source/mecanisms/variations.ts +++ b/publicodes/source/mecanisms/variations.ts @@ -1,5 +1,5 @@ import { or } from 'ramda' -import { evaluationFunction } from '..' +import { EvaluationFunction } from '..' import Variations from '../components/mecanisms/Variations' import { ASTNode, EvaluatedNode, Unit } from '../AST/types' import { typeWarning } from '../error' @@ -66,7 +66,7 @@ export default function parseVariations(v, context): VariationNode { } } -const evaluate: evaluationFunction<'variations'> = function (node) { +const evaluate: EvaluationFunction<'variations'> = function (node) { const [temporalValue, explanation, unit] = node.explanation.reduce< [ Temporal, diff --git a/publicodes/source/parsePublicodes.ts b/publicodes/source/parsePublicodes.ts index 48f0dd44f..84ece7965 100644 --- a/publicodes/source/parsePublicodes.ts +++ b/publicodes/source/parsePublicodes.ts @@ -1,12 +1,8 @@ import yaml from 'yaml' import { ParsedRules } from '.' -import { traverseParsedRules, updateAST } from './AST' +import { transformAST, traverseParsedRules } from './AST' import parse from './parse' -import { - getReplacements, - inlineReplacements, - ReplacementNode, -} from './replacement' +import { getReplacements, inlineReplacements } from './replacement' import { Rule, RuleNode } from './rule' import { disambiguateRuleReference } from './ruleUtils' @@ -104,7 +100,7 @@ function transpileRef(object: Record | string | Array) { } export const disambiguateReference = (parsedRules: Record) => - updateAST((node) => { + transformAST((node) => { if (node.nodeKind === 'reference') { const dottedName = disambiguateRuleReference( parsedRules, diff --git a/publicodes/source/replacement.tsx b/publicodes/source/replacement.tsx index 611234833..2e922db12 100644 --- a/publicodes/source/replacement.tsx +++ b/publicodes/source/replacement.tsx @@ -1,5 +1,5 @@ import { groupBy } from 'ramda' -import { updateAST } from './AST' +import { transformAST } from './AST' import { ASTNode } from './AST/types' import { InternalError, warning } from './error' import { defaultNode, makeJsx } from './evaluation' @@ -99,7 +99,7 @@ export function getReplacements( export function inlineReplacements( replacements: Record> ): (n: ASTNode) => ASTNode { - return updateAST((n, fn) => { + return transformAST((n, fn) => { if ( n.nodeKind === 'replacement' || n.nodeKind === 'inversion' ||