populate-components.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. var configuration = require('../../configuration');
  2. var InvalidPropertyError = require('../../invalid-property-error');
  3. function populateComponents(properties, validator, warnings) {
  4. var component;
  5. var j, m;
  6. for (var i = properties.length - 1; i >= 0; i--) {
  7. var property = properties[i];
  8. var descriptor = configuration[property.name];
  9. if (!property.dynamic && descriptor && descriptor.shorthand) {
  10. if (onlyValueIsVariable(property, validator) || moreThanOneValueIsVariable(property, validator)) {
  11. property.optimizable = false;
  12. continue;
  13. }
  14. property.shorthand = true;
  15. property.dirty = true;
  16. try {
  17. property.components = descriptor.breakUp(property, configuration, validator);
  18. if (descriptor.shorthandComponents) {
  19. for (j = 0, m = property.components.length; j < m; j++) {
  20. component = property.components[j];
  21. component.components = configuration[component.name].breakUp(component, configuration, validator);
  22. }
  23. }
  24. } catch (e) {
  25. if (e instanceof InvalidPropertyError) {
  26. property.components = []; // this will set property.unused to true below
  27. warnings.push(e.message);
  28. } else {
  29. throw e;
  30. }
  31. }
  32. if (property.components.length > 0) {
  33. property.multiplex = property.components[0].multiplex;
  34. } else {
  35. property.unused = true;
  36. }
  37. }
  38. }
  39. }
  40. function onlyValueIsVariable(property, validator) {
  41. return property.value.length == 1 && validator.isVariable(property.value[0][1]);
  42. }
  43. function moreThanOneValueIsVariable(property, validator) {
  44. return property.value.length > 1
  45. && property.value.filter(
  46. function(value) {
  47. return validator.isVariable(value[1]);
  48. }
  49. ).length > 1;
  50. }
  51. module.exports = populateComponents;