-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): complete wrap-schedule-instead-of-ctx-schedule-r…
…ule (#845)
- Loading branch information
1 parent
ec71d33
commit c4d044d
Showing
6 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
import './rules/unit-naming-rule.test' | ||
import './rules/async-rule.test' | ||
import './rules/wrap-schedule-instead-of-ctx-schedule-rule.test' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/eslint-plugin/src/rules/wrap-schedule-instead-of-ctx-schedule-rule.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { deprecateCtxScheduleRule } from "./wrap-schedule-instead-of-ctx-schedule-rule"; | ||
|
||
const { RuleTester } = require("eslint"); | ||
|
||
|
||
const tester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2022, | ||
sourceType: 'module', | ||
}, | ||
}) | ||
|
||
tester.run( | ||
"wrap-schedule-instead-ctx-schedule", | ||
deprecateCtxScheduleRule, | ||
{ | ||
valid: [ | ||
{ | ||
code: `import { wrap } from "@reatom/framework";\nwrap(ctx, () => {})`, | ||
}, | ||
{ | ||
code: `import { wrap } from "@reatom/framework";\nwrap(ctx, () => 'Dev')`, | ||
}, | ||
{ | ||
code: `import { schedule } from "@reatom/framework";\nschedule(ctx, () => 'Dev', -1)`, | ||
}, | ||
{ | ||
code: `import { schedule } from "@reatom/framework";\nschedule(ctx, () => 'Dev', -1)`, | ||
}, | ||
|
||
], | ||
invalid: [ | ||
{ | ||
code: "ctx.schedule()", | ||
output: `import { wrap } from "@reatom/framework";\nwrap(ctx, () => {})`, | ||
errors: [{message: "Use 'wrap(ctx, cb)' instead of deprecated 'ctx.schedule(cb, n)'."}] | ||
}, | ||
{ | ||
code: "ctx.schedule(() => 'Dev')", | ||
output: `import { wrap } from "@reatom/framework";\nwrap(ctx, () => 'Dev')`, | ||
errors: [{message: "Use 'wrap(ctx, cb)' instead of deprecated 'ctx.schedule(cb, n)'."}] | ||
}, | ||
{ | ||
code: "ctx.schedule(() => 'Dev', -1)", | ||
output: `import { schedule } from "@reatom/framework";\nschedule(ctx, () => 'Dev', -1)`, | ||
errors: [{message: "Use 'schedule(ctx, cb, n)' instead of deprecated 'ctx.schedule(cb, n)'."}] | ||
}, | ||
{ | ||
code: `import { schedule } from "@reatom/framework";\nctx.schedule(() => 'Dev', -1)`, | ||
output: `import { schedule } from "@reatom/framework";\nschedule(ctx, () => 'Dev', -1)`, | ||
errors: [{message: "Use 'schedule(ctx, cb, n)' instead of deprecated 'ctx.schedule(cb, n)'."}] | ||
}, | ||
], | ||
} | ||
); |
89 changes: 89 additions & 0 deletions
89
packages/eslint-plugin/src/rules/wrap-schedule-instead-of-ctx-schedule-rule.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import * as estree from 'estree' | ||
import { Rule } from 'eslint' | ||
import { checkCallExpressionNode } from '../shared' | ||
|
||
const importsMap = { | ||
wrap: 'import { wrap } from "@reatom/framework";\n', | ||
schedule: 'import { schedule } from "@reatom/framework";\n', | ||
} | ||
|
||
const getTextToReplace = (nText: string, callbackText: string) => { | ||
if (Boolean(nText)) { | ||
return `schedule(ctx, ${callbackText}, ${nText})` | ||
} | ||
return `wrap(ctx, ${callbackText})` | ||
} | ||
|
||
const getMessage = (n?: estree.Expression | estree.SpreadElement) => { | ||
if (Boolean(n)) { | ||
return "Use 'schedule(ctx, cb, n)' instead of deprecated 'ctx.schedule(cb, n)'." | ||
} | ||
|
||
return "Use 'wrap(ctx, cb)' instead of deprecated 'ctx.schedule(cb, n)'." | ||
} | ||
|
||
const isImportICareAbout = (node: estree.ImportDeclaration) => { | ||
return node.specifiers.some( | ||
(specifier) => | ||
specifier.type === 'ImportSpecifier' && | ||
(specifier.imported.name === 'wrap' || specifier.imported.name === 'schedule') && | ||
node.source.value === '@reatom/framework', | ||
) | ||
} | ||
|
||
export const deprecateCtxScheduleRule: Rule.RuleModule = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: "Method 'ctx.schedule' is deprecated in v4", | ||
}, | ||
fixable: 'code', | ||
hasSuggestions: true, | ||
schema: [], | ||
}, | ||
create(context) { | ||
let hasImport = false | ||
let lastImport: estree.ImportDeclaration | null = null | ||
|
||
return { | ||
ImportDeclaration(node: estree.ImportDeclaration) { | ||
lastImport = node | ||
if (isImportICareAbout(node)) { | ||
hasImport = true | ||
} | ||
}, | ||
|
||
CallExpression(node: estree.CallExpression) { | ||
if (checkCallExpressionNode(node)) { | ||
let cb = node.arguments[0] | ||
let n = node.arguments[1] | ||
|
||
context.report({ | ||
node, | ||
message: getMessage(n), | ||
fix(fixer) { | ||
const fixes = [] | ||
const sourceCode = context.sourceCode | ||
|
||
const callbackText = cb ? sourceCode.getText(cb) : '() => {}' | ||
const nText = n ? sourceCode.getText(n) : '' | ||
|
||
fixes.push(fixer.replaceText(node, getTextToReplace(nText, callbackText))) | ||
|
||
if (!hasImport) { | ||
const newImport = importsMap[n ? 'schedule' : 'wrap'] | ||
fixes.push( | ||
lastImport | ||
? fixer.insertTextBefore(lastImport, newImport) | ||
: fixer.insertTextAfterRange([0, 0], newImport), | ||
) | ||
} | ||
|
||
return fixes | ||
}, | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters