Skip to content

fix(eslint-plugin): [no-shadow] don't report unnecessarily on valid ways of using module augmentation #10616

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

Conversation

ronami
Copy link
Member

@ronami ronami commented Jan 5, 2025

PR Checklist

Overview

This PR addresses #5681 and makes the rule not report on some valid ways to use module augmentation.

The rule already has logic that excludes declaration merging in module augmentation from being reported (added in #3959):

function isExternalDeclarationMerging(
scope: TSESLint.Scope.Scope,
variable: TSESLint.Scope.Variable,
shadowed: TSESLint.Scope.Variable,
): boolean {
const [firstDefinition] = shadowed.defs;
const [secondDefinition] = variable.defs;
return (
isTypeImport(firstDefinition) &&
isImportDeclaration(firstDefinition.parent) &&
isExternalModuleDeclarationWithName(
scope,
firstDefinition.parent.source.value,
) &&
secondDefinition.node.type === AST_NODE_TYPES.TSInterfaceDeclaration &&
secondDefinition.node.parent.type ===
AST_NODE_TYPES.ExportNamedDeclaration
);
}

The current logic specifically looks for interface declarations (and not type alias declarations) and requires types to be explicitly exported.

To my understanding, these two restrictions aren't relevant, as these are valid ways to use module augmentation. I'm basing this on the typescript's handbook and testing this locally (I don't think this can be reproduced on the playground, as TypeScript would show a Cannot find module error).

As far as I know, type aliases cannot be used for declaration merging (e.g. defined multiple times) but can be exported from an augmented module (and extend the original module with new type exports).

Testing this locally, it seems that TypeScript is OK with type and interfaces declarations not being explicitly exported, but I couldn't find anything official on this.

I'm far from having a thorough understanding of declaration merging, so I'd be happy to hear opinions on this.

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @ronami!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint.

Copy link

nx-cloud bot commented Jan 5, 2025

View your CI Pipeline Execution ↗ for commit c889be9.

Command Status Duration Result
nx test eslint-plugin ✅ Succeeded 5m 53s View ↗
nx test eslint-plugin --coverage=false ✅ Succeeded 7m 47s View ↗
nx test visitor-keys ✅ Succeeded <1s View ↗
nx run types:build ✅ Succeeded <1s View ↗
nx test utils ✅ Succeeded <1s View ↗
nx test type-utils ✅ Succeeded <1s View ↗
nx run-many --target=build --exclude website --... ✅ Succeeded 25s View ↗
nx test typescript-eslint ✅ Succeeded 4s View ↗
Additional runs (24) ✅ Succeeded ... View ↗

☁️ Nx Cloud last updated this comment at 2025-01-23 20:26:49 UTC

Copy link

netlify bot commented Jan 5, 2025

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit c889be9
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/6792a121cb9d390009b32c26
😎 Deploy Preview https://deploy-preview-10616--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 98 (no change from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 98 (no change from production)
PWA: 80 (no change from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

@ronami ronami marked this pull request as ready for review January 5, 2025 19:32
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was procrastinating reviewing this pretty intensely because of it touching declaration merging and shadowing... but it's actually really not that much code I think. 👍 from me.

Also ccing @bradzacher in case they have time to take a look too, as Brad's seen some things I tend to not recall.

@JoshuaKGoldberg JoshuaKGoldberg added the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Jan 19, 2025
bradzacher
bradzacher previously approved these changes Jan 22, 2025
@JoshuaKGoldberg
Copy link
Member

@ronami the merge conflicts are in no-shadow from your other PR #10593, could you resolve conflicts? I started trying but they look nontrivial.

@JoshuaKGoldberg JoshuaKGoldberg removed the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Jan 23, 2025
@ronami ronami dismissed stale reviews from bradzacher and JoshuaKGoldberg via a3e2979 January 23, 2025 18:44
@ronami
Copy link
Member Author

ronami commented Jan 23, 2025

@ronami the merge conflicts are in no-shadow from your other PR #10593, could you resolve conflicts? I started trying but they look nontrivial.

Oh, I didn't noticed this had conflicts. Sure, I've resolved them 👍

Copy link

codecov bot commented Jan 23, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.19%. Comparing base (9e8828b) to head (c889be9).
Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #10616   +/-   ##
=======================================
  Coverage   87.19%   87.19%           
=======================================
  Files         450      450           
  Lines       15632    15632           
  Branches     4570     4570           
=======================================
  Hits        13630    13630           
  Misses       1645     1645           
  Partials      357      357           
Flag Coverage Δ
unittest 87.19% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
packages/eslint-plugin/src/rules/no-shadow.ts 80.64% <ø> (ø)

@ronami
Copy link
Member Author

ronami commented Jan 23, 2025

I'm not sure why CI failed, the error I see is GitHub Actions has encountered an internal error when running your job.. Should I try re-running CI?

Tests seem to pass locally.

Edit: Green, thanks! 😄

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

@JoshuaKGoldberg JoshuaKGoldberg merged commit 586e7eb into typescript-eslint:main Jan 24, 2025
64 checks passed
@ronami ronami deleted the no-shadow-fix-module-augmentation branch January 25, 2025 09:52
renovate bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request Jan 29, 2025
| datasource | package                          | from   | to     |
| ---------- | -------------------------------- | ------ | ------ |
| npm        | @typescript-eslint/eslint-plugin | 8.21.0 | 8.22.0 |
| npm        | @typescript-eslint/parser        | 8.21.0 | 8.22.0 |


## [v8.22.0](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8220-2025-01-27)

##### 🩹 Fixes

-   **eslint-plugin:** \[no-unnecessary-template-expression] handle template literal type ([#10612](typescript-eslint/typescript-eslint#10612))
-   **eslint-plugin:** \[prefer-readonly] autofixer doesn't add type to property that is mutated in the constructor ([#10552](typescript-eslint/typescript-eslint#10552))
-   **eslint-plugin:** \[no-extraneous-class] handle accessor keyword ([#10678](typescript-eslint/typescript-eslint#10678))
-   **eslint-plugin:** \[no-shadow] don't report unnecessarily on valid ways of using module augmentation ([#10616](typescript-eslint/typescript-eslint#10616))
-   **eslint-plugin:** \[no-duplicate-type-constituents] handle nested types ([#10638](typescript-eslint/typescript-eslint#10638))
-   **eslint-plugin:** \[prefer-nullish-coalescing] doesn't report on ternary but on equivalent || ([#10517](typescript-eslint/typescript-eslint#10517))

##### ❤️ Thank You

-   mdm317
-   Olivier Zalmanski [@OlivierZal](https://github.com/OlivierZal)
-   Ronen Amiel
-   YeonJuan [@yeonjuan](https://github.com/yeonjuan)

You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.
renovate bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request Jan 29, 2025
| datasource | package                          | from   | to     |
| ---------- | -------------------------------- | ------ | ------ |
| npm        | @typescript-eslint/eslint-plugin | 8.21.0 | 8.22.0 |
| npm        | @typescript-eslint/parser        | 8.21.0 | 8.22.0 |


## [v8.22.0](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8220-2025-01-27)

##### 🩹 Fixes

-   **eslint-plugin:** \[no-unnecessary-template-expression] handle template literal type ([#10612](typescript-eslint/typescript-eslint#10612))
-   **eslint-plugin:** \[prefer-readonly] autofixer doesn't add type to property that is mutated in the constructor ([#10552](typescript-eslint/typescript-eslint#10552))
-   **eslint-plugin:** \[no-extraneous-class] handle accessor keyword ([#10678](typescript-eslint/typescript-eslint#10678))
-   **eslint-plugin:** \[no-shadow] don't report unnecessarily on valid ways of using module augmentation ([#10616](typescript-eslint/typescript-eslint#10616))
-   **eslint-plugin:** \[no-duplicate-type-constituents] handle nested types ([#10638](typescript-eslint/typescript-eslint#10638))
-   **eslint-plugin:** \[prefer-nullish-coalescing] doesn't report on ternary but on equivalent || ([#10517](typescript-eslint/typescript-eslint#10517))

##### ❤️ Thank You

-   mdm317
-   Olivier Zalmanski [@OlivierZal](https://github.com/OlivierZal)
-   Ronen Amiel
-   YeonJuan [@yeonjuan](https://github.com/yeonjuan)

You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 2, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: [no-shadow] false positive with augmented modules
3 participants