Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat(eslint-plugin): add extension rule
dot-notation
(#1867)
- Loading branch information
1 parent
f667ff1
commit a85c3e1
Showing
7 changed files
with
409 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# enforce dot notation whenever possible (`dot-notation`) | ||
|
||
## Rule Details | ||
|
||
This rule extends the base [`eslint/dot-notation`](https://eslint.org/docs/rules/dot-notation) rule. | ||
It adds support for optionally ignoring computed `private` member access. | ||
|
||
## How to use | ||
|
||
```cjson | ||
{ | ||
// note you must disable the base rule as it can report incorrect errors | ||
"dot-notation": "off", | ||
"@typescript-eslint/dot-notation": ["error"] | ||
} | ||
``` | ||
|
||
## Options | ||
|
||
See [`eslint/dot-notation`](https://eslint.org/docs/rules/dot-notation#options) options. | ||
This rule adds the following options: | ||
|
||
```ts | ||
interface Options extends BaseDotNotationOptions { | ||
allowPrivateClassPropertyAccess?: boolean; | ||
} | ||
const defaultOptions: Options = { | ||
...baseDotNotationDefaultOptions, | ||
allowPrivateClassPropertyAccess: false, | ||
}; | ||
``` | ||
|
||
### `allowPrivateClassPropertyAccess` | ||
|
||
Example of a correct code when `allowPrivateClassPropertyAccess` is set to `true` | ||
|
||
```ts | ||
class X { | ||
private priv_prop = 123; | ||
} | ||
const x = new X(); | ||
x['priv_prop'] = 123; | ||
``` | ||
|
||
<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/dot-notation.md)</sup> |
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,80 @@ | ||
import { TSESTree } from '@typescript-eslint/experimental-utils'; | ||
import * as ts from 'typescript'; | ||
import baseRule from 'eslint/lib/rules/dot-notation'; | ||
import { | ||
InferOptionsTypeFromRule, | ||
InferMessageIdsTypeFromRule, | ||
createRule, | ||
getParserServices, | ||
} from '../util'; | ||
|
||
export type Options = InferOptionsTypeFromRule<typeof baseRule>; | ||
export type MessageIds = InferMessageIdsTypeFromRule<typeof baseRule>; | ||
|
||
export default createRule<Options, MessageIds>({ | ||
name: 'dot-notation', | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'enforce dot notation whenever possible', | ||
category: 'Best Practices', | ||
recommended: false, | ||
extendsBaseRule: true, | ||
requiresTypeChecking: true, | ||
}, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
allowKeywords: { | ||
type: 'boolean', | ||
default: true, | ||
}, | ||
allowPattern: { | ||
type: 'string', | ||
default: '', | ||
}, | ||
allowPrivateClassPropertyAccess: { | ||
tyoe: 'boolean', | ||
default: false, | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
fixable: baseRule.meta.fixable, | ||
messages: baseRule.meta.messages, | ||
}, | ||
defaultOptions: [ | ||
{ | ||
allowPrivateClassPropertyAccess: false, | ||
allowKeywords: true, | ||
allowPattern: '', | ||
}, | ||
], | ||
create(context, [options]) { | ||
const rules = baseRule.create(context); | ||
const allowPrivateClassPropertyAccess = | ||
options.allowPrivateClassPropertyAccess; | ||
|
||
const parserServices = getParserServices(context); | ||
const typeChecker = parserServices.program.getTypeChecker(); | ||
|
||
return { | ||
MemberExpression(node: TSESTree.MemberExpression): void { | ||
const objectSymbol = typeChecker.getSymbolAtLocation( | ||
parserServices.esTreeNodeToTSNodeMap.get(node.property), | ||
); | ||
|
||
if ( | ||
allowPrivateClassPropertyAccess && | ||
objectSymbol?.declarations[0]?.modifiers?.[0].kind === | ||
ts.SyntaxKind.PrivateKeyword | ||
) { | ||
return; | ||
} | ||
rules.MemberExpression(node); | ||
}, | ||
}; | ||
}, | ||
}); |
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
Oops, something went wrong.