propertyDescriptors.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. const parsers = require("../parsers");
  3. const { AST_TYPES } = parsers;
  4. const getPropertyDescriptor = (property) => ({
  5. set(v) {
  6. const value = parsers.prepareValue(v);
  7. if (parsers.hasVarFunc(value)) {
  8. this._setProperty(property, value);
  9. } else {
  10. const parsedValue = parsers.parsePropertyValue(property, v, {
  11. globalObject: this._global,
  12. inArray: true
  13. });
  14. if (Array.isArray(parsedValue)) {
  15. if (parsedValue.length === 1) {
  16. const [{ name, type, value: itemValue }] = parsedValue;
  17. switch (type) {
  18. case AST_TYPES.CALC: {
  19. this._setProperty(property, `${name}(${itemValue})`);
  20. break;
  21. }
  22. case AST_TYPES.GLOBAL_KEYWORD:
  23. case AST_TYPES.IDENTIFIER: {
  24. // Set the normalized name for keywords or identifiers.
  25. this._setProperty(property, name);
  26. break;
  27. }
  28. default: {
  29. // Set the prepared value for Dimension, Function, etc.
  30. this._setProperty(property, value);
  31. }
  32. }
  33. } else {
  34. // Set the prepared value for lists containing multiple values.
  35. this._setProperty(property, value);
  36. }
  37. } else if (typeof parsedValue === "string") {
  38. // Empty string.
  39. this._setProperty(property, parsedValue);
  40. }
  41. }
  42. },
  43. get() {
  44. return this.getPropertyValue(property);
  45. },
  46. enumerable: true,
  47. configurable: true
  48. });
  49. module.exports = {
  50. getPropertyDescriptor
  51. };