Skip to content

Commit 70d4b06

Browse files
authored
minor refactor (#394)
1 parent c59eaa0 commit 70d4b06

File tree

2 files changed

+57
-84
lines changed

2 files changed

+57
-84
lines changed

.changeset/rich-toes-live.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-jsonc": minor
3+
---
4+
5+
minor refactor

lib/utils/fix-sort-elements.ts

Lines changed: 52 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export function* fixForSorting(
8888
}
8989

9090
/**
91-
* Calculate the range of the target information.
91+
* Calculate the fix information of the target.
9292
*/
9393
function calcTargetInfo(
9494
sourceCode: SourceCode,
@@ -97,14 +97,12 @@ function calcTargetInfo(
9797
insertCode: string;
9898
removeRanges: ESLintAST.Range[];
9999
} {
100-
if (!target.node) {
101-
return calcTargetInfoFromAround(sourceCode, target);
102-
}
103-
const node = target.node;
104-
const nodeLastToken = getLastTokenOfNode(sourceCode, node);
100+
const nodeEndIndex = target.node
101+
? getLastTokenOfNode(sourceCode, target.node).range[1]
102+
: target.after.range[0];
105103

106-
const endInfo = getElementEndInfo(sourceCode, node);
107-
const prevInfo = getPrevElementInfo(sourceCode, { node });
104+
const endInfo = getElementEndInfo(sourceCode, target);
105+
const prevInfo = getPrevElementInfo(sourceCode, target);
108106

109107
let insertCode: string;
110108

@@ -113,17 +111,14 @@ function calcTargetInfo(
113111
insertCode = `${sourceCode.text.slice(
114112
prevInfo.last.range![1],
115113
prevInfo.comma.range[0],
116-
)}${sourceCode.text.slice(prevInfo.comma.range[1], nodeLastToken.range[1])}`;
114+
)}${sourceCode.text.slice(prevInfo.comma.range[1], nodeEndIndex)}`;
117115
removeRanges.push(
118116
[prevInfo.last.range![1], prevInfo.comma.range[0]],
119-
[prevInfo.comma.range[1], nodeLastToken.range[1]],
117+
[prevInfo.comma.range[1], nodeEndIndex],
120118
);
121119
} else {
122-
insertCode = sourceCode.text.slice(
123-
prevInfo.last.range![1],
124-
nodeLastToken.range[1],
125-
);
126-
removeRanges.push([prevInfo.last.range![1], nodeLastToken.range[1]]);
120+
insertCode = sourceCode.text.slice(prevInfo.last.range![1], nodeEndIndex);
121+
removeRanges.push([prevInfo.last.range![1], nodeEndIndex]);
127122
}
128123

129124
const hasTrailingComma =
@@ -134,55 +129,15 @@ function calcTargetInfo(
134129
removeRanges.push(prevInfo.comma.range);
135130
}
136131
}
137-
insertCode += sourceCode.text.slice(
138-
nodeLastToken.range[1],
139-
endInfo.last.range![1],
140-
);
141-
removeRanges.push([nodeLastToken.range[1], endInfo.last.range![1]]);
132+
insertCode += sourceCode.text.slice(nodeEndIndex, endInfo.last.range![1]);
133+
removeRanges.push([nodeEndIndex, endInfo.last.range![1]]);
142134

143135
return {
144136
insertCode,
145137
removeRanges,
146138
};
147139
}
148140

149-
/**
150-
* Calculate the range of the target information from the around tokens.
151-
*/
152-
function calcTargetInfoFromAround(
153-
sourceCode: SourceCode,
154-
target: AroundTarget,
155-
): {
156-
insertCode: string;
157-
removeRanges: ESLintAST.Range[];
158-
} {
159-
const hasTrailingComma = isComma(target.after);
160-
const codeStart = target.before.range[1]; // to include comments
161-
let codeEnd: number;
162-
if (hasTrailingComma) {
163-
// , /**/,
164-
// ^^^^^^
165-
codeEnd = target.after.range[1];
166-
} else {
167-
// , /**/ ]
168-
// ^^^^^^
169-
codeEnd = target.after.range[0];
170-
}
171-
let removeStart = codeStart;
172-
if (!hasTrailingComma) {
173-
// The target is always the second or subsequent element, so it always has a leading comma.
174-
// , /**/ ]
175-
// ^^^^^^^
176-
removeStart = target.before.range[0];
177-
}
178-
179-
return {
180-
insertCode:
181-
sourceCode.text.slice(codeStart, codeEnd) + (hasTrailingComma ? "" : ","),
182-
removeRanges: [[removeStart, codeEnd]],
183-
};
184-
}
185-
186141
/**
187142
* Get the first token of the node.
188143
*/
@@ -193,7 +148,7 @@ function getFirstTokenOfNode(
193148
let token = sourceCode.getFirstToken(node as never)!;
194149
let target: ESLintAST.Token | null = token;
195150
while (
196-
(target = sourceCode.getTokenBefore(target)) &&
151+
(target = sourceCode.getTokenBefore(token)) &&
197152
isOpeningParenToken(target)
198153
) {
199154
token = target;
@@ -211,7 +166,7 @@ function getLastTokenOfNode(
211166
let token = sourceCode.getLastToken(node as never)!;
212167
let target: ESLintAST.Token | null = token;
213168
while (
214-
(target = sourceCode.getTokenAfter(target)) &&
169+
(target = sourceCode.getTokenAfter(token)) &&
215170
isClosingParenToken(target)
216171
) {
217172
token = target;
@@ -224,7 +179,7 @@ function getLastTokenOfNode(
224179
*/
225180
function getElementEndInfo(
226181
sourceCode: SourceCode,
227-
node: AST.JSONNode | ESLintAST.Token,
182+
target: Target | { node: ESLintAST.Token },
228183
): {
229184
// Trailing comma
230185
comma: ESLintAST.Token | null;
@@ -233,14 +188,15 @@ function getElementEndInfo(
233188
// The last token of the target element
234189
last: ESLintAST.Token | ESTree.Comment;
235190
} {
236-
const lastToken = getLastTokenOfNode(sourceCode, node);
237-
const afterToken = sourceCode.getTokenAfter(lastToken)!;
191+
const afterToken = target.node
192+
? sourceCode.getTokenAfter(getLastTokenOfNode(sourceCode, target.node))!
193+
: target.after;
238194
if (isNotCommaToken(afterToken)) {
239195
// If there is no comma, the element is the last element.
240196
return {
241197
comma: null,
242198
nextElement: null,
243-
last: getLastTokenWithTrailingComments(),
199+
last: getLastTokenWithTrailingComments(sourceCode, target),
244200
};
245201
}
246202
const comma = afterToken;
@@ -260,11 +216,13 @@ function getElementEndInfo(
260216
return {
261217
comma,
262218
nextElement: null,
263-
last: getLastTokenWithTrailingComments(),
219+
last: getLastTokenWithTrailingComments(sourceCode, target),
264220
};
265221
}
266222

267-
if (node.loc.end.line === nextElement.loc.start.line) {
223+
const node = target.node;
224+
225+
if (node && node.loc.end.line === nextElement.loc.start.line) {
268226
// There is no line break between the target element and the next element.
269227
return {
270228
comma,
@@ -274,6 +232,7 @@ function getElementEndInfo(
274232
}
275233
// There are line breaks between the target element and the next element.
276234
if (
235+
node &&
277236
node.loc.end.line < comma.loc.start.line &&
278237
comma.loc.end.line < nextElement.loc.start.line
279238
) {
@@ -289,29 +248,38 @@ function getElementEndInfo(
289248
return {
290249
comma,
291250
nextElement,
292-
last: getLastTokenWithTrailingComments(),
251+
last: getLastTokenWithTrailingComments(sourceCode, target),
293252
};
253+
}
294254

295-
/**
296-
* Get the last token of the target element with trailing comments.
297-
*/
298-
function getLastTokenWithTrailingComments() {
299-
if (lastToken == null) return afterToken;
300-
let last: ESLintAST.Token | ESTree.Comment = lastToken;
301-
let after = sourceCode.getTokenAfter(lastToken, {
255+
/**
256+
* Get the last token of the target element with trailing comments.
257+
*/
258+
function getLastTokenWithTrailingComments(
259+
sourceCode: SourceCode,
260+
target: Target | { node: ESLintAST.Token },
261+
) {
262+
if (!target.node) {
263+
return sourceCode.getTokenBefore(target.after, {
302264
includeComments: true,
303265
})!;
304-
while (
305-
(isCommentToken(after) || isComma(after)) &&
306-
node.loc.end.line === after.loc!.end.line
307-
) {
308-
last = after;
309-
after = sourceCode.getTokenAfter(after, {
310-
includeComments: true,
311-
})!;
312-
}
313-
return last;
314266
}
267+
const node = target.node;
268+
let last: ESLintAST.Token | ESTree.Comment = getLastTokenOfNode(
269+
sourceCode,
270+
node,
271+
);
272+
let after: ESLintAST.Token | ESTree.Comment | null;
273+
while (
274+
(after = sourceCode.getTokenAfter(last, {
275+
includeComments: true,
276+
})) &&
277+
(isCommentToken(after) || isComma(after)) &&
278+
node.loc.end.line === after.loc!.end.line
279+
) {
280+
last = after;
281+
}
282+
return last;
315283
}
316284

317285
/**
@@ -352,7 +320,7 @@ function getPrevElementInfo(
352320
};
353321
}
354322

355-
const endInfo = getElementEndInfo(sourceCode, prevElement);
323+
const endInfo = getElementEndInfo(sourceCode, { node: prevElement });
356324

357325
return {
358326
comma: endInfo.comma,

0 commit comments

Comments
 (0)