Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Higangssh
Copy link

Fixes #11759

PR Checklist

Overview

This PR fixes an issue where the restrict-template-expressions rule incorrectly rejects derived class instances
in template literals when their base class is in the allow list.

Problem

Currently, when a base class is included in the allow option, derived classes extending it are still flagged as
errors, violating the Liskov Substitution Principle where subtypes should be substitutable for their supertypes.

Example:

// Configuration: { allow: [{ from: 'file', name: 'Base' }] }
class Base { }
class Derived extends Base { }
const bar = new Derived();
`${bar}`;  // Error: Invalid type "Derived"

Solution

Implemented recursive base type checking following the pattern established in the no-base-to-string rule:

  1. Added hasBaseTypes() helper function to identify types with base types (Interface or Class)
  2. Added isAllowedTypeOrBase() function that recursively checks if a type or its base types match the allow list
  3. Included cycle detection using a Set to prevent infinite recursion
  4. Updated recursivelyCheckType() to utilize the new base type checking mechanism

Changes

  • packages/eslint-plugin/src/rules/restrict-template-expressions.ts: Added base type checking logic
  • packages/eslint-plugin/tests/rules/restrict-template-expressions.test.ts: Added test case for inheritance
    scenario

Allow derived types in template expressions when base types are in allow list

Fixes typescript-eslint#11759
@typescript-eslint
Copy link
Contributor

Thanks for the PR, @Higangssh!

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.

@netlify
Copy link

netlify bot commented Nov 16, 2025

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 8eaddd5
🔍 Latest deploy log https://app.netlify.com/projects/typescript-eslint/deploys/691b17e51f644a00082941c7
😎 Deploy Preview https://deploy-preview-11764--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: 81 (🔴 down 15 from production)
Accessibility: 97 (no change from production)
Best Practices: 100 (no change from production)
SEO: 92 (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 project configuration.

@nx-cloud
Copy link

nx-cloud bot commented Nov 16, 2025

View your CI Pipeline Execution ↗ for commit 8eaddd5

Command Status Duration Result
nx run-many -t lint ✅ Succeeded 3m 13s View ↗
nx run-many -t typecheck ✅ Succeeded 2m 7s View ↗
nx run types:build ✅ Succeeded 5s View ↗
nx test eslint-plugin --coverage=false ✅ Succeeded 4s View ↗
nx test eslint-plugin-internal --coverage=false ✅ Succeeded 2s View ↗
nx test typescript-estree --coverage=false ✅ Succeeded 1s View ↗
nx run integration-tests:test ✅ Succeeded 3s View ↗
nx run generate-configs ✅ Succeeded 8s View ↗
Additional runs (29) ✅ Succeeded ... View ↗

☁️ Nx Cloud last updated this comment at 2025-11-17 12:47:40 UTC

@Higangssh Higangssh changed the title fix(restrict-template-expressions): check base types in allow list fix(eslint-plugin): [restrict-template-expressions] check base types in allow list Nov 16, 2025
@codecov
Copy link

codecov bot commented Nov 16, 2025

Codecov Report

❌ Patch coverage is 86.95652% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.56%. Comparing base (1d15881) to head (8eaddd5).

Files with missing lines Patch % Lines
...-plugin/src/rules/restrict-template-expressions.ts 86.95% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #11764      +/-   ##
==========================================
- Coverage   90.56%   90.56%   -0.01%     
==========================================
  Files         522      522              
  Lines       53063    53084      +21     
  Branches     8838     8844       +6     
==========================================
+ Hits        48058    48076      +18     
- Misses       4990     4993       +3     
  Partials       15       15              
Flag Coverage Δ
unittest 90.56% <86.95%> (-0.01%) ⬇️

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

Files with missing lines Coverage Δ
...-plugin/src/rules/restrict-template-expressions.ts 98.02% <86.95%> (-1.98%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: [restrict-template-expressions] should check if superclass is ignored

1 participant