4
0

isStandardSyntaxDeclaration.mjs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { isRule } from './typeGuards.mjs';
  2. import isScssVariable from './isScssVariable.mjs';
  3. /**
  4. * Check whether a declaration is standard
  5. *
  6. * @param {import('postcss').Declaration | import('postcss-less').Declaration} decl
  7. * @returns {boolean}
  8. */
  9. export default function isStandardSyntaxDeclaration(decl) {
  10. const prop = decl.prop;
  11. const parent = decl.parent;
  12. // SCSS var; covers map and list declarations
  13. if (isScssVariable(prop)) {
  14. return false;
  15. }
  16. // Less var (e.g. @var: x), but exclude variable interpolation (e.g. @{var})
  17. if (prop[0] === '@' && prop[1] !== '{') {
  18. return false;
  19. }
  20. // Less map declaration
  21. if (parent && parent.type === 'atrule' && parent.raws.afterName === ':') {
  22. return false;
  23. }
  24. // Less map (e.g. #my-map() { myprop: red; })
  25. if (
  26. parent &&
  27. isRule(parent) &&
  28. parent.selector &&
  29. parent.selector.startsWith('#') &&
  30. parent.selector.endsWith('()')
  31. ) {
  32. return false;
  33. }
  34. // Sass nested properties (e.g. border: { style: solid; color: red; })
  35. if (
  36. parent &&
  37. isRule(parent) &&
  38. parent.selector &&
  39. parent.selector[parent.selector.length - 1] === ':' &&
  40. parent.selector.substring(0, 2) !== '--'
  41. ) {
  42. return false;
  43. }
  44. // Less &:extend
  45. if ('extend' in decl && decl.extend) {
  46. return false;
  47. }
  48. return true;
  49. }