Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eslint-plugin): add extension rule dot-notation #1867

Merged
merged 11 commits into from Apr 26, 2020
1 change: 1 addition & 0 deletions packages/eslint-plugin/README.md
Expand Up @@ -178,6 +178,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/no-array-constructor`](./docs/rules/no-array-constructor.md) | Disallow generic `Array` constructors | :heavy_check_mark: | :wrench: | |
Expand Down
40 changes: 40 additions & 0 deletions packages/eslint-plugin/docs/rules/dot-notation.md
@@ -0,0 +1,40 @@
# 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 supports all options and features of the base rule.
bradzacher marked this conversation as resolved.
Show resolved Hide resolved

## 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

New options

- `allowPrivateClassPropertyAccess`
bradzacher marked this conversation as resolved.
Show resolved Hide resolved

This allows square-bracket notation for private class members.

```ts
interface Options {
allowPrivateClassPropertyAccess?: boolean;
}
```

```cjson
{
"allowPrivateClassPropertyAccess": true,
}
```

See [`eslint/dot-notation` options](https://eslint.org/docs/rules/dot-notation#options).
bradzacher marked this conversation as resolved.
Show resolved Hide resolved

<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
81 changes: 81 additions & 0 deletions packages/eslint-plugin/src/rules/dot-notation.ts
@@ -0,0 +1,81 @@
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: true,
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
},
},
additionalProperties: false,
},
],
fixable: baseRule.meta.fixable,
messages: baseRule.meta.messages,
},
defaultOptions: [
{
allowPrivateClassPropertyAccess: true,
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
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),
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
);

if (
allowPrivateClassPropertyAccess &&
objectSymbol?.declarations[0]?.modifiers &&
objectSymbol?.declarations[0]?.modifiers[0]?.kind ===
ts.SyntaxKind.PrivateKeyword
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
) {
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 @@ -108,6 +109,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