@nx/eslint 24.0.3 → 24.0.5

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.
@@ -3,7 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.convertToInferred = convertToInferred;
4
4
  const internal_1 = require("@nx/devkit/internal");
5
5
  const devkit_1 = require("@nx/devkit");
6
+ const node_path_1 = require("node:path");
6
7
  const posix_1 = require("node:path/posix");
8
+ const node_util_1 = require("node:util");
7
9
  const plugin_1 = require("../../plugins/plugin");
8
10
  const assert_supported_eslint_version_1 = require("../../utils/assert-supported-eslint-version");
9
11
  const config_file_1 = require("../../utils/config-file");
@@ -11,10 +13,18 @@ const target_options_map_1 = require("./lib/target-options-map");
11
13
  async function convertToInferred(tree, options) {
12
14
  (0, assert_supported_eslint_version_1.assertSupportedEslintVersion)(tree);
13
15
  const projectGraph = await (0, devkit_1.createProjectGraphAsync)();
16
+ const nxJson = (0, devkit_1.readNxJson)(tree) ?? {};
14
17
  const migratedProjects = await (0, internal_1.migrateProjectExecutorsToPlugin)(tree, projectGraph, '@nx/eslint/plugin', plugin_1.createNodes, { targetName: 'lint' }, [
15
18
  {
16
19
  executors: ['@nx/eslint:lint', '@nrwl/linter:eslint'],
17
- postTargetTransformer,
20
+ postTargetTransformer: (target, tree, projectDetails, inferredTargetConfiguration) => {
21
+ const projectNode = projectGraph.nodes[projectDetails.projectName];
22
+ return postTargetTransformer(target, tree, projectDetails, inferredTargetConfiguration, getConvertedTarget(target, tree, projectDetails, inferredTargetConfiguration.name, nxJson, projectNode.data.tags),
23
+ // the migrator already dropped empty outputs and those equal to
24
+ // the plugin's, so read the ones Nx resolved before the conversion
25
+ projectNode.data.targets?.[inferredTargetConfiguration.name]
26
+ ?.outputs);
27
+ },
18
28
  targetPluginOptionMapper: (targetName) => ({ targetName }),
19
29
  skipTargetFilter,
20
30
  },
@@ -26,7 +36,36 @@ async function convertToInferred(tree, options) {
26
36
  await (0, devkit_1.formatFiles)(tree);
27
37
  }
28
38
  }
29
- function postTargetTransformer(target, tree, projectDetails, inferredTargetConfiguration) {
39
+ /**
40
+ * The target Nx runs after the conversion: the plugin's command and cwd, then
41
+ * the matching target defaults, then the converted project target.
42
+ */
43
+ function getConvertedTarget(target, tree, projectDetails, targetName, nxJson, tags) {
44
+ const { root, projectName } = projectDetails;
45
+ const pluginTarget = {
46
+ command: (0, plugin_1.getLintCommand)(root, (0, plugin_1.getStandaloneSrcPath)(root, tree.root)),
47
+ options: { cwd: root },
48
+ };
49
+ let convertedTarget = (0, internal_1.resolveCommandSyntacticSugar)(pluginTarget, root);
50
+ for (const [, , { projects }] of (0, internal_1.createTargetDefaultsResults)({ [root]: { root, targets: { [targetName]: pluginTarget } } }, {
51
+ [root]: {
52
+ root,
53
+ name: projectName,
54
+ tags,
55
+ targets: { [targetName]: target },
56
+ },
57
+ }, nxJson,
58
+ // lets `filter.plugin` entries match the plugin's target
59
+ {
60
+ [root]: {
61
+ [`targets.${targetName}.command`]: [null, '@nx/eslint/plugin'],
62
+ },
63
+ })) {
64
+ convertedTarget = (0, internal_1.mergeTargetConfigurations)((0, internal_1.resolveCommandSyntacticSugar)(projects[root].targets[targetName], root), convertedTarget);
65
+ }
66
+ return (0, internal_1.mergeTargetConfigurations)(structuredClone(target), convertedTarget);
67
+ }
68
+ function postTargetTransformer(target, tree, projectDetails, inferredTargetConfiguration, convertedTarget, originalOutputs) {
30
69
  if (target.inputs) {
31
70
  const normalizeInput = (input) => {
32
71
  return input
@@ -55,14 +94,45 @@ function postTargetTransformer(target, tree, projectDetails, inferredTargetConfi
55
94
  delete target.inputs;
56
95
  }
57
96
  }
97
+ const cwd = resolveCwd(convertedTarget.options?.cwd, projectDetails, tree.root);
98
+ const baseOptions = structuredClone(target.options ?? {});
99
+ const reportPaths = [];
58
100
  if (target.options) {
59
- handlePropertiesInOptions(target.options, projectDetails, target);
101
+ handlePropertiesInOptions(target.options, projectDetails, cwd, false);
102
+ if (target.options['output-file']) {
103
+ reportPaths.push((0, devkit_1.joinPathFragments)(cwd, target.options['output-file']));
104
+ }
60
105
  }
61
- if (target.configurations) {
62
- for (const configurationName in target.configurations) {
63
- const configuration = target.configurations[configurationName];
64
- handlePropertiesInOptions(configuration, projectDetails, target);
106
+ for (const configurationName in convertedTarget.configurations) {
107
+ const configurationCwd = resolveCwd({
108
+ ...convertedTarget.options,
109
+ ...convertedTarget.configurations[configurationName],
110
+ }.cwd, projectDetails, tree.root);
111
+ const configuration = target.configurations?.[configurationName] ?? {};
112
+ handlePropertiesInOptions(configuration, projectDetails, configurationCwd, true);
113
+ // the base options were rebased onto the base cwd
114
+ if (configurationCwd !== cwd) {
115
+ const inheritedOptions = structuredClone(baseOptions);
116
+ handlePropertiesInOptions(inheritedOptions, projectDetails, configurationCwd, false);
117
+ for (const [option, value] of Object.entries(inheritedOptions)) {
118
+ // only `convertedTarget` has the target defaults, but it keeps the
119
+ // project's own options under the executor's names
120
+ if (!(option in configuration) &&
121
+ !(option in convertedTarget.configurations[configurationName]) &&
122
+ !(0, node_util_1.isDeepStrictEqual)(value, target.options?.[option])) {
123
+ configuration[option] = value;
124
+ }
125
+ }
126
+ }
127
+ if (configuration['output-file']) {
128
+ reportPaths.push((0, devkit_1.joinPathFragments)(configurationCwd, configuration['output-file']));
65
129
  }
130
+ if (Object.keys(configuration).length !== 0) {
131
+ target.configurations ??= {};
132
+ target.configurations[configurationName] = configuration;
133
+ }
134
+ }
135
+ if (target.configurations) {
66
136
  if (Object.keys(target.configurations).length !== 0) {
67
137
  for (const configuration in target.configurations) {
68
138
  if (Object.keys(target.configurations[configuration]).length === 0) {
@@ -74,15 +144,41 @@ function postTargetTransformer(target, tree, projectDetails, inferredTargetConfi
74
144
  }
75
145
  }
76
146
  }
147
+ const isReportOutput = (output) => output.includes(reportOutputToken);
77
148
  if (target.outputs) {
149
+ target.outputs = target.outputs.filter((output) => !isReportOutput(output));
78
150
  (0, internal_1.processTargetOutputs)(target, [], inferredTargetConfiguration, {
79
151
  projectName: projectDetails.projectName,
80
152
  projectRoot: projectDetails.root,
81
153
  });
82
154
  }
155
+ // added after processTargetOutputs, which would root them at the project,
156
+ // in place of the plugin's report output it appends
157
+ const declaredReportOutputs = (originalOutputs ?? []).filter(isReportOutput);
158
+ if (declaredReportOutputs.length !== 0) {
159
+ target.outputs = [
160
+ ...(target.outputs ?? []).filter((output) => output !== plugin_1.lintReportOutput),
161
+ ...declaredReportOutputs.map((output) => output.replaceAll(reportOutputToken, plugin_1.lintReportOutput)),
162
+ ];
163
+ if ((0, node_util_1.isDeepStrictEqual)(target.outputs, inferredTargetConfiguration.outputs)) {
164
+ delete target.outputs;
165
+ }
166
+ }
167
+ // Nx fails a task whose output lands outside the workspace, so a target
168
+ // writing its report there keeps only its own outputs
169
+ if (declaredReportOutputs.length === 0 &&
170
+ reportPaths.some((reportPath) => reportPath.startsWith('../'))) {
171
+ target.outputs = (target.outputs ?? []).filter((output) => output !== plugin_1.lintReportOutput);
172
+ }
173
+ // the migrator drops an empty `outputs`, which would let the target inherit
174
+ // the plugin's report output
175
+ if (originalOutputs?.length === 0) {
176
+ target.outputs = [];
177
+ }
83
178
  return target;
84
179
  }
85
- function handlePropertiesInOptions(options, projectDetails, target) {
180
+ const reportOutputToken = '{options.outputFile}';
181
+ function handlePropertiesInOptions(options, projectDetails, cwd, isConfiguration) {
86
182
  // inferred targets are only identified after known files that ESLint would
87
183
  // pick up, so we can remove the eslintConfig option
88
184
  delete options.eslintConfig;
@@ -101,12 +197,44 @@ function handlePropertiesInOptions(options, projectDetails, target) {
101
197
  }
102
198
  delete options.errorOnUnmatchedPattern;
103
199
  }
104
- if ('outputFile' in options) {
105
- target.outputs ??= [];
106
- target.outputs.push(options.outputFile);
200
+ for (const option of [
201
+ 'outputFile',
202
+ 'ignorePath',
203
+ 'resolvePluginsRelativeTo',
204
+ 'printConfig',
205
+ ]) {
206
+ if (!(option in options)) {
207
+ continue;
208
+ }
209
+ // The executor treated an empty value as unset. ESLint rejects an empty
210
+ // path, and run-commands skips `null`, which still overrides the base.
211
+ if (!options[option]) {
212
+ if (isConfiguration) {
213
+ options[option] = null;
214
+ }
215
+ else {
216
+ delete options[option];
217
+ }
218
+ continue;
219
+ }
220
+ options[option] = toCwdRelativePath(options[option], option, projectDetails, cwd);
221
+ }
222
+ if (Array.isArray(options.rulesdir)) {
223
+ options.rulesdir = options.rulesdir.map((dir) => toCwdRelativePath(dir, 'rulesdir', projectDetails, cwd));
224
+ }
225
+ if (typeof options.format === 'string') {
226
+ const format = resolveNxTokens(options.format, 'format', projectDetails).replace(/\\/g, '/');
227
+ // ESLint loads a formatter from a file, resolved from its cwd, when the
228
+ // name contains a `/` and is not a scoped package
229
+ if (format.includes('/') && !/^@.*\//.test(format)) {
230
+ const formatPath = toCwdRelativePath(options.format, 'format', projectDetails, cwd);
231
+ options.format = /[\\/]/.test(formatPath)
232
+ ? formatPath
233
+ : `./${formatPath}`;
234
+ }
107
235
  }
108
236
  for (const key in target_options_map_1.targetOptionsToCliMap) {
109
- if (options[key]) {
237
+ if (options[key] || options[key] === null) {
110
238
  const prevValue = options[key];
111
239
  delete options[key];
112
240
  options[target_options_map_1.targetOptionsToCliMap[key]] = prevValue;
@@ -135,6 +263,35 @@ function handlePropertiesInOptions(options, projectDetails, target) {
135
263
  delete options.lintFilePatterns;
136
264
  }
137
265
  }
266
+ function resolveNxTokens(value, option, projectDetails) {
267
+ return (0, internal_1.resolveNxTokensInOptions)({ [option]: value }, { root: projectDetails.root, name: projectDetails.projectName }, option)[option];
268
+ }
269
+ /**
270
+ * The workspace-relative directory run-commands runs the converted target in,
271
+ * resolved like its `calculateCwd`.
272
+ */
273
+ function resolveCwd(cwd, projectDetails, workspaceRoot) {
274
+ if (!cwd) {
275
+ return '.';
276
+ }
277
+ const resolvedCwd = resolveNxTokens(cwd, 'cwd', projectDetails);
278
+ return ((0, devkit_1.normalizePath)((0, node_path_1.relative)(workspaceRoot, (0, node_path_1.resolve)(workspaceRoot, resolvedCwd))) || '.');
279
+ }
280
+ /**
281
+ * Rewrites a path the executor resolved from the workspace root so that ESLint
282
+ * resolves it to the same location from `cwd`, where the converted target
283
+ * runs it.
284
+ */
285
+ function toCwdRelativePath(value, option, projectDetails, cwd) {
286
+ const resolvedValue = resolveNxTokens(value, option, projectDetails);
287
+ // A relative path to an absolute location would depend on where the
288
+ // workspace is on disk. The executor joined the output file onto the
289
+ // workspace root, so its leading `/` stays inside the workspace.
290
+ if (option !== 'outputFile' && (0, node_path_1.isAbsolute)(resolvedValue)) {
291
+ return resolvedValue;
292
+ }
293
+ return (0, internal_1.toProjectRelativePath)((0, devkit_1.joinPathFragments)('.', resolvedValue), cwd);
294
+ }
138
295
  exports.default = convertToInferred;
139
296
  function skipTargetFilter(targetOptions, project) {
140
297
  if (targetOptions.eslintConfig) {
@@ -5,3 +5,6 @@ export interface EslintPluginOptions {
5
5
  }
6
6
  export declare const createNodes: CreateNodes<EslintPluginOptions>;
7
7
  export declare const createNodesV2: CreateNodes<EslintPluginOptions>;
8
+ export declare function getStandaloneSrcPath(projectRoot: string, workspaceRoot: string): string | undefined;
9
+ export declare const lintReportOutput = "{options.cwd ?? .}/{options.o ?? options.output-file}";
10
+ export declare function getLintCommand(projectRoot: string, standaloneSrcPath: string | undefined): string;
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createNodesV2 = exports.createNodes = void 0;
3
+ exports.lintReportOutput = exports.createNodesV2 = exports.createNodes = void 0;
4
+ exports.getStandaloneSrcPath = getStandaloneSrcPath;
5
+ exports.getLintCommand = getLintCommand;
4
6
  const internal_1 = require("@nx/devkit/internal");
5
7
  const devkit_1 = require("@nx/devkit");
6
8
  const js_1 = require("@nx/js");
@@ -256,16 +258,7 @@ function getProjectUsingESLintConfig(configFilePath, projectRoot, options, conte
256
258
  ...config_file_1.ESLINT_CONFIG_FILENAMES,
257
259
  ].find((f) => (0, node_fs_1.existsSync)((0, posix_1.join)(context.workspaceRoot, f)));
258
260
  // Add a lint target for each child project without an eslint config, with the root level config as an input
259
- let standaloneSrcPath;
260
- if (projectRoot === '.' &&
261
- (0, node_fs_1.existsSync)((0, posix_1.join)(context.workspaceRoot, projectRoot, 'package.json'))) {
262
- if ((0, node_fs_1.existsSync)((0, posix_1.join)(context.workspaceRoot, projectRoot, 'src'))) {
263
- standaloneSrcPath = 'src';
264
- }
265
- else if ((0, node_fs_1.existsSync)((0, posix_1.join)(context.workspaceRoot, projectRoot, 'lib'))) {
266
- standaloneSrcPath = 'lib';
267
- }
268
- }
261
+ const standaloneSrcPath = getStandaloneSrcPath(projectRoot, context.workspaceRoot);
269
262
  if (projectRoot === '.' && !standaloneSrcPath) {
270
263
  return null;
271
264
  }
@@ -277,11 +270,30 @@ function getProjectUsingESLintConfig(configFilePath, projectRoot, options, conte
277
270
  targets: buildEslintTargets(eslintConfigs, projectRoot, context.workspaceRoot, options, pmc, standaloneSrcPath, tsconfigChainOutsideProjectRoot),
278
271
  };
279
272
  }
273
+ function getStandaloneSrcPath(projectRoot, workspaceRoot) {
274
+ if (projectRoot !== '.' ||
275
+ !(0, node_fs_1.existsSync)((0, posix_1.join)(workspaceRoot, projectRoot, 'package.json'))) {
276
+ return undefined;
277
+ }
278
+ if ((0, node_fs_1.existsSync)((0, posix_1.join)(workspaceRoot, projectRoot, 'src'))) {
279
+ return 'src';
280
+ }
281
+ if ((0, node_fs_1.existsSync)((0, posix_1.join)(workspaceRoot, projectRoot, 'lib'))) {
282
+ return 'lib';
283
+ }
284
+ return undefined;
285
+ }
286
+ // ESLint resolves `--output-file`, or its short form `-o`, from the cwd
287
+ // run-commands runs it in. `o` comes first so a command-line `-o` overrides
288
+ // the `output-file` the converter writes, since ESLint keeps the last flag
289
+ exports.lintReportOutput = '{options.cwd ?? .}/{options.o ?? options.output-file}';
290
+ function getLintCommand(projectRoot, standaloneSrcPath) {
291
+ return `eslint ${projectRoot === '.' && standaloneSrcPath ? `./${standaloneSrcPath}` : '.'}`;
292
+ }
280
293
  function buildEslintTargets(eslintConfigs, projectRoot, workspaceRoot, options, pmc, standaloneSrcPath, tsconfigChainOutsideProjectRoot = []) {
281
- const isRootProject = projectRoot === '.';
282
294
  const targets = {};
283
295
  const targetConfig = {
284
- command: `eslint ${isRootProject && standaloneSrcPath ? `./${standaloneSrcPath}` : '.'}`,
296
+ command: getLintCommand(projectRoot, standaloneSrcPath),
285
297
  cache: true,
286
298
  options: {
287
299
  cwd: projectRoot,
@@ -300,7 +312,7 @@ function buildEslintTargets(eslintConfigs, projectRoot, workspaceRoot, options,
300
312
  '{workspaceRoot}/tools/eslint-rules/**/*',
301
313
  { externalDependencies: ['eslint'] },
302
314
  ],
303
- outputs: ['{options.outputFile}'],
315
+ outputs: [exports.lintReportOutput],
304
316
  metadata: {
305
317
  technologies: ['eslint'],
306
318
  description: 'Runs ESLint on project',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nx/eslint",
3
- "version": "24.0.3",
3
+ "version": "24.0.5",
4
4
  "private": false,
5
5
  "type": "commonjs",
6
6
  "files": [
@@ -71,19 +71,19 @@
71
71
  }
72
72
  },
73
73
  "peerDependencies": {
74
- "@nx/jest": "24.0.3",
74
+ "@nx/jest": "24.0.5",
75
75
  "@zkochan/js-yaml": "0.0.7",
76
76
  "eslint": "^9.0.0 || ^10.0.0"
77
77
  },
78
78
  "dependencies": {
79
- "@nx/devkit": "24.0.3",
80
- "@nx/js": "24.0.3",
79
+ "@nx/devkit": "24.0.5",
80
+ "@nx/js": "24.0.5",
81
81
  "semver": "^7.6.3",
82
82
  "tslib": "^2.3.0",
83
83
  "typescript": "~6.0.3"
84
84
  },
85
85
  "devDependencies": {
86
- "nx": "24.0.3"
86
+ "nx": "24.0.5"
87
87
  },
88
88
  "peerDependenciesMeta": {
89
89
  "@nx/jest": {