Skip to content

Commit

Permalink
fix(eslint-plugin): [no-shadow] ignore global module augmentation (#2729
Browse files Browse the repository at this point in the history
)
  • Loading branch information
cherryblossom000 committed Nov 1, 2020
1 parent 5df2719 commit d8c67a5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/eslint-plugin/docs/rules/no-shadow.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## 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.
It adds support for TypeScript's `this` parameters and global augmentation, and adds options for TypeScript features.

## How to use

Expand Down
16 changes: 16 additions & 0 deletions packages/eslint-plugin/src/rules/no-shadow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
TSESLint,
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';
import { ScopeType } from '@typescript-eslint/scope-manager';
import * as util from '../util';

type MessageIds = 'noShadow';
Expand Down Expand Up @@ -67,6 +68,16 @@ export default util.createRule<Options, MessageIds>({
},
],
create(context, [options]) {
/**
* Check if a scope is a TypeScript module augmenting the global namespace.
*/
function isGlobalAugmentation(scope: TSESLint.Scope.Scope): boolean {
return (
(scope.type === ScopeType.tsModule && !!scope.block.global) ||
(!!scope.upper && isGlobalAugmentation(scope.upper))
);
}

/**
* Check if variable is a `this` parameter.
*/
Expand Down Expand Up @@ -261,6 +272,11 @@ export default util.createRule<Options, MessageIds>({
* @param {Scope} scope Fixme
*/
function checkForShadows(scope: TSESLint.Scope.Scope): void {
// ignore global augmentation
if (isGlobalAugmentation(scope)) {
return;
}

const variables = scope.variables;

for (const variable of variables) {
Expand Down
43 changes: 43 additions & 0 deletions packages/eslint-plugin/tests/rules/no-shadow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,49 @@ type Fn = (Foo: string) => typeof Foo;
Foo: 'writable',
},
},
// https://github.com/typescript-eslint/typescript-eslint/issues/2724
{
code: `
declare global {
interface ArrayConstructor {}
}
export {};
`,
options: [{ builtinGlobals: true }],
},
`
declare global {
const a: string;
namespace Foo {
const a: number;
}
}
export {};
`,
{
code: `
declare global {
type A = 'foo';
namespace Foo {
type A = 'bar';
}
}
export {};
`,
options: [{ ignoreTypeValueShadow: false }],
},
{
code: `
declare global {
const foo: string;
type Fn = (foo: number) => void;
}
export {};
`,
options: [{ ignoreFunctionTypeParameterNameValueShadow: false }],
},
],
invalid: [
{
Expand Down

0 comments on commit d8c67a5

Please sign in to comment.