Skip to content

Commit

Permalink
feat(core): add another rule for circular nx
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Apr 13, 2023
1 parent 5adfdba commit ba25de2
Show file tree
Hide file tree
Showing 18 changed files with 48 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {
"@nrwl/nx/workspace/restrict-js-plugin-deep-import": "error"
"@nrwl/nx/workspace/restrict-nx-imports": "error"
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { DependencyType } from '@nrwl/devkit';
import * as parser from '@typescript-eslint/parser';
import { TSESLint } from '@typescript-eslint/utils';
import { vol } from 'memfs';
// eslint-disable-next-line @nrwl/nx/workspace/restrict-nx-imports
import { TargetProjectLocator } from 'nx/src/plugins/js/project-graph/build-dependencies/target-project-locator';
import enforceModuleBoundaries, {
RULE_NAME as enforceModuleBoundariesRuleName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
isComboDepConstraint,
} from '../utils/runtime-lint-utils';
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
// eslint-disable-next-line @nrwl/nx/workspace/restrict-nx-imports
import { TargetProjectLocator } from 'nx/src/plugins/js/project-graph/build-dependencies/target-project-locator';
import { basename, dirname, relative } from 'path';
import {
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin-nx/src/utils/runtime-lint-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from '@nrwl/devkit';
import { getPath, pathExists } from './graph-utils';
import { readFileIfExisting } from 'nx/src/utils/fileutils';
// eslint-disable-next-line @nrwl/nx/workspace/restrict-nx-imports
import { TargetProjectLocator } from 'nx/src/plugins/js/project-graph/build-dependencies/target-project-locator';
import {
findProjectForPath,
Expand Down
4 changes: 2 additions & 2 deletions packages/js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export * from './utils/assets';
export * from './utils/package-json/update-package-json';
export { libraryGenerator } from './generators/library/library';
export { initGenerator } from './generators/init/init';
export { createLockFile } from 'nx/src/plugins/js/lock-file/lock-file';
export { createPackageJson } from 'nx/src/plugins/js/package-json/create-package-json';
export { createLockFile } from 'nx/src/plugins/js';
export { createPackageJson } from 'nx/src/plugins/js';
7 changes: 2 additions & 5 deletions packages/js/src/utils/package-json/update-package-json.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import {
createLockFile,
getLockFileName,
} from 'nx/src/plugins/js/lock-file/lock-file';
import { createPackageJson } from 'nx/src/plugins/js/package-json/create-package-json';
import { createLockFile, getLockFileName } from 'nx/src/plugins/js';
import { createPackageJson } from 'nx/src/plugins/js';
import {
ExecutorContext,
getOutputsForTargetAndConfiguration,
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/executors/build/build.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { createNextConfigFile } from './lib/create-next-config-file';
import { checkPublicDirectory } from './lib/check-project';
import { NextBuildBuilderOptions } from '../../utils/types';

import { getLockFileName } from 'nx/src/plugins/js/lock-file/lock-file';
import { getLockFileName } from 'nx/src/plugins/js';

export default async function buildExecutor(
options: NextBuildBuilderOptions,
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/devkit-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ export { Hash, Hasher } from './hasher/hasher';
*/
export { cacheDir } from './utils/cache-directory';

import { createLockFile as _createLockFile } from './plugins/js/lock-file/lock-file';
import { createPackageJson as _createPackageJson } from './plugins/js/package-json/create-package-json';
import { createLockFile as _createLockFile } from './plugins/js';
import { createPackageJson as _createPackageJson } from './plugins/js';

/**
* @category Package Manager
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
3 changes: 3 additions & 0 deletions packages/nx/src/plugins/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import { workspaceRoot } from '../../utils/workspace-root';
import { ensureDirSync } from 'fs-extra';
import { removeNpmNodes } from './lock-file/remove-npm-nodes';

export { getLockFileName, createLockFile } from './lock-file/lock-file';
export { createPackageJson } from './package-json/create-package-json';

export const processProjectGraph: ProjectGraphProcessor = async (
graph,
context
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
HelperDependency,
readTsConfig,
} from '@nrwl/js';
import { getLockFileName } from 'nx/src/plugins/js/lock-file/lock-file';
import { getLockFileName } from 'nx/src/plugins/js';

const pluginName = 'GeneratePackageJsonPlugin';

Expand Down
8 changes: 4 additions & 4 deletions tools/eslint-rules/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
RULE_NAME as restrictJsPluginDeepImportName,
rule as restrictJsPluginDeepImport,
} from './rules/restrict-js-plugin-deep-import';
RULE_NAME as restrictNxImportsName,
rule as restrictNxImports,
} from './rules/restrict-nx-imports';
import {
RULE_NAME as validSchemaDescriptionName,
rule as validSchemaDescription,
Expand Down Expand Up @@ -33,6 +33,6 @@ module.exports = {
*/
rules: {
[validSchemaDescriptionName]: validSchemaDescription,
[restrictJsPluginDeepImportName]: restrictJsPluginDeepImport,
[restrictNxImportsName]: restrictNxImports,
},
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TSESLint } from '@typescript-eslint/utils';
import { rule, RULE_NAME } from './restrict-js-plugin-deep-import';
import { rule, RULE_NAME } from './restrict-nx-imports';

const ruleTester = new TSESLint.RuleTester({
parser: require.resolve('@typescript-eslint/parser'),
Expand All @@ -20,6 +20,10 @@ ruleTester.run(RULE_NAME, rule, {
code: `import { createLockFile } from '../plugins/js';`,
filename: '/root/packages/nx/src/path/to.ts',
},
{
code: `import { updateJson } from '../../generators/utils/json';`,
filename: '/root/packages/nx/src/path/to.ts',
},
],
invalid: [
{
Expand All @@ -32,5 +36,10 @@ ruleTester.run(RULE_NAME, rule, {
code: `import { createLockFile } from '../plugins/js/lock-file/lock-file';`,
filename: '/root/packages/nx/src/path/to.ts',
},
{
errors: [{ messageId: 'noCircularNx' }],
code: `import { updateJson } from 'nx/src/devkit-exports';`,
filename: '/root/packages/nx/src/path/to.ts',
},
],
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import {
TSESTree,
} from '@typescript-eslint/utils';

// NOTE: The rule will be available in ESLint configs as "@nrwl/nx/workspace/restrict-js-plugin-deep-import"
export const RULE_NAME = 'restrict-js-plugin-deep-import';
// NOTE: The rule will be available in ESLint configs as "@nrwl/nx/workspace/restrict-nx-imports"
export const RULE_NAME = 'restrict-nx-imports';

export const rule = ESLintUtils.RuleCreator(() => __filename)({
name: RULE_NAME,
Expand All @@ -34,6 +34,8 @@ export const rule = ESLintUtils.RuleCreator(() => __filename)({
},
schema: [],
messages: {
noCircularNx:
'Functions within "nx" should be imported with relative path. Alias import found: {{imp}}',
noDeepImport:
'Functions from "nx/src/plugins/js" should be imported via barrel import. Deep import found: {{imp}}',
noDeepRelativeImport:
Expand Down Expand Up @@ -84,6 +86,15 @@ export const rule = ESLintUtils.RuleCreator(() => __filename)({
},
});
}
if (imp.includes('nx/src') && fileName.startsWith('packages/nx/')) {
context.report({
node,
messageId: 'noCircularNx',
data: {
imp,
},
});
}
}

return {
Expand Down

0 comments on commit ba25de2

Please sign in to comment.