@nx/eslint 20.8.3 → 20.8.4

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",
3
- "version": "20.8.3",
3
+ "version": "20.8.4",
4
4
  "private": false,
5
5
  "description": "The ESLint plugin for Nx contains executors, generators and utilities used for linting JavaScript/TypeScript projects within an Nx workspace.",
6
6
  "repository": {
@@ -35,8 +35,8 @@
35
35
  "eslint": "^8.0.0 || ^9.0.0"
36
36
  },
37
37
  "dependencies": {
38
- "@nx/devkit": "20.8.3",
39
- "@nx/js": "20.8.3",
38
+ "@nx/devkit": "20.8.4",
39
+ "@nx/js": "20.8.4",
40
40
  "semver": "^7.5.3",
41
41
  "tslib": "^2.3.0",
42
42
  "typescript": "~5.7.2"
@@ -8,7 +8,8 @@ export declare function addPatternsToFlatConfigIgnoresBlock(content: string, ign
8
8
  export declare function hasFlatConfigIgnoresBlock(content: string): boolean;
9
9
  export declare function hasOverride(content: string, lookup: (override: Linter.ConfigOverride<Linter.RulesRecord>) => boolean): boolean;
10
10
  /**
11
- * Finds an override matching the lookup function and applies the update function to it
11
+ * Finds an override matching the lookup function and applies the update function to it.
12
+ * Uses property-level AST updates to preserve properties with variable references.
12
13
  */
13
14
  export declare function replaceOverride(content: string, root: string, lookup: (override: Linter.ConfigOverride<Linter.RulesRecord>) => boolean, update?: (override: Partial<Linter.ConfigOverride<Linter.RulesRecord>>) => Partial<Linter.ConfigOverride<Linter.RulesRecord>>): string;
14
15
  /**
@@ -245,7 +245,52 @@ function extractPropertiesFromObjectLiteral(node) {
245
245
  return result;
246
246
  }
247
247
  /**
248
- * Finds an override matching the lookup function and applies the update function to it
248
+ * Find a property assignment node by name in an object literal.
249
+ */
250
+ function findPropertyNode(node, propertyName) {
251
+ for (const prop of node.properties) {
252
+ if (ts.isPropertyAssignment(prop)) {
253
+ const name = prop.name.getText().replace(/['"]/g, '');
254
+ if (name === propertyName) {
255
+ return prop;
256
+ }
257
+ }
258
+ }
259
+ return undefined;
260
+ }
261
+ /**
262
+ * Find properties that are added, changed, or removed.
263
+ */
264
+ function findChangedProperties(original, updated) {
265
+ const changed = [];
266
+ // Check modified/added properties
267
+ for (const key of Object.keys(updated)) {
268
+ if (JSON.stringify(original[key]) !== JSON.stringify(updated[key])) {
269
+ changed.push(key);
270
+ }
271
+ }
272
+ // Check removed properties
273
+ for (const key of Object.keys(original)) {
274
+ if (!(key in updated)) {
275
+ changed.push(key);
276
+ }
277
+ }
278
+ return changed;
279
+ }
280
+ /**
281
+ * Serialize a value to JavaScript source code. Handles converting eslintrc parser format to flat config format.
282
+ */
283
+ function serializeValue(value, format) {
284
+ const parserReplacement = format === 'mjs'
285
+ ? (parser) => `(await import('${parser}'))`
286
+ : (parser) => `require('${parser}')`;
287
+ return JSON.stringify(value, null, 2)
288
+ .replace(/"parser": "([^"]+)"/g, (_, parser) => `"parser": ${parserReplacement(parser)}`)
289
+ .replaceAll(/\n/g, '\n '); // Maintain indentation
290
+ }
291
+ /**
292
+ * Finds an override matching the lookup function and applies the update function to it.
293
+ * Uses property-level AST updates to preserve properties with variable references.
249
294
  */
250
295
  function replaceOverride(content, root, lookup, update) {
251
296
  const source = ts.createSourceFile('', content, ts.ScriptTarget.Latest, true, ts.ScriptKind.JS);
@@ -256,45 +301,78 @@ function replaceOverride(content, root, lookup, update) {
256
301
  }
257
302
  const changes = [];
258
303
  exportsArray.forEach((node) => {
259
- if (isOverride(node)) {
260
- let objSource;
261
- let start, end;
262
- if (ts.isObjectLiteralExpression(node)) {
263
- objSource = node.getFullText();
264
- start = node.properties.pos + 1; // keep leading line break
265
- end = node.properties.end;
304
+ if (!isOverride(node)) {
305
+ return;
306
+ }
307
+ let objectLiteralNode;
308
+ if (ts.isObjectLiteralExpression(node)) {
309
+ objectLiteralNode = node;
310
+ }
311
+ else {
312
+ // Handle compat.config(...).map(...) pattern
313
+ const arrowBody = node['expression'].arguments[0].body.expression;
314
+ if (ts.isObjectLiteralExpression(arrowBody)) {
315
+ objectLiteralNode = arrowBody;
266
316
  }
267
317
  else {
268
- const fullNodeText = node['expression'].arguments[0].body.expression.getFullText();
269
- // strip any spread elements
270
- objSource = fullNodeText.replace(SPREAD_ELEMENTS_REGEXP, '');
271
- start =
272
- node['expression'].arguments[0].body.expression.properties.pos +
273
- (fullNodeText.length - objSource.length);
274
- end = node['expression'].arguments[0].body.expression.properties.end;
318
+ return;
275
319
  }
276
- const data = parseTextToJson(objSource);
277
- if (lookup(data)) {
320
+ }
321
+ // Use AST-based extraction to handle variable references (e.g., plugins: { 'abc': abc })
322
+ const data = extractPropertiesFromObjectLiteral(objectLiteralNode);
323
+ if (lookup(data)) {
324
+ // Deep clone before update (update functions may mutate nested objects)
325
+ const originalData = structuredClone(data);
326
+ const updatedData = update?.(data);
327
+ // If update function was provided and returns undefined, delete the entire override block
328
+ if (update && updatedData === undefined) {
278
329
  changes.push({
279
330
  type: devkit_1.ChangeType.Delete,
280
- start,
281
- length: end - start,
331
+ start: node.pos,
332
+ length: node.end - node.pos + 1, // +1 for trailing comma
282
333
  });
283
- let updatedData = update(data);
284
- if (updatedData) {
285
- updatedData = mapFilePaths(updatedData);
286
- const parserReplacement = format === 'mjs'
287
- ? (parser) => `(await import('${parser}'))`
288
- : (parser) => `require('${parser}')`;
289
- changes.push({
290
- type: devkit_1.ChangeType.Insert,
291
- index: start,
292
- text: ' ' +
293
- JSON.stringify(updatedData, null, 2)
294
- .replace(/"parser": "([^"]+)"/g, (_, parser) => `"parser": ${parserReplacement(parser)}`)
295
- .slice(2, -2) // Remove curly braces and start/end line breaks
296
- .replaceAll(/\n/g, '\n '), // Maintain indentation
297
- });
334
+ }
335
+ else if (updatedData) {
336
+ const mappedData = mapFilePaths(updatedData);
337
+ const changedProps = findChangedProperties(originalData, mappedData);
338
+ for (const propName of changedProps) {
339
+ const originalNode = findPropertyNode(objectLiteralNode, propName);
340
+ const updatedValue = mappedData[propName];
341
+ if (originalNode && !(propName in mappedData)) {
342
+ // Delete property that was removed
343
+ changes.push({
344
+ type: devkit_1.ChangeType.Delete,
345
+ start: originalNode.pos,
346
+ length: originalNode.end - originalNode.pos,
347
+ });
348
+ }
349
+ else if (originalNode) {
350
+ // Replace existing property value
351
+ const valueNode = originalNode.initializer;
352
+ changes.push({
353
+ type: devkit_1.ChangeType.Delete,
354
+ start: valueNode.pos,
355
+ length: valueNode.end - valueNode.pos,
356
+ });
357
+ changes.push({
358
+ type: devkit_1.ChangeType.Insert,
359
+ index: valueNode.pos,
360
+ text: ' ' + serializeValue(updatedValue, format),
361
+ });
362
+ }
363
+ else {
364
+ // Add new property at the end of the object
365
+ const lastProp = objectLiteralNode.properties[objectLiteralNode.properties.length - 1];
366
+ const insertPos = lastProp
367
+ ? lastProp.end
368
+ : objectLiteralNode.pos + 1;
369
+ const needsComma = lastProp ? ',' : '';
370
+ changes.push({
371
+ type: devkit_1.ChangeType.Insert,
372
+ index: insertPos,
373
+ text: `${needsComma}\n "${propName}": ${serializeValue(updatedValue, format)}`,
374
+ });
375
+ }
298
376
  }
299
377
  }
300
378
  }