isStandardSyntaxDeclaration.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. const isScssVariable = require('./isScssVariable');
  3. const { isRoot } = require('./typeGuards');
  4. /**
  5. * @param {string} [lang]
  6. */
  7. function isStandardSyntaxLang(lang) {
  8. return lang && (lang === 'css' || lang === 'custom-template' || lang === 'template-literal');
  9. }
  10. /**
  11. * Check whether a declaration is standard
  12. *
  13. * @param {import('postcss').Declaration} decl
  14. */
  15. module.exports = function (decl) {
  16. const prop = decl.prop;
  17. const parent = decl.parent;
  18. // Declarations belong in a declaration block or standard CSS source
  19. if (
  20. isRoot(parent) &&
  21. parent.source &&
  22. !isStandardSyntaxLang(
  23. /** @type {import('postcss').NodeSource & {lang?: string}} */ (parent.source).lang,
  24. )
  25. ) {
  26. return false;
  27. }
  28. // SCSS var
  29. if (isScssVariable(prop)) {
  30. return false;
  31. }
  32. // Less var (e.g. @var: x), but exclude variable interpolation (e.g. @{var})
  33. if (prop[0] === '@' && prop[1] !== '{') {
  34. return false;
  35. }
  36. // Sass nested properties (e.g. border: { style: solid; color: red; })
  37. if (
  38. // @ts-ignore TODO TYPES selector does not exists
  39. parent.selector &&
  40. // @ts-ignore
  41. parent.selector[parent.selector.length - 1] === ':' &&
  42. // @ts-ignore
  43. parent.selector.substring(0, 2) !== '--'
  44. ) {
  45. return false;
  46. }
  47. // Less &:extend
  48. // @ts-ignore TODO TYPES extend does not exists
  49. if (decl.extend) {
  50. return false;
  51. }
  52. return true;
  53. };