eslint-plugin-tailwind-variants 2.0.1 → 2.0.2

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.
@@ -19,6 +19,12 @@ export const MESSAGE_IDS = {
19
19
  patternTooLong: "patternTooLong",
20
20
  unsortedCustomProperties: "unsortedCustomProperties",
21
21
  };
22
+ function isNodeWithOffset(node) {
23
+ return (node.loc != null &&
24
+ typeof node.loc.start.offset ===
25
+ "number" &&
26
+ typeof node.loc.end.offset === "number");
27
+ }
22
28
  export const rule = createRule({
23
29
  name: "sort-custom-properties",
24
30
  meta: {
@@ -138,21 +144,28 @@ export const rule = createRule({
138
144
  }
139
145
  return a.property.localeCompare(b.property);
140
146
  });
141
- const getFullLine = (node) => {
142
- const lines = sourceCode.lines;
143
- const startLine = node.loc.start.line - 1;
144
- return lines[startLine];
147
+ const allNodesHaveOffsets = sorted.every((item) => isNodeWithOffset(item.node));
148
+ if (!allNodesHaveOffsets)
149
+ return null;
150
+ const getFullDeclaration = (node) => {
151
+ const lineStartIndex = sourceCode.getIndexFromLoc({
152
+ column: 1,
153
+ line: node.loc.start.line,
154
+ });
155
+ const endIndex = node.loc.end.offset + 1; // Include semicolon
156
+ return sourceCode.text.slice(lineStartIndex, endIndex);
145
157
  };
146
158
  const fixes = currentBlockProperties.map((prop, index) => {
147
159
  const sortedNode = sorted[index].node;
148
- const sortedLine = getFullLine(sortedNode);
160
+ const sortedDeclaration = getFullDeclaration(sortedNode);
161
+ // Column is 1-based in ESLint loc
149
162
  const currentLineStart = sourceCode.getIndexFromLoc({
150
163
  column: 1,
151
164
  line: prop.node.loc.start.line,
152
165
  });
153
166
  const currentLineEnd = sourceCode.getIndexFromLoc({
154
167
  column: 1,
155
- line: prop.node.loc.start.line + 1,
168
+ line: prop.node.loc.end.line + 1,
156
169
  });
157
170
  let replacement = "";
158
171
  if (emptyLineBetweenGroups && index > 0) {
@@ -162,7 +175,7 @@ export const rule = createRule({
162
175
  replacement = "\n";
163
176
  }
164
177
  }
165
- replacement += sortedLine + "\n";
178
+ replacement += sortedDeclaration + "\n";
166
179
  return fixer.replaceTextRange([currentLineStart, currentLineEnd], replacement);
167
180
  });
168
181
  return fixes;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-tailwind-variants",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "ESLint plugin for Tailwind Variants",
5
5
  "repository": {
6
6
  "type": "git",