Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1515,7 +1515,7 @@ Violation detected in:
expect(failures.length).toEqual(1);
});

it('should error on importing an app', () => {
it('should error on importing an app (in tsconig paths)', () => {
const failures = runRule(
{},
`${process.cwd()}/proj/libs/mylib/src/main.ts`,
Expand Down Expand Up @@ -1560,6 +1560,62 @@ Violation detected in:
expect(failures[1].message).toEqual(message);
});

it('should error on importing an app (not in tsconig paths)', () => {
const failures = runRule(
{},
`${process.cwd()}/proj/libs/mylib/src/main.ts`,
`
import 'myapp';
import('myapp');
import('myapp/subpath');
import 'myapp/subpath';
`,
{
nodes: {
mylibName: {
name: 'mylibName',
type: 'lib',
data: {
root: 'libs/mylib',
tags: [],
implicitDependencies: [],
targets: {},
},
},
myappName: {
name: 'myappName',
type: 'app',
data: {
root: 'apps/myapp',
tags: [],
implicitDependencies: [],
targets: {},
metadata: {
js: {
packageName: 'myapp',
isInPackageManagerWorkspaces: true,
packageMain: 'dist/main.js',
},
},
},
},
},
dependencies: {},
},
{
mylibName: [createFile(`libs/mylib/src/main.ts`)],
myappName: [createFile(`apps/myapp/src/index.ts`)],
}
);

const message = 'Imports of apps are forbidden';
expect(failures.length).toEqual(4);
expect(failures[0].message).toEqual(message);
expect(failures[1].message).toEqual(message);
expect(failures[2].message).toEqual(message);
expect(failures[3].message).toEqual(message);
});

it('should error on importing an e2e project', () => {
const failures = runRule(
{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@ export default ESLintUtils.RuleCreator(
sourceFilePath,
imp
);

if (!targetProject) {
// non-project imports cannot use relative or absolute paths
if (isRelativePath(imp) || imp.startsWith('/')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,7 @@ describe('TargetProjectLocator', () => {
${{ './features/*.js': './dist/features/*.js' }} | ${'@org/pkg1/features/some-file.js'}
${{ import: './dist/index.js', default: './dist/index.js' }} | ${'@org/pkg1'}
`(
'should find "$importPath" as "pkg1" project when exports="$exports"',
'should find lib "$importPath" as "pkg1" project when exports="$exports"',
({ exports, importPath }) => {
let projects: Record<string, ProjectGraphProjectNode> = {
pkg1: {
Expand Down Expand Up @@ -1276,6 +1276,42 @@ describe('TargetProjectLocator', () => {
}
);

it.each`
exports | importPath
${'dist/index.js'} | ${'app1'}
${{ '.': 'dist/index.js' }} | ${'app1/subpath'}
`(
'should find app "$importPath" as "app1" project when exports="$exports"',
({ exports, importPath }) => {
let projects: Record<string, ProjectGraphProjectNode> = {
app1: {
name: 'app1',
type: 'app' as const,
data: {
root: 'app1',
metadata: {
js: {
packageName: 'app1',
packageExports: exports,
isInPackageManagerWorkspaces: true,
},
},
},
},
};

const targetProjectLocator = new TargetProjectLocator(
projects,
{},
new Map()
);
const result =
targetProjectLocator.findImportInWorkspaceProjects(importPath);

expect(result).toEqual('app1');
}
);

it.each`
exports | importPath
${'dist/index.js'} | ${'@org/pkg1'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,13 @@ export class TargetProjectLocator {

findImportInWorkspaceProjects(importPath: string): string | null {
this.packagesMetadata ??= getWorkspacePackagesMetadata(this.nodes);

if (this.packagesMetadata.entryPointsToProjectMap[importPath]) {
return this.packagesMetadata.entryPointsToProjectMap[importPath].name;
const slashIndex = importPath.indexOf('/');
const impPath =
importPath[0] !== '@' && slashIndex > 0
? importPath.slice(0, slashIndex)
: importPath;
if (this.packagesMetadata.entryPointsToProjectMap[impPath]) {
return this.packagesMetadata.entryPointsToProjectMap[impPath].name;
}

const project = matchImportToWildcardEntryPointsToProjectMap(
Expand Down