@nx/eslint-plugin 22.1.0 → 22.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nx/eslint-plugin",
3
- "version": "22.1.0",
3
+ "version": "22.1.1",
4
4
  "private": false,
5
5
  "description": "The eslint-plugin package is an ESLint plugin that contains a collection of recommended ESLint rule configurations which you can extend from in your own ESLint configs, as well as an Nx-specific lint rule called enforce-module-boundaries.",
6
6
  "repository": {
@@ -34,8 +34,8 @@
34
34
  }
35
35
  },
36
36
  "dependencies": {
37
- "@nx/devkit": "22.1.0",
38
- "@nx/js": "22.1.0",
37
+ "@nx/devkit": "22.1.1",
38
+ "@nx/js": "22.1.1",
39
39
  "@phenomnomnominal/tsquery": "~5.0.1",
40
40
  "@typescript-eslint/type-utils": "^8.0.0",
41
41
  "@typescript-eslint/utils": "^8.0.0",
package/src/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { loadWorkspaceRules } from './resolve-workspace-rules';
1
2
  declare const configs: {
2
3
  typescript: {
3
4
  parser: string;
@@ -557,5 +558,5 @@ declare const _default: {
557
558
  };
558
559
  };
559
560
  export default _default;
560
- export { configs, rules };
561
+ export { configs, rules, loadWorkspaceRules };
561
562
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../packages/eslint-plugin/src/index.ts"],"names":[],"mappings":"AA0BA,QAAA,MAAM,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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,OAAO;;;;;;;;AAY3F,wBAAkC;AAClC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,kBAAkB,EAAE,CAAC"}
package/src/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.rules = exports.configs = void 0;
3
+ exports.loadWorkspaceRules = exports.rules = exports.configs = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const typescript_1 = tslib_1.__importDefault(require("./configs/typescript"));
6
6
  const javascript_1 = tslib_1.__importDefault(require("./configs/javascript"));
@@ -16,6 +16,7 @@ const nx_plugin_checks_1 = tslib_1.__importStar(require("./rules/nx-plugin-check
16
16
  const dependency_checks_1 = tslib_1.__importStar(require("./rules/dependency-checks"));
17
17
  // Resolve any custom rules that might exist in the current workspace
18
18
  const resolve_workspace_rules_1 = require("./resolve-workspace-rules");
19
+ Object.defineProperty(exports, "loadWorkspaceRules", { enumerable: true, get: function () { return resolve_workspace_rules_1.loadWorkspaceRules; } });
19
20
  const configs = {
20
21
  // eslintrc configs
21
22
  typescript: typescript_1.default,
@@ -1,5 +1,39 @@
1
1
  import type { TSESLint } from '@typescript-eslint/utils';
2
2
  type ESLintRules = Record<string, TSESLint.RuleModule<string, unknown[]>>;
3
+ /**
4
+ * Load ESLint rules from a directory.
5
+ *
6
+ * This utility allows loading custom ESLint rules from any directory within the workspace,
7
+ * not just the default `tools/eslint-rules` location. It's useful for:
8
+ * - Loading rules from a custom directory structure
9
+ * - Loading rules from npm workspace packages (e.g., linked packages via npm/yarn/pnpm workspaces)
10
+ * - Loading rules from multiple directories with different configurations
11
+ *
12
+ * The directory must contain an index file (index.ts, index.js, etc.) that exports the rules.
13
+ *
14
+ * @param directory - The directory path to load rules from. Can be absolute or relative to workspace root.
15
+ * @param tsConfigPath - Optional path to tsconfig.json for TypeScript compilation.
16
+ * If not provided, will search for tsconfig.json starting from
17
+ * the directory and traversing up to the workspace root.
18
+ * @returns An object containing the loaded ESLint rules (without any prefix).
19
+ * Returns an empty object if the directory doesn't exist or loading fails.
20
+ *
21
+ * @example
22
+ * ```typescript
23
+ * // Load rules from a custom directory (relative to workspace root)
24
+ * const customRules = await loadWorkspaceRules('packages/my-eslint-plugin/rules');
25
+ *
26
+ * // Load rules with a specific tsconfig
27
+ * const customRules = await loadWorkspaceRules(
28
+ * 'packages/my-eslint-plugin/rules',
29
+ * 'packages/my-eslint-plugin/tsconfig.json'
30
+ * );
31
+ *
32
+ * // Or use absolute paths
33
+ * const customRules = loadWorkspaceRules('/absolute/path/to/rules');
34
+ * ```
35
+ */
36
+ export declare function loadWorkspaceRules(directory: string, tsConfigPath?: string): Promise<ESLintRules>;
3
37
  export declare const workspaceRules: ESLintRules;
4
38
  export {};
5
39
  //# sourceMappingURL=resolve-workspace-rules.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"resolve-workspace-rules.d.ts","sourceRoot":"","sources":["../../../../packages/eslint-plugin/src/resolve-workspace-rules.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AAMzD,KAAK,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,UAAU,CAAC,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC;AAE1E,eAAO,MAAM,cAAc,aAgCvB,CAAC"}
1
+ {"version":3,"file":"resolve-workspace-rules.d.ts","sourceRoot":"","sources":["../../../../packages/eslint-plugin/src/resolve-workspace-rules.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AAKzD,KAAK,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,UAAU,CAAC,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC;AAiF1E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,wBAAsB,kBAAkB,CACtC,SAAS,EAAE,MAAM,EACjB,YAAY,CAAC,EAAE,MAAM,GACpB,OAAO,CAAC,WAAW,CAAC,CA2CtB;AAED,eAAO,MAAM,cAAc,aAgCvB,CAAC"}
@@ -1,10 +1,136 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.workspaceRules = void 0;
4
- const fs_1 = require("fs");
4
+ exports.loadWorkspaceRules = loadWorkspaceRules;
5
+ const devkit_1 = require("@nx/devkit");
6
+ const config_utils_1 = require("@nx/devkit/src/utils/config-utils");
5
7
  const internal_1 = require("@nx/js/src/internal");
6
- const constants_1 = require("./constants");
8
+ const fs_1 = require("fs");
7
9
  const path_1 = require("path");
10
+ const constants_1 = require("./constants");
11
+ // ESM import() cannot resolve directories to index files like require() can
12
+ const INDEX_FILE_EXTENSIONS = [
13
+ '.ts',
14
+ '.mts',
15
+ '.cts',
16
+ '.js',
17
+ '.mjs',
18
+ '.cjs',
19
+ ];
20
+ function resolveDirectoryEntryFile(directory) {
21
+ for (const ext of INDEX_FILE_EXTENSIONS) {
22
+ const candidatePath = (0, path_1.join)(directory, `index${ext}`);
23
+ if ((0, fs_1.existsSync)(candidatePath)) {
24
+ return candidatePath;
25
+ }
26
+ }
27
+ throw new Error(`No index file found in directory: ${directory}. ` +
28
+ `Expected one of: ${INDEX_FILE_EXTENSIONS.map((ext) => `index${ext}`).join(', ')}`);
29
+ }
30
+ function normalizePath(path) {
31
+ return `${(0, path_1.normalize)(path).replace(/[\/\\]$/g, '')}${path_1.sep}`;
32
+ }
33
+ function findTsConfig(directory, tsConfigPath) {
34
+ let effectiveTsConfigPath = tsConfigPath;
35
+ const normalizedWorkspaceRoot = normalizePath(devkit_1.workspaceRoot);
36
+ if (effectiveTsConfigPath) {
37
+ if (!(0, path_1.isAbsolute)(effectiveTsConfigPath)) {
38
+ effectiveTsConfigPath = (0, path_1.resolve)(devkit_1.workspaceRoot, effectiveTsConfigPath);
39
+ }
40
+ if (!effectiveTsConfigPath.startsWith(normalizedWorkspaceRoot)) {
41
+ console.warn(`TypeScript config "${effectiveTsConfigPath}" is outside the workspace root "${devkit_1.workspaceRoot}". Falling back to automatic tsconfig detection.`);
42
+ effectiveTsConfigPath = undefined;
43
+ }
44
+ }
45
+ if (!effectiveTsConfigPath) {
46
+ let currentDir = directory;
47
+ while (currentDir.startsWith(normalizedWorkspaceRoot)) {
48
+ const candidatePath = (0, path_1.join)(currentDir, 'tsconfig.json');
49
+ if ((0, fs_1.existsSync)(candidatePath)) {
50
+ effectiveTsConfigPath = candidatePath;
51
+ break;
52
+ }
53
+ const parentDir = (0, path_1.dirname)(currentDir);
54
+ if (normalizePath(parentDir) === normalizedWorkspaceRoot) {
55
+ break;
56
+ }
57
+ currentDir = parentDir;
58
+ }
59
+ }
60
+ if (!tsConfigPath && !effectiveTsConfigPath) {
61
+ console.warn(`No TypeScript config found. Crawled up to workspace root "${devkit_1.workspaceRoot}" ` +
62
+ `from directory "${directory}" without finding a tsconfig.json file. Provide ` +
63
+ `a tsconfig.json file path to the loadWorkspaceRules function.`);
64
+ }
65
+ return effectiveTsConfigPath;
66
+ }
67
+ /**
68
+ * Load ESLint rules from a directory.
69
+ *
70
+ * This utility allows loading custom ESLint rules from any directory within the workspace,
71
+ * not just the default `tools/eslint-rules` location. It's useful for:
72
+ * - Loading rules from a custom directory structure
73
+ * - Loading rules from npm workspace packages (e.g., linked packages via npm/yarn/pnpm workspaces)
74
+ * - Loading rules from multiple directories with different configurations
75
+ *
76
+ * The directory must contain an index file (index.ts, index.js, etc.) that exports the rules.
77
+ *
78
+ * @param directory - The directory path to load rules from. Can be absolute or relative to workspace root.
79
+ * @param tsConfigPath - Optional path to tsconfig.json for TypeScript compilation.
80
+ * If not provided, will search for tsconfig.json starting from
81
+ * the directory and traversing up to the workspace root.
82
+ * @returns An object containing the loaded ESLint rules (without any prefix).
83
+ * Returns an empty object if the directory doesn't exist or loading fails.
84
+ *
85
+ * @example
86
+ * ```typescript
87
+ * // Load rules from a custom directory (relative to workspace root)
88
+ * const customRules = await loadWorkspaceRules('packages/my-eslint-plugin/rules');
89
+ *
90
+ * // Load rules with a specific tsconfig
91
+ * const customRules = await loadWorkspaceRules(
92
+ * 'packages/my-eslint-plugin/rules',
93
+ * 'packages/my-eslint-plugin/tsconfig.json'
94
+ * );
95
+ *
96
+ * // Or use absolute paths
97
+ * const customRules = loadWorkspaceRules('/absolute/path/to/rules');
98
+ * ```
99
+ */
100
+ async function loadWorkspaceRules(directory, tsConfigPath) {
101
+ const resolvedDirectory = (0, path_1.isAbsolute)(directory)
102
+ ? directory
103
+ : (0, path_1.resolve)(devkit_1.workspaceRoot, directory);
104
+ if (!resolvedDirectory.startsWith(normalizePath(devkit_1.workspaceRoot))) {
105
+ console.warn(`Directory "${resolvedDirectory}" is outside the workspace root "${devkit_1.workspaceRoot}". ESLint rules can only be loaded from within the workspace.`);
106
+ return {};
107
+ }
108
+ if (!(0, fs_1.existsSync)(resolvedDirectory)) {
109
+ console.warn(`Directory "${resolvedDirectory}" does not exist. Skipping loading ESLint rules from this directory.`);
110
+ return {};
111
+ }
112
+ let registrationCleanup = null;
113
+ try {
114
+ const effectiveTsConfigPath = findTsConfig(resolvedDirectory, tsConfigPath);
115
+ if (effectiveTsConfigPath) {
116
+ registrationCleanup = (0, internal_1.registerTsProject)(effectiveTsConfigPath);
117
+ }
118
+ const entryFile = resolveDirectoryEntryFile(resolvedDirectory);
119
+ // Only rules are supported (not configs, processors, etc.)
120
+ const module = await (0, config_utils_1.loadConfigFile)(entryFile);
121
+ const rules = module.rules || module;
122
+ return rules;
123
+ }
124
+ catch (err) {
125
+ console.error(err);
126
+ return {};
127
+ }
128
+ finally {
129
+ if (registrationCleanup) {
130
+ registrationCleanup();
131
+ }
132
+ }
133
+ }
8
134
  exports.workspaceRules = (() => {
9
135
  // If `tools/eslint-rules` folder doesn't exist, there is no point trying to register and load it
10
136
  if (!(0, fs_1.existsSync)(constants_1.WORKSPACE_PLUGIN_DIR)) {