diff --git a/packages/eslint/src/generators/init/global-eslint-config.ts b/packages/eslint/src/generators/init/global-eslint-config.ts index 1e2ad3efe04e7..95d7683718d35 100644 --- a/packages/eslint/src/generators/init/global-eslint-config.ts +++ b/packages/eslint/src/generators/init/global-eslint-config.ts @@ -105,11 +105,11 @@ export const getGlobalFlatEslintConfiguration = ( ); content = addBlockToFlatConfigExport( content, - generateFlatPredefinedConfig('flat/typescript') + generateFlatPredefinedConfig('flat/typescript', typeScriptOverride.files) ); content = addBlockToFlatConfigExport( content, - generateFlatPredefinedConfig('flat/javascript') + generateFlatPredefinedConfig('flat/javascript', javaScriptOverride.files) ); content = addBlockToFlatConfigExport( diff --git a/packages/eslint/src/generators/lint-project/lint-project.spec.ts b/packages/eslint/src/generators/lint-project/lint-project.spec.ts index 118e09e566f65..207b7801f3678 100644 --- a/packages/eslint/src/generators/lint-project/lint-project.spec.ts +++ b/packages/eslint/src/generators/lint-project/lint-project.spec.ts @@ -58,8 +58,14 @@ describe('@nx/eslint:lint-project', () => { module.exports = [ ...nx.configs["flat/base"], - ...nx.configs["flat/typescript"], - ...nx.configs["flat/javascript"], + ...nx.configs["flat/typescript"].map(c => ({ + ...c, + files: ["*.ts", "*.tsx"] + })), + ...nx.configs["flat/javascript"].map(c => ({ + ...c, + files: ["*.js", "*.jsx"] + })), { ignores: ["**/dist"] }, diff --git a/packages/eslint/src/generators/utils/eslint-file.ts b/packages/eslint/src/generators/utils/eslint-file.ts index 2db5e49f3f469..dae76ce2d2eda 100644 --- a/packages/eslint/src/generators/utils/eslint-file.ts +++ b/packages/eslint/src/generators/utils/eslint-file.ts @@ -422,7 +422,7 @@ export function addPredefinedConfigToFlatLintConfig( predefinedConfigName: string, moduleName = 'nx', moduleImportPath = '@nx/eslint-plugin', - spread = true, + hasMultipleConfigBlocks = true, insertAtTheEnd = true ): void { if (!useFlatConfig(tree)) @@ -437,7 +437,12 @@ export function addPredefinedConfigToFlatLintConfig( content = addImportToFlatConfig(content, moduleName, moduleImportPath); content = addBlockToFlatConfigExport( content, - generateFlatPredefinedConfig(predefinedConfigName, moduleName, spread), + generateFlatPredefinedConfig( + predefinedConfigName, + undefined, + moduleName, + hasMultipleConfigBlocks + ), { insertAtTheEnd } ); diff --git a/packages/eslint/src/generators/utils/flat-config/ast-utils.ts b/packages/eslint/src/generators/utils/flat-config/ast-utils.ts index 351488ffb1b4e..cbd512f57c6b3 100644 --- a/packages/eslint/src/generators/utils/flat-config/ast-utils.ts +++ b/packages/eslint/src/generators/utils/flat-config/ast-utils.ts @@ -1075,10 +1075,11 @@ export function generateFlatOverride( export function generateFlatPredefinedConfig( predefinedConfigName: string, + files: string[] | undefined = undefined, moduleName = 'nx', - spread = true + hasMultipleConfigBlocks = true ): ts.ObjectLiteralExpression | ts.SpreadElement | ts.ElementAccessExpression { - const node = ts.factory.createElementAccessExpression( + const configNode = ts.factory.createElementAccessExpression( ts.factory.createPropertyAccessExpression( ts.factory.createIdentifier(moduleName), ts.factory.createIdentifier('configs') @@ -1086,7 +1087,65 @@ export function generateFlatPredefinedConfig( ts.factory.createStringLiteral(predefinedConfigName) ); - return spread ? ts.factory.createSpreadElement(node) : node; + if (files?.length) { + const filesNode = ts.factory.createPropertyAssignment( + ts.factory.createIdentifier('files'), + ts.factory.createArrayLiteralExpression( + files.map((file) => ts.factory.createStringLiteral(file)), + false + ) + ); + + if (hasMultipleConfigBlocks) { + return ts.factory.createSpreadElement( + ts.factory.createCallExpression( + ts.factory.createPropertyAccessExpression( + configNode, + ts.factory.createIdentifier('map') + ), + undefined, + [ + ts.factory.createArrowFunction( + undefined, + undefined, + [ + ts.factory.createParameterDeclaration( + undefined, + undefined, + ts.factory.createIdentifier('c'), + undefined, + undefined, + undefined + ), + ], + undefined, + ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), + ts.factory.createParenthesizedExpression( + ts.factory.createObjectLiteralExpression( + [ + ts.factory.createSpreadAssignment( + ts.factory.createIdentifier('c') + ), + filesNode, + ], + true + ) + ) + ), + ] + ) + ); + } + + return ts.factory.createObjectLiteralExpression( + [ts.factory.createSpreadAssignment(configNode), filesNode], + true + ); + } + + return hasMultipleConfigBlocks + ? ts.factory.createSpreadElement(configNode) + : configNode; } export function mapFilePaths<