isStandardSyntaxDeclaration.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. 'use strict';
  2. const isScssVariable = require('./isScssVariable');
  3. const { isRoot, isRule } = 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 | import('postcss-less').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. parent &&
  21. isRoot(parent) &&
  22. parent.source &&
  23. !isStandardSyntaxLang(
  24. /** @type {import('postcss').Source & {lang?: string}} */ (parent.source).lang,
  25. )
  26. ) {
  27. return false;
  28. }
  29. // SCSS var; covers map and list declarations
  30. if (isScssVariable(prop)) {
  31. return false;
  32. }
  33. // Less var (e.g. @var: x), but exclude variable interpolation (e.g. @{var})
  34. if (prop[0] === '@' && prop[1] !== '{') {
  35. return false;
  36. }
  37. // Less map declaration
  38. if (parent && parent.type === 'atrule' && parent.raws.afterName === ':') {
  39. return false;
  40. }
  41. // Less map (e.g. #my-map() { myprop: red; })
  42. if (
  43. parent &&
  44. isRule(parent) &&
  45. parent.selector &&
  46. parent.selector.startsWith('#') &&
  47. parent.selector.endsWith('()')
  48. ) {
  49. return false;
  50. }
  51. // Sass nested properties (e.g. border: { style: solid; color: red; })
  52. if (
  53. parent &&
  54. isRule(parent) &&
  55. parent.selector &&
  56. parent.selector[parent.selector.length - 1] === ':' &&
  57. parent.selector.substring(0, 2) !== '--'
  58. ) {
  59. return false;
  60. }
  61. // Less &:extend
  62. if ('extend' in decl && decl.extend) {
  63. return false;
  64. }
  65. return true;
  66. };