Skip to content

Commit 3478b86

Browse files
committed
Lint rule
1 parent dddb883 commit 3478b86

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

eslint-rules/no-barrel-imports.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ const moduleCache = new Map();
1111
const owningPackageCache = new Map();
1212
const wildcardExportsCache = new Map();
1313

14+
// Barrels we deliberately don't rewrite imports from. `@glimmer/component`
15+
// has its own legacy-resolution tsconfig that can't follow deep paths;
16+
// `@ember/version` is a one-line shim that re-exports from `ember/version`
17+
// (a different package), and we want to keep `@ember/version` as the canonical
18+
// import path for the framework version.
19+
const EXCLUDED_BARRELS = new Set(['@glimmer/component', '@ember/version']);
20+
1421
const idOrStr = (n) => (n.type === 'Identifier' ? n.name : n.value);
1522

1623
function resolveBarrelPath(specifier) {
@@ -156,9 +163,14 @@ const SCOPE_PREFIXES = [
156163
path.join(PACKAGES_ROOT, '@ember') + path.sep,
157164
path.join(PACKAGES_ROOT, '@glimmer') + path.sep,
158165
];
166+
const EXCLUDED_FILE_PREFIXES = [
167+
path.join(PACKAGES_ROOT, '@glimmer/component') + path.sep,
168+
];
159169
const TEST_DIR_RE = /[\\/](?:test|tests)[\\/]/;
160170

161171
const isInScope = (filename) => SCOPE_PREFIXES.some((p) => filename.startsWith(p));
172+
const isExcludedFile = (filename) =>
173+
EXCLUDED_FILE_PREFIXES.some((p) => filename.startsWith(p));
162174
const isInTestFile = (filename) =>
163175
TEST_DIR_RE.test(filename) || filename.includes(`${path.sep}internal-test-helpers${path.sep}`);
164176

@@ -299,7 +311,9 @@ module.exports = {
299311
},
300312
create(context) {
301313
const { filename } = context;
302-
if (!filename || !isInScope(filename) || isInTestFile(filename)) return {};
314+
if (!filename || !isInScope(filename) || isInTestFile(filename) || isExcludedFile(filename)) {
315+
return {};
316+
}
303317

304318
function resolveSpecifier(moduleExports, importedName, barrelPath, originalSpec) {
305319
const entry = moduleExports.get(importedName);
@@ -360,6 +374,7 @@ module.exports = {
360374
function check(node) {
361375
const spec = node.source?.value;
362376
if (typeof spec !== 'string') return;
377+
if (EXCLUDED_BARRELS.has(spec)) return;
363378
const barrelPath = resolveBarrelPath(spec);
364379
if (!barrelPath || filename === barrelPath) return;
365380

0 commit comments

Comments
 (0)