Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

exports[`addLinting generator should correctly generate the .eslintrc.json file 1`] = `
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ describe('addLinting generator', () => {
import baseConfig from "../../eslint.config.mjs";

export default [
...nx.configs["flat/angular"],
...nx.configs["flat/angular-template"],
...baseConfig,
{
files: [
Expand All @@ -144,8 +146,6 @@ describe('addLinting generator', () => {
parser: await import("jsonc-eslint-parser")
}
},
...nx.configs["flat/angular"],
...nx.configs["flat/angular-template"],
{
files: [
"**/*.ts"
Expand Down
6 changes: 4 additions & 2 deletions packages/angular/src/generators/add-linting/add-linting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ export async function addLintingGenerator(
addPredefinedConfigToFlatLintConfig(
tree,
options.projectRoot,
'flat/angular'
'flat/angular',
{ checkBaseConfig: true }
);
addPredefinedConfigToFlatLintConfig(
tree,
options.projectRoot,
'flat/angular-template'
'flat/angular-template',
{ checkBaseConfig: true }
);
addOverrideToLintConfig(tree, options.projectRoot, {
files: ['*.ts'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,8 @@ describe('app', () => {
const baseConfig = require("../eslint.config.cjs");

module.exports = [
...nx.configs["flat/angular"],
...nx.configs["flat/angular-template"],
...baseConfig,
{
files: [
Expand All @@ -777,8 +779,6 @@ describe('app', () => {
}
}
},
...nx.configs["flat/angular"],
...nx.configs["flat/angular-template"],
{
files: [
"**/*.ts"
Expand Down
6 changes: 3 additions & 3 deletions packages/angular/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1222,9 +1222,9 @@ describe('lib', () => {
const baseConfig = require("../eslint.config.cjs");

module.exports = [
...baseConfig,
...nx.configs["flat/angular"],
...nx.configs["flat/angular-template"],
...baseConfig,
{
files: [
"**/*.ts"
Expand Down Expand Up @@ -1274,6 +1274,8 @@ describe('lib', () => {
const baseConfig = require("../eslint.config.cjs");

module.exports = [
...nx.configs["flat/angular"],
...nx.configs["flat/angular-template"],
...baseConfig,
{
files: [
Expand All @@ -1290,8 +1292,6 @@ describe('lib', () => {
}
}
},
...nx.configs["flat/angular"],
...nx.configs["flat/angular-template"],
{
files: [
"**/*.ts"
Expand Down
10 changes: 6 additions & 4 deletions packages/cypress/src/utils/add-linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,12 @@ export async function addLinterToCyProject(
tree,
projectConfig.root,
'recommended',
'cypress',
'eslint-plugin-cypress/flat',
false,
false
{
moduleName: 'cypress',
moduleImportPath: 'eslint-plugin-cypress/flat',
spread: false,
insertAtTheEnd: false,
}
);
addOverrideToLintConfig(tree, projectConfig.root, {
files: ['*.ts', '*.js'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export async function addLinting(host: Tree, options: NormalizedSchema) {
addPredefinedConfigToFlatLintConfig(
host,
options.e2eProjectRoot,
'flat/react'
'flat/react',
{ checkBaseConfig: true }
);
// Add an empty rules object to users know how to add/override rules
addOverrideToLintConfig(host, options.e2eProjectRoot, {
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/flat-configs/angular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const config: ConfigArray = tseslint.config(
...c,
})),
{
files: ['**/*.ts'],
languageOptions: {
globals: {
...globals.browser,
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/flat-configs/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const config: ConfigArray = tseslint.config(
extends: [eslint.configs.recommended, ...tseslint.configs.recommended],
},
{
files: ['**/*.js', '**/*.jsx', '**/*.cjs', '**/*.mjs'],
languageOptions: {
parser: tseslint.parser,
ecmaVersion: 2020,
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/flat-configs/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const config: ConfigArray = tseslint.config(
extends: [eslint.configs.recommended, ...tseslint.configs.recommended],
},
{
files: ['**/*.ts', '**/*.tsx', '**/*.cts', '**/*.mts'],
plugins: { '@typescript-eslint': tseslint.plugin },
languageOptions: {
parser: tseslint.parser,
Expand Down
21 changes: 16 additions & 5 deletions packages/eslint/src/generators/utils/eslint-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,11 +535,22 @@ export function addPredefinedConfigToFlatLintConfig(
tree: Tree,
root: string,
predefinedConfigName: string,
moduleName = 'nx',
moduleImportPath = '@nx/eslint-plugin',
spread = true,
insertAtTheEnd = true
options: {
moduleName?: string;
moduleImportPath?: string;
spread?: boolean;
insertAtTheEnd?: boolean;
checkBaseConfig?: boolean;
} = {}
): void {
const {
moduleName = 'nx',
moduleImportPath = '@nx/eslint-plugin',
spread = true,
insertAtTheEnd = true,
checkBaseConfig = false,
} = options;

if (!useFlatConfig(tree))
throw new Error('Predefined configs can only be used with flat configs');

Expand All @@ -556,7 +567,7 @@ export function addPredefinedConfigToFlatLintConfig(
content = addBlockToFlatConfigExport(
content,
generateFlatPredefinedConfig(predefinedConfigName, moduleName, spread),
{ insertAtTheEnd }
{ insertAtTheEnd, checkBaseConfig }
);

tree.write(fileName, content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
addImportToFlatConfig,
generateAst,
generateFlatOverride,
generateFlatPredefinedConfig,
generatePluginExtendsElementWithCompatFixup,
hasOverride,
removeCompatExtends,
Expand Down Expand Up @@ -272,6 +273,85 @@ describe('ast-utils', () => {
"
`);
});

it('should insert before baseConfig when checkBaseConfig is true (ESM)', () => {
const content = `import baseConfig from "../../eslint.config.mjs";
export default [
...baseConfig,
{
files: ["**/*.ts"],
rules: {}
},
];`;
const result = addBlockToFlatConfigExport(
content,
generateFlatPredefinedConfig('flat/react', 'nx', true),
{ checkBaseConfig: true }
);
expect(result).toMatchInlineSnapshot(`
"import baseConfig from "../../eslint.config.mjs";

export default [
...nx.configs["flat/react"],
...baseConfig,
{
files: ["**/*.ts"],
rules: {}
}
];
"
`);
});

it('should append when checkBaseConfig is true but no baseConfig exists (ESM)', () => {
const content = `import nx from "@nx/eslint-plugin";
export default [
...nx.configs["flat/base"],
...nx.configs["flat/typescript"],
];`;
const result = addBlockToFlatConfigExport(
content,
generateFlatPredefinedConfig('flat/react', 'nx', true),
{ checkBaseConfig: true }
);
expect(result).toMatchInlineSnapshot(`
"import nx from "@nx/eslint-plugin";

export default [
...nx.configs["flat/base"],
...nx.configs["flat/typescript"],
...nx.configs["flat/react"]
];
"
`);
});

it('should insert before baseConfig when checkBaseConfig is true (CJS)', () => {
const content = `const baseConfig = require("../../eslint.config.cjs");
module.exports = [
...baseConfig,
{
files: ["**/*.ts"],
rules: {}
},
];`;
const result = addBlockToFlatConfigExport(
content,
generateFlatPredefinedConfig('flat/react', 'nx', true),
{ checkBaseConfig: true }
);
expect(result).toMatchInlineSnapshot(`
"const baseConfig = require("../../eslint.config.cjs");
module.exports = [
...nx.configs["flat/react"],
...baseConfig,
{
files: ["**/*.ts"],
rules: {}
},
];"
`);
});
});

describe('addImportToFlatConfig', () => {
Expand Down
50 changes: 47 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 @@ -912,9 +912,26 @@ function addBlockToFlatConfigExportESM(
const exportArrayLiteral =
exportDefaultStatement.expression as ts.ArrayLiteralExpression;

const updatedArrayElements = options.insertAtTheEnd
? [...exportArrayLiteral.elements, config]
: [config, ...exportArrayLiteral.elements];
let updatedArrayElements: ts.Expression[];
if (options.checkBaseConfig) {
const baseConfigIndex = exportArrayLiteral.elements.findIndex(
(el) =>
ts.isSpreadElement(el) &&
ts.isIdentifier(el.expression) &&
el.expression.text === 'baseConfig'
);
Comment thread
leosvelperez marked this conversation as resolved.
if (baseConfigIndex >= 0) {
const before = exportArrayLiteral.elements.slice(0, baseConfigIndex);
const after = exportArrayLiteral.elements.slice(baseConfigIndex);
updatedArrayElements = [...before, config, ...after];
} else {
updatedArrayElements = [...exportArrayLiteral.elements, config];
}
} else {
updatedArrayElements = options.insertAtTheEnd
? [...exportArrayLiteral.elements, config]
: [config, ...exportArrayLiteral.elements];
}

const updatedExportDefault = ts.factory.createExportAssignment(
undefined,
Expand Down Expand Up @@ -964,6 +981,33 @@ function addBlockToFlatConfigExportCJS(
printer
.printNode(ts.EmitHint.Expression, config, source)
.replaceAll(/\n/g, '\n ');

if (options.checkBaseConfig) {
const baseConfigElement = exportsArray.find(
(el) =>
ts.isSpreadElement(el) &&
ts.isIdentifier(el.expression) &&
el.expression.text === 'baseConfig'
);
if (baseConfigElement) {
// Match baseConfig's indentation for consistent formatting
const baseConfigStart = baseConfigElement.getStart();
const lineStart = content.lastIndexOf('\n', baseConfigStart) + 1;
const indentation = content.substring(lineStart, baseConfigStart);
const configText = printer
.printNode(ts.EmitHint.Expression, config, source)
.replaceAll(/\n/g, `\n${indentation}`);
return applyChangesToString(content, [
{
type: ChangeType.Insert,
index: baseConfigStart,
text: `${configText},\n${indentation}`,
},
]);
}
// baseConfig not found — fall through to insertAtTheEnd behavior
}

if (options.insertAtTheEnd) {
const index =
exportsArray.length > 0
Expand Down
6 changes: 3 additions & 3 deletions packages/expo/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,16 +341,16 @@ describe('lib', () => {
moduleFileExtensions: ['ts', 'js', 'html', 'tsx', 'jsx'],
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
moduleNameMapper: {
'\\\\.svg$': '@nx/expo/plugins/jest/svg-mock',
'[.]svg$': '@nx/expo/plugins/jest/svg-mock',
},
transform: {
'\\\\.[jt]sx?$': [
'[.][jt]sx?$': [
'babel-jest',
{
configFile: __dirname + '/.babelrc.js',
},
],
'^.+\\\\.(bmp|gif|jpg|jpeg|mp4|png|psd|svg|webp|ttf|otf|m4v|mov|mp4|mpeg|mpg|webm|aac|aiff|caf|m4a|mp3|wav|html|pdf|obj)$':
'^.+[.](bmp|gif|jpg|jpeg|mp4|png|psd|svg|webp|ttf|otf|m4v|mov|mp4|mpeg|mpg|webm|aac|aiff|caf|m4a|mp3|wav|html|pdf|obj)$':
require.resolve('jest-expo/src/preset/assetFileTransformer.js'),
},
coverageDirectory: '../coverage/my-lib',
Expand Down
3 changes: 2 additions & 1 deletion packages/expo/src/utils/add-linting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ export async function addLinting(host: Tree, options: NormalizedSchema) {
addPredefinedConfigToFlatLintConfig(
host,
options.projectRoot,
'flat/react'
'flat/react',
{ checkBaseConfig: true }
);
// Add an empty rules object to users know how to add/override rules
addOverrideToLintConfig(host, options.projectRoot, {
Expand Down
6 changes: 3 additions & 3 deletions packages/expo/src/utils/jest/add-jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ module.exports = {
moduleFileExtensions: ['ts', 'js', 'html', 'tsx', 'jsx'],
setupFilesAfterEnv: ['<rootDir>/src/test-setup.${js ? 'js' : 'ts'}'],
moduleNameMapper: {
'\\\\.svg$': '@nx/expo/plugins/jest/svg-mock'
'[.]svg$': '@nx/expo/plugins/jest/svg-mock'
},
transform: {
'\\\\.[jt]sx?$': [
'[.][jt]sx?$': [
'babel-jest',
{
configFile: __dirname + '/.babelrc.js',
},
],
'^.+\\\\.(bmp|gif|jpg|jpeg|mp4|png|psd|svg|webp|ttf|otf|m4v|mov|mp4|mpeg|mpg|webm|aac|aiff|caf|m4a|mp3|wav|html|pdf|obj)$': require.resolve(
'^.+[.](bmp|gif|jpg|jpeg|mp4|png|psd|svg|webp|ttf|otf|m4v|mov|mp4|mpeg|mpg|webm|aac|aiff|caf|m4a|mp3|wav|html|pdf|obj)$': require.resolve(
'jest-expo/src/preset/assetFileTransformer.js'
),
},
Expand Down
Loading
Loading