Skip to content

fix(eslint-plugin): stop warning on @ts-nocheck comments which aren't at the beginning of the file #10046

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

Merged

Conversation

ronami
Copy link
Member

@ronami ronami commented Sep 23, 2024

PR Checklist

Overview

This PR resolves #8753 and stops warning on // @ts-nocheck comments which are not at the beginning of the file:

const a = 1;

// @ts-nocheck - should not be reported

// TS error is not actually suppressed
const b: string = a;

@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

netlify bot commented Sep 23, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit d07c3ee
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/66f973306a897b00089df637
😎 Deploy Preview https://deploy-preview-10046--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: 97 (🔴 down 2 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 90 (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.

Copy link

nx-cloud bot commented Sep 23, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit d07c3ee. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 2 targets

Sent with 💌 from NxCloud.

Copy link

codecov bot commented Sep 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.02%. Comparing base (b88ea33) to head (d07c3ee).
Report is 22 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #10046      +/-   ##
==========================================
- Coverage   88.73%   86.02%   -2.72%     
==========================================
  Files         426      427       +1     
  Lines       14881    14896      +15     
  Branches     4327     4331       +4     
==========================================
- Hits        13205    12814     -391     
- Misses       1534     1736     +202     
- Partials      142      346     +204     
Flag Coverage Δ
unittest 86.02% <100.00%> (-2.72%) ⬇️

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/ban-ts-comment.ts 100.00% <100.00%> (ø)

... and 110 files with indirect coverage changes

@ronami ronami marked this pull request as ready for review September 23, 2024 20:11
if (
directive === 'nocheck' &&
firstStatement &&
isPositionEarlierThan(firstStatement.loc.start, comment.loc.start)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
isPositionEarlierThan(firstStatement.loc.start, comment.loc.start)
firstStatement.loc.start.line <= comment.loc.start.line

[Bug] If // @ts-nocheck comment is placed on the line before actual code, but its loc.start.column is greater than firstStatement.loc.start.column, the nocheck directive is not reported, even though this is a valid TS pragma and it effectively suppresses all TS errors.

 /// @ts-nocheck
const a: true = false

playground link

I don't think we need to compare loc columns here. Since singleline comments should be placed either on their own line or at the end of a line.
And if // @ts-nocheck is placed at the end of a line, it's no longer a valid TS pragma.

Copy link
Member Author

@ronami ronami Sep 25, 2024

Choose a reason for hiding this comment

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

Oh oops. 🙈

Yeah, I understand that checking columns alone is a bug, my intention was not to report the following:

/* @ts-nocheck - should not be reported */ const a = 1;

The function I was going to write was something like this (I also missed adding a test for such a case or checking it actually fails at all):

function isPositionEarlierThan(
  a: TSESTree.Position,
  b: TSESTree.Position,
): boolean {
  if (a.line > b.line) {
    return false;
  }
  
  return a.line === b.line && a.column < b.column;
}

But I can see block comments with ts-nocheck don't disable type-checking and the lint rule covers this already.

I've changed my code accordingly, thanks 🙏

Copy link
Member

Choose a reason for hiding this comment

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

I think it's worth including my example in the test suite as well. That way we can prevent regressions in the future.

@auvred auvred added the awaiting response Issues waiting for a reply from the OP or another party label Sep 25, 2024
@@ -1127,6 +1119,22 @@ if (false) {
},
],
},
{
// comment's column > first statement's column
// eslint-disable-next-line @typescript-eslint/internal/plugin-test-formatting
Copy link
Member Author

Choose a reason for hiding this comment

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

I used an eslint-disable comment over noFormat() since it doesn't handle disabling indentation checks.

Copy link
Member

@auvred auvred left a comment

Choose a reason for hiding this comment

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

💯

@auvred auvred added 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge and removed awaiting response Issues waiting for a reply from the OP or another party labels Oct 3, 2024
@JoshuaKGoldberg JoshuaKGoldberg merged commit 2d6ee87 into typescript-eslint:main Oct 7, 2024
65 of 66 checks passed
@ronami ronami deleted the ban-ts-comment-false-positive branch October 7, 2024 17:08
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: [ban-ts-comment] false positive when @ts-nocheck is not at the beginning of the file
3 participants