Skip to content

Commit 1f54943

Browse files
committed
Create fix generator for converting imports to namespace imports
1 parent 9929da0 commit 1f54943

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

rules/import-style.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,14 @@ const getNamespaceIdentifier = moduleName => {
170170
};
171171

172172
/**
173-
Creates a fix function to convert imports to namespace imports.
173+
Creates a fix generator function to convert imports to namespace imports.
174174
@param {Object} options - The options object.
175175
@param {import('estree').Node} options.node - The AST node representing the import/require statement.
176176
@param {import('eslint').SourceCode} options.sourceCode - The ESLint source code object.
177177
@param {string} options.moduleName - The name of the module being imported.
178-
@returns {(fixer: import('eslint').Rule.RuleFixer) => Array<import('eslint').Rule.Fix>|void} A function that takes a fixer and returns an array of fixes or void.
178+
@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.
179179
*/
180-
const createFix = ({node, sourceCode, moduleName}) => fixer => {
180+
const createFix = ({node, sourceCode, moduleName}) => function * (fixer) {
181181
const isImportDeclaration = node.type === 'ImportDeclaration';
182182
const isVariableDeclarator = node.type === 'VariableDeclarator';
183183
const isRequireCall = isCallExpression(node.init, {name: 'require'});
@@ -237,7 +237,7 @@ const createFix = ({node, sourceCode, moduleName}) => fixer => {
237237
node.type === 'VariableDeclarator' ? node.parent : node,
238238
).endsWith(';');
239239

240-
const importFix = fixer.replaceTextRange(
240+
yield fixer.replaceTextRange(
241241
node.type === 'VariableDeclarator'
242242
? [node.parent.range[0], node.parent.range[1]]
243243
: [node.range[0], node.range[1]],
@@ -249,15 +249,14 @@ const createFix = ({node, sourceCode, moduleName}) => fixer => {
249249
);
250250

251251
if (hasMatchingNamedImport) {
252-
return [importFix];
252+
return;
253253
}
254254

255-
const referenceFixes = importedNames.flatMap(({localName, importedName}) => {
255+
for (const {localName, importedName} of importedNames) {
256256
// Skip rest patterns since they should be kept as is
257257
if (importedName === undefined) {
258-
return [];
258+
continue;
259259
}
260-
261260
const programScope = sourceCode.getScope(sourceCode.ast);
262261

263262
const getAllReferences = scope => {
@@ -278,11 +277,10 @@ const createFix = ({node, sourceCode, moduleName}) => fixer => {
278277

279278
const references = getAllReferences(programScope);
280279

281-
return references.map(reference => fixer.replaceText(reference.identifier, `${uniqueNamespaceIdentifier}.${importedName}`),
282-
);
283-
});
284-
285-
return [importFix, ...referenceFixes];
280+
for (const reference of references) {
281+
yield fixer.replaceText(reference.identifier, `${uniqueNamespaceIdentifier}.${importedName}`);
282+
}
283+
}
286284
};
287285

288286
/** @param {import('eslint').Rule.RuleContext} context */

0 commit comments

Comments
 (0)