mon-entreprise/publicodes/source/parseReference.ts

76 lines
1.8 KiB
TypeScript
Raw Normal View History

import { Leaf } from './components/mecanisms/common'
import parseRule from './parseRule'
2020-03-26 15:03:19 +00:00
import { disambiguateRuleReference } from './ruleUtils'
export const parseReference = (
rules,
rule,
parsedRules,
filter
) => partialReference => {
const dottedName = disambiguateRuleReference(
2020-03-26 15:03:19 +00:00
rules,
rule.dottedName,
partialReference
)
const inInversionFormula = rule.formule?.['inversion numérique']
const parsedRule =
parsedRules[dottedName] ||
// the 'inversion numérique' formula should not exist. The instructions to the evaluation should be enough to infer that an inversion is necessary (assuming it is possible, the client decides this)
2020-03-26 15:03:19 +00:00
(!inInversionFormula && parseRule(rules, dottedName, parsedRules))
const unit = parsedRule.unit
return {
nodeKind: 'reference',
jsx: Leaf,
name: partialReference,
2019-07-01 15:59:57 +00:00
category: 'reference',
partialReference,
dottedName,
explanation: { ...parsedRule, filter, contextRuleName: rule.dottedName },
unit
}
}
type parseReferenceTransformsParameters = {
variable: { fragments: Array<string> }
filter?: string
unit?: string
}
export const parseReferenceTransforms = (rules, rule, parsedRules) => ({
variable,
filter,
unit
}: parseReferenceTransformsParameters) => {
const referenceName = variable.fragments.join(' . ')
const originalNode = parseReference(
rules,
rule,
parsedRules,
filter
)(referenceName)
return {
...originalNode,
nodeKind: 'referenceWithTransforms',
explanation: {
originalNode,
filter,
unit
},
// Decorate node with the composante filter (either who is paying, either tax free)
...(filter
? {
cotisation: {
...(originalNode as any).cotisation,
'dû par': filter,
'impôt sur le revenu': filter
}
}
: {}),
unit: unit || originalNode.unit
}
}