Skip to content
Permalink
Browse files

feat: consume new scope analysis package (#2039)

  • Loading branch information
bradzacher committed Aug 29, 2020
1 parent e9d2ab6 commit 3be125d9bdbee1984ac6037874edf619213bd3d0
Showing with 3,129 additions and 166,901 deletions.
  1. +2 −0 .cspell.json
  2. +1 −0 .eslintrc.js
  3. +30 −29 packages/eslint-plugin/README.md
  4. +69 −0 packages/eslint-plugin/docs/rules/no-redeclare.md
  5. +50 −0 packages/eslint-plugin/docs/rules/no-shadow.md
  6. +0 −2 packages/eslint-plugin/docs/rules/no-unused-vars.md
  7. +31 −20 packages/eslint-plugin/docs/rules/no-use-before-define.md
  8. +1 −0 packages/eslint-plugin/package.json
  9. +4 −1 packages/eslint-plugin/src/configs/all.ts
  10. +4 −0 packages/eslint-plugin/src/rules/index.ts
  11. +2 −10 packages/eslint-plugin/src/rules/no-empty-interface.ts
  12. +297 −0 packages/eslint-plugin/src/rules/no-redeclare.ts
  13. +284 −0 packages/eslint-plugin/src/rules/no-shadow.ts
  14. +2 −0 packages/eslint-plugin/src/rules/no-unused-vars-experimental.ts
  15. +250 −29 packages/eslint-plugin/src/rules/no-unused-vars.ts
  16. +36 −35 packages/eslint-plugin/src/rules/no-use-before-define.ts
  17. +3 −3 packages/eslint-plugin/tests/eslint-rules/arrow-parens.test.ts
  18. +1 −1 packages/eslint-plugin/tests/eslint-rules/no-dupe-args.test.ts
  19. +1 −1 packages/eslint-plugin/tests/eslint-rules/no-implicit-globals.test.ts
  20. +13 −14 packages/eslint-plugin/tests/eslint-rules/no-restricted-globals.test.ts
  21. +0 −33 packages/eslint-plugin/tests/eslint-rules/no-shadow.test.ts
  22. +38 −12 packages/eslint-plugin/tests/eslint-rules/no-undef.test.ts
  23. +4 −4 packages/eslint-plugin/tests/eslint-rules/strict.test.ts
  24. +323 −28 packages/eslint-plugin/tests/{eslint-rules → rules}/no-redeclare.test.ts
  25. +1,223 −0 packages/eslint-plugin/tests/rules/no-shadow.test.ts
  26. +10 −0 packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts
  27. +282 −25 packages/eslint-plugin/tests/rules/no-unused-vars.test.ts
  28. +6 −19 packages/eslint-plugin/tests/rules/no-use-before-define.test.ts
  29. +1 −0 packages/eslint-plugin/tsconfig.build.json
  30. +5 −5 packages/eslint-plugin/typings/eslint-rules.d.ts
  31. +1 −0 packages/experimental-utils/package.json
  32. +45 −105 packages/experimental-utils/src/ts-eslint/Scope.ts
  33. +1 −0 packages/experimental-utils/tsconfig.build.json
  34. +30 −0 packages/parser/README.md
  35. +5 −4 packages/parser/package.json
  36. +0 −866 packages/parser/src/analyze-scope.ts
  37. +61 −4 packages/parser/src/parser.ts
  38. +0 −29 packages/parser/src/scope/scope-manager.ts
  39. +0 −25 packages/parser/src/scope/scopes.ts
  40. +0 −1,292 packages/parser/tests/lib/__snapshots__/basics.ts.snap
  41. +0 −20,131 packages/parser/tests/lib/__snapshots__/comments.ts.snap
  42. +0 −62,958 packages/parser/tests/lib/__snapshots__/javascript.ts.snap
  43. +0 −1,586 packages/parser/tests/lib/__snapshots__/jsx.ts.snap
  44. +0 −24,059 packages/parser/tests/lib/__snapshots__/scope-analysis.ts.snap
  45. +0 −355 packages/parser/tests/lib/__snapshots__/tsx.ts.snap
  46. +0 −54,697 packages/parser/tests/lib/__snapshots__/typescript.ts.snap
  47. +0 −79 packages/parser/tests/lib/basics.ts
  48. +0 −31 packages/parser/tests/lib/comments.ts
  49. +0 −24 packages/parser/tests/lib/javascript.ts
  50. +0 −51 packages/parser/tests/lib/jsx.ts
  51. +8 −21 packages/parser/tests/lib/parser.ts
  52. +0 −47 packages/parser/tests/lib/scope-analysis.ts
  53. +0 −24 packages/parser/tests/lib/tsx.ts
  54. +0 −24 packages/parser/tests/lib/typescript.ts
  55. +0 −174 packages/parser/tests/tools/scope-analysis.ts
  56. +0 −40 packages/parser/tests/tools/test-utils.ts
  57. +3 −2 packages/parser/tsconfig.build.json
  58. +2 −2 packages/types/src/ts-estree.ts
@@ -77,6 +77,8 @@
"Premade",
"prettier's",
"recurse",
"redeclaration",
"redeclarations",
"redeclared",
"reimplement",
"resync",
@@ -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',

Large diffs are not rendered by default.

@@ -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>
@@ -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>
@@ -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.
@@ -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,
}
```

@@ -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:

@@ -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>
@@ -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",
@@ -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',
@@ -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',
@@ -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';
@@ -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,
@@ -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 = [
{
@@ -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;
}
}

0 comments on commit 3be125d

Please sign in to comment.