validatePrimaryOption.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const { isObject, isString } = require('../../utils/validateType');
  2. module.exports = function validatePrimaryOption(actualOptions) {
  3. // Otherwise, begin checking array options
  4. if (!Array.isArray(actualOptions)) {
  5. return false;
  6. }
  7. // Every item in the array must be a certain string or an object
  8. // with a "type" property
  9. if (
  10. !actualOptions.every((item) => {
  11. if (isString(item)) {
  12. return [
  13. 'custom-properties',
  14. 'dollar-variables',
  15. 'at-variables',
  16. 'declarations',
  17. 'rules',
  18. 'at-rules',
  19. 'less-mixins',
  20. ].includes(item);
  21. }
  22. return isObject(item) && item.type !== undefined;
  23. })
  24. ) {
  25. return false;
  26. }
  27. const objectItems = actualOptions.filter(isObject);
  28. if (
  29. !objectItems.every((item) => {
  30. let result = true;
  31. if (item.type !== 'at-rule' && item.type !== 'rule') {
  32. return false;
  33. }
  34. if (item.type === 'at-rule') {
  35. // if parameter is specified, name should be specified also
  36. if (item.parameter !== undefined && item.name === undefined) {
  37. return false;
  38. }
  39. if (item.hasBlock !== undefined) {
  40. result = item.hasBlock === true || item.hasBlock === false;
  41. }
  42. if (item.name !== undefined) {
  43. result = isString(item.name) && item.name.length;
  44. }
  45. if (item.parameter !== undefined) {
  46. result =
  47. (isString(item.parameter) && item.parameter.length) ||
  48. isRegExp(item.parameter);
  49. }
  50. }
  51. if (item.type === 'rule') {
  52. if (item.selector !== undefined) {
  53. result =
  54. (isString(item.selector) && item.selector.length) ||
  55. isRegExp(item.selector);
  56. }
  57. if (result && item.name !== undefined) {
  58. result = isString(item.name) && item.name.length;
  59. }
  60. }
  61. return result;
  62. })
  63. ) {
  64. return false;
  65. }
  66. return true;
  67. };
  68. function isRegExp(value) {
  69. return Object.prototype.toString.call(value) === '[object RegExp]';
  70. }