@so1ve/eslint-plugin 3.20.2 → 3.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +36 -33
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -239,6 +239,32 @@ var no_import_promises_as_default = rule$7;
|
|
|
239
239
|
//#endregion
|
|
240
240
|
//#region src/rules/no-inline-type-import.ts
|
|
241
241
|
const RULE_NAME$5 = "no-inline-type-import";
|
|
242
|
+
function generateImportsText(specifiers) {
|
|
243
|
+
let text = "{ ";
|
|
244
|
+
const texts = [];
|
|
245
|
+
for (const s of specifiers) {
|
|
246
|
+
const importedName = s.imported.type === AST_NODE_TYPES.Identifier ? s.imported.name : s.imported.raw;
|
|
247
|
+
texts.push(importedName === s.local.name ? s.local.name : `${importedName} as ${s.local.name}`);
|
|
248
|
+
}
|
|
249
|
+
text += texts.join(", ");
|
|
250
|
+
text += " }";
|
|
251
|
+
return text;
|
|
252
|
+
}
|
|
253
|
+
function generateTypeImportText(typeSpecifiers) {
|
|
254
|
+
let text = "type ";
|
|
255
|
+
text += generateImportsText(typeSpecifiers);
|
|
256
|
+
return text;
|
|
257
|
+
}
|
|
258
|
+
function generateValueImportText(defaultImportSpecifier, valueSpecifiers) {
|
|
259
|
+
const hasValueImport = valueSpecifiers.length > 0;
|
|
260
|
+
let text = "";
|
|
261
|
+
if (defaultImportSpecifier) {
|
|
262
|
+
text += defaultImportSpecifier.local.name;
|
|
263
|
+
if (hasValueImport) text += ", ";
|
|
264
|
+
}
|
|
265
|
+
if (hasValueImport) text += generateImportsText(valueSpecifiers);
|
|
266
|
+
return text;
|
|
267
|
+
}
|
|
242
268
|
const rule$6 = createEslintRule({
|
|
243
269
|
name: RULE_NAME$5,
|
|
244
270
|
meta: {
|
|
@@ -254,42 +280,19 @@ const rule$6 = createEslintRule({
|
|
|
254
280
|
const typeSpecifiers = specifiers.filter((s) => s.type === AST_NODE_TYPES.ImportSpecifier && s.importKind === "type");
|
|
255
281
|
const valueSpecifiers = specifiers.filter((s) => s.type === AST_NODE_TYPES.ImportSpecifier && s.importKind === "value");
|
|
256
282
|
const defaultImportSpecifier = specifiers.find((s) => s.type === AST_NODE_TYPES.ImportDefaultSpecifier);
|
|
257
|
-
|
|
283
|
+
const hasDefaultImport = !!defaultImportSpecifier;
|
|
284
|
+
const hasTypeImport = typeSpecifiers.length > 0;
|
|
285
|
+
const hasValueImport = valueSpecifiers.length > 0;
|
|
286
|
+
if (!hasTypeImport) return;
|
|
287
|
+
const texts = [];
|
|
288
|
+
texts.push(generateTypeImportText(typeSpecifiers));
|
|
289
|
+
if (hasDefaultImport || hasValueImport) texts.push(generateValueImportText(defaultImportSpecifier, valueSpecifiers));
|
|
290
|
+
const textToReport = texts.map((text) => `import ${text} from "${node.source.value}";`).join("\n");
|
|
291
|
+
context.report({
|
|
258
292
|
node,
|
|
259
293
|
messageId: "noInlineTypeImport",
|
|
260
294
|
fix(fixer) {
|
|
261
|
-
|
|
262
|
-
if (s.type === AST_NODE_TYPES.ImportSpecifier) {
|
|
263
|
-
const importedName = s.imported.type === AST_NODE_TYPES.Identifier ? s.imported.name : s.imported.value;
|
|
264
|
-
return importedName === s.local.name ? s.local.name : `${importedName} as ${s.local.name}`;
|
|
265
|
-
}
|
|
266
|
-
return s.local.name;
|
|
267
|
-
}).join(", ");
|
|
268
|
-
const valueSpecifiersText = valueSpecifiers.map((s) => {
|
|
269
|
-
if (s.type === AST_NODE_TYPES.ImportSpecifier) {
|
|
270
|
-
const importedName = s.imported.type === AST_NODE_TYPES.Identifier ? s.imported.name : s.imported.value;
|
|
271
|
-
return importedName === s.local.name ? s.local.name : `${importedName} as ${s.local.name}`;
|
|
272
|
-
}
|
|
273
|
-
return s.local.name;
|
|
274
|
-
}).join(", ");
|
|
275
|
-
const defaultImportSpecifierText = defaultImportSpecifier?.local.name;
|
|
276
|
-
const defaultAndValueSpecifiersText = defaultImportSpecifier ? `import ${defaultImportSpecifierText}, { ${valueSpecifiersText} } from "${node.source.value}";` : `import { ${valueSpecifiersText} } from "${node.source.value}";`;
|
|
277
|
-
const texts = [`import type { ${typeSpecifiersText} } from "${node.source.value}";`, defaultAndValueSpecifiersText];
|
|
278
|
-
return fixer.replaceText(node, texts.join("\n"));
|
|
279
|
-
}
|
|
280
|
-
});
|
|
281
|
-
else context.report({
|
|
282
|
-
node,
|
|
283
|
-
messageId: "noInlineTypeImport",
|
|
284
|
-
fix(fixer) {
|
|
285
|
-
const typeSpecifiersText = typeSpecifiers.map((s) => {
|
|
286
|
-
if (s.type === AST_NODE_TYPES.ImportSpecifier) {
|
|
287
|
-
const importedName = s.imported.type === AST_NODE_TYPES.Identifier ? s.imported.name : s.imported.value;
|
|
288
|
-
return importedName === s.local.name ? s.local.name : `${importedName} as ${s.local.name}`;
|
|
289
|
-
}
|
|
290
|
-
return s.local.name;
|
|
291
|
-
}).join(", ");
|
|
292
|
-
return fixer.replaceText(node, `import type { ${typeSpecifiersText} } from "${node.source.value}";`);
|
|
295
|
+
return fixer.replaceText(node, textToReport);
|
|
293
296
|
}
|
|
294
297
|
});
|
|
295
298
|
} })
|