in eslint-rules/enforce-extensions.js [125:168]
ImportDeclaration(node) {
const source = node.source.value;
// Check if it's a relative import or matches a manually specified include path
const isRelativeImport = source.startsWith("./") || source.startsWith("../");
const isAliasedPath = Object.keys(aliases).some(alias => source === alias || source.startsWith(`${alias}/`));
const isIncludedPath = includePaths.some(pattern => source.startsWith(pattern));
// Skip if it's not a relative import, aliased path, or included path
if (!isRelativeImport && !isAliasedPath && !isIncludedPath) {
return;
}
// Skip ignored paths
if (ignorePaths.some(path => source.includes(path))) {
return;
}
// Check if the import already has an extension
const hasExtension = path.extname(source) !== "";
if (!hasExtension) {
// Get current file path to resolve the import
const currentFilePath = context.getFilename();
// Try to determine the correct file by checking what exists
const resolvedPath = resolveImportPath(source, currentFilePath);
const fileInfo = findActualFile(resolvedPath);
context.report({
node,
messageId: fileInfo ? "missingExtension" : "noFileFound",
fix(fixer) {
// Only provide a fix if we found a file
if (fileInfo) {
// Replace the string literal with one that includes the extension
return fixer.replaceText(node.source, `"${source}${fileInfo.importExt}"`);
}
// Otherwise, don't try to fix
return null;
},
});
}
},