Skip to content

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

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

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
4a9bc16
Enhance `import-style` rule with auto-fix capability
CasperEngl Jan 17, 2025
98598d5
Refactor `import-style` rule: Remove `autoFix` option and update docu…
CasperEngl Jan 17, 2025
2fc70ec
Enhance `import-style` rule with improved namespace handling and test…
CasperEngl Jan 17, 2025
0abcc93
Fix references to the namespace imports
CasperEngl Jan 20, 2025
49b7cd7
Rename test/import-style.js to test/import-style.mjs and add special …
CasperEngl Jan 20, 2025
e4a832b
Refactor import-style.js
CasperEngl Jan 20, 2025
e74db1f
Refactor import styles configuration and handling
CasperEngl Jan 20, 2025
9536269
Refactor import and dynamic import rules
CasperEngl Jan 20, 2025
c632b34
Move create method back into root scope
CasperEngl Jan 22, 2025
53b9d13
Refactor specialCases object in import-style.js
CasperEngl Jan 22, 2025
9ef18bd
Fix node property keys
CasperEngl Jan 22, 2025
c9e24ee
Add tests for scoped packages, relative imports with hashes and exten…
CasperEngl Jan 22, 2025
8c5fdc5
improve namespace identifier generation for scoped packages
CasperEngl Jan 22, 2025
7588a95
move getNamespaceIdentifier function to module scope
CasperEngl Jan 22, 2025
36f1a03
rename variables to be more descriptive in import specifiers handling
CasperEngl Jan 22, 2025
6c152fa
fix code formatting and spacing in import-style.js
CasperEngl Jan 22, 2025
3374c91
optimize regex
CasperEngl Jan 22, 2025
db3699d
Refactor import-style.js and add test cases for numeric module names
CasperEngl Jan 23, 2025
a2239f3
Add createFix function to handle namespace imports in import-style.js
CasperEngl Jan 23, 2025
51c0ddf
Revert to use context.on handlers
CasperEngl Jan 23, 2025
82d41e3
Move schema to module scope
CasperEngl Jan 23, 2025
1268fd8
Use correct type
CasperEngl Jan 23, 2025
f2af0eb
Remove unnecessary imports for moment and date-fns libraries
CasperEngl Jan 23, 2025
87a1ad7
Trim trailing slashes in import paths
CasperEngl Jan 23, 2025
a8fd20e
Refactor getNamespaceIdentifier to use lastPart instead of packageName
CasperEngl Jan 23, 2025
c7b350c
Refactor createFix function to only create a fix if allowedImportStyl…
CasperEngl Jan 23, 2025
6b1e679
Create fix generator for converting imports to namespace imports
CasperEngl Jan 23, 2025
cfe9cc8
Refactor avoidCapture to use getAvailableVariableName
CasperEngl Jan 23, 2025
43ff202
Add blank line
CasperEngl Jan 23, 2025
3dba112
Refactor
fisker Jan 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor avoidCapture to use getAvailableVariableName
  • Loading branch information
CasperEngl committed Jan 23, 2025
commit cfe9cc84902be157520fc63582fa917674c72102
4 changes: 2 additions & 2 deletions rules/import-style.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {getStringIfConstant} from '@eslint-community/eslint-utils';
import {isCallExpression} from './ast/index.js';
import avoidCapture from './utils/avoid-capture.js';
import getAvailableVariableName from './utils/get-available-variable-name.js';
import {defaultsDeep} from './utils/lodash.js';

const MESSAGE_ID = 'importStyle';
Expand Down Expand Up @@ -177,7 +177,7 @@
@param {string} options.moduleName - The name of the module being imported.
@returns {(fixer: import('eslint').Rule.RuleFixer) => Generator<import('eslint').Rule.Fix, void, undefined>} A function that takes a fixer and returns a generator of fixes.
*/
const createFix = ({node, sourceCode, moduleName}) => function * (fixer) {

Check warning on line 180 in rules/import-style.js

View workflow job for this annotation

GitHub Actions / lint-test (ubuntu-latest)

Generator function has a complexity of 21. Maximum allowed is 20
const isImportDeclaration = node.type === 'ImportDeclaration';
const isVariableDeclarator = node.type === 'VariableDeclarator';
const isRequireCall = isCallExpression(node.init, {name: 'require'});
Expand Down Expand Up @@ -230,7 +230,7 @@
// Only avoid capture if there's no matching named import
const uniqueNamespaceIdentifier = hasMatchingNamedImport
? namespaceIdentifier
: avoidCapture(namespaceIdentifier, [scope]);
: getAvailableVariableName(namespaceIdentifier, [scope]);

// For VariableDeclarator, we need to handle the parent VariableDeclaration
const hasSemicolon = sourceCode.getText(
Expand All @@ -257,7 +257,7 @@
if (importedName === undefined) {
continue;
}
const programScope = sourceCode.getScope(sourceCode.ast);

Check failure on line 260 in rules/import-style.js

View workflow job for this annotation

GitHub Actions / lint-test (ubuntu-latest)

Expected blank line before this statement

const getAllReferences = scope => {
let references = scope.references.filter(
Expand Down Expand Up @@ -315,7 +315,7 @@
actualImportStyles,
allowedImportStyles,
isRequire = false,
) => {

Check warning on line 318 in rules/import-style.js

View workflow job for this annotation

GitHub Actions / lint-test (ubuntu-latest)

Arrow function has too many parameters (5). Maximum allowed is 4
if (!allowedImportStyles || allowedImportStyles.size === 0) {
return;
}
Expand Down
Loading