Skip to content

Commit

Permalink
feat(eslint-plugin): add extension rule dot-notation (#1867)
Browse files Browse the repository at this point in the history
  • Loading branch information
anikethsaha committed Apr 26, 2020
1 parent f667ff1 commit a85c3e1
Show file tree
Hide file tree
Showing 7 changed files with 409 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/eslint-plugin/README.md
Expand Up @@ -182,6 +182,7 @@ In these cases, we create what we call an extension rule; a rule within our plug
| [`@typescript-eslint/brace-style`](./docs/rules/brace-style.md) | Enforce consistent brace style for blocks | | :wrench: | |
| [`@typescript-eslint/comma-spacing`](./docs/rules/comma-spacing.md) | Enforces consistent spacing before and after commas | | :wrench: | |
| [`@typescript-eslint/default-param-last`](./docs/rules/default-param-last.md) | Enforce default parameters to be last | | | |
| [`@typescript-eslint/dot-notation`](./docs/rules/dot-notation.md) | enforce dot notation whenever possible | | :wrench: | :thought_balloon: |
| [`@typescript-eslint/func-call-spacing`](./docs/rules/func-call-spacing.md) | Require or disallow spacing between function identifiers and their invocations | | :wrench: | |
| [`@typescript-eslint/indent`](./docs/rules/indent.md) | Enforce consistent indentation | | :wrench: | |
| [`@typescript-eslint/init-declarations`](./docs/rules/init-declarations.md) | require or disallow initialization in variable declarations | | | |
Expand Down
46 changes: 46 additions & 0 deletions packages/eslint-plugin/docs/rules/dot-notation.md
@@ -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>
2 changes: 2 additions & 0 deletions packages/eslint-plugin/src/configs/all.json
Expand Up @@ -15,6 +15,8 @@
"@typescript-eslint/consistent-type-definitions": "error",
"default-param-last": "off",
"@typescript-eslint/default-param-last": "error",
"dot-notation": "off",
"@typescript-eslint/dot-notation": "error",
"@typescript-eslint/explicit-function-return-type": "error",
"@typescript-eslint/explicit-member-accessibility": "error",
"@typescript-eslint/explicit-module-boundary-types": "error",
Expand Down
80 changes: 80 additions & 0 deletions packages/eslint-plugin/src/rules/dot-notation.ts
@@ -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);
},
};
},
});
2 changes: 2 additions & 0 deletions packages/eslint-plugin/src/rules/index.ts
Expand Up @@ -12,6 +12,7 @@ import commaSpacing from './comma-spacing';
import consistentTypeAssertions from './consistent-type-assertions';
import consistentTypeDefinitions from './consistent-type-definitions';
import defaultParamLast from './default-param-last';
import dotNotation from './dot-notation';
import explicitFunctionReturnType from './explicit-function-return-type';
import explicitMemberAccessibility from './explicit-member-accessibility';
import explicitModuleBoundaryTypes from './explicit-module-boundary-types';
Expand Down Expand Up @@ -114,6 +115,7 @@ export default {
'consistent-type-assertions': consistentTypeAssertions,
'consistent-type-definitions': consistentTypeDefinitions,
'default-param-last': defaultParamLast,
'dot-notation': dotNotation,
'explicit-function-return-type': explicitFunctionReturnType,
'explicit-member-accessibility': explicitMemberAccessibility,
'explicit-module-boundary-types': explicitModuleBoundaryTypes,
Expand Down

0 comments on commit a85c3e1

Please sign in to comment.