Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(no-hooks): migrate to TS (#322)
- Loading branch information
Showing
6 changed files
with
110 additions
and
111 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,60 @@ | ||
import { TSESLint } from '@typescript-eslint/experimental-utils'; | ||
import rule from '../no-hooks'; | ||
import { HookName } from '../tsUtils'; | ||
|
||
const ruleTester = new TSESLint.RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 6, | ||
}, | ||
}); | ||
|
||
ruleTester.run('no-hooks', rule, { | ||
valid: [ | ||
'test("foo")', | ||
'describe("foo", () => { it("bar") })', | ||
'test("foo", () => { expect(subject.beforeEach()).toBe(true) })', | ||
{ | ||
code: 'afterEach(() => {}); afterAll(() => {});', | ||
options: [{ allow: [HookName.afterEach, HookName.afterAll] }], | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: 'beforeAll(() => {})', | ||
errors: [ | ||
{ messageId: 'unexpectedHook', data: { hookName: HookName.beforeAll } }, | ||
], | ||
}, | ||
{ | ||
code: 'beforeEach(() => {})', | ||
errors: [ | ||
{ | ||
messageId: 'unexpectedHook', | ||
data: { hookName: HookName.beforeEach }, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'afterAll(() => {})', | ||
errors: [ | ||
{ messageId: 'unexpectedHook', data: { hookName: HookName.afterAll } }, | ||
], | ||
}, | ||
{ | ||
code: 'afterEach(() => {})', | ||
errors: [ | ||
{ messageId: 'unexpectedHook', data: { hookName: HookName.afterEach } }, | ||
], | ||
}, | ||
{ | ||
code: 'beforeEach(() => {}); afterEach(() => { jest.resetModules() });', | ||
options: [{ allow: [HookName.afterEach] }], | ||
errors: [ | ||
{ | ||
messageId: 'unexpectedHook', | ||
data: { hookName: HookName.beforeEach }, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
This file was deleted.
Oops, something went wrong.
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,50 @@ | ||
import { HookName, createRule, isHook } from './tsUtils'; | ||
|
||
export default createRule({ | ||
name: __filename, | ||
meta: { | ||
docs: { | ||
category: 'Best Practices', | ||
description: 'Disallow setup and teardown hooks', | ||
recommended: false, | ||
}, | ||
messages: { | ||
unexpectedHook: "Unexpected '{{ hookName }}' hook", | ||
}, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
allow: { | ||
type: 'array', | ||
contains: ['beforeAll', 'beforeEach', 'afterAll', 'afterEach'], | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
type: 'suggestion', | ||
}, | ||
defaultOptions: [{ allow: [] } as { allow: readonly HookName[] }], | ||
create(context, [{ allow }]) { | ||
const whitelistedHookNames = allow.reduce((hashMap, value) => { | ||
hashMap[value] = true; | ||
return hashMap; | ||
}, Object.create(null)); | ||
|
||
const isWhitelisted = (node: { callee: { name: string } }) => | ||
whitelistedHookNames[node.callee.name]; | ||
|
||
return { | ||
CallExpression(node) { | ||
if (isHook(node) && !isWhitelisted(node)) { | ||
context.report({ | ||
node, | ||
messageId: 'unexpectedHook', | ||
data: { hookName: node.callee.name }, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
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