-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
es-roikoren/no-private-in
rule (#11)
* feat: add `es-roikoren/no-private-in` rule * test: fix
- Loading branch information
1 parent
20f6399
commit ae75c1b
Showing
7 changed files
with
97 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'eslint-plugin-es-roikoren': patch | ||
--- | ||
|
||
feat: add `es-roikoren/no-private-in` rule |
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,25 @@ | ||
# es-roikoren/no-private-in | ||
> disallow `#x in obj`. | ||
- ✅ The following configurations enable this rule: `plugin:es-roikoren/no-new-in-esnext` | ||
|
||
This rule reports ES2022 private in (`#x in obj`) as errors. | ||
|
||
## Examples | ||
|
||
⛔ Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
/*eslint es-roikoren/no-private-in: error */ | ||
class A { | ||
#x; | ||
fn() { | ||
var hasX = #x in obj; | ||
} | ||
} | ||
``` | ||
|
||
## 📚 References | ||
|
||
- [Rule source](https://github.com/roikoren755/eslint-plugin-es/blob/v0.0.6/src/rules/no-private-in.ts) | ||
- [Test source](https://github.com/roikoren755/eslint-plugin-es/blob/v0.0.6/tests/src/rules/no-private-in.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
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,33 @@ | ||
import type { TSESTree } from '@typescript-eslint/typescript-estree'; | ||
|
||
import { createRule } from '../util/create-rule'; | ||
|
||
export const category = 'ES2022'; | ||
export default createRule<[], 'forbidden'>({ | ||
name: 'no-private-in', | ||
meta: { | ||
type: 'problem', | ||
docs: { description: 'disallow `#x in obj`.', recommended: false }, | ||
schema: [], | ||
messages: { forbidden: 'ES2022 private in (`#{{private}} in {{object}}`) is forbidden.' }, | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
"BinaryExpression[operator='in'] > PrivateIdentifier.left"(node: TSESTree.PrivateIdentifier) { | ||
if (node.parent?.type !== 'BinaryExpression') { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node, | ||
messageId: 'forbidden', | ||
data: { | ||
private: node.name, | ||
object: node.parent.right.type === 'Identifier' ? node.parent.right.name : 'object', | ||
}, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |
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,30 @@ | ||
import type { TSESLint } from '@typescript-eslint/experimental-utils'; | ||
import { AST_NODE_TYPES } from '@typescript-eslint/types'; | ||
|
||
import { RuleTester } from '../../tester'; | ||
import rule from '../../../src/rules/no-private-in'; | ||
|
||
const error = (object: string): TSESLint.TestCaseError<'forbidden'> => ({ | ||
messageId: 'forbidden' as const, | ||
line: 1, | ||
column: 31, | ||
type: AST_NODE_TYPES.PrivateIdentifier, | ||
data: { object, private: 'x' }, | ||
}); | ||
|
||
if (!RuleTester.isSupported(2022)) { | ||
console.log('Skip the tests of no-private-in.'); | ||
} else { | ||
new RuleTester().run('no-private-in', rule, { | ||
valid: [ | ||
"class A { f(obj) { return '#x' in obj } }", | ||
'class A { f(obj) { return x in obj } }', | ||
'class A { #x; f(obj) { return foo in obj.#x } }', | ||
], | ||
invalid: [ | ||
{ code: 'class A { #x; f(obj) { return #x in obj } }', errors: [error('obj')] }, | ||
{ code: 'class A { #x; f(obj) { return #x in obj.foo } }', errors: [error('object')] }, | ||
{ code: 'class A { #x; f(obj) { return #x in obj.#x } }', errors: [error('object')] }, | ||
], | ||
}); | ||
} |