From 377454416acaa3c03dd0fe02b577737b4325c1f9 Mon Sep 17 00:00:00 2001 From: Alessio Gravili Date: Sun, 2 Mar 2025 12:51:49 -0700 Subject: [PATCH] chore(eslint): speed up no-imports-from-self rule by ensuring cache is used (#11483) Our `no-imports-from-self` eslint rule was supposed to cache the package.json name to ensure it doesn't try to find and read the package.json for every single import statement. Turns out that cache was never used. Credits to @etrepum for [finding this issue](https://github.com/facebook/lexical/pull/7272#discussion_r1976666227) --- .../eslint-plugin/customRules/no-imports-from-self.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/eslint-plugin/customRules/no-imports-from-self.js b/packages/eslint-plugin/customRules/no-imports-from-self.js index da6a9b511..77b92e283 100644 --- a/packages/eslint-plugin/customRules/no-imports-from-self.js +++ b/packages/eslint-plugin/customRules/no-imports-from-self.js @@ -18,11 +18,11 @@ export const rule = { return { ImportDeclaration(node) { const importPath = node.source.value - const pkgName = getPackageName(context, packageName) - if (pkgName && importPath.startsWith(pkgName)) { + packageName = getPackageName(context, packageName) + if (packageName && importPath.startsWith(packageName)) { context.report({ node, - message: `Package "${pkgName}" should not import from itself. Use relative instead.`, + message: `Package "${packageName}" should not import from itself. Use relative instead.`, }) } }, @@ -41,8 +41,7 @@ function getPackageName(context, packageName) { return packageName } - const fileName = context.getFilename() - const pkg = findNearestPackageJson(path.dirname(fileName)) + const pkg = findNearestPackageJson(path.dirname(context.filename)) if (pkg) { return pkg.name }