diff --git a/publicodes/source/evaluation.tsx b/publicodes/source/evaluation.tsx index ab4c5217b..dc115a485 100644 --- a/publicodes/source/evaluation.tsx +++ b/publicodes/source/evaluation.tsx @@ -208,7 +208,7 @@ export const evaluationFunctions = { export function registerEvaluationFunction( nodeKind: string, - evaluationFunction: any // TODO: type evaluationFunction + evaluationFunction: evaluationFunction ) { if (evaluationFunctions[nodeKind]) { throw Error( diff --git a/publicodes/source/grammarFunctions.js b/publicodes/source/grammarFunctions.js index e4a66afa8..16b6fff0e 100644 --- a/publicodes/source/grammarFunctions.js +++ b/publicodes/source/grammarFunctions.js @@ -39,9 +39,7 @@ export let variable = ([firstFragment, nextFragment], _, reject) => { return reject } return { - variable: { - fragments - } + variable: fragments.join(' . ') } } diff --git a/publicodes/source/mecanisms/inversion.ts b/publicodes/source/mecanisms/inversion.ts index 7c4d90671..f64c74adc 100644 --- a/publicodes/source/mecanisms/inversion.ts +++ b/publicodes/source/mecanisms/inversion.ts @@ -86,7 +86,7 @@ export const evaluateInversion: evaluationFunction = function(node) { } } -export const mecanismInversion = dottedName => (recurse, v) => { +export const mecanismInversion = (recurse, v, dottedName) => { if (!v.avec) { throw new Error( "Une formule d'inversion doit préciser _avec_ quoi on peut inverser la variable" diff --git a/publicodes/source/mecanisms/one-possibility.tsx b/publicodes/source/mecanisms/one-possibility.tsx index 13c30525b..08e0fd5d0 100644 --- a/publicodes/source/mecanisms/one-possibility.tsx +++ b/publicodes/source/mecanisms/one-possibility.tsx @@ -1,14 +1,17 @@ import { registerEvaluationFunction } from '../evaluation' // TODO : This isn't a real mecanism, cf. #963 -export const mecanismOnePossibility = dottedName => (recurse, v) => ({ +export const mecanismOnePossibility = (recurse, v, dottedName) => ({ ...v, 'une possibilité': 'oui', context: dottedName, nodeKind: 'une possibilité' }) -registerEvaluationFunction('une possibilité', node => ({ - ...node, - missingVariables: { [node.context]: 1 } -})) +registerEvaluationFunction( + 'une possibilité', + (node: ReturnType) => ({ + ...node, + missingVariables: { [node.context]: 1 } + }) +) diff --git a/publicodes/source/mecanisms/recalcul.ts b/publicodes/source/mecanisms/recalcul.ts index 09af180af..5f2f4e1b3 100644 --- a/publicodes/source/mecanisms/recalcul.ts +++ b/publicodes/source/mecanisms/recalcul.ts @@ -53,7 +53,7 @@ const evaluateRecalcul: evaluationFunction = function(node) { } } -export const mecanismRecalcul = dottedNameContext => (recurse, v) => { +export const mecanismRecalcul = (recurse, v, dottedNameContext) => { const amendedSituation = Object.keys(v.avec).map(dottedName => [ recurse(dottedName), recurse(v.avec[dottedName]) diff --git a/publicodes/source/parse.tsx b/publicodes/source/parse.tsx index b83db32f6..7c812f156 100644 --- a/publicodes/source/parse.tsx +++ b/publicodes/source/parse.tsx @@ -29,7 +29,7 @@ import { mecanismSynchronisation } from './mecanisms/synchronisation' import tauxProgressif from './mecanisms/tauxProgressif' import variableTemporelle from './mecanisms/variableTemporelle' import variations, { devariate } from './mecanisms/variations' -import { parseReferenceTransforms } from './parseReference' +import { parseReference, parseReferenceTransforms } from './parseReference' import { EvaluatedRule } from './types' export const parse = (rules, rule, parsedRules) => rawNode => { @@ -101,11 +101,10 @@ Cela vient probablement d'une erreur dans l'indentation const mecanismName = Object.keys(rawNode)[0] const values = rawNode[mecanismName] + // TODO: All parse functions should be "stateless" (ie be simple functions + // with the same list of parameters and not curried fantasy): const parseFunctions = { ...statelessParseFunction, - 'une possibilité': mecanismOnePossibility(rule.dottedName), - 'inversion numérique': mecanismInversion(rule.dottedName), - recalcul: mecanismRecalcul(rule.dottedName), filter: () => parseReferenceTransforms( rules, @@ -115,8 +114,7 @@ Cela vient probablement d'une erreur dans l'indentation filter: values.filter, variable: values.explanation }), - variable: () => - parseReferenceTransforms(rules, rule, parsedRules)({ variable: values }), + variable: () => parseReference(rules, rule, parsedRules, '')(values), unitConversion: () => parseReferenceTransforms( rules, @@ -147,7 +145,7 @@ Vérifiez qu'il n'y ait pas d'erreur dans l'orthographe du nom.` if (values?.variations) { return devariate(recurse, mecanismName, values) } - return parseFn(recurse, values) + return parseFn(recurse, values, rule.dottedName) } catch (e) { if (e instanceof EngineError) { throw e @@ -205,6 +203,9 @@ const statelessParseFunction = { allègement: mecanismReduction, variations, synchronisation: mecanismSynchronisation, + 'une possibilité': mecanismOnePossibility, + 'inversion numérique': mecanismInversion, + recalcul: mecanismRecalcul, valeur: (recurse, v) => recurse(v), constant: (_, v) => ({ type: v.type, diff --git a/publicodes/source/parseReference.ts b/publicodes/source/parseReference.ts index 233e659a1..a6625fa87 100644 --- a/publicodes/source/parseReference.ts +++ b/publicodes/source/parseReference.ts @@ -44,13 +44,12 @@ export const parseReferenceTransforms = (rules, rule, parsedRules) => ({ filter, unit }: parseReferenceTransformsParameters) => { - const referenceName = variable.fragments.join(' . ') const originalNode = parseReference( rules, rule, parsedRules, filter - )(referenceName) + )(variable) return { ...originalNode,