123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- const postcss = require('postcss');
- const _ = require('lodash');
- const checkAlphabeticalOrder = require('../checkAlphabeticalOrder');
- // eslint-disable-next-line consistent-return
- module.exports = function checkOrder({
- firstPropertyData,
- secondPropertyData,
- allPropertiesData,
- unspecified,
- }) {
- function report(isCorrect, firstNode = firstPropertyData, secondNode = secondPropertyData) {
- return {
- isCorrect,
- firstNode,
- secondNode,
- };
- }
- if (firstPropertyData.unprefixedName === secondPropertyData.unprefixedName) {
- // If first property has no prefix and second property has prefix
- if (
- !postcss.vendor.prefix(firstPropertyData.name).length &&
- postcss.vendor.prefix(secondPropertyData.name).length
- ) {
- return report(false);
- }
- return report(true);
- }
- const firstPropIsSpecified = Boolean(firstPropertyData.orderData);
- const secondPropIsSpecified = Boolean(secondPropertyData.orderData);
- // Check actual known properties
- if (firstPropIsSpecified && secondPropIsSpecified) {
- return report(
- firstPropertyData.orderData.expectedPosition <=
- secondPropertyData.orderData.expectedPosition
- );
- }
- if (!firstPropIsSpecified && secondPropIsSpecified) {
- // If first prop is unspecified, look for a specified prop before it to
- // compare to the current prop
- const priorSpecifiedPropData = _.findLast(allPropertiesData.slice(0, -1), (d) =>
- Boolean(d.orderData)
- );
- if (
- priorSpecifiedPropData &&
- priorSpecifiedPropData.orderData &&
- priorSpecifiedPropData.orderData.expectedPosition >
- secondPropertyData.orderData.expectedPosition
- ) {
- return report(false, priorSpecifiedPropData, secondPropertyData);
- }
- }
- // Now deal with unspecified props
- // Starting with bottomAlphabetical as it requires more specific conditionals
- if (unspecified === 'bottomAlphabetical' && firstPropIsSpecified && !secondPropIsSpecified) {
- return report(true);
- }
- if (unspecified === 'bottomAlphabetical' && !firstPropIsSpecified && !secondPropIsSpecified) {
- if (checkAlphabeticalOrder(firstPropertyData, secondPropertyData)) {
- return report(true);
- }
- return report(false);
- }
- if (unspecified === 'bottomAlphabetical' && !firstPropIsSpecified) {
- return report(false);
- }
- if (!firstPropIsSpecified && !secondPropIsSpecified) {
- return report(true);
- }
- if (unspecified === 'ignore' && (!firstPropIsSpecified || !secondPropIsSpecified)) {
- return report(true);
- }
- if (unspecified === 'top' && !firstPropIsSpecified) {
- return report(true);
- }
- if (unspecified === 'top' && !secondPropIsSpecified) {
- return report(false);
- }
- if (unspecified === 'bottom' && !secondPropIsSpecified) {
- return report(true);
- }
- if (unspecified === 'bottom' && !firstPropIsSpecified) {
- return report(false);
- }
- };
|