Skip to content
Permalink
Browse files

fix(eslint-plugin): [no-shadow] ignore global module augmentation (#2729

)
  • Loading branch information
cherryblossom000 committed Nov 1, 2020
1 parent 5df2719 commit d8c67a564a4cada5add8587f655aee2305cbc562
@@ -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

@@ -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';
@@ -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.
*/
@@ -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) {
@@ -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: [
{

0 comments on commit d8c67a5

Please sign in to comment.