Skip to content

Commit

Permalink
feat: consume new scope analysis package (#2039)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher committed Aug 29, 2020
1 parent e9d2ab6 commit 3be125d
Show file tree
Hide file tree
Showing 58 changed files with 3,129 additions and 166,901 deletions.
2 changes: 2 additions & 0 deletions .cspell.json
Expand Up @@ -77,6 +77,8 @@
"Premade",
"prettier's",
"recurse",
"redeclaration",
"redeclarations",
"redeclared",
"reimplement",
"resync",
Expand Down
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -214,6 +214,7 @@ module.exports = {
'packages/eslint-plugin-internal/tests/rules/**/*.test.ts',
'packages/eslint-plugin-tslint/tests/rules/**/*.test.ts',
'packages/eslint-plugin/tests/rules/**/*.test.ts',
'packages/eslint-plugin/tests/eslint-rules/**/*.test.ts',
],
rules: {
'@typescript-eslint/internal/plugin-test-formatting': 'error',
Expand Down
59 changes: 30 additions & 29 deletions packages/eslint-plugin/README.md

Large diffs are not rendered by default.

69 changes: 69 additions & 0 deletions packages/eslint-plugin/docs/rules/no-redeclare.md
@@ -0,0 +1,69 @@
# Disallow variable redeclaration (`no-redeclare`)

## Rule Details

This rule extends the base [`eslint/no-redeclare`](https://eslint.org/docs/rules/no-redeclare) rule.
It adds support for TypeScript function overloads, and declaration merging.

## How to use

```jsonc
{
// note you must disable the base rule as it can report incorrect errors
"no-redeclare": "off",
"@typescript-eslint/no-redeclare": ["error"]
}
```

## Options

See [`eslint/no-redeclare` options](https://eslint.org/docs/rules/no-redeclare#options).
This rule adds the following options:

```ts
interface Options extends BaseNoShadowOptions {
ignoreDeclarationMerge?: boolean;
}

const defaultOptions: Options = {
...baseNoShadowDefaultOptions,
ignoreDeclarationMerge: true,
};
```

### `ignoreDeclarationMerge`

When set to `true`, the rule will ignore declaration merges between the following sets:

- interface + interface
- namespace + namespace
- class + interface
- class + namespace
- class + interface + namespace
- function + namespace

Examples of **correct** code with `{ ignoreDeclarationMerge: true }`:

```ts
interface A {
prop1: 1;
}
interface A {
prop2: 2;
}

namespace Foo {
export const a = 1;
}
namespace Foo {
export const b = 2;
}

class Bar {}
namespace Bar {}

function Baz() {}
namespace Baz {}
```

<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/no-redeclare.md)</sup>
50 changes: 50 additions & 0 deletions packages/eslint-plugin/docs/rules/no-shadow.md
@@ -0,0 +1,50 @@
# Disallow variable declarations from shadowing variables declared in the outer scope (`no-shadow`)

## Rule Details

This rule extends the base [`eslint/no-shadow`](https://eslint.org/docs/rules/no-shadow) rule.
It adds support for TypeScript's `this` parameters, and adds options for TypeScript features.

## How to use

```jsonc
{
// note you must disable the base rule as it can report incorrect errors
"no-shadow": "off",
"@typescript-eslint/no-shadow": ["error"]
}
```

## Options

See [`eslint/no-shadow` options](https://eslint.org/docs/rules/no-shadow#options).
This rule adds the following options:

```ts
interface Options extends BaseNoShadowOptions {
ignoreTypeValueShadow?: boolean;
}

const defaultOptions: Options = {
...baseNoShadowDefaultOptions,
ignoreTypeValueShadow: true,
};
```

### `ignoreTypeValueShadow`

When set to `true`, the rule will ignore when you name a type and a variable with the same name.

Examples of **correct** code with `{ ignoreTypeValueShadow: true }`:

```ts
type Foo = number;
const Foo = 1;

interface Bar {
prop: number;
}
const Bar = 'test';
```

<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/no-shadow.md)</sup>
2 changes: 0 additions & 2 deletions packages/eslint-plugin/docs/rules/no-unused-vars.md
@@ -1,7 +1,5 @@
# Disallow unused variables (`no-unused-vars`)

## PLEASE READ THIS ISSUE BEFORE USING THIS RULE [#1856](https://github.com/typescript-eslint/typescript-eslint/issues/1856)

## Rule Details

This rule extends the base [`eslint/no-unused-vars`](https://eslint.org/docs/rules/no-unused-vars) rule.
Expand Down
51 changes: 31 additions & 20 deletions packages/eslint-plugin/docs/rules/no-use-before-define.md
Expand Up @@ -23,42 +23,34 @@ See [`eslint/no-use-before-define` options](https://eslint.org/docs/rules/no-use
This rule adds the following options:

```ts
interface Options extends BaseNoMagicNumbersOptions {
interface Options extends BaseNoUseBeforeDefineOptions {
enums?: boolean;
typedefs?: boolean;
ignoreTypeReferences?: boolean;
}

const defaultOptions: Options = {
...baseNoMagicNumbersDefaultOptions,
...baseNoUseBeforeDefineDefaultOptions,
enums: true,
typedefs: true,
ignoreTypeReferences: true,
};
```

### `enums`

The flag which shows whether or not this rule checks enum declarations of upper scopes.
If this is `true`, this rule warns every reference to a enum before the enum declaration.
Otherwise, ignores those references.
If this is `false`, this rule will ignore references to enums, when the reference is in a child scope.

Examples of **incorrect** code for the `{ "enums": true }` option:
Examples of **incorrect** code for the `{ "enums": false }` option:

```ts
/*eslint no-use-before-define: ["error", { "enums": true }]*/
/*eslint no-use-before-define: ["error", { "enums": false }]*/

function foo() {
return Foo.FOO;
}

class Test {
foo() {
return Foo.FOO;
}
}
const x = Foo.FOO;

enum Foo {
FOO,
BAR,
}
```

Expand All @@ -78,10 +70,8 @@ enum Foo {

### `typedefs`

The flag which shows whether or not this rule checks type declarations.
If this is `true`, this rule warns every reference to a type before the type declaration.
Otherwise, ignores those references.
Type declarations are hoisted, so it's safe.
If this is `false`, this rule will ignore references to types.

Examples of **correct** code for the `{ "typedefs": false }` option:

Expand All @@ -92,4 +82,25 @@ let myVar: StringOrNumber;
type StringOrNumber = string | number;
```

Copied from [the original ESLint rule docs](https://github.com/eslint/eslint/blob/a113cd3/docs/rules/no-use-before-define.md)
### `ignoreTypeReferences`

If this is `true`, this rule ignores all type references, such as in type annotations and assertions.
If this is `false`, this will will check all type references.

Examples of **correct** code for the `{ "ignoreTypeReferences": true }` option:

```ts
/*eslint no-use-before-define: ["error", { "ignoreTypeReferences": true }]*/

let var1: StringOrNumber;
type StringOrNumber = string | number;

let var2: Enum;
enum Enum {}
```

### Other Options

See [`eslint/no-use-before-define` options](https://eslint.org/docs/rules/no-use-before-define#options).

<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/no-use-before-define.md)</sup>
1 change: 1 addition & 0 deletions packages/eslint-plugin/package.json
Expand Up @@ -43,6 +43,7 @@
},
"dependencies": {
"@typescript-eslint/experimental-utils": "3.10.1",
"@typescript-eslint/scope-manager": "3.10.1",
"debug": "^4.1.1",
"functional-red-black-tree": "^1.0.1",
"regexpp": "^3.0.0",
Expand Down
5 changes: 4 additions & 1 deletion packages/eslint-plugin/src/configs/all.ts
Expand Up @@ -74,7 +74,11 @@ export = {
'@typescript-eslint/no-non-null-asserted-optional-chain': 'error',
'@typescript-eslint/no-non-null-assertion': 'error',
'@typescript-eslint/no-parameter-properties': 'error',
'no-redeclare': 'off',
'@typescript-eslint/no-redeclare': 'error',
'@typescript-eslint/no-require-imports': 'error',
'no-shadow': 'off',
'@typescript-eslint/no-shadow': 'error',
'@typescript-eslint/no-this-alias': 'error',
'@typescript-eslint/no-throw-literal': 'error',
'@typescript-eslint/no-type-alias': 'error',
Expand All @@ -91,7 +95,6 @@ export = {
'@typescript-eslint/no-unused-expressions': 'error',
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': 'error',
'@typescript-eslint/no-unused-vars-experimental': 'error',
'no-use-before-define': 'off',
'@typescript-eslint/no-use-before-define': 'error',
'no-useless-constructor': 'off',
Expand Down
4 changes: 4 additions & 0 deletions packages/eslint-plugin/src/rules/index.ts
Expand Up @@ -51,7 +51,9 @@ import noNamespace from './no-namespace';
import noNonNullAssertedOptionalChain from './no-non-null-asserted-optional-chain';
import noNonNullAssertion from './no-non-null-assertion';
import noParameterProperties from './no-parameter-properties';
import noRedeclare from './no-redeclare';
import noRequireImports from './no-require-imports';
import noShadow from './no-shadow';
import noThisAlias from './no-this-alias';
import noThrowLiteral from './no-throw-literal';
import noTypeAlias from './no-type-alias';
Expand Down Expand Up @@ -152,7 +154,9 @@ export default {
'no-non-null-asserted-optional-chain': noNonNullAssertedOptionalChain,
'no-non-null-assertion': noNonNullAssertion,
'no-parameter-properties': noParameterProperties,
'no-redeclare': noRedeclare,
'no-require-imports': noRequireImports,
'no-shadow': noShadow,
'no-this-alias': noThisAlias,
'no-throw-literal': noThrowLiteral,
'no-type-alias': noTypeAlias,
Expand Down
12 changes: 2 additions & 10 deletions packages/eslint-plugin/src/rules/no-empty-interface.ts
@@ -1,8 +1,5 @@
import * as util from '../util';
import {
AST_NODE_TYPES,
TSESLint,
} from '@typescript-eslint/experimental-utils';
import { TSESLint } from '@typescript-eslint/experimental-utils';

type Options = [
{
Expand Down Expand Up @@ -81,12 +78,7 @@ export default util.createRule<Options, MessageIds>({
let useAutoFix = true;
if (util.isDefinitionFile(filename)) {
const scope = context.getScope();
if (
scope.block.parent &&
scope.block.parent.type ===
AST_NODE_TYPES.TSModuleDeclaration &&
scope.block.parent.declare
) {
if (scope.type === 'tsModule' && scope.block.declare) {
useAutoFix = false;
}
}
Expand Down

0 comments on commit 3be125d

Please sign in to comment.