Skip to content

Commit e5baff2

Browse files
authored
Fix debug patronum (#126)
Fix false positives from the rule Add test case when there are no import Fix delete without formatting loss when using semicolon
1 parent 7f3cb07 commit e5baff2

File tree

2 files changed

+60
-8
lines changed

2 files changed

+60
-8
lines changed

rules/no-patronum-debug/no-patronum-debug.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ module.exports = {
3131
});
3232
},
3333
CallExpression(node) {
34-
const currentMethodName = node.callee?.name ?? node.callee?.object.name;
34+
const currentMethod = node?.callee?.name ?? node?.callee?.object.name;
3535
const importedDebugFromPatronum = importedFromPatronum.get("debug");
3636

37-
if (currentMethodName !== importedDebugFromPatronum) {
37+
if (
38+
!importedDebugFromPatronum ||
39+
currentMethod !== importedDebugFromPatronum
40+
) {
3841
return;
3942
}
4043

@@ -71,7 +74,12 @@ function* removeDebugFromPatronum({
7174
const startToken = sourceCode.getTokenBefore(node);
7275

7376
// remove line with debug
74-
yield fixer.removeRange([startToken.range[1], node.range[1] + 1]);
77+
yield fixer.removeRange([startToken.range[1], node.range[1]]);
78+
const semi = sourceCode.getTokenBefore(node, {
79+
filter: (token) => token.value === ";",
80+
});
81+
82+
if (semi) yield fixer.remove(semi);
7583

7684
const importDebugNode = importNodes.get(targetMethod);
7785

@@ -81,12 +89,13 @@ function* removeDebugFromPatronum({
8189

8290
// remove import with debug
8391
const importParentNode = importDebugNode.parent;
92+
const amountImportFromPatronum = importParentNode.specifiers.length;
8493

8594
/**
8695
* import { debug } from 'patronum'
8796
* import { debug } from 'patronum/debug'
8897
*/
89-
if (importParentNode.specifiers.length === 1) {
98+
if (amountImportFromPatronum === 1) {
9099
yield fixer.removeRange([
91100
importParentNode.range[0],
92101
importParentNode.range[1] + 1,
@@ -95,9 +104,7 @@ function* removeDebugFromPatronum({
95104
return null;
96105
}
97106

98-
const amountImportFromPatronum = importParentNode.specifiers.length;
99107
const importLast = importParentNode.specifiers[amountImportFromPatronum - 1];
100-
101108
const filterTokenComma = { filter: (token) => token.value === "," };
102109

103110
/**

rules/no-patronum-debug/no-patronum-debug.test.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ debug(store);
2626
`
2727
import { debug } from 'patronum/debug'
2828
sample({ clock: tick, target: runFx })
29+
`,
30+
`
31+
const $store = createStore(false).on(open, () => true)
2932
`,
3033
].map((code) => ({ code })),
3134

@@ -80,7 +83,7 @@ const buttonClicked = createEvent();
8083
{
8184
code: `
8285
import { delay, debug, condition } from 'patronum';
83-
const $store = createEffect(() => 'effectFx');
86+
const effectFx = createEffect(() => 'effectFx');
8487
debug({ trace: true }, $store);
8588
`,
8689
errors: [
@@ -92,7 +95,7 @@ debug({ trace: true }, $store);
9295
messageId: "removePatronumDebug",
9396
output: `
9497
import { delay, condition } from 'patronum';
95-
const $store = createEffect(() => 'effectFx');
98+
const effectFx = createEffect(() => 'effectFx');
9699
`,
97100
},
98101
],
@@ -214,5 +217,47 @@ const event = createEvent()`,
214217
},
215218
],
216219
},
220+
{
221+
code: `
222+
import { condition, debug, delay } from 'patronum'
223+
224+
const $store = createStore(false)
225+
226+
const createTest = () => {
227+
const state = {
228+
equal(a, b) {
229+
return a === b
230+
}
231+
}
232+
debug($store)
233+
return state
234+
}
235+
`,
236+
errors: [
237+
{
238+
messageId: "noPatronumDebug",
239+
type: "CallExpression",
240+
suggestions: [
241+
{
242+
messageId: "removePatronumDebug",
243+
output: `
244+
import { condition, delay } from 'patronum'
245+
246+
const $store = createStore(false)
247+
248+
const createTest = () => {
249+
const state = {
250+
equal(a, b) {
251+
return a === b
252+
}
253+
}
254+
return state
255+
}
256+
`,
257+
},
258+
],
259+
},
260+
],
261+
},
217262
],
218263
});

0 commit comments

Comments
 (0)