-
-
Notifications
You must be signed in to change notification settings - Fork 385
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
base: main
Are you sure you want to change the base?
Enhance import-style
rule with auto-fix capability
#2528
Conversation
2162322
to
d332fe4
Compare
147ef24
to
31e6953
Compare
2779f35
to
f23a338
Compare
Can you try to reduce the diff? It's hard to see what's the change. |
I reverted a lot of changes (by using new commits), and now the diff looks much nicer. Let me know what you think! |
- 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.
…es has 'namespace' and does not have 'named'.
1f54943
to
6b1e679
Compare
// Skip rest patterns since they should be kept as is | ||
if (importedName === undefined) { | ||
continue; | ||
} |
There was a problem hiding this comment.
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}`); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 => { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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') { |
There was a problem hiding this comment.
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 import
s, leave require()
alone, ESM should be preferred.
There was a problem hiding this comment.
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.
import-style
rule to include anautoFix
option, allowing automatic fixing of import styles.autoFix
feature and its default behavior.This change improves usability by enabling automatic corrections for import style violations.
This fixes #963