123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- const stylelint = require('stylelint');
- const postcss = require('postcss');
- const postcssSorting = require('postcss-sorting');
- const checkOrder = require('./checkOrder');
- const getOrderData = require('./getOrderData');
- const ruleName = require('./ruleName');
- const messages = require('./messages');
- module.exports = function checkNode({
- node,
- originalNode,
- isFixEnabled,
- orderInfo,
- primaryOption,
- result,
- unspecified,
- }) {
- if (isFixEnabled) {
- let shouldFix = false;
- let allNodesData = [];
- node.each(function processEveryNode(child) {
- // return early if we know there is a violation and auto fix should be applied
- if (shouldFix) {
- return;
- }
- let { shouldSkip, isCorrectOrder } = handleCycle(child, allNodesData);
- if (shouldSkip) {
- return;
- }
- if (!isCorrectOrder) {
- shouldFix = true;
- }
- });
- if (shouldFix) {
- let sortingOptions = {
- order: primaryOption,
- };
- // creating PostCSS Root node with current node as a child,
- // so PostCSS Sorting can process it
- let tempRoot = postcss.root({ nodes: [originalNode] });
- postcssSorting(sortingOptions)(tempRoot);
- }
- }
- let allNodesData = [];
- node.each(function processEveryNode(child) {
- let { shouldSkip, isCorrectOrder, nodeData, previousNodeData } = handleCycle(
- child,
- allNodesData
- );
- if (shouldSkip) {
- return;
- }
- if (isCorrectOrder) {
- return;
- }
- stylelint.utils.report({
- message: messages.expected(nodeData.description, previousNodeData.description),
- node: child,
- result,
- ruleName,
- });
- });
- function handleCycle(child, allNodes) {
- // Skip comments
- if (child.type === 'comment') {
- return {
- shouldSkip: true,
- };
- }
- // Receive node description and expectedPosition
- let nodeOrderData = getOrderData(orderInfo, child);
- let nodeData = {
- node: child,
- description: nodeOrderData.description,
- expectedPosition: nodeOrderData.expectedPosition,
- };
- allNodes.push(nodeData);
- let previousNodeData = allNodes[allNodes.length - 2];
- // Skip first node
- if (!previousNodeData) {
- return {
- shouldSkip: true,
- };
- }
- return {
- isCorrectOrder: checkOrder({
- firstNodeData: previousNodeData,
- secondNodeData: nodeData,
- allNodesData: allNodes,
- isFixEnabled,
- result,
- unspecified,
- }),
- nodeData,
- previousNodeData,
- };
- }
- };
|