From c9a783a09cfa19e74387e02db8c605725899384c Mon Sep 17 00:00:00 2001 From: Maxime Quandalle Date: Wed, 18 Nov 2020 18:53:37 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=A6=20D=C3=A9place=20les=20r=C3=A8gles?= =?UTF-8?q?=20dans=20un=20paquet=20NPM=20systeme-social?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .prettierignore | 1 + mon-entreprise/package.json | 1 + mon-entreprise/scripts/dottednames.js | 25 ---------- mon-entreprise/scripts/i18n/utils.js | 7 +-- mon-entreprise/scripts/prepare.js | 1 - mon-entreprise/scripts/rules.js | 28 ----------- mon-entreprise/source/actions/actions.ts | 2 +- .../source/components/Distribution.tsx | 2 +- .../source/components/EngineValue.tsx | 2 +- mon-entreprise/source/components/PaySlip.tsx | 2 +- .../source/components/PaySlipSections.tsx | 2 +- .../source/components/QuickLinks.tsx | 2 +- mon-entreprise/source/components/RuleLink.tsx | 2 +- .../source/components/SearchBar.tsx | 2 +- .../source/components/StackedBarChart.tsx | 2 +- .../source/components/TargetSelection.tsx | 2 +- .../components/conversation/AnswerList.tsx | 2 +- .../components/conversation/Explicable.tsx | 2 +- .../components/conversation/RuleInput.tsx | 3 +- .../simulationExplanation/AidesCovid.tsx | 2 +- .../IndépendantExplanation.tsx | 2 +- .../source/components/utils/EngineContext.tsx | 2 +- .../components/utils/useNextQuestion.tsx | 2 +- mon-entreprise/source/reducers/rootReducer.ts | 2 +- mon-entreprise/source/rules/index.ts | 47 ------------------- .../source/selectors/simulationSelectors.ts | 2 +- .../source/selectors/storageSelectors.ts | 2 +- mon-entreprise/source/server.js | 2 +- .../source/sites/mon-entreprise.fr/App.tsx | 2 +- .../sites/mon-entreprise.fr/entry.en.tsx | 2 +- .../sites/mon-entreprise.fr/entry.fr.tsx | 2 +- .../middlewares/trackSimulatorActions.ts | 2 +- .../AideDéclarationIndépendant/index.tsx | 2 +- .../pages/Simulateurs/ArtisteAuteur.tsx | 2 +- .../pages/Simulateurs/ChômagePartiel.tsx | 2 +- .../source/sites/publi.codes/Studio.tsx | 1 - mon-entreprise/test/conversation.test.js | 2 +- mon-entreprise/test/cycles.test.js | 2 +- mon-entreprise/test/real-rules.test.js | 4 +- .../test/regressions/simulations.jest.js | 2 +- mon-entreprise/test/unit-translations.test.js | 2 +- mon-entreprise/tsconfig.json | 2 - package.json | 5 +- systeme-social/CHANGELOG.md | 0 systeme-social/README.md | 6 +++ systeme-social/build.js | 46 ++++++++++++++++++ systeme-social/index.d.ts | 19 ++++++++ systeme-social/package.json | 31 ++++++++++++ .../règles}/.gitignore | 0 .../règles/aaa-impôt.yaml | 0 .../règles}/artiste-auteur.yaml | 0 .../rules => systeme-social/règles}/base.yaml | 0 .../règles}/chômage-partiel.yaml | 0 .../conventions-collectives/bâtiment.yaml | 0 .../experts-comptables.yaml | 0 .../hôtels-cafés-restaurants.yaml | 0 .../conventions-collectives/optique.yaml | 0 .../spectacle-vivant.yaml | 0 .../conventions-collectives/sport.yaml | 0 .../règles}/dirigeant.yaml | 0 .../déclaration-revenu-indépendant.yaml | 0 .../règles}/entreprise-établissement.yaml | 0 .../règles}/profession-libérale.yaml | 0 .../règles}/protection-sociale.yaml | 0 .../règles}/salarié.yaml | 0 .../règles}/situation-personnelle.yaml | 0 yarn.lock | 2 +- 67 files changed, 144 insertions(+), 147 deletions(-) delete mode 100644 mon-entreprise/scripts/dottednames.js delete mode 100644 mon-entreprise/scripts/rules.js delete mode 100644 mon-entreprise/source/rules/index.ts create mode 100644 systeme-social/CHANGELOG.md create mode 100644 systeme-social/README.md create mode 100644 systeme-social/build.js create mode 100644 systeme-social/index.d.ts create mode 100644 systeme-social/package.json rename {mon-entreprise/source/rules => systeme-social/règles}/.gitignore (100%) rename mon-entreprise/source/rules/impôt.yaml => systeme-social/règles/aaa-impôt.yaml (100%) rename {mon-entreprise/source/rules => systeme-social/règles}/artiste-auteur.yaml (100%) rename {mon-entreprise/source/rules => systeme-social/règles}/base.yaml (100%) rename {mon-entreprise/source/rules => systeme-social/règles}/chômage-partiel.yaml (100%) rename {mon-entreprise/source/rules => systeme-social/règles}/conventions-collectives/bâtiment.yaml (100%) rename {mon-entreprise/source/rules => systeme-social/règles}/conventions-collectives/experts-comptables.yaml (100%) rename {mon-entreprise/source/rules => systeme-social/règles}/conventions-collectives/hôtels-cafés-restaurants.yaml (100%) rename {mon-entreprise/source/rules => systeme-social/règles}/conventions-collectives/optique.yaml (100%) rename {mon-entreprise/source/rules => systeme-social/règles}/conventions-collectives/spectacle-vivant.yaml (100%) rename {mon-entreprise/source/rules => systeme-social/règles}/conventions-collectives/sport.yaml (100%) rename {mon-entreprise/source/rules => systeme-social/règles}/dirigeant.yaml (100%) rename {mon-entreprise/source/rules => systeme-social/règles}/déclaration-revenu-indépendant.yaml (100%) rename {mon-entreprise/source/rules => systeme-social/règles}/entreprise-établissement.yaml (100%) rename {mon-entreprise/source/rules => systeme-social/règles}/profession-libérale.yaml (100%) rename {mon-entreprise/source/rules => systeme-social/règles}/protection-sociale.yaml (100%) rename {mon-entreprise/source/rules => systeme-social/règles}/salarié.yaml (100%) rename {mon-entreprise/source/rules => systeme-social/règles}/situation-personnelle.yaml (100%) diff --git a/.prettierignore b/.prettierignore index a9ba028ce..62d24c8fa 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1 +1,2 @@ .eslintrc.js +dist diff --git a/mon-entreprise/package.json b/mon-entreprise/package.json index ea8aaa597..85e2812e0 100644 --- a/mon-entreprise/package.json +++ b/mon-entreprise/package.json @@ -99,6 +99,7 @@ "reselect": "^4.0.0", "styled-components": "^5.1.0", "swr": "^0.1.16", + "systeme-social": "^1.0.0", "whatwg-fetch": "^3.0.0", "yaml": "^1.9.2" }, diff --git a/mon-entreprise/scripts/dottednames.js b/mon-entreprise/scripts/dottednames.js deleted file mode 100644 index d5d928803..000000000 --- a/mon-entreprise/scripts/dottednames.js +++ /dev/null @@ -1,25 +0,0 @@ -// It is currently not possible to automatically type yaml import with -// Typescript types, so we manually watch the yaml file containing the rules, -// convert it to json and persit it on the file system so that we can access the -// list of dotted names in the Typescript types. -// -// A fututre version of typescript may support "plugin" to type files such as -// yaml. - -const fs = require('fs') -const path = require('path') -const { readRules } = require('./rules') - -const sourceDirPath = path.resolve(__dirname, '../source/rules') -// Note: we can't put the output file in the fs.watched directory -const outPath = path.resolve(__dirname, '../source/types/dottednames.json') - -function persistJsonFileFromYaml() { - const rules = readRules() - const jsonString = JSON.stringify(rules, null, 2) - fs.writeFileSync(outPath, jsonString) -} - -persistJsonFileFromYaml() -exports.watchDottedNames = () => - fs.watch(sourceDirPath, persistJsonFileFromYaml) diff --git a/mon-entreprise/scripts/i18n/utils.js b/mon-entreprise/scripts/i18n/utils.js index d75d86a42..c1bc1886d 100644 --- a/mon-entreprise/scripts/i18n/utils.js +++ b/mon-entreprise/scripts/i18n/utils.js @@ -4,10 +4,9 @@ var fs = require('fs') var path = require('path') let R = require('ramda') var querystring = require('querystring') -let { readRules } = require('../rules') -// let { utils } = require('publicodes') - +let rules = require('systeme-social') let { parse } = require('yaml') + let rulesTranslationPath = path.resolve('source/locales/rules-en.yaml') let UiTranslationPath = path.resolve('source/locales/ui-en.yaml') @@ -21,8 +20,6 @@ let attributesToTranslate = [ ] function getRulesMissingTranslations() { - let rules = readRules() - let currentExternalization = parse( fs.readFileSync(rulesTranslationPath, 'utf-8') ) diff --git a/mon-entreprise/scripts/prepare.js b/mon-entreprise/scripts/prepare.js index 307b42c34..ebf6bf168 100644 --- a/mon-entreprise/scripts/prepare.js +++ b/mon-entreprise/scripts/prepare.js @@ -1,3 +1,2 @@ -require('./dottednames.js') require('./fetch-releases.js') require('./fetch-stats.js') diff --git a/mon-entreprise/scripts/rules.js b/mon-entreprise/scripts/rules.js deleted file mode 100644 index e61deca19..000000000 --- a/mon-entreprise/scripts/rules.js +++ /dev/null @@ -1,28 +0,0 @@ -// In a vanilla NodeJS environment it is not possible to use the "import" -// statement with the Webpack transformer (from yaml to json). - -const fs = require('fs') -const path = require('path') -const yaml = require('yaml') - -const publicodesDir = path.resolve(__dirname, '../source/rules') - -function concatenateFilesInDir(dirPath = publicodesDir) { - return fs - .readdirSync(dirPath) - .map(filename => { - const fullpath = path.join(dirPath, filename) - if (fs.statSync(fullpath).isDirectory()) { - return concatenateFilesInDir(fullpath) - } else { - return filename.endsWith('.yaml') ? fs.readFileSync(fullpath) : '' - } - }) - .reduce((acc, cur) => acc + '\n' + cur, '') -} - -function readRules() { - return yaml.parse(concatenateFilesInDir()) -} - -exports.readRules = readRules diff --git a/mon-entreprise/source/actions/actions.ts b/mon-entreprise/source/actions/actions.ts index f460c9149..f291a3b1e 100644 --- a/mon-entreprise/source/actions/actions.ts +++ b/mon-entreprise/source/actions/actions.ts @@ -2,7 +2,7 @@ import { SitePaths } from 'Components/utils/SitePathsContext' import { History } from 'history' import { RootState, SimulationConfig } from 'Reducers/rootReducer' import { ThunkAction } from 'redux-thunk' -import { DottedName } from 'Rules' +import { DottedName } from 'systeme-social' import { deletePersistedSimulation } from '../storage/persistSimulation' import { CompanyStatusAction } from './companyStatusActions' diff --git a/mon-entreprise/source/components/Distribution.tsx b/mon-entreprise/source/components/Distribution.tsx index 54a9cceee..93637452b 100644 --- a/mon-entreprise/source/components/Distribution.tsx +++ b/mon-entreprise/source/components/Distribution.tsx @@ -2,7 +2,7 @@ import { EngineContext, useEngine } from 'Components/utils/EngineContext' import { max } from 'ramda' import { useContext } from 'react' import { useSelector } from 'react-redux' -import { DottedName } from 'Rules' +import { DottedName } from 'systeme-social' import { targetUnitSelector } from 'Selectors/simulationSelectors' import BarChartBranch from './BarChart' import './Distribution.css' diff --git a/mon-entreprise/source/components/EngineValue.tsx b/mon-entreprise/source/components/EngineValue.tsx index 571a4a795..7abcae83f 100644 --- a/mon-entreprise/source/components/EngineValue.tsx +++ b/mon-entreprise/source/components/EngineValue.tsx @@ -1,7 +1,7 @@ import Engine, { formatValue } from 'publicodes' import React, { useContext } from 'react' import { useTranslation } from 'react-i18next' -import { DottedName } from 'Rules' +import { DottedName } from 'systeme-social' import { coerceArray } from '../utils' import RuleLink from './RuleLink' import { EngineContext, useEngine } from './utils/EngineContext' diff --git a/mon-entreprise/source/components/PaySlip.tsx b/mon-entreprise/source/components/PaySlip.tsx index 654dfad54..2f0f2ba7d 100644 --- a/mon-entreprise/source/components/PaySlip.tsx +++ b/mon-entreprise/source/components/PaySlip.tsx @@ -10,7 +10,7 @@ import { } from 'publicodes' import { Fragment, useContext } from 'react' import { Trans, useTranslation } from 'react-i18next' -import { DottedName } from 'Rules' +import { DottedName } from 'systeme-social' import './PaySlip.css' import { Line, SalaireBrutSection, SalaireNetSection } from './PaySlipSections' diff --git a/mon-entreprise/source/components/PaySlipSections.tsx b/mon-entreprise/source/components/PaySlipSections.tsx index d72f38768..7a5876dda 100644 --- a/mon-entreprise/source/components/PaySlipSections.tsx +++ b/mon-entreprise/source/components/PaySlipSections.tsx @@ -1,7 +1,7 @@ import Value, { Condition, ValueProps } from 'Components/EngineValue' import RuleLink from 'Components/RuleLink' import { Trans } from 'react-i18next' -import { DottedName } from 'Rules' +import { DottedName } from 'systeme-social' export const SalaireBrutSection = () => { return ( diff --git a/mon-entreprise/source/components/QuickLinks.tsx b/mon-entreprise/source/components/QuickLinks.tsx index e63a33903..18b6c5a4a 100644 --- a/mon-entreprise/source/components/QuickLinks.tsx +++ b/mon-entreprise/source/components/QuickLinks.tsx @@ -3,7 +3,7 @@ import { contains, filter, pipe, reject, toPairs } from 'ramda' import { Trans } from 'react-i18next' import { useDispatch, useSelector } from 'react-redux' import { RootState } from 'Reducers/rootReducer' -import { DottedName } from 'Rules' +import { DottedName } from 'systeme-social' import { useNextQuestions } from './utils/useNextQuestion' import { diff --git a/mon-entreprise/source/components/RuleLink.tsx b/mon-entreprise/source/components/RuleLink.tsx index 88d297017..05bf95728 100644 --- a/mon-entreprise/source/components/RuleLink.tsx +++ b/mon-entreprise/source/components/RuleLink.tsx @@ -1,7 +1,7 @@ import { RuleLink as EngineRuleLink } from 'publicodes' import React, { useContext } from 'react' import { Link } from 'react-router-dom' -import { DottedName } from 'Rules' +import { DottedName } from 'systeme-social' import { EngineContext } from './utils/EngineContext' import { SitePathsContext } from './utils/SitePathsContext' diff --git a/mon-entreprise/source/components/SearchBar.tsx b/mon-entreprise/source/components/SearchBar.tsx index c282af22b..3489a2644 100644 --- a/mon-entreprise/source/components/SearchBar.tsx +++ b/mon-entreprise/source/components/SearchBar.tsx @@ -1,6 +1,6 @@ import React, { useContext, useEffect, useMemo, useState } from 'react' import { Trans, useTranslation } from 'react-i18next' -import { DottedName } from 'Rules' +import { DottedName } from 'systeme-social' import Worker from 'worker-loader!./SearchBar.worker.js' import RuleLink from './RuleLink' import './SearchBar.css' diff --git a/mon-entreprise/source/components/StackedBarChart.tsx b/mon-entreprise/source/components/StackedBarChart.tsx index 43f28d173..62a98475b 100644 --- a/mon-entreprise/source/components/StackedBarChart.tsx +++ b/mon-entreprise/source/components/StackedBarChart.tsx @@ -3,7 +3,7 @@ import useDisplayOnIntersecting from 'Components/utils/useDisplayOnIntersecting' import { EvaluatedNode, EvaluatedRule } from 'publicodes' import React from 'react' import { animated, useSpring } from 'react-spring' -import { DottedName } from 'Rules' +import { DottedName } from 'systeme-social' import styled from 'styled-components' const BarStack = styled.div` diff --git a/mon-entreprise/source/components/TargetSelection.tsx b/mon-entreprise/source/components/TargetSelection.tsx index 3006cdb02..67a2fddbf 100644 --- a/mon-entreprise/source/components/TargetSelection.tsx +++ b/mon-entreprise/source/components/TargetSelection.tsx @@ -26,7 +26,7 @@ import { Trans, useTranslation } from 'react-i18next' import { useDispatch, useSelector } from 'react-redux' import { useLocation } from 'react-router-dom' import { RootState } from 'Reducers/rootReducer' -import { DottedName } from 'Rules' +import { DottedName } from 'systeme-social' import { targetUnitSelector } from 'Selectors/simulationSelectors' import CurrencyInput from './CurrencyInput/CurrencyInput' import './TargetSelection.css' diff --git a/mon-entreprise/source/components/conversation/AnswerList.tsx b/mon-entreprise/source/components/conversation/AnswerList.tsx index 752ff16cb..8ab39a1e7 100644 --- a/mon-entreprise/source/components/conversation/AnswerList.tsx +++ b/mon-entreprise/source/components/conversation/AnswerList.tsx @@ -6,7 +6,7 @@ import { EvaluatedNode, formatValue } from 'publicodes' import emoji from 'react-easy-emoji' import { Trans, useTranslation } from 'react-i18next' import { useDispatch, useSelector } from 'react-redux' -import { DottedName } from 'Rules' +import { DottedName } from 'systeme-social' import { situationSelector } from 'Selectors/simulationSelectors' import './AnswerList.css' diff --git a/mon-entreprise/source/components/conversation/Explicable.tsx b/mon-entreprise/source/components/conversation/Explicable.tsx index 2a301d240..97b9e0d13 100644 --- a/mon-entreprise/source/components/conversation/Explicable.tsx +++ b/mon-entreprise/source/components/conversation/Explicable.tsx @@ -4,7 +4,7 @@ import { EngineContext } from 'Components/utils/EngineContext' import React, { useContext, useState } from 'react' import emoji from 'react-easy-emoji' import { useDispatch } from 'react-redux' -import { DottedName } from 'Rules' +import { DottedName } from 'systeme-social' import { TrackerContext } from '../utils/withTracker' import './Explicable.css' import usePortal from 'react-useportal' diff --git a/mon-entreprise/source/components/conversation/RuleInput.tsx b/mon-entreprise/source/components/conversation/RuleInput.tsx index 9a6f1599d..ba7cb6689 100644 --- a/mon-entreprise/source/components/conversation/RuleInput.tsx +++ b/mon-entreprise/source/components/conversation/RuleInput.tsx @@ -17,8 +17,7 @@ import { import { Evaluation } from 'publicodes/dist/types/AST/types' import React, { useContext } from 'react' import { useTranslation } from 'react-i18next' -import { DottedName } from 'Rules' -import { serialize } from 'storage/serializeSimulation' +import { DottedName } from 'systeme-social' import DateInput from './DateInput' import ParagrapheInput from './ParagrapheInput' import SelectEuropeCountry from './select/SelectEuropeCountry' diff --git a/mon-entreprise/source/components/simulationExplanation/AidesCovid.tsx b/mon-entreprise/source/components/simulationExplanation/AidesCovid.tsx index fa00ce9c8..479e92118 100644 --- a/mon-entreprise/source/components/simulationExplanation/AidesCovid.tsx +++ b/mon-entreprise/source/components/simulationExplanation/AidesCovid.tsx @@ -1,7 +1,7 @@ import Value, { Condition } from 'Components/EngineValue' import emoji from 'react-easy-emoji' import { Trans } from 'react-i18next' -import { DottedName } from 'Rules' +import { DottedName } from 'systeme-social' type AidesCovidProps = { aidesRule?: DottedName diff --git a/mon-entreprise/source/components/simulationExplanation/IndépendantExplanation.tsx b/mon-entreprise/source/components/simulationExplanation/IndépendantExplanation.tsx index 136fbb855..8d6b1434c 100644 --- a/mon-entreprise/source/components/simulationExplanation/IndépendantExplanation.tsx +++ b/mon-entreprise/source/components/simulationExplanation/IndépendantExplanation.tsx @@ -15,7 +15,7 @@ import { max } from 'ramda' import { useContext } from 'react' import { Trans, useTranslation } from 'react-i18next' import { useSelector } from 'react-redux' -import { DottedName } from 'Rules' +import { DottedName } from 'systeme-social' import { targetUnitSelector } from 'Selectors/simulationSelectors' import styled from 'styled-components' import AidesCovid from './AidesCovid' diff --git a/mon-entreprise/source/components/utils/EngineContext.tsx b/mon-entreprise/source/components/utils/EngineContext.tsx index 949825dc5..e7e326ecd 100644 --- a/mon-entreprise/source/components/utils/EngineContext.tsx +++ b/mon-entreprise/source/components/utils/EngineContext.tsx @@ -1,6 +1,6 @@ import Engine from 'publicodes' import React, { createContext, useContext } from 'react' -import { DottedName } from 'Rules' +import { DottedName } from 'systeme-social' export const EngineContext = createContext(new Engine({})) export const EngineProvider = EngineContext.Provider diff --git a/mon-entreprise/source/components/utils/useNextQuestion.tsx b/mon-entreprise/source/components/utils/useNextQuestion.tsx index 8507bdc4c..b747a0f12 100644 --- a/mon-entreprise/source/components/utils/useNextQuestion.tsx +++ b/mon-entreprise/source/components/utils/useNextQuestion.tsx @@ -25,7 +25,7 @@ import { import { useContext, useMemo } from 'react' import { useSelector } from 'react-redux' import { Simulation, SimulationConfig } from 'Reducers/rootReducer' -import { DottedName } from 'Rules' +import { DottedName } from 'systeme-social' import { answeredQuestionsSelector, configSelector, diff --git a/mon-entreprise/source/reducers/rootReducer.ts b/mon-entreprise/source/reducers/rootReducer.ts index a19ddd061..7ea817dea 100644 --- a/mon-entreprise/source/reducers/rootReducer.ts +++ b/mon-entreprise/source/reducers/rootReducer.ts @@ -3,7 +3,7 @@ import { defaultTo, omit, without } from 'ramda' import reduceReducers from 'reduce-reducers' import { combineReducers, Reducer } from 'redux' import { SavedSimulation } from 'Selectors/storageSelectors' -import { DottedName } from '../rules/index' +import { DottedName } from 'systeme-social' import { objectifsSelector } from '../selectors/simulationSelectors' import inFranceAppReducer, { Company } from './inFranceAppReducer' import storageRootReducer from './storageReducer' diff --git a/mon-entreprise/source/rules/index.ts b/mon-entreprise/source/rules/index.ts deleted file mode 100644 index 8035a053d..000000000 --- a/mon-entreprise/source/rules/index.ts +++ /dev/null @@ -1,47 +0,0 @@ -// Currenty we systematically bundle all the rules even if we only need a -// sub-section of them. We might support "code-splitting" the rules in the -// future. -import jsonRules from '../types/dottednames.json' -import artisteAuteur from './artiste-auteur.yaml' -import base from './base.yaml' -import chômagePartiel from './chômage-partiel.yaml' -import CCBatiment from './conventions-collectives/bâtiment.yaml' -import CCCompta from './conventions-collectives/experts-comptables.yaml' -import CCHotels from './conventions-collectives/hôtels-cafés-restaurants.yaml' -import CCOptique from './conventions-collectives/optique.yaml' -import CCSpectacleVivant from './conventions-collectives/spectacle-vivant.yaml' -import CCSport from './conventions-collectives/sport.yaml' -import dirigeant from './dirigeant.yaml' -import déclarationIndépendant from './déclaration-revenu-indépendant.yaml' -import entrepriseEtablissement from './entreprise-établissement.yaml' -import impot from './impôt.yaml' -import professionLibérale from './profession-libérale.yaml' -import protectionSociale from './protection-sociale.yaml' -import salarié from './salarié.yaml' -import situationPersonnelle from './situation-personnelle.yaml' - -export type DottedName = keyof typeof jsonRules - -const rules = { - ...base, - // TODO: rule order shouldn't matter but there is a bug if "impot" is after - // "dirigeant". - ...impot, - ...déclarationIndépendant, - ...artisteAuteur, - ...dirigeant, - ...professionLibérale, - ...entrepriseEtablissement, - ...protectionSociale, - ...salarié, - ...CCBatiment, - ...CCHotels, - ...CCOptique, - ...CCSpectacleVivant, - ...CCSport, - ...CCCompta, - ...situationPersonnelle, - ...chômagePartiel, -} - -export default rules diff --git a/mon-entreprise/source/selectors/simulationSelectors.ts b/mon-entreprise/source/selectors/simulationSelectors.ts index fbac8c52d..2cbde0d8b 100644 --- a/mon-entreprise/source/selectors/simulationSelectors.ts +++ b/mon-entreprise/source/selectors/simulationSelectors.ts @@ -1,4 +1,4 @@ -import { DottedName } from '../rules/index' +import { DottedName } from 'systeme-social' import { createSelector } from 'reselect' import { RootState, SimulationConfig } from 'Reducers/rootReducer' diff --git a/mon-entreprise/source/selectors/storageSelectors.ts b/mon-entreprise/source/selectors/storageSelectors.ts index ad5abc5b1..7216471e1 100644 --- a/mon-entreprise/source/selectors/storageSelectors.ts +++ b/mon-entreprise/source/selectors/storageSelectors.ts @@ -1,5 +1,5 @@ import { RootState, Simulation } from 'Reducers/rootReducer' -import { DottedName } from 'Rules' +import { DottedName } from 'systeme-social' // Note: it is currently not possible to define SavedSimulation as the return // type of the currentSimulationSelector function because the type would then diff --git a/mon-entreprise/source/server.js b/mon-entreprise/source/server.js index 94a433dd9..932dad33b 100644 --- a/mon-entreprise/source/server.js +++ b/mon-entreprise/source/server.js @@ -7,7 +7,7 @@ const config = require('../webpack.dev.js') const compiler = webpack(config) const history = require('connect-history-api-fallback') -const { watchDottedNames } = require('../scripts/dottednames') +const { watchDottedNames } = require('../../systeme-social/build') watchDottedNames() const rewrite = (basename) => ({ diff --git a/mon-entreprise/source/sites/mon-entreprise.fr/App.tsx b/mon-entreprise/source/sites/mon-entreprise.fr/App.tsx index d795b4df9..e87f7828f 100644 --- a/mon-entreprise/source/sites/mon-entreprise.fr/App.tsx +++ b/mon-entreprise/source/sites/mon-entreprise.fr/App.tsx @@ -14,7 +14,7 @@ import { useTranslation } from 'react-i18next' import { useSelector } from 'react-redux' import { Route, Switch } from 'react-router-dom' import createSentryMiddleware from 'redux-sentry-middleware' -import { DottedName } from 'Rules' +import { DottedName } from 'systeme-social' import { configSituationSelector, situationSelector, diff --git a/mon-entreprise/source/sites/mon-entreprise.fr/entry.en.tsx b/mon-entreprise/source/sites/mon-entreprise.fr/entry.en.tsx index a4bf5b8b1..527a5b416 100644 --- a/mon-entreprise/source/sites/mon-entreprise.fr/entry.en.tsx +++ b/mon-entreprise/source/sites/mon-entreprise.fr/entry.en.tsx @@ -3,7 +3,7 @@ import { hot } from 'react-hot-loader/root' import { translateRules } from 'publicodes' import { render } from 'react-dom' import 'regenerator-runtime/runtime' -import rules from 'Rules' +import rules from 'systeme-social' import i18next from '../../i18n' import translations from '../../locales/ui-en.yaml' import ruleTranslations from '../../locales/rules-en.yaml' diff --git a/mon-entreprise/source/sites/mon-entreprise.fr/entry.fr.tsx b/mon-entreprise/source/sites/mon-entreprise.fr/entry.fr.tsx index aafc1f0e7..06ef6647c 100644 --- a/mon-entreprise/source/sites/mon-entreprise.fr/entry.fr.tsx +++ b/mon-entreprise/source/sites/mon-entreprise.fr/entry.fr.tsx @@ -3,7 +3,7 @@ import 'core-js/stable' import 'react-hot-loader' import { render } from 'react-dom' import 'regenerator-runtime/runtime' -import rules from 'Rules' +import rules from 'systeme-social' import App from './App' import i18next from '../../i18n' diff --git a/mon-entreprise/source/sites/mon-entreprise.fr/middlewares/trackSimulatorActions.ts b/mon-entreprise/source/sites/mon-entreprise.fr/middlewares/trackSimulatorActions.ts index 447a25d71..af6805e37 100644 --- a/mon-entreprise/source/sites/mon-entreprise.fr/middlewares/trackSimulatorActions.ts +++ b/mon-entreprise/source/sites/mon-entreprise.fr/middlewares/trackSimulatorActions.ts @@ -1,4 +1,4 @@ -import { DottedName } from 'Rules' +import { DottedName } from 'systeme-social' import { situationSelector } from 'Selectors/simulationSelectors' import Tracker from 'Tracker' diff --git a/mon-entreprise/source/sites/mon-entreprise.fr/pages/Gérer/AideDéclarationIndépendant/index.tsx b/mon-entreprise/source/sites/mon-entreprise.fr/pages/Gérer/AideDéclarationIndépendant/index.tsx index 8e059c8c4..f58a8baeb 100644 --- a/mon-entreprise/source/sites/mon-entreprise.fr/pages/Gérer/AideDéclarationIndépendant/index.tsx +++ b/mon-entreprise/source/sites/mon-entreprise.fr/pages/Gérer/AideDéclarationIndépendant/index.tsx @@ -17,7 +17,7 @@ import emoji from 'react-easy-emoji' import { Trans } from 'react-i18next' import { useDispatch, useSelector } from 'react-redux' import { RootState } from 'Reducers/rootReducer' -import { DottedName } from 'Rules' +import { DottedName } from 'systeme-social' import { situationSelector } from 'Selectors/simulationSelectors' import styled from 'styled-components' import { CompanySection } from '../Home' diff --git a/mon-entreprise/source/sites/mon-entreprise.fr/pages/Simulateurs/ArtisteAuteur.tsx b/mon-entreprise/source/sites/mon-entreprise.fr/pages/Simulateurs/ArtisteAuteur.tsx index 5d6e149c2..e36b4feff 100644 --- a/mon-entreprise/source/sites/mon-entreprise.fr/pages/Simulateurs/ArtisteAuteur.tsx +++ b/mon-entreprise/source/sites/mon-entreprise.fr/pages/Simulateurs/ArtisteAuteur.tsx @@ -12,7 +12,7 @@ import { equals } from 'ramda' import { createContext, useContext, useEffect, useState } from 'react' import { Trans } from 'react-i18next' import { useDispatch, useSelector } from 'react-redux' -import { DottedName } from 'Rules' +import { DottedName } from 'systeme-social' import { situationSelector } from 'Selectors/simulationSelectors' import styled from 'styled-components' import config from './configs/artiste-auteur.yaml' diff --git a/mon-entreprise/source/sites/mon-entreprise.fr/pages/Simulateurs/ChômagePartiel.tsx b/mon-entreprise/source/sites/mon-entreprise.fr/pages/Simulateurs/ChômagePartiel.tsx index bb7597539..a91234c71 100644 --- a/mon-entreprise/source/sites/mon-entreprise.fr/pages/Simulateurs/ChômagePartiel.tsx +++ b/mon-entreprise/source/sites/mon-entreprise.fr/pages/Simulateurs/ChômagePartiel.tsx @@ -7,7 +7,7 @@ import { EngineContext, useEngine } from 'Components/utils/EngineContext' import { EvaluatedRule, evaluateRule, formatValue } from 'publicodes' import React, { useContext, useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' -import { DottedName } from 'Rules' +import { DottedName } from 'systeme-social' import styled from 'styled-components' declare global { diff --git a/mon-entreprise/source/sites/publi.codes/Studio.tsx b/mon-entreprise/source/sites/publi.codes/Studio.tsx index c62e55a2c..0f68e9a9e 100644 --- a/mon-entreprise/source/sites/publi.codes/Studio.tsx +++ b/mon-entreprise/source/sites/publi.codes/Studio.tsx @@ -14,7 +14,6 @@ const EXAMPLE_CODE = ` # Pour en savoir plus sur le langage, consultez le tutoriel : # => https://publi.codes -prix: prix . carottes: 2€/kg prix . champignons: 5€/kg prix . avocat: 2€/avocat diff --git a/mon-entreprise/test/conversation.test.js b/mon-entreprise/test/conversation.test.js index f426c957c..cfe660b20 100644 --- a/mon-entreprise/test/conversation.test.js +++ b/mon-entreprise/test/conversation.test.js @@ -4,7 +4,7 @@ import { getNextQuestions, getNextSteps, } from '../source/components/utils/useNextQuestion' -import rules from '../source/rules' +import rules from 'systeme-social' describe('conversation', function () { it('should start with the first missing variable', function () { diff --git a/mon-entreprise/test/cycles.test.js b/mon-entreprise/test/cycles.test.js index 90fcf68d4..2d015b1d1 100644 --- a/mon-entreprise/test/cycles.test.js +++ b/mon-entreprise/test/cycles.test.js @@ -1,6 +1,6 @@ import { expect } from 'chai' import { cyclesLib } from 'publicodes' -import rules from '../source/rules' +import rules from 'systeme-social' describe('DottedNames graph', () => { it("shouldn't have cycles", () => { diff --git a/mon-entreprise/test/real-rules.test.js b/mon-entreprise/test/real-rules.test.js index 6b46afdaa..999fe1bed 100644 --- a/mon-entreprise/test/real-rules.test.js +++ b/mon-entreprise/test/real-rules.test.js @@ -1,7 +1,7 @@ import { AssertionError } from 'chai' import Engine, { parsePublicodes } from 'publicodes' import { disambiguateRuleReference } from '../../publicodes/source/ruleUtils' -import rules from 'Rules' +import rules from 'systeme-social' // les variables dans les tests peuvent être exprimées relativement à l'espace de nom de la règle, // comme dans sa formule @@ -46,7 +46,7 @@ describe('Tests des règles de notre base de règles', () => if (!example.ok) { throw new AssertionError(` Valeur attendue : ${example['valeur attendue']} - Valeur obtenue : ${example.rule.nodeValue} + Valeur obtenue : ${example.rule.nodeValue} ${ example.rule.nodeValue === null ? 'Variables manquantes : ' + diff --git a/mon-entreprise/test/regressions/simulations.jest.js b/mon-entreprise/test/regressions/simulations.jest.js index fc62c0ed6..79e189312 100644 --- a/mon-entreprise/test/regressions/simulations.jest.js +++ b/mon-entreprise/test/regressions/simulations.jest.js @@ -7,7 +7,7 @@ /* eslint-disable no-undef */ import Engine, { evaluateRule } from 'publicodes' -import rules from '../../source/rules' +import rules from 'systeme-social' import artisteAuteurConfig from '../../source/sites/mon-entreprise.fr/pages/Simulateurs/configs/artiste-auteur.yaml' import autoentrepreneurConfig from '../../source/sites/mon-entreprise.fr/pages/Simulateurs/configs/auto-entrepreneur.yaml' import independantConfig from '../../source/sites/mon-entreprise.fr/pages/Simulateurs/configs/indépendant.yaml' diff --git a/mon-entreprise/test/unit-translations.test.js b/mon-entreprise/test/unit-translations.test.js index 5379665a5..264265f26 100644 --- a/mon-entreprise/test/unit-translations.test.js +++ b/mon-entreprise/test/unit-translations.test.js @@ -1,7 +1,7 @@ import { expect } from 'chai' import { parsePublicodes } from 'publicodes' import { uniq } from 'ramda' -import rawRules from '../source/rules' +import rawRules from 'systeme-social' import unitsTranslations from '../../publicodes/source/locales/units.yaml' it('use unit that exists in publicode', () => { diff --git a/mon-entreprise/tsconfig.json b/mon-entreprise/tsconfig.json index add7b0cb4..53dce93c1 100644 --- a/mon-entreprise/tsconfig.json +++ b/mon-entreprise/tsconfig.json @@ -8,8 +8,6 @@ "Components/*": ["components/*"], "Images/*": ["images/*"], "Reducers/*": ["reducers/*"], - "Rules/*": ["rules/*"], - "Rules": ["rules"], "Selectors/*": ["selectors/*"], "Types/*": ["types/*"] }, diff --git a/package.json b/package.json index 6af0698b2..59593154b 100644 --- a/package.json +++ b/package.json @@ -109,19 +109,20 @@ "lint:eslintrc": "eslint --print-config .eslintrc.js | eslint-config-prettier-check", "lint:eslint": "export NODE_OPTIONS='--max-old-space-size=4096'; eslint . --ext .js,.jsx,.ts,.tsx", "lint:eslint:fix": "yarn lint:eslint --fix", - "lint:prettier": "yarn workspaces run prettier --check \"{source,test,cypress}/**/*.{js,jsx,ts,tsx}\"", + "lint:prettier": "yarn run prettier --check \"**/*.{js,jsx,ts,tsx}\"", "lint:prettier:fix": "yarn lint:prettier --write", "lint:fix": "yarn lint:eslint:fix && yarn lint:prettier:fix", "prepare": "if [ -z \"$NETLIFY\" ]; then yarn workspaces run prepare; fi", "lint": "yarn lint:eslintrc && yarn lint:eslint && yarn lint:prettier", "test": "yarn workspaces run test", - "test:type": "yarn workspaces run tsc", + "test:type": "yarn workspace publicodes run tsc && yarn workspace mon-entreprise run tsc", "test:regressions": "jest", "clean": "yarn workspaces run clean", "start": "yarn workspace publicodes build --watch & yarn workspace mon-entreprise start" }, "workspaces": [ "publicodes", + "systeme-social", "mon-entreprise" ] } diff --git a/systeme-social/CHANGELOG.md b/systeme-social/CHANGELOG.md new file mode 100644 index 000000000..e69de29bb diff --git a/systeme-social/README.md b/systeme-social/README.md new file mode 100644 index 000000000..8663f4850 --- /dev/null +++ b/systeme-social/README.md @@ -0,0 +1,6 @@ +# Système social français en publicodes + +Ce paquet contient les règles publicodes utilisées sur https://mon-entreprise.fr +pour le calcul des cotisations sociales, des impôts et des droits sociaux. + +Pour voir des exemples d'utilisation rendez-vous sur https://mon-entreprise.fr/int%C3%A9gration/biblioth%C3%A8que-de-calcul diff --git a/systeme-social/build.js b/systeme-social/build.js new file mode 100644 index 000000000..c7595cfd7 --- /dev/null +++ b/systeme-social/build.js @@ -0,0 +1,46 @@ +/* eslint-env node */ + +const fs = require('fs') +const path = require('path') +const yaml = require('yaml') + +const publicodesDir = path.resolve(__dirname, './règles') +const outDir = path.resolve(__dirname, './dist') + +if (!fs.existsSync(outDir)) { + fs.mkdirSync(outDir) +} + +function concatenateFilesInDir(dirPath = publicodesDir) { + return fs + .readdirSync(dirPath) + .map((filename) => { + const fullpath = path.join(dirPath, filename) + if (fs.statSync(fullpath).isDirectory()) { + return concatenateFilesInDir(fullpath) + } else { + return filename.endsWith('.yaml') ? fs.readFileSync(fullpath) : '' + } + }) + .reduce((acc, cur) => acc + '\n' + cur, '') +} + +function readRules() { + return yaml.parse(concatenateFilesInDir()) +} + +// Note: we can't put the output file in the fs.watched directory + +function writeJSFile() { + const rules = readRules() + const names = Object.keys(rules) + const jsString = `module.exports = ${JSON.stringify(rules, null, 2)}` + fs.writeFileSync(path.resolve(outDir, 'index.js'), jsString) + fs.writeFileSync( + path.resolve(outDir, 'names.ts'), + `\nexport type Names = ${names.map((name) => `"${name}"`).join('\n | ')}\n` + ) +} + +writeJSFile() +exports.watchDottedNames = () => fs.watch(publicodesDir, writeJSFile) diff --git a/systeme-social/index.d.ts b/systeme-social/index.d.ts new file mode 100644 index 000000000..f20226051 --- /dev/null +++ b/systeme-social/index.d.ts @@ -0,0 +1,19 @@ +// Currenty we systematically bundle all the rules even if we only need a +// sub-section of them. We might support "code-splitting" the rules in the +// future. +import { + EvaluatedRule as GenericEvaluatedRule, + ParsedRule as GenericParsedRule, + ParsedRules as GenericParsedRules, + Rules as GenericRules +} from 'publicodes' +import { Names } from './dist/names' + +export type DottedName = Names +export type Rules = GenericRules +export type ParsedRules = GenericParsedRules +export type ParsedRule = GenericParsedRule +export type EvaluatedRule = GenericEvaluatedRule +export type Situation = Partial> +declare let rules: Rules +export default rules diff --git a/systeme-social/package.json b/systeme-social/package.json new file mode 100644 index 000000000..13157fb6b --- /dev/null +++ b/systeme-social/package.json @@ -0,0 +1,31 @@ +{ + "name": "systeme-social", + "version": "1.0.0", + "description": "Les règles publicodes du système social français", + "main": "./dist/index.js", + "types": "./index.d.ts", + "repository": { + "type": "git", + "url": "https://github.com/betagouv/mon-entreprise.git", + "directory": "systeme-social" + }, + "bugs": "https://github.com/betagouv/mon-entreprise/issues?q=is%3Aopen+is%3Aissue+label%3A%22%F0%9F%93%95+l%C3%A9gislation%22", + "homepage": "https://mon-entreprise.fr/int%C3%A9gration/biblioth%C3%A8que-de-calcul", + "license": "MIT", + "files": [ + "dist/index.js" + ], + "devDependencies": { + "yaml": "^1.10.0", + "publicodes": "^1.0.0-beta.4" + }, + "dependencies": {}, + "peerDependencies": { + "publicodes": "^1.0.0-beta.4" + }, + "scripts": { + "build": "node build.js", + "prepare": "yarn run build", + "test": "echo 1" + } +} diff --git a/mon-entreprise/source/rules/.gitignore b/systeme-social/règles/.gitignore similarity index 100% rename from mon-entreprise/source/rules/.gitignore rename to systeme-social/règles/.gitignore diff --git a/mon-entreprise/source/rules/impôt.yaml b/systeme-social/règles/aaa-impôt.yaml similarity index 100% rename from mon-entreprise/source/rules/impôt.yaml rename to systeme-social/règles/aaa-impôt.yaml diff --git a/mon-entreprise/source/rules/artiste-auteur.yaml b/systeme-social/règles/artiste-auteur.yaml similarity index 100% rename from mon-entreprise/source/rules/artiste-auteur.yaml rename to systeme-social/règles/artiste-auteur.yaml diff --git a/mon-entreprise/source/rules/base.yaml b/systeme-social/règles/base.yaml similarity index 100% rename from mon-entreprise/source/rules/base.yaml rename to systeme-social/règles/base.yaml diff --git a/mon-entreprise/source/rules/chômage-partiel.yaml b/systeme-social/règles/chômage-partiel.yaml similarity index 100% rename from mon-entreprise/source/rules/chômage-partiel.yaml rename to systeme-social/règles/chômage-partiel.yaml diff --git a/mon-entreprise/source/rules/conventions-collectives/bâtiment.yaml b/systeme-social/règles/conventions-collectives/bâtiment.yaml similarity index 100% rename from mon-entreprise/source/rules/conventions-collectives/bâtiment.yaml rename to systeme-social/règles/conventions-collectives/bâtiment.yaml diff --git a/mon-entreprise/source/rules/conventions-collectives/experts-comptables.yaml b/systeme-social/règles/conventions-collectives/experts-comptables.yaml similarity index 100% rename from mon-entreprise/source/rules/conventions-collectives/experts-comptables.yaml rename to systeme-social/règles/conventions-collectives/experts-comptables.yaml diff --git a/mon-entreprise/source/rules/conventions-collectives/hôtels-cafés-restaurants.yaml b/systeme-social/règles/conventions-collectives/hôtels-cafés-restaurants.yaml similarity index 100% rename from mon-entreprise/source/rules/conventions-collectives/hôtels-cafés-restaurants.yaml rename to systeme-social/règles/conventions-collectives/hôtels-cafés-restaurants.yaml diff --git a/mon-entreprise/source/rules/conventions-collectives/optique.yaml b/systeme-social/règles/conventions-collectives/optique.yaml similarity index 100% rename from mon-entreprise/source/rules/conventions-collectives/optique.yaml rename to systeme-social/règles/conventions-collectives/optique.yaml diff --git a/mon-entreprise/source/rules/conventions-collectives/spectacle-vivant.yaml b/systeme-social/règles/conventions-collectives/spectacle-vivant.yaml similarity index 100% rename from mon-entreprise/source/rules/conventions-collectives/spectacle-vivant.yaml rename to systeme-social/règles/conventions-collectives/spectacle-vivant.yaml diff --git a/mon-entreprise/source/rules/conventions-collectives/sport.yaml b/systeme-social/règles/conventions-collectives/sport.yaml similarity index 100% rename from mon-entreprise/source/rules/conventions-collectives/sport.yaml rename to systeme-social/règles/conventions-collectives/sport.yaml diff --git a/mon-entreprise/source/rules/dirigeant.yaml b/systeme-social/règles/dirigeant.yaml similarity index 100% rename from mon-entreprise/source/rules/dirigeant.yaml rename to systeme-social/règles/dirigeant.yaml diff --git a/mon-entreprise/source/rules/déclaration-revenu-indépendant.yaml b/systeme-social/règles/déclaration-revenu-indépendant.yaml similarity index 100% rename from mon-entreprise/source/rules/déclaration-revenu-indépendant.yaml rename to systeme-social/règles/déclaration-revenu-indépendant.yaml diff --git a/mon-entreprise/source/rules/entreprise-établissement.yaml b/systeme-social/règles/entreprise-établissement.yaml similarity index 100% rename from mon-entreprise/source/rules/entreprise-établissement.yaml rename to systeme-social/règles/entreprise-établissement.yaml diff --git a/mon-entreprise/source/rules/profession-libérale.yaml b/systeme-social/règles/profession-libérale.yaml similarity index 100% rename from mon-entreprise/source/rules/profession-libérale.yaml rename to systeme-social/règles/profession-libérale.yaml diff --git a/mon-entreprise/source/rules/protection-sociale.yaml b/systeme-social/règles/protection-sociale.yaml similarity index 100% rename from mon-entreprise/source/rules/protection-sociale.yaml rename to systeme-social/règles/protection-sociale.yaml diff --git a/mon-entreprise/source/rules/salarié.yaml b/systeme-social/règles/salarié.yaml similarity index 100% rename from mon-entreprise/source/rules/salarié.yaml rename to systeme-social/règles/salarié.yaml diff --git a/mon-entreprise/source/rules/situation-personnelle.yaml b/systeme-social/règles/situation-personnelle.yaml similarity index 100% rename from mon-entreprise/source/rules/situation-personnelle.yaml rename to systeme-social/règles/situation-personnelle.yaml diff --git a/yarn.lock b/yarn.lock index 348e8fafa..80c0f30a3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -14119,7 +14119,7 @@ yaml-loader@^0.5.0: dependencies: js-yaml "^3.5.2" -yaml@^1.9.2: +yaml@^1.10.0, yaml@^1.9.2: version "1.10.0" resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.0.tgz#3b593add944876077d4d683fee01081bd9fff31e" integrity sha512-yr2icI4glYaNG+KWONODapy2/jDdMSDnrONSjblABjD9B4Z5LgiircSt8m8sRZFNi08kG9Sm0uSHtEmP3zaEGg==