-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create
no-deprecated-functions
(#560)
- Loading branch information
Showing
6 changed files
with
171 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Disallow use of deprecated functions (no-deprecated-functions) | ||
|
||
Over the years Jest has accrued some debt in the form of functions that have | ||
either been renamed for clarity, or replaced with more powerful APIs. | ||
|
||
While typically these deprecated functions are kept in the codebase for a number | ||
of majors, eventually they are removed completely. | ||
|
||
## Rule details | ||
|
||
This rule warns about calls to deprecated functions, and provides details on | ||
what to replace them with. | ||
|
||
This rule can also autofix a number of these deprecations for you. | ||
|
||
### `require.requireActual` & `require.requireMock` | ||
|
||
These functions were replaced in Jest 21 and removed in Jest 26. | ||
|
||
Originally, the `requireActual` & `requireMock` the `requireActual`& | ||
`requireMock` functions were placed onto the `require` function. | ||
|
||
These functions were later moved onto the `jest` object in order to be easier | ||
for type checkers to handle, and their use via `require` deprecated. Finally, | ||
the release of Jest 26 saw them removed from the `require` function all | ||
together. | ||
|
||
### `jest.addMatchers` | ||
|
||
This function was replaced with `expect.extend` in Jest 17, and is scheduled for | ||
removal in Jest 27. | ||
|
||
### `jest.resetModuleRegistry` | ||
|
||
This function was renamed to `resetModules` in Jest 15, and is scheduled for | ||
removal in Jest 27. | ||
|
||
### `jest.runTimersToTime` | ||
|
||
This function was renamed to `advanceTimersByTime` in Jest 22, and is scheduled | ||
for removal in Jest 27. | ||
|
||
### `jest.genMockFromModule` | ||
|
||
This function was renamed to `createMockFromModule` in Jest 26, and is scheduled | ||
for removal in a future version of Jest. |
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
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,49 @@ | ||
import { TSESLint } from '@typescript-eslint/experimental-utils'; | ||
import rule from '../no-deprecated-functions'; | ||
|
||
const ruleTester = new TSESLint.RuleTester(); | ||
|
||
[ | ||
['require.requireMock', 'jest.requireMock'], | ||
['require.requireActual', 'jest.requireActual'], | ||
['jest.addMatchers', 'expect.extend'], | ||
['jest.resetModuleRegistry', 'jest.resetModules'], | ||
['jest.runTimersToTime', 'jest.advanceTimersByTime'], | ||
['jest.genMockFromModule', 'jest.createMockFromModule'], | ||
].forEach(([deprecation, replacement]) => { | ||
const [deprecatedName, deprecatedFunc] = deprecation.split('.'); | ||
const [replacementName, replacementFunc] = replacement.split('.'); | ||
|
||
ruleTester.run(`${deprecation} -> ${replacement}`, rule, { | ||
valid: [ | ||
'jest', | ||
'require("fs")', | ||
`${replacement}()`, | ||
replacement, | ||
`${replacementName}['${replacementFunc}']()`, | ||
`${replacementName}['${replacementFunc}']`, | ||
], | ||
invalid: [ | ||
{ | ||
code: `${deprecation}()`, | ||
output: `${replacement}()`, | ||
errors: [ | ||
{ | ||
messageId: 'deprecatedFunction', | ||
data: { deprecation, replacement }, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `${deprecatedName}['${deprecatedFunc}']()`, | ||
output: `${replacementName}['${replacementFunc}']()`, | ||
errors: [ | ||
{ | ||
messageId: 'deprecatedFunction', | ||
data: { deprecation, replacement }, | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
}); |
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,72 @@ | ||
import { | ||
AST_NODE_TYPES, | ||
TSESTree, | ||
} from '@typescript-eslint/experimental-utils'; | ||
import { createRule, getNodeName } from './utils'; | ||
|
||
export default createRule({ | ||
name: __filename, | ||
meta: { | ||
docs: { | ||
category: 'Best Practices', | ||
description: 'Disallow use of deprecated functions', | ||
recommended: false, | ||
}, | ||
messages: { | ||
deprecatedFunction: | ||
'`{{ deprecation }}` has been deprecated in favor of `{{ replacement }}`', | ||
}, | ||
type: 'suggestion', | ||
schema: [], | ||
fixable: 'code', | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
const deprecations: Record<string, string> = { | ||
'require.requireMock': 'jest.requireMock', | ||
'require.requireActual': 'jest.requireActual', | ||
'jest.addMatchers': 'expect.extend', | ||
'jest.resetModuleRegistry': 'jest.resetModules', | ||
'jest.runTimersToTime': 'jest.advanceTimersByTime', | ||
'jest.genMockFromModule': 'jest.createMockFromModule', | ||
}; | ||
|
||
return { | ||
CallExpression(node: TSESTree.CallExpression) { | ||
if (node.callee.type !== AST_NODE_TYPES.MemberExpression) { | ||
return; | ||
} | ||
|
||
const deprecation = getNodeName(node); | ||
|
||
if (!deprecation || !(deprecation in deprecations)) { | ||
return; | ||
} | ||
|
||
const replacement = deprecations[deprecation]; | ||
const { callee } = node; | ||
|
||
context.report({ | ||
messageId: 'deprecatedFunction', | ||
data: { | ||
deprecation, | ||
replacement, | ||
}, | ||
node, | ||
fix(fixer) { | ||
let [name, func] = replacement.split('.'); | ||
|
||
if (callee.property.type === AST_NODE_TYPES.Literal) { | ||
func = `'${func}'`; | ||
} | ||
|
||
return [ | ||
fixer.replaceText(callee.object, name), | ||
fixer.replaceText(callee.property, func), | ||
]; | ||
}, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |