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

Enhance import-style rule with auto-fix capability #2528

Open
wants to merge 30 commits into
base: main
Choose a base branch
from

Conversation

CasperEngl
Copy link

  • Updated the import-style rule to include an autoFix option, allowing automatic fixing of import styles.
  • Modified the rule's documentation to reflect the new autoFix feature and its default behavior.
  • Added test cases to verify the functionality of the new auto-fix feature for various import scenarios.

This change improves usability by enabling automatic corrections for import style violations.

This fixes #963

@CasperEngl CasperEngl force-pushed the fix-import-style-autofix branch from 2162322 to d332fe4 Compare January 17, 2025 15:07
rules/import-style.js Outdated Show resolved Hide resolved
rules/import-style.js Outdated Show resolved Hide resolved
rules/import-style.js Outdated Show resolved Hide resolved
rules/import-style.js Outdated Show resolved Hide resolved
rules/import-style.js Outdated Show resolved Hide resolved
rules/import-style.js Outdated Show resolved Hide resolved
rules/import-style.js Outdated Show resolved Hide resolved
@CasperEngl CasperEngl force-pushed the fix-import-style-autofix branch from 2779f35 to f23a338 Compare January 22, 2025 01:47
@fisker
Copy link
Collaborator

fisker commented Jan 22, 2025

Can you try to reduce the diff? It's hard to see what's the change.

@CasperEngl
Copy link
Author

I reverted a lot of changes (by using new commits), and now the diff looks much nicer. Let me know what you think!

rules/import-style.js Outdated Show resolved Hide resolved
rules/import-style.js Outdated Show resolved Hide resolved
rules/import-style.js Outdated Show resolved Hide resolved
rules/import-style.js Outdated Show resolved Hide resolved
- Updated the `import-style` rule to include an `autoFix` option, allowing automatic fixing of import styles.
- Modified the rule's documentation to reflect the new `autoFix` feature and its default behavior.
- Added test cases to verify the functionality of the new auto-fix feature for various import scenarios.

This change improves usability by enabling automatic corrections for import style violations.
…mentation

- Removed the `autoFix` option from the `import-style` rule, simplifying its configuration.
- Updated the documentation to reflect the removal of the `autoFix` feature.
- Adjusted test cases to align with the new rule behavior, ensuring accurate error reporting without automatic fixes.

This change streamlines the rule's functionality and clarifies its usage for developers.
… coverage

- Refactored the `import-style` rule to better handle namespace imports, allowing for more flexible import declarations and `require` statements.
- Introduced special cases for common libraries to automatically determine namespace identifiers.
- Expanded test cases to cover a wide range of scenarios for namespace imports, ensuring accurate error reporting and compliance with the updated rule behavior.

This update enhances the usability of the `import-style` rule by providing clearer guidelines for namespace imports and improving the overall robustness of the linting process.
@CasperEngl CasperEngl force-pushed the fix-import-style-autofix branch from 1f54943 to 6b1e679 Compare January 23, 2025 11:45
// Skip rest patterns since they should be kept as is
if (importedName === undefined) {
continue;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please help me understand this. Since you turn the VariableDeclarator into an identifier, so the RestElement just removed?

const references = getAllReferences(programScope);

for (const reference of references) {
yield fixer.replaceText(reference.identifier, `${uniqueNamespaceIdentifier}.${importedName}`);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is not the current fix.

import {bar} from 'foo'

a = {bar}

will fix to

import foo from 'foo'

a = {foo.bar} // Syntax error

Copy link
Collaborator

Choose a reason for hiding this comment

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

You can use the existing renameVariable utility.


const programScope = sourceCode.getScope(sourceCode.ast);

const getAllReferences = scope => {
Copy link
Collaborator

@fisker fisker Jan 24, 2025

Choose a reason for hiding this comment

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

This part should use sourceCode.getDeclaredVariables() to get variable from the ImportDeclaration and then we have all the references. Including child scopes.

Copy link
Collaborator

Choose a reason for hiding this comment

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

consistent-assert has similar logic, you can take a look. https://github.com/sindresorhus/eslint-plugin-unicorn/pull/2535/files

localName: specifier.local.name,
importedName: specifier.type === 'ImportDefaultSpecifier' ? 'default' : specifier.imported.name,
}));
} else if (node.type === 'VariableDeclarator' && node.id.type === 'ObjectPattern') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can focus on imports, leave require() alone, ESM should be preferred.

Copy link
Owner

Choose a reason for hiding this comment

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

Yes, we no longer plan to handle anything CommonJS.

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.

Auto-fix for import-style?
3 participants