Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(linter): add internal rules to prevent deep nx imports #16430

Merged
merged 1 commit into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 12 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-nx/src/utils/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
3 changes: 3 additions & 0 deletions packages/js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions packages/js/src/internal.ts
Original file line number Diff line number Diff line change
@@ -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';
2 changes: 2 additions & 0 deletions packages/js/src/utils/package-json/update-package-json.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
1 change: 1 addition & 0 deletions packages/js/src/utils/typescript/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
6 changes: 6 additions & 0 deletions packages/nx/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
"message": "TypeScript is an optional dependency for Nx. If you need to use it, make sure its installed first with ensureTypescript.",
"allowTypeImports": true
}
],
"patterns": [
{
"group": ["nx/*"],
"message": "Circular import in 'nx' found. Use relative path."
}
]
}
]
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/adapter/ngcli-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import {
ExecutorsJson,
TaskGraphExecutor,
} from '../config/misc-interfaces';
import { readPluginPackageJson } from 'nx/src/utils/nx-plugin';
import { readPluginPackageJson } from '../utils/nx-plugin';

class WrappedWorkspaceNodeModulesArchitectHost extends WorkspaceNodeModulesArchitectHost {
private workspaces = new Workspaces(this.root);
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/generators/utils/project-configuration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { basename, dirname, join, relative } from 'path';
import { basename, join, relative } from 'path';
import {
ProjectConfiguration,
ProjectsConfigurations,
Expand All @@ -18,7 +18,7 @@ import { readJson, writeJson } from './json';
import { PackageJson } from '../../utils/package-json';
import { readNxJson } from './nx-json';
import { output } from '../../utils/output';
import { getNxRequirePaths } from 'nx/src/utils/installation-directory';
import { getNxRequirePaths } from '../../utils/installation-directory';

export { readNxJson, updateNxJson } from './nx-json';
export {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { dirname } from 'path';
import { readJson, writeJson } from '../../generators/utils/json';
import { formatChangedFilesWithPrettierIfAvailable } from '../../generators/internal-utils/format-changed-files-with-prettier-if-available';
import { getNxRequirePaths } from 'nx/src/utils/installation-directory';
import { getNxRequirePaths } from '../../utils/installation-directory';

export default async function (tree: Tree) {
const nxJson = readNxJson(tree);
Expand Down
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/plugins/js/lock-file/pnpm-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down