@mrhenry/stylelint-mrhenry-prop-order 3.0.14 → 3.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.mjs +50 -36
  2. package/package.json +3 -3
package/index.mjs CHANGED
@@ -5,8 +5,8 @@ const orderSet = new Set(order);
5
5
 
6
6
  const ruleName = "@mrhenry/stylelint-mrhenry-prop-order";
7
7
  const messages = stylelint.utils.ruleMessages(ruleName, {
8
- expected: (name) => {
9
- return `Expected ${name} to appear at a different position.`;
8
+ expected: (names, orderedNames) => {
9
+ return `Expected ${names.join(', ')} to appear in ${orderedNames.join(', ')} order.`;
10
10
  }
11
11
  });
12
12
 
@@ -63,6 +63,7 @@ const ruleFunction = (primaryOption, secondaryOption, context) => {
63
63
  }
64
64
  });
65
65
 
66
+ /** @type {Array<Array<import('postcss').Node>>} */
66
67
  let declarationsSections = [[]];
67
68
  container.each((node) => {
68
69
  if (matchedComments.has(node)) {
@@ -99,47 +100,60 @@ const ruleFunction = (primaryOption, secondaryOption, context) => {
99
100
  const firstNodeIndex = Math.min.apply(Math, section.map((x) => container.index(x)));
100
101
  const originalFirstNode = container.nodes[firstNodeIndex];
101
102
 
102
- if (!context.fix) {
103
- sortedSection.forEach((decl, index) => {
104
- const oldIndex = section.indexOf(decl);
105
- if (index === oldIndex) {
106
- return;
107
- }
103
+ const sectionPropNames = section.map((decl) => decl.prop);
104
+ const sortedSectionPropNames = sortedSection.map((decl) => decl.prop);
105
+
106
+ /** @type {import('postcss').Node} */
107
+ const firstDecl = section.at(0);
108
+ /** @type {import('postcss').Node} */
109
+ const lastDecl = section.at(-1);
110
+
111
+ const containerStartOffset = container.source?.start.offset;
112
+ const index = firstDecl.source?.start.offset - containerStartOffset;
113
+ const endIndex = lastDecl.source?.end.offset - containerStartOffset;
114
+
115
+ for (let i = 0; i < sectionPropNames.length; i++) {
116
+ const original = sectionPropNames[i];
117
+ const sorted = sortedSectionPropNames[i];
108
118
 
109
- if (index < section.length - 1) {
110
- stylelint.utils.report({
111
- message: messages.expected(decl.prop),
112
- node: decl,
113
- index: 0,
114
- endIndex: decl.prop.length,
115
- result: postcssResult,
116
- ruleName,
119
+ if (original === sorted) {
120
+ continue;
121
+ }
122
+
123
+ stylelint.utils.report({
124
+ message: messages.expected(sectionPropNames, sortedSectionPropNames),
125
+ node: container,
126
+ index,
127
+ endIndex,
128
+ result: postcssResult,
129
+ ruleName,
130
+ fix: () => {
131
+ sortedSection.reverse().forEach((decl) => {
132
+ container.insertBefore(firstNodeIndex, decl);
133
+ return;
117
134
  });
135
+
136
+ const finalFirstNode = container.nodes[firstNodeIndex];
137
+ if (originalFirstNode.raws.before && finalFirstNode.raws.before) {
138
+ const originalRawBefore = originalFirstNode.raws.before;
139
+ originalFirstNode.raws.before = finalFirstNode.raws.before;
140
+ finalFirstNode.raws.before = originalRawBefore;
141
+ }
142
+
143
+ for (const [comment, prev] of matchedComments) {
144
+ if (!section.includes(prev)) {
145
+ continue;
146
+ }
147
+
148
+ comment.remove();
149
+ container.insertAfter(prev, comment);
150
+ }
118
151
  }
119
152
  });
120
- }
121
153
 
122
- if (context.fix) {
123
- sortedSection.reverse().forEach((decl) => {
124
- container.insertBefore(firstNodeIndex, decl);
125
- return;
126
- });
127
-
128
- const finalFirstNode = container.nodes[firstNodeIndex];
129
- if (originalFirstNode.raws.before && finalFirstNode.raws.before) {
130
- const originalRawBefore = originalFirstNode.raws.before;
131
- originalFirstNode.raws.before = finalFirstNode.raws.before;
132
- finalFirstNode.raws.before = originalRawBefore;
133
- }
154
+ break;
134
155
  }
135
156
  });
136
-
137
- if (context.fix) {
138
- for (const [comment, prev] of matchedComments) {
139
- comment.remove();
140
- container.insertAfter(prev, comment);
141
- }
142
- }
143
157
  });
144
158
  };
145
159
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mrhenry/stylelint-mrhenry-prop-order",
3
- "version": "3.0.14",
3
+ "version": "3.0.15",
4
4
  "description": "Mr. Henry's preferred order for CSS properties",
5
5
  "type": "module",
6
6
  "main": "index.mjs",
@@ -27,10 +27,10 @@
27
27
  "stylelint-plugin"
28
28
  ],
29
29
  "peerDependencies": {
30
- "stylelint": "^16.0.0"
30
+ "stylelint": "^16.8.2"
31
31
  },
32
32
  "devDependencies": {
33
33
  "css-tree": "^2.3.1",
34
- "stylelint": "^16.7.0"
34
+ "stylelint": "^16.8.2"
35
35
  }
36
36
  }