call.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var tslib_1 = require("tslib");
  4. var node_1 = tslib_1.__importDefault(require("./node"));
  5. var anonymous_1 = tslib_1.__importDefault(require("./anonymous"));
  6. var function_caller_1 = tslib_1.__importDefault(require("../functions/function-caller"));
  7. //
  8. // A function call node.
  9. //
  10. var Call = function (name, args, index, currentFileInfo) {
  11. this.name = name;
  12. this.args = args;
  13. this.calc = name === 'calc';
  14. this._index = index;
  15. this._fileInfo = currentFileInfo;
  16. };
  17. Call.prototype = Object.assign(new node_1.default(), {
  18. type: 'Call',
  19. accept: function (visitor) {
  20. if (this.args) {
  21. this.args = visitor.visitArray(this.args);
  22. }
  23. },
  24. //
  25. // When evaluating a function call,
  26. // we either find the function in the functionRegistry,
  27. // in which case we call it, passing the evaluated arguments,
  28. // if this returns null or we cannot find the function, we
  29. // simply print it out as it appeared originally [2].
  30. //
  31. // The reason why we evaluate the arguments, is in the case where
  32. // we try to pass a variable to a function, like: `saturate(@color)`.
  33. // The function should receive the value, not the variable.
  34. //
  35. eval: function (context) {
  36. var _this = this;
  37. /**
  38. * Turn off math for calc(), and switch back on for evaluating nested functions
  39. */
  40. var currentMathContext = context.mathOn;
  41. context.mathOn = !this.calc;
  42. if (this.calc || context.inCalc) {
  43. context.enterCalc();
  44. }
  45. var exitCalc = function () {
  46. if (_this.calc || context.inCalc) {
  47. context.exitCalc();
  48. }
  49. context.mathOn = currentMathContext;
  50. };
  51. var result;
  52. var funcCaller = new function_caller_1.default(this.name, context, this.getIndex(), this.fileInfo());
  53. if (funcCaller.isValid()) {
  54. try {
  55. result = funcCaller.call(this.args);
  56. exitCalc();
  57. }
  58. catch (e) {
  59. // eslint-disable-next-line no-prototype-builtins
  60. if (e.hasOwnProperty('line') && e.hasOwnProperty('column')) {
  61. throw e;
  62. }
  63. throw {
  64. type: e.type || 'Runtime',
  65. message: "Error evaluating function `" + this.name + "`" + (e.message ? ": " + e.message : ''),
  66. index: this.getIndex(),
  67. filename: this.fileInfo().filename,
  68. line: e.lineNumber,
  69. column: e.columnNumber
  70. };
  71. }
  72. }
  73. if (result !== null && result !== undefined) {
  74. // Results that that are not nodes are cast as Anonymous nodes
  75. // Falsy values or booleans are returned as empty nodes
  76. if (!(result instanceof node_1.default)) {
  77. if (!result || result === true) {
  78. result = new anonymous_1.default(null);
  79. }
  80. else {
  81. result = new anonymous_1.default(result.toString());
  82. }
  83. }
  84. result._index = this._index;
  85. result._fileInfo = this._fileInfo;
  86. return result;
  87. }
  88. var args = this.args.map(function (a) { return a.eval(context); });
  89. exitCalc();
  90. return new Call(this.name, args, this.getIndex(), this.fileInfo());
  91. },
  92. genCSS: function (context, output) {
  93. output.add(this.name + "(", this.fileInfo(), this.getIndex());
  94. for (var i = 0; i < this.args.length; i++) {
  95. this.args[i].genCSS(context, output);
  96. if (i + 1 < this.args.length) {
  97. output.add(', ');
  98. }
  99. }
  100. output.add(')');
  101. }
  102. });
  103. exports.default = Call;
  104. //# sourceMappingURL=call.js.map