From b778d2820bc6be49bb55d9d7bc56d05a6567c1fb Mon Sep 17 00:00:00 2001 From: Miroslav Jonas Date: Thu, 20 Apr 2023 10:28:29 +0200 Subject: [PATCH] feat(linter): add internal rules to prevent deep nx imports --- .eslintrc.json | 13 ++++++++++++- .../src/rules/enforce-module-boundaries.spec.ts | 2 +- packages/eslint-plugin-nx/src/utils/ast-utils.ts | 2 +- packages/js/src/index.ts | 3 +++ packages/js/src/internal.ts | 3 +++ .../src/utils/package-json/update-package-json.ts | 2 ++ packages/js/src/utils/typescript/ast-utils.ts | 1 + packages/nx/.eslintrc.json | 14 +++++++++++++- .../update-depends-on-to-tokens.spec.ts | 4 +--- .../src/plugins/js/lock-file/pnpm-parser.spec.ts | 2 +- .../build-nodes/workspace-projects.spec.ts | 2 +- 11 files changed, 39 insertions(+), 9 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index c883c73986789d..b6d0b279831c4e 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -9,7 +9,18 @@ "extends": [], "rules": { "@typescript-eslint/explicit-module-boundary-types": "off", - "no-restricted-imports": ["error", "create-nx-workspace"] + "no-restricted-imports": ["error", "create-nx-workspace"], + "@typescript-eslint/no-restricted-imports": [ + "error", + { + "patterns": [ + { + "group": ["nx/src/plugins/js*"], + "message": "Imports from 'nx/src/plugins/js' are not allowed. Use '@nx/js' instead" + } + ] + } + ] }, "overrides": [ { diff --git a/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.spec.ts b/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.spec.ts index d9f306f21e1104..410262a103bb5a 100644 --- a/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.spec.ts +++ b/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.spec.ts @@ -9,7 +9,7 @@ import { DependencyType } from '@nx/devkit'; import * as parser from '@typescript-eslint/parser'; import { TSESLint } from '@typescript-eslint/utils'; import { vol } from 'memfs'; -import { TargetProjectLocator } from 'nx/src/plugins/js/project-graph/build-dependencies/target-project-locator'; +import { TargetProjectLocator } from '@nx/js/src/internal'; import enforceModuleBoundaries, { RULE_NAME as enforceModuleBoundariesRuleName, } from '../../src/rules/enforce-module-boundaries'; diff --git a/packages/eslint-plugin-nx/src/utils/ast-utils.ts b/packages/eslint-plugin-nx/src/utils/ast-utils.ts index 36f3d25ee439af..f3fe1d4d4e3ed4 100644 --- a/packages/eslint-plugin-nx/src/utils/ast-utils.ts +++ b/packages/eslint-plugin-nx/src/utils/ast-utils.ts @@ -3,7 +3,7 @@ import { ProjectGraphProjectNode, readJsonFile, } from '@nx/devkit'; -import { findNodes } from 'nx/src/plugins/js/utils/typescript'; +import { findNodes } from '@nx/js'; import { existsSync, readFileSync } from 'fs'; import { dirname } from 'path'; import ts = require('typescript'); diff --git a/packages/js/src/index.ts b/packages/js/src/index.ts index 7bf1f416c0b0f7..cc5f0be48577d7 100644 --- a/packages/js/src/index.ts +++ b/packages/js/src/index.ts @@ -12,11 +12,14 @@ export * from './utils/package-json/update-package-json'; export { libraryGenerator } from './generators/library/library'; export { initGenerator } from './generators/init/init'; +// eslint-disable-next-line @typescript-eslint/no-restricted-imports export { createLockFile, getLockFileName, } from 'nx/src/plugins/js/lock-file/lock-file'; +// eslint-disable-next-line @typescript-eslint/no-restricted-imports export { createPackageJson } from 'nx/src/plugins/js/package-json/create-package-json'; +// eslint-disable-next-line @typescript-eslint/no-restricted-imports export { findNodes, getRootTsConfigPath, diff --git a/packages/js/src/internal.ts b/packages/js/src/internal.ts index 215854c879a252..4bb9d5727dc89d 100644 --- a/packages/js/src/internal.ts +++ b/packages/js/src/internal.ts @@ -1,6 +1,9 @@ +// eslint-disable-next-line @typescript-eslint/no-restricted-imports export { resolveModuleByImport } from 'nx/src/plugins/js/utils/typescript'; +// eslint-disable-next-line @typescript-eslint/no-restricted-imports export { registerTsProject, registerTsConfigPaths, } from 'nx/src/plugins/js/utils/register'; +// eslint-disable-next-line @typescript-eslint/no-restricted-imports export { TargetProjectLocator } from 'nx/src/plugins/js/project-graph/build-dependencies/target-project-locator'; diff --git a/packages/js/src/utils/package-json/update-package-json.ts b/packages/js/src/utils/package-json/update-package-json.ts index 7727aaa6b67f84..9791e5ac5448a3 100644 --- a/packages/js/src/utils/package-json/update-package-json.ts +++ b/packages/js/src/utils/package-json/update-package-json.ts @@ -1,7 +1,9 @@ +// eslint-disable-next-line @typescript-eslint/no-restricted-imports import { createLockFile, getLockFileName, } from 'nx/src/plugins/js/lock-file/lock-file'; +// eslint-disable-next-line @typescript-eslint/no-restricted-imports import { createPackageJson } from 'nx/src/plugins/js/package-json/create-package-json'; import { ExecutorContext, diff --git a/packages/js/src/utils/typescript/ast-utils.ts b/packages/js/src/utils/typescript/ast-utils.ts index 7a1ae1204d101c..1f6a7285449f9a 100644 --- a/packages/js/src/utils/typescript/ast-utils.ts +++ b/packages/js/src/utils/typescript/ast-utils.ts @@ -2,6 +2,7 @@ import type { Tree } from '@nx/devkit'; import type * as ts from 'typescript'; // TODO(colum): replace when https://github.com/nrwl/nx/pull/15497 is merged import { getSourceNodes } from '@nx/workspace/src/utilities/typescript'; +// eslint-disable-next-line @typescript-eslint/no-restricted-imports import { findNodes } from 'nx/src/plugins/js/utils/typescript'; import { ensureTypescript } from './ensure-typescript'; diff --git a/packages/nx/.eslintrc.json b/packages/nx/.eslintrc.json index 2160aa7bb58211..c5241b67880dfc 100644 --- a/packages/nx/.eslintrc.json +++ b/packages/nx/.eslintrc.json @@ -5,7 +5,19 @@ "overrides": [ { "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], - "rules": {} + "rules": { + "@typescript-eslint/no-restricted-imports": [ + "error", + { + "patterns": [ + { + "group": ["nx/*"], + "message": "Circular import in 'nx' found. Use relative path." + } + ] + } + ] + } }, { "files": ["*.ts"], diff --git a/packages/nx/src/migrations/update-16-0-0/update-depends-on-to-tokens.spec.ts b/packages/nx/src/migrations/update-16-0-0/update-depends-on-to-tokens.spec.ts index 86c66a9dbd3c21..8f80e43c9496f1 100644 --- a/packages/nx/src/migrations/update-16-0-0/update-depends-on-to-tokens.spec.ts +++ b/packages/nx/src/migrations/update-16-0-0/update-depends-on-to-tokens.spec.ts @@ -1,13 +1,11 @@ import { addProjectConfiguration, - getProjects, readNxJson, readProjectConfiguration, } from '../../generators/utils/project-configuration'; -import { Tree } from '../../generators/tree'; import update from './update-depends-on-to-tokens'; -import { updateJson, writeJson } from 'nx/src/devkit-exports'; +import { updateJson, writeJson } from '../../generators/utils/json'; import { createTreeWithEmptyWorkspace } from '../../generators/testing-utils/create-tree-with-empty-workspace'; describe('update-depends-on-to-tokens', () => { diff --git a/packages/nx/src/plugins/js/lock-file/pnpm-parser.spec.ts b/packages/nx/src/plugins/js/lock-file/pnpm-parser.spec.ts index a2601fcbcbd865..258bd81de7af13 100644 --- a/packages/nx/src/plugins/js/lock-file/pnpm-parser.spec.ts +++ b/packages/nx/src/plugins/js/lock-file/pnpm-parser.spec.ts @@ -3,7 +3,7 @@ import { parsePnpmLockfile, stringifyPnpmLockfile } from './pnpm-parser'; import { ProjectGraph } from '../../../config/project-graph'; import { vol } from 'memfs'; import { pruneProjectGraph } from './project-graph-pruning'; -import { ProjectGraphBuilder } from 'nx/src/project-graph/project-graph-builder'; +import { ProjectGraphBuilder } from '../../../project-graph/project-graph-builder'; jest.mock('fs', () => require('memfs').fs); diff --git a/packages/nx/src/project-graph/build-nodes/workspace-projects.spec.ts b/packages/nx/src/project-graph/build-nodes/workspace-projects.spec.ts index 1e8a6d469b7722..e69b0a3a0d1418 100644 --- a/packages/nx/src/project-graph/build-nodes/workspace-projects.spec.ts +++ b/packages/nx/src/project-graph/build-nodes/workspace-projects.spec.ts @@ -1,4 +1,4 @@ -import { ProjectGraphProjectNode } from 'nx/src/config/project-graph'; +import { ProjectGraphProjectNode } from '../../config/project-graph'; import { normalizeImplicitDependencies } from './workspace-projects'; describe('workspace-projects', () => {