Skip to content

Commit

Permalink
fix(linter): scope js and ts shared configs to js and ts files
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez committed Oct 10, 2024
1 parent db10812 commit 54dac49
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/eslint/src/generators/init/global-eslint-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 8 additions & 2 deletions packages/eslint/src/generators/lint-project/lint-project.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
},
Expand Down
9 changes: 7 additions & 2 deletions packages/eslint/src/generators/utils/eslint-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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 }
);

Expand Down
65 changes: 62 additions & 3 deletions packages/eslint/src/generators/utils/flat-config/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1075,18 +1075,77 @@ 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')
),
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<
Expand Down

0 comments on commit 54dac49

Please sign in to comment.