Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(eslint-plugin): [no-shadow] ignore declare variables in definition files shadowing global variables#10710

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletionpackages/eslint-plugin/src/rules/no-shadow.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,7 @@ import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import{DefinitionType,ScopeType}from'@typescript-eslint/scope-manager';
import{AST_NODE_TYPES,ASTUtils}from'@typescript-eslint/utils';

import{createRule}from'../util';
import{createRule,isDefinitionFile}from'../util';
import{isTypeImport}from'../util/isTypeImport';

exporttypeMessageIds='noShadow'|'noShadowGlobal';
Expand DownExpand Up@@ -566,6 +566,25 @@ export default createRule<Options, MessageIds>({
};
}

/**
* Checks if the initialization of a variable has the declare modifier in a
* definition file.
*/
functionisDeclareInDTSFile(variable:TSESLint.Scope.Variable):boolean{
constfileName=context.filename;
if(!isDefinitionFile(fileName)){
returnfalse;
}
returnvariable.defs.some(def=>{
return(
(def.type===DefinitionType.Variable&&def.parent.declare)||
(def.type===DefinitionType.ClassName&&def.node.declare)||
(def.type===DefinitionType.TSEnumName&&def.node.declare)||
(def.type===DefinitionType.TSModuleName&&def.node.declare)
);
});
}

/**
* Checks the current context for shadowed variables.
*@param scope Fixme
Expand DownExpand Up@@ -604,6 +623,11 @@ export default createRule<Options, MessageIds>({
continue;
}

// ignore variables with the declare keyword in .d.ts files
if(isDeclareInDTSFile(variable)){
continue;
}

// Gets shadowed variable.
constshadowed=scope.upper
?ASTUtils.findVariable(scope.upper,variable.name)
Expand Down
171 changes: 171 additions & 0 deletionspackages/eslint-plugin/tests/rules/no-shadow/no-shadow.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -847,6 +847,48 @@ function foo<T extends (...args: any[]) => any>(fn: T, args: any[]) {}
},
],
},
{
code: `
declare const has = (environment: 'dev' | 'prod' | 'test') => boolean;
`,
errors: [
{
data: {
name: 'has',
},
messageId: 'noShadowGlobal',
},
],
languageOptions: {
globals: {
has: false,
},
},
options: [{ builtinGlobals: true }],
},
{
code: `
declare const has: (environment: 'dev' | 'prod' | 'test') => boolean;
const fn = (has: string) => {};
`,
errors: [
{
data: {
name: 'has',
shadowedColumn: 15,
shadowedLine: 2,
},
messageId: 'noShadow',
},
],
filename: 'foo.d.ts',
languageOptions: {
globals: {
has: false,
},
},
options: [{ builtinGlobals: true }],
},
],
valid: [
'function foo<T = (arg: any) => any>(arg: T) {}',
Expand DownExpand Up@@ -1455,5 +1497,134 @@ declare module 'bar' {
}
`,
},
{
code: `
declare const foo1: boolean;
`,
filename: 'baz.d.ts',
languageOptions: {
globals: {
foo1: false,
},
},
options: [{ builtinGlobals: true }],
},
{
code: `
declare let foo2: boolean;
`,
filename: 'baz.d.ts',
languageOptions: {
globals: {
foo2: false,
},
},
options: [{ builtinGlobals: true }],
},
{
code: `
declare var foo3: boolean;
`,
filename: 'baz.d.ts',
languageOptions: {
globals: {
foo3: false,
},
},
options: [{ builtinGlobals: true }],
},
{
code: `
function foo4(name: string): void;
`,
filename: 'baz.d.ts',
languageOptions: {
globals: {
foo4: false,
},
},
options: [{ builtinGlobals: true }],
},
{
code: `
declare class Foopy1 {
name: string;
}
`,
filename: 'baz.d.ts',
languageOptions: {
globals: {
Foopy1: false,
},
},
options: [{ builtinGlobals: true }],
},
{
code: `
declare interface Foopy2 {
name: string;
}
`,
filename: 'baz.d.ts',
languageOptions: {
globals: {
Foopy2: false,
},
},
options: [{ builtinGlobals: true }],
},
{
code: `
declare type Foopy3 = {
x: number;
};
`,
filename: 'baz.d.ts',
languageOptions: {
globals: {
Foopy3: false,
},
},
options: [{ builtinGlobals: true }],
},
{
code: `
declare enum Foopy4 {
x,
}
`,
filename: 'baz.d.ts',
languageOptions: {
globals: {
Foopy4: false,
},
},
options: [{ builtinGlobals: true }],
},
{
code: `
declare namespace Foopy5 {}
`,
filename: 'baz.d.ts',
languageOptions: {
globals: {
Foopy5: false,
},
},
options: [{ builtinGlobals: true }],
},
{
code: `
declare;
foo5: boolean;
`,
filename: 'baz.d.ts',
languageOptions: {
globals: {
foo5: false,
},
},
options: [{ builtinGlobals: true }],
},
],
});
Loading

[8]ページ先頭

©2009-2025 Movatter.jp