getTemplateVariables.js 3.2 KB

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