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-var-requires, no-require-imports] support template literal #8408

Merged
merged 8 commits into from Mar 6, 2024

Conversation

yeonjuan
Copy link
Contributor

@yeonjuan yeonjuan commented Feb 8, 2024

PR Checklist

Overview

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @yeonjuan!

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 Feb 8, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 392b61d
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/65e85d9ed526e20008dcb2e0
😎 Deploy Preview https://deploy-preview-8408--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: 90 (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.

Copy link

codecov bot commented Feb 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.18%. Comparing base (3ac824b) to head (392b61d).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8408      +/-   ##
==========================================
+ Coverage   87.14%   87.18%   +0.04%     
==========================================
  Files         251      251              
  Lines       12272    12279       +7     
  Branches     3871     3873       +2     
==========================================
+ Hits        10694    10706      +12     
+ Misses       1306     1304       -2     
+ Partials      272      269       -3     
Flag Coverage Δ
unittest 87.18% <100.00%> (+0.04%) ⬆️

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

Files Coverage Δ
...ages/eslint-plugin/src/rules/no-require-imports.ts 100.00% <100.00%> (+5.88%) ⬆️
...ackages/eslint-plugin/src/rules/no-var-requires.ts 100.00% <100.00%> (+6.66%) ⬆️

... and 1 file with indirect coverage changes

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.

LGTM, thanks! 🙌

Note: there are other discussions around shared utilities around static things (#8323). Cross-linking there too.

@auvred @kirkwaiblinger you were active in those discussions too. I'd feel bad about not giving you a chance to look at this, if you have time. 😄

@JoshuaKGoldberg JoshuaKGoldberg added the 1 approval PR that a maintainer has LGTM'd - any maintainer can merge this when ready label Feb 23, 2024
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.

LGTM 🔥


Note: there are other discussions around shared utilities around static things (#8323). Cross-linking there too.

I think getStaticStringValue would be best friends with isStaticMemberAccessOfValue from #8323 😄

Comment on lines +59 to +60
if (node.arguments[0] && isStringOrTemplateLiteral(node.arguments[0])) {
const argValue = getStaticStringValue(node.arguments[0]);
Copy link
Member

Choose a reason for hiding this comment

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

[non-actionable] getStaticStringValue internally checks for node.type === AST_NODE_TYPES.Literal || node.type === AST_NODE_TYPES.TemplateLiteral, so calling isStringOrTemplateLiteral seems a bit redundant here.. Although it returns modified values for non-string literals, but in this particular case (with the require call) I think it is fine, since passing non-string values to the require call would result in a TS error.

I'm fine with the current implementation, just leaving my thoughts here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @auvred Thanks for sharing your thoughts.
you're right, I added that condition because I was concerned about cases where non-strings(regex, number,..) are used in the arguments of the require() (probably no one writes code like that).
I thought it would be nice if TypeScript Eslint didn't throw an error when non-string value is used and a TS error occurs.

Copy link
Member

Choose a reason for hiding this comment

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

didn't throw an error when (invalid thing)

Aha, yes, that's a good thing to look out for in general. This is a very good + timely thread! 😄 Thanks - #8549

@JoshuaKGoldberg JoshuaKGoldberg merged commit 5292399 into typescript-eslint:main Mar 6, 2024
57 of 59 checks passed
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
1 approval PR that a maintainer has LGTM'd - any maintainer can merge this when ready
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: [no-var-requires, no-require-imports] support template literal strings for allow
3 participants