isStandardSyntaxRule.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. const _ = require('lodash');
  3. const isCustomPropertySet = require('../utils/isCustomPropertySet');
  4. const isStandardSyntaxSelector = require('../utils/isStandardSyntaxSelector');
  5. /**
  6. * Check whether a Node is a standard rule
  7. *
  8. * @param {import('postcss').Rule} rule
  9. * @returns {boolean}
  10. */
  11. module.exports = function (rule) {
  12. // Get full selector
  13. const selector = _.get(rule, 'raws.selector.raw', rule.selector);
  14. if (!isStandardSyntaxSelector(rule.selector)) {
  15. return false;
  16. }
  17. // Custom property set (e.g. --custom-property-set: {})
  18. if (isCustomPropertySet(rule)) {
  19. return false;
  20. }
  21. // Called Less mixin (e.g. a { .mixin() })
  22. // @ts-ignore TODO TYPES support LESS and SASS types somehow
  23. if (rule.mixin) {
  24. return false;
  25. }
  26. // Less detached rulesets
  27. if (selector.startsWith('@') && selector.endsWith(':')) {
  28. return false;
  29. }
  30. // Ignore Less &:extend rule
  31. // @ts-ignore TODO TYPES support LESS and SASS types somehow
  32. if (rule.extend) {
  33. return false;
  34. }
  35. // Ignore mixin or &:extend rule
  36. // https://github.com/shellscape/postcss-less/blob/master/lib/less-parser.js#L52
  37. // @ts-ignore TODO TYPES support LESS and SASS types somehow
  38. if (rule.params && rule.params[0]) {
  39. return false;
  40. }
  41. // Non-outputting Less mixin definition (e.g. .mixin() {})
  42. if (selector.endsWith(')') && !selector.includes(':')) {
  43. return false;
  44. }
  45. // Less guards
  46. if (/when\s+(not\s+)*\(/.test(selector)) {
  47. return false;
  48. }
  49. // Ignore Scss nested properties
  50. if (selector.endsWith(':')) {
  51. return false;
  52. }
  53. return true;
  54. };