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

feat(eslint-plugin): [naming-convention] add support for default and namespace imports #7269

Merged

Conversation

binoche9
Copy link
Contributor

PR Checklist

Overview

Currently the naming-convention rule cannot be used to enforce conventions on default and namespace imports. This PR introduces that capability by introducing an import selector and default and namespace modifiers.

Named imports are not checked since they have to match the name from the imported file.

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @binoche9!

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 Jul 19, 2023

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit bc92533
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/64d19e5a6f988700073ad170
😎 Deploy Preview https://deploy-preview-7269--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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

@nx-cloud
Copy link

nx-cloud bot commented Jul 19, 2023

☁️ Nx Cloud Report

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

📂 See all runs for this branch


✅ Successfully ran 37 targets

Sent with 💌 from NxCloud.

Copy link
Contributor Author

@binoche9 binoche9 left a comment

Choose a reason for hiding this comment

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

Let me know if there's any other place I should be making changes.

I'm also not sure if this counts as a breaking change since for people who have a default style it may break for them since imports that previously weren't matched will start getting the default style enforced.

Comment on lines +47 to +48
// other
import = 1 << 18,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I decided it didn't belong to any selector group (I guess variableLike comes closest) but feel free to disagree

modifiers.add(Modifiers.namespace);
break;
case AST_NODE_TYPES.ImportSpecifier:
// Handle `import { default as Foo }`
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I decided import { default as Foo } counts as a default import, but feel free to disagree

@binoche9 binoche9 changed the title feat(eslint-plugin) [naming-convention] add support for default and namespace imports feat(eslint-plugin): [naming-convention] add support for default and namespace imports Jul 19, 2023
@bradzacher bradzacher added the enhancement: plugin rule option New rule option for an existing eslint-plugin rule label Jul 19, 2023
JoshuaKGoldberg
JoshuaKGoldberg previously approved these changes Aug 5, 2023
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.

Generally looks great to me, thanks for sending in! 🙌

Just requesting changes on splitting the tests up. It all strikes me as reasonable otherwise though.

cc @bradzacher / will defer to you since you'd interacted with the feature request in #2106.

@JoshuaKGoldberg JoshuaKGoldberg added the 1 approval PR that a maintainer has LGTM'd - any maintainer can merge this when ready label Aug 5, 2023
@codecov
Copy link

codecov bot commented Aug 8, 2023

Codecov Report

Merging #7269 (bc92533) into main (0a1de14) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7269      +/-   ##
==========================================
+ Coverage   87.41%   87.42%   +0.01%     
==========================================
  Files         381      381              
  Lines       13321    13332      +11     
  Branches     3938     3940       +2     
==========================================
+ Hits        11645    11656      +11     
  Misses       1298     1298              
  Partials      378      378              
Flag Coverage Δ
unittest 87.42% <100.00%> (+0.01%) ⬆️

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

Files Changed Coverage Δ
...-plugin/src/rules/naming-convention-utils/enums.ts 100.00% <ø> (ø)
...plugin/src/rules/naming-convention-utils/schema.ts 100.00% <ø> (ø)
...kages/eslint-plugin/src/rules/naming-convention.ts 90.68% <100.00%> (+0.53%) ⬆️

@binoche9
Copy link
Contributor Author

binoche9 commented Aug 8, 2023

I split up the tests as requested 👍

@binoche9
Copy link
Contributor Author

Friendly bump here!

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.

Sorry for the delay! I just re-reviewed and it looks great. Thanks again for sending!

@JoshuaKGoldberg JoshuaKGoldberg merged commit bb15aae into typescript-eslint:main Oct 17, 2023
50 checks passed
@binoche9 binoche9 deleted the import-naming-convention branch October 18, 2023 14:51
@karlhorky
Copy link

karlhorky commented Oct 24, 2023

Just upgraded to @​typescript-eslint/eslint-plugin@6.9.0 and received a number of new warnings, such as this one:

checkbox/index.js
Warning:   1:8  warning  Import name `React` must match one of the following formats: camelCase  @typescript-eslint/naming-convention

With this code:

import React from 'react';

This is using the default rule configuration mentioned in the docs:

// the default config is similar to ESLint's camelcase rule but more strict
const defaultOptions: Options = [
  {
    selector: 'default',
    format: ['camelCase'],
    leadingUnderscore: 'allow',
    trailingUnderscore: 'allow',
  },

  {
    selector: 'variable',
    format: ['camelCase', 'UPPER_CASE'],
    leadingUnderscore: 'allow',
    trailingUnderscore: 'allow',
  },

  {
    selector: 'typeLike',
    format: ['PascalCase'],
  },
];

I guess this is going to be a pretty common pattern that gets rejected now. 😬

Maybe the default configuration should accept every format as an import? (format: null)

Alternative: expect all users with default PascalCase imports (pretty common across the npm ecosystem, incl. React) to add the following to their config:

// the default config is similar to ESLint's camelcase rule but more strict
const defaultOptions: Options = [
  // ...

+  // Disable @typescript-eslint/naming-convention format for imports
+  // https://github.com/typescript-eslint/typescript-eslint/pull/7269#issuecomment-1777628591
+  {
+    selector: 'import',
+    format: null,
+  },
];

@JoshuaKGoldberg
Copy link
Member

Hm good point @binoche9. Could I ask you to file a new issue for this please, for tracking and attribution?

@mrazauskas

This comment has been minimized.

@karlhorky
Copy link

@typescript-eslint typescript-eslint locked and limited conversation to collaborators Oct 25, 2023
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 enhancement: plugin rule option New rule option for an existing eslint-plugin rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[naming-convention] Add the ability to lint imports
5 participants