fix(linter): fix line endings handling in '@nx/eslint:convert-to-flat-config' generator (#31043)

## Current Behavior

When running `nx g @nx/eslint:convert-to-flat-config` on windows, the
ignores path is not handled correctly. After converting, the path will
have the additional `/r`

## Expected Behavior

When running `nx g @nx/eslint:convert-to-flat-config` on windows, the
ignores path should be correct.
This commit is contained in:
Terry 2025-05-30 15:45:11 +08:00 committed by GitHub
parent b51676a89a
commit e249109615
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 5 deletions

View File

@ -1,6 +1,7 @@
import { Tree, readJson } from '@nx/devkit'; import { Tree, readJson } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { convertEslintJsonToFlatConfig } from './json-converter'; import { convertEslintJsonToFlatConfig } from './json-converter';
import { EOL } from 'node:os';
describe('convertEslintJsonToFlatConfig', () => { describe('convertEslintJsonToFlatConfig', () => {
let tree: Tree; let tree: Tree;
@ -63,7 +64,7 @@ describe('convertEslintJsonToFlatConfig', () => {
}) })
); );
tree.write('.eslintignore', 'node_modules\nsomething/else'); tree.write('.eslintignore', `node_modules${EOL}something/else`);
const { content } = convertEslintJsonToFlatConfig( const { content } = convertEslintJsonToFlatConfig(
tree, tree,
@ -227,7 +228,7 @@ describe('convertEslintJsonToFlatConfig', () => {
}) })
); );
tree.write('mylib/.eslintignore', 'node_modules\nsomething/else'); tree.write('mylib/.eslintignore', `node_modules${EOL}something/else`);
const { content } = convertEslintJsonToFlatConfig( const { content } = convertEslintJsonToFlatConfig(
tree, tree,
@ -376,7 +377,7 @@ describe('convertEslintJsonToFlatConfig', () => {
}) })
); );
tree.write('.eslintignore', 'node_modules\nsomething/else'); tree.write('.eslintignore', `node_modules${EOL}something/else`);
const { content } = convertEslintJsonToFlatConfig( const { content } = convertEslintJsonToFlatConfig(
tree, tree,
@ -537,7 +538,7 @@ describe('convertEslintJsonToFlatConfig', () => {
}) })
); );
tree.write('mylib/.eslintignore', 'node_modules\nsomething/else'); tree.write('mylib/.eslintignore', `node_modules${EOL}something/else`);
const { content } = convertEslintJsonToFlatConfig( const { content } = convertEslintJsonToFlatConfig(
tree, tree,

View File

@ -185,7 +185,7 @@ export function convertEslintJsonToFlatConfig(
if (tree.exists(ignorePath)) { if (tree.exists(ignorePath)) {
const patterns = tree const patterns = tree
.read(ignorePath, 'utf-8') .read(ignorePath, 'utf-8')
.split('\n') .split(/\r\n|\r|\n/)
.filter((line) => line.length > 0 && line !== 'node_modules') .filter((line) => line.length > 0 && line !== 'node_modules')
.map((path) => mapFilePath(path)); .map((path) => mapFilePath(path));
if (patterns.length > 0) { if (patterns.length > 0) {