Skip to content

Commit

Permalink
cleanup(core): move runtime-lint-utils to eslint plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Nov 17, 2022
1 parent 4860281 commit 8856865
Show file tree
Hide file tree
Showing 41 changed files with 171 additions and 120 deletions.
39 changes: 39 additions & 0 deletions docs/generated/devkit/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ It only uses language primitives and immutable objects
### Utils Type aliases

- [AdditionalSharedConfig](../../devkit/index#additionalsharedconfig)
- [MappedProjectGraph](../../devkit/index#mappedprojectgraph)
- [ModuleFederationLibrary](../../devkit/index#modulefederationlibrary)
- [Remotes](../../devkit/index#remotes)
- [SharedFunction](../../devkit/index#sharedfunction)
Expand Down Expand Up @@ -161,6 +162,7 @@ It only uses language primitives and immutable objects
- [installPackagesTask](../../devkit/index#installpackagestask)
- [isStandaloneProject](../../devkit/index#isstandaloneproject)
- [joinPathFragments](../../devkit/index#joinpathfragments)
- [mapProjectGraphFiles](../../devkit/index#mapprojectgraphfiles)
- [mapRemotes](../../devkit/index#mapremotes)
- [mapRemotesForSSR](../../devkit/index#mapremotesforssr)
- [moveFilesToNewDirectory](../../devkit/index#movefilestonewdirectory)
Expand Down Expand Up @@ -558,6 +560,18 @@ A plugin for Nx

---

### MappedProjectGraph

Ƭ **MappedProjectGraph**<`T`\>: [`ProjectGraph`](../../devkit/index#projectgraph)<`T`\> & { `allFiles`: `Record`<`string`, `string`\> }

#### Type parameters

| Name | Type |
| :--- | :---- |
| `T` | `any` |

---

### ModuleFederationLibrary

Ƭ **ModuleFederationLibrary**: `Object`
Expand Down Expand Up @@ -1475,6 +1489,31 @@ Normalized path fragments and joins them

---

### mapProjectGraphFiles

**mapProjectGraphFiles**<`T`\>(`projectGraph`): [`MappedProjectGraph`](../../devkit/index#mappedprojectgraph) \| `null`

Maps the project graph to a format that makes it easier to find the project
based on the file path.

#### Type parameters

| Name |
| :--- |
| `T` |

#### Parameters

| Name | Type |
| :------------- | :------------------------------------------------------ |
| `projectGraph` | [`ProjectGraph`](../../devkit/index#projectgraph)<`T`\> |

#### Returns

[`MappedProjectGraph`](../../devkit/index#mappedprojectgraph) \| `null`

---

### mapRemotes

**mapRemotes**(`remotes`, `remoteEntryExt`, `determineRemoteUrl`): `Record`<`string`, `string`\>
Expand Down
2 changes: 1 addition & 1 deletion docs/generated/packages/devkit.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/angular/plugins/component-testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ExecutorContext,
joinPathFragments,
logger,
mapProjectGraphFiles,
offsetFromRoot,
parseTargetString,
ProjectConfiguration,
Expand All @@ -21,7 +22,6 @@ import {
stripIndents,
workspaceRoot,
} from '@nrwl/devkit';
import { mapProjectGraphFiles } from '@nrwl/workspace/src/utils/runtime-lint-utils';
import { lstatSync, mkdirSync, writeFileSync } from 'fs';
import { dirname, join, relative } from 'path';
import type { BrowserBuilderSchema } from '../src/builders/webpack-browser/webpack-browser.impl';
Expand Down Expand Up @@ -74,7 +74,7 @@ ${e.stack ? e.stack : e}`
const buildTarget = getBuildableTarget(ctContext);

if (!buildTarget.project && !graph.nodes?.[buildTarget.project]?.data) {
throw new Error(stripIndents`Unable to find project configuration for build target.
throw new Error(stripIndents`Unable to find project configuration for build target.
Project Name? ${buildTarget.project}
Has project config? ${!!graph.nodes?.[buildTarget.project]?.data}`);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Tree } from '@nrwl/devkit';
import { findNodes } from '@nrwl/workspace/src/utilities/typescript';
import { findNodes } from 'nx/src/utils/typescript';
import type { PropertyAssignment } from 'typescript';
import { SyntaxKind } from 'typescript';
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
stripIndents,
visitNotIgnoredFiles,
} from '@nrwl/devkit';
import { findNodes } from '@nrwl/workspace/src/utilities/typescript';
import { findNodes } from 'nx/src/utils/typescript';
import { tsquery } from '@phenomnomnominal/tsquery';
import { extname } from 'path';
import type {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { Tree } from '@nrwl/devkit';
import {
findNodes,
getSourceNodes,
} from '@nrwl/workspace/src/utilities/typescript';
import { getSourceNodes } from '@nrwl/workspace/src/utilities/typescript';
import { findNodes } from 'nx/src/utils/typescript';

import type { PropertyDeclaration } from 'typescript';
import { SyntaxKind } from 'typescript';
import { getTsSourceFile } from '../../../utils/nx-devkit/ast-utils';
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/src/utils/nx-devkit/ast-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as ts from 'typescript';
import { findNodes } from '@nrwl/workspace/src/utilities/typescript/find-nodes';
import { findNodes } from 'nx/src/utils/typescript';
import { getSourceNodes } from '@nrwl/workspace/src/utilities/typescript/get-source-nodes';
import * as path from 'path';
import { names, readProjectConfiguration, Tree } from '@nrwl/devkit';
Expand Down
6 changes: 3 additions & 3 deletions packages/cypress/plugins/cypress-preset.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ExecutorContext,
mapProjectGraphFiles,
normalizePath,
ProjectConfiguration,
ProjectGraph,
Expand All @@ -8,7 +9,6 @@ import {
TargetConfiguration,
workspaceRoot,
} from '@nrwl/devkit';
import { mapProjectGraphFiles } from '@nrwl/workspace/src/utils/runtime-lint-utils';
import { readProjectsConfigurationFromProjectGraph } from 'nx/src/project-graph/project-graph';
import { dirname, extname, join, relative } from 'path';
import { lstatSync } from 'fs';
Expand Down Expand Up @@ -98,8 +98,8 @@ export function getProjectConfigByPath(
!graph.nodes[componentTestingProjectName]?.data
) {
throw new Error(
stripIndents`Unable to find the project configuration that includes ${normalizedPathFromWorkspaceRoot}.
Found project name? ${componentTestingProjectName}.
stripIndents`Unable to find the project configuration that includes ${normalizedPathFromWorkspaceRoot}.
Found project name? ${componentTestingProjectName}.
Graph has data? ${!!graph.nodes[componentTestingProjectName]?.data}`
);
}
Expand Down
8 changes: 8 additions & 0 deletions packages/devkit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,11 @@ export { Hash, Hasher } from 'nx/src/hasher/hasher';
* @category Utils
*/
export { cacheDir } from 'nx/src/utils/cache-directory';

/**
* @category Utils
*/
export {
MappedProjectGraph,
mapProjectGraphFiles,
} from './src/utils/mapped-graph';
40 changes: 40 additions & 0 deletions packages/devkit/src/utils/mapped-graph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
ProjectGraph,
ProjectGraphProjectNode,
} from 'nx/src/config/project-graph';

export type MappedProjectGraph<T = any> = ProjectGraph<T> & {
allFiles: Record<string, string>;
};

export function removeExt(file: string): string {
return file.replace(/(?<!(^|\/))\.[^/.]+$/, '');
}

/**
* Maps the project graph to a format that makes it easier to find the project
* based on the file path.
* @param projectGraph
* @returns
*/
export function mapProjectGraphFiles<T>(
projectGraph: ProjectGraph<T>
): MappedProjectGraph | null {
if (!projectGraph) {
return null;
}
const allFiles: Record<string, string> = {};
Object.entries(
projectGraph.nodes as Record<string, ProjectGraphProjectNode>
).forEach(([name, node]) => {
node.data.files.forEach(({ file }) => {
const fileName = removeExt(file);
allFiles[fileName] = name;
});
});

return {
...projectGraph,
allFiles,
};
}
1 change: 0 additions & 1 deletion packages/eslint-plugin-nx/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"rules": {
"no-restricted-imports": [
"error",
"@nrwl/workspace",
"@angular-devkit/core",
"@angular-devkit/architect",
"@angular-devkit/schematics"
Expand Down
1 change: 0 additions & 1 deletion packages/eslint-plugin-nx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
},
"dependencies": {
"@nrwl/devkit": "file:../devkit",
"@nrwl/workspace": "file:../workspace",
"@typescript-eslint/utils": "^5.36.1",
"chalk": "4.1.0",
"confusing-browser-globals": "^1.0.9",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type { FileData, ProjectGraph } from '@nrwl/devkit';
import { DependencyType } from '@nrwl/devkit';
import { mapProjectGraphFiles } from '@nrwl/workspace/src/utils/runtime-lint-utils';
import { DependencyType, mapProjectGraphFiles } from '@nrwl/devkit';
import * as parser from '@typescript-eslint/parser';
import { TSESLint } from '@typescript-eslint/utils';
import { vol } from 'memfs';
import { TargetProjectLocator } from 'nx/src/utils/target-project-locator';
import enforceModuleBoundaries, {
RULE_NAME as enforceModuleBoundariesRuleName,
} from '../../src/rules/enforce-module-boundaries';
} from './enforce-module-boundaries';

jest.mock('fs', () => require('memfs').fs);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {
ProjectGraphProjectNode,
workspaceRoot,
} from '@nrwl/devkit';
import { isRelativePath } from '@nrwl/workspace/src/utilities/fileutils';
import { isRelativePath } from 'nx/src/utils/fileutils';
import {
checkCircularPath,
findFilesInCircularPath,
} from '@nrwl/workspace/src/utils/graph-utils';
} from '../utils/graph-utils';
import {
DepConstraint,
findConstraintsFor,
Expand All @@ -31,7 +31,7 @@ import {
matchImportWithWildcard,
onlyLoadChildren,
stringifyTags,
} from '@nrwl/workspace/src/utils/runtime-lint-utils';
} from '../utils/runtime-lint-utils';
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
import { TargetProjectLocator } from 'nx/src/utils/target-project-locator';
import { basename, dirname, relative } from 'path';
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-nx/src/rules/nx-plugin-checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import {
findSourceProject,
getSourceFilePath,
} from '@nrwl/workspace/src/utils/runtime-lint-utils';
} from '../utils/runtime-lint-utils';
import { existsSync } from 'fs';
import { registerTsProject } from 'nx/src/utils/register';
import * as path from 'path';
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 '@nrwl/devkit';
import { findNodes } from '@nrwl/workspace/src/utilities/typescript';
import { findNodes } from 'nx/src/utils/typescript';
import { existsSync, readFileSync } from 'fs';
import { dirname } from 'path';
import ts = require('typescript');
Expand Down
File renamed without changes.
7 changes: 4 additions & 3 deletions packages/eslint-plugin-nx/src/utils/project-graph-utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { readCachedProjectGraph, readNxJson } from '@nrwl/devkit';
import {
isTerminalRun,
readCachedProjectGraph,
readNxJson,
MappedProjectGraph,
mapProjectGraphFiles,
} from '@nrwl/workspace/src/utils/runtime-lint-utils';
} from '@nrwl/devkit';
import { isTerminalRun } from './runtime-lint-utils';
import * as chalk from 'chalk';

export function ensureGlobalProjectGraph(ruleName: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import { getPath, pathExists } from './graph-utils';
import { existsSync } from 'fs';
import { readFileIfExisting } from 'nx/src/project-graph/file-utils';
import { TargetProjectLocator } from 'nx/src/utils/target-project-locator';

export type MappedProjectGraph<T = any> = ProjectGraph<T> & {
allFiles: Record<string, string>;
};
import {
MappedProjectGraph,
removeExt,
} from '@nrwl/devkit/src/utils/mapped-graph';

export type Deps = { [projectName: string]: ProjectGraphDependency[] };
export type DepConstraint = {
Expand Down Expand Up @@ -70,10 +70,6 @@ function hasTag(proj: ProjectGraphProjectNode, tag: string) {
return tag === '*' || (proj.data.tags || []).indexOf(tag) > -1;
}

export function removeExt(file: string): string {
return file.replace(/(?<!(^|\/))\.[^/.]+$/, '');
}

export function matchImportWithWildcard(
// This may or may not contain wildcards ("*")
allowableImport: string,
Expand Down Expand Up @@ -353,28 +349,6 @@ export function hasBuildExecutor(
);
}

export function mapProjectGraphFiles<T>(
projectGraph: ProjectGraph<T>
): MappedProjectGraph | null {
if (!projectGraph) {
return null;
}
const allFiles: Record<string, string> = {};
Object.entries(
projectGraph.nodes as Record<string, ProjectGraphProjectNode>
).forEach(([name, node]) => {
node.data.files.forEach(({ file }) => {
const fileName = removeExt(file);
allFiles[fileName] = name;
});
});

return {
...projectGraph,
allFiles,
};
}

const ESLINT_REGEX = /node_modules.*[\/\\]eslint$/;
const JEST_REGEX = /node_modules\/.bin\/jest$/; // when we run unit tests in jest
const NRWL_CLI_REGEX = /nx[\/\\]bin[\/\\]run-executor\.js$/;
Expand Down
2 changes: 1 addition & 1 deletion packages/expo/src/generators/component/lib/add-import.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { findNodes } from '@nrwl/workspace/src/utilities/typescript';
import { findNodes } from 'nx/src/utils/typescript';
import * as ts from 'typescript';
import { ChangeType, StringChange } from '@nrwl/devkit';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { existsSync, readFileSync, writeFileSync } from 'fs';
import { join } from 'path';

import type { NextBuildBuilderOptions } from '../../../utils/types';
import { findNodes } from '@nrwl/workspace/src/utilities/typescript';
import { findNodes } from 'nx/src/utils/typescript';

export function createNextConfigFile(
options: NextBuildBuilderOptions,
Expand Down
Loading

0 comments on commit 8856865

Please sign in to comment.