getTemplateVariables.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { HandlebarHelpersEnum } from './handlebarHelpers';
  2. import { TemplateVariableTypeEnum } from '../../types';
  3. export function getTemplateVariables(bod) {
  4. const stringVariables = bod
  5. .filter((body) => body.type === 'MustacheStatement')
  6. .flatMap((body) => {
  7. var _a, _b, _c;
  8. const varName = ((_a = body.params[0]) === null || _a === void 0 ? void 0 : _a.original) || body.path.original;
  9. if (body.path.original === HandlebarHelpersEnum.I18N) {
  10. return [];
  11. }
  12. if (!shouldAddVariable(varName)) {
  13. return [];
  14. }
  15. if ((_b = body.params[0]) === null || _b === void 0 ? void 0 : _b.original) {
  16. if (!Object.values(HandlebarHelpersEnum).includes(body.path.original)) {
  17. return [];
  18. }
  19. }
  20. return {
  21. type: TemplateVariableTypeEnum.STRING,
  22. name: ((_c = body.params[0]) === null || _c === void 0 ? void 0 : _c.original) || body.path.original,
  23. defaultValue: '',
  24. required: false,
  25. };
  26. });
  27. const arrayVariables = bod
  28. .filter((body) => body.type === 'BlockStatement' && ['each', 'with'].includes(body.path.original))
  29. .flatMap((body) => {
  30. var _a;
  31. const varName = ((_a = body.params[0]) === null || _a === void 0 ? void 0 : _a.original) || body.path.original;
  32. if (!shouldAddVariable(varName)) {
  33. return [];
  34. }
  35. const nestedVariablesInBlock = getTemplateVariables(body.program.body).map((mustVar) => {
  36. return Object.assign(Object.assign({}, mustVar), { name: `${varName}.${mustVar.name}` });
  37. });
  38. if (['with'].includes(body.path.original)) {
  39. return [...nestedVariablesInBlock];
  40. }
  41. return [
  42. {
  43. type: TemplateVariableTypeEnum.ARRAY,
  44. name: varName,
  45. required: false,
  46. },
  47. ...nestedVariablesInBlock,
  48. ];
  49. });
  50. const boolVariables = bod
  51. .filter((body) => body.type === 'BlockStatement' && ['if', 'unless'].includes(body.path.original))
  52. .flatMap((body) => {
  53. var _a;
  54. const varName = ((_a = body.params[0]) === null || _a === void 0 ? void 0 : _a.original) || body.path.original;
  55. if (!shouldAddVariable(varName)) {
  56. return [];
  57. }
  58. if (body.params.length > 1) {
  59. return [];
  60. }
  61. const nestedVariablesInBlock = getTemplateVariables(body.program.body);
  62. return [
  63. {
  64. type: TemplateVariableTypeEnum.BOOLEAN,
  65. name: varName,
  66. defaultValue: true,
  67. required: false,
  68. },
  69. ...nestedVariablesInBlock,
  70. ];
  71. });
  72. return stringVariables.concat(arrayVariables).concat(boolVariables);
  73. }
  74. const shouldAddVariable = (variableName) => {
  75. const validRegExp = /^[a-zA-Z_][a-zA-Z0-9_-]*?/;
  76. const isValid = variableName.match(validRegExp);
  77. return isValid;
  78. };