publicodes / tools

Set of utility functions to write tooling for Publicodes models
https://publicodes.github.io/tools/
MIT License
8 stars 0 forks source link

Fix: use the parse function from Publicodes instead of simple regexp #11

Closed EmileRolley closed 11 months ago

EmileRolley commented 1 year ago

The problem

Currently, the variable substitution is done via a regular expression:

https://github.com/incubateur-ademe/publiopti/blob/92dd9b2354b1b2b231ab7909ce23b85b61fcd935/source/constantFolding.ts#L131-L142

However, isn't perfect. Indeed, when a rule name is a subset of an other rule name it causes trouble.

For example, the expression tasse de café * café substituting the rule café by its corresponding constant value it will produces the new invalid expression tasse de 42 * 42.

The solution

To avoid this errors, we need to be aware of the semantic of the expression: to parse the AST.

@laem already open a PR to expose the function parseExpression from the publicodes package.