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

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

Merged
merged 1 commit into from Nov 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/eslint-plugin/docs/rules/no-shadow.md
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
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
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