This repository has been archived by the owner on Jul 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add use-selector-in-select rule (#14)
- Loading branch information
Showing
5 changed files
with
189 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
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,42 @@ | ||
import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils' | ||
|
||
import { select } from './utils' | ||
|
||
export const ruleName = 'use-selector-in-select' | ||
|
||
export const messageId = 'useSelectorInSelect' | ||
export type MessageIds = typeof messageId | ||
|
||
type Options = [] | ||
|
||
export default ESLintUtils.RuleCreator(name => name)<Options, MessageIds>({ | ||
name: ruleName, | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
category: 'Possible Errors', | ||
description: | ||
'Using a selector in a select function is preferred in favor of strings/props drilling', | ||
extraDescription: [ | ||
'A selector is more performant, shareable and maintainable', | ||
], | ||
recommended: 'error', | ||
}, | ||
schema: [], | ||
messages: { | ||
[messageId]: | ||
'Using string or props drilling is not preferred, use a selector instead', | ||
}, | ||
}, | ||
defaultOptions: [], | ||
create: context => { | ||
return { | ||
[select](node: TSESTree.Literal | TSESTree.ArrowFunctionExpression) { | ||
context.report({ | ||
node, | ||
messageId, | ||
}) | ||
}, | ||
} | ||
}, | ||
}) |
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,135 @@ | ||
import { stripIndent } from 'common-tags' | ||
import rule, { | ||
ruleName, | ||
messageId, | ||
} from '../../src/rules/use-selector-in-select' | ||
import { ruleTester } from '../utils' | ||
|
||
ruleTester().run(ruleName, rule, { | ||
valid: [ | ||
`store.pipe(select(selectCustomers))`, | ||
`store.pipe(select(selectorsObj.selectCustomers))`, | ||
`store.select(selectCustomers)`, | ||
`store.select(selectorsObj.selectCustomers)`, | ||
], | ||
invalid: [ | ||
{ | ||
code: stripIndent` | ||
store.pipe(select('customers'))`, | ||
errors: [ | ||
{ | ||
messageId, | ||
line: 1, | ||
column: 19, | ||
endLine: 1, | ||
endColumn: 30, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: stripIndent` | ||
store.select('customers')`, | ||
errors: [ | ||
{ | ||
messageId, | ||
line: 1, | ||
column: 14, | ||
endLine: 1, | ||
endColumn: 25, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: stripIndent` | ||
store.pipe(select('customers', 'orders'))`, | ||
errors: [ | ||
{ | ||
messageId, | ||
line: 1, | ||
column: 19, | ||
endLine: 1, | ||
endColumn: 30, | ||
}, | ||
{ | ||
messageId, | ||
line: 1, | ||
column: 32, | ||
endLine: 1, | ||
endColumn: 40, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: stripIndent` | ||
store.select('customers', 'orders')`, | ||
errors: [ | ||
{ | ||
messageId, | ||
line: 1, | ||
column: 14, | ||
endLine: 1, | ||
endColumn: 25, | ||
}, | ||
{ | ||
messageId, | ||
line: 1, | ||
column: 27, | ||
endLine: 1, | ||
endColumn: 35, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: stripIndent` | ||
store.pipe(select(state => state.customers))`, | ||
errors: [ | ||
{ | ||
messageId, | ||
line: 1, | ||
column: 19, | ||
endLine: 1, | ||
endColumn: 43, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: stripIndent` | ||
store.select(state => state.customers)`, | ||
errors: [ | ||
{ | ||
messageId, | ||
line: 1, | ||
column: 14, | ||
endLine: 1, | ||
endColumn: 38, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: stripIndent` | ||
store.pipe(select(state => state.customers.orders))`, | ||
errors: [ | ||
{ | ||
messageId, | ||
line: 1, | ||
column: 19, | ||
endLine: 1, | ||
endColumn: 50, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: stripIndent` | ||
store.select(state => state.customers.orders)`, | ||
errors: [ | ||
{ | ||
messageId, | ||
line: 1, | ||
column: 14, | ||
endLine: 1, | ||
endColumn: 45, | ||
}, | ||
], | ||
}, | ||
], | ||
}) |