Skip to content

Commit

Permalink
feat(core): move runtime-lint-utils to eslint plugin (#13222)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Nov 18, 2022
1 parent e971ffb commit ab6f62a
Show file tree
Hide file tree
Showing 38 changed files with 133 additions and 120 deletions.
4 changes: 2 additions & 2 deletions packages/angular/plugins/component-testing.ts
Expand Up @@ -21,7 +21,7 @@ import {
stripIndents,
workspaceRoot,
} from '@nrwl/devkit';
import { mapProjectGraphFiles } from '@nrwl/workspace/src/utils/runtime-lint-utils';
import { mapProjectGraphFiles } from 'nx/src/utils/target-project-locator';
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
@@ -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
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
@@ -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
@@ -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
Expand Up @@ -8,8 +8,8 @@ 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 { mapProjectGraphFiles } from 'nx/src/utils/target-project-locator';
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
1 change: 0 additions & 1 deletion packages/eslint-plugin-nx/.eslintrc.json
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
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
@@ -1,13 +1,15 @@
import type { FileData, ProjectGraph } from '@nrwl/devkit';
import { DependencyType } from '@nrwl/devkit';
import { mapProjectGraphFiles } from '@nrwl/workspace/src/utils/runtime-lint-utils';
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 {
TargetProjectLocator,
mapProjectGraphFiles,
} 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
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
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
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.
6 changes: 3 additions & 3 deletions packages/eslint-plugin-nx/src/utils/project-graph-utils.ts
@@ -1,9 +1,9 @@
import { readCachedProjectGraph, readNxJson } from '@nrwl/devkit';
import {
isTerminalRun,
MappedProjectGraph,
mapProjectGraphFiles,
} from '@nrwl/workspace/src/utils/runtime-lint-utils';
MappedProjectGraph,
} from 'nx/src/utils/target-project-locator';
import { isTerminalRun } from './runtime-lint-utils';
import * as chalk from 'chalk';

export function ensureGlobalProjectGraph(ruleName: string) {
Expand Down
Expand Up @@ -14,11 +14,11 @@ import { join } from 'path';
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 {
TargetProjectLocator,
MappedProjectGraph,
removeExt,
} from 'nx/src/utils/target-project-locator';

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
@@ -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
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
47 changes: 47 additions & 0 deletions packages/nx/src/utils/target-project-locator.ts
Expand Up @@ -3,6 +3,7 @@ import { isRelativePath, readJsonFile } from './fileutils';
import { dirname, join, posix } from 'path';
import { workspaceRoot } from './workspace-root';
import {
ProjectGraph,
ProjectGraphExternalNode,
ProjectGraphProjectNode,
} from '../config/project-graph';
Expand Down Expand Up @@ -198,6 +199,11 @@ function filterRootExternalDependencies(
return nodes;
}

/**
* Mapps the project root paths to the project name
* @param nodes
* @returns
*/
export function createProjectRootMappings(
nodes: Record<string, ProjectGraphProjectNode>
) {
Expand All @@ -212,6 +218,47 @@ export function createProjectRootMappings(
return projectRootMappings;
}

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

/**
* Strips the file extension from the file path
* @param file
* @returns
*/
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,
};
}

/**
* Locates a project in projectRootMap based on a file within it
* @param filePath path that is inside of projectName
Expand Down
35 changes: 35 additions & 0 deletions packages/nx/src/utils/typescript.ts
Expand Up @@ -104,3 +104,38 @@ export function getRootTsConfigPath(): string | null {

return tsConfigFileName ? join(workspaceRoot, tsConfigFileName) : null;
}

export function findNodes(
node: ts.Node,
kind: ts.SyntaxKind | ts.SyntaxKind[],
max = Infinity
): ts.Node[] {
if (!node || max == 0) {
return [];
}

const arr: ts.Node[] = [];
const hasMatch = Array.isArray(kind)
? kind.includes(node.kind)
: node.kind === kind;
if (hasMatch) {
arr.push(node);
max--;
}
if (max > 0) {
for (const child of node.getChildren()) {
findNodes(child, kind, max).forEach((node) => {
if (max > 0) {
arr.push(node);
}
max--;
});

if (max <= 0) {
break;
}
}
}

return arr;
}
@@ -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
Expand Up @@ -7,7 +7,7 @@ import {
} from '@nrwl/devkit';

import ts = require('typescript');
import { findNodes } from '@nrwl/workspace/src/utilities/typescript/find-nodes';
import { findNodes } from 'nx/src/utils/typescript';

export async function migrateToWebPack5(tree: Tree) {
allReactProjectsWithStorybookConfiguration(tree).forEach((project) => {
Expand Down
Expand Up @@ -7,7 +7,7 @@ import {
Tree,
formatFiles,
} from '@nrwl/devkit';
import { findNodes } from '@nrwl/workspace/src/utilities/typescript/find-nodes';
import { findNodes } from 'nx/src/utils/typescript';
import * as ts from 'typescript';

export async function update(tree: Tree) {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/module-federation/ast-utils.ts
@@ -1,6 +1,6 @@
import * as ts from 'typescript';
import { ChangeType, StringChange } from '@nrwl/devkit';
import { findNodes } from '@nrwl/workspace/src/utilities/typescript/find-nodes';
import { findNodes } from 'nx/src/utils/typescript';
import {
addImport,
findClosestOpening,
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/utils/ast-utils.ts
@@ -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 {
ChangeType,
logger,
Expand Down
6 changes: 3 additions & 3 deletions packages/storybook/src/executors/utils.ts
@@ -1,5 +1,5 @@
import { ExecutorContext, joinPathFragments, logger } from '@nrwl/devkit';
import { findNodes } from '@nrwl/workspace/src/utilities/typescript/find-nodes';
import { findNodes } from 'nx/src/utils/typescript';
import 'dotenv/config';
import { existsSync, readFileSync } from 'fs';
import { join } from 'path';
Expand Down Expand Up @@ -58,9 +58,9 @@ function reactWebpack5Check(options: CommonNxStorybookConfig) {
It looks like you use Webpack 5 but your Storybook setup is not configured to leverage that
and thus falls back to Webpack 4.
Make sure you upgrade your Storybook config to use Webpack 5.
- https://gist.github.com/shilman/8856ea1786dcd247139b47b270912324#upgrade
`);
}
}
Expand Down

1 comment on commit ab6f62a

@vercel
Copy link

@vercel vercel bot commented on ab6f62a Nov 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev
nx-dev-nrwl.vercel.app

Please sign in to comment.