Skip to content

Commit

Permalink
feat(linter): add flat support to react, detox and expo
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Aug 9, 2023
1 parent f76a5ec commit 14d600c
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 62 deletions.
10 changes: 3 additions & 7 deletions packages/detox/src/generators/application/lib/add-linting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import {
joinPathFragments,
runTasksInSerial,
Tree,
updateJson,
} from '@nx/devkit';
import { extendReactEslintJson, extraEslintDependencies } from '@nx/react';
import { extraEslintDependencies } from '@nx/react';
import { NormalizedSchema } from './normalize-options';
import { addExtendsToLintConfig } from '@nx/linter/src/generators/utils/eslint-file';

export async function addLinting(host: Tree, options: NormalizedSchema) {
if (options.linter === Linter.None) {
Expand All @@ -24,11 +24,7 @@ export async function addLinting(host: Tree, options: NormalizedSchema) {
skipFormat: true,
});

updateJson(
host,
joinPathFragments(options.e2eProjectRoot, '.eslintrc.json'),
extendReactEslintJson
);
addExtendsToLintConfig(host, options.e2eProjectRoot, 'plugin:@nx/react');

const installTask = addDependenciesToPackageJson(
host,
Expand Down
35 changes: 11 additions & 24 deletions packages/expo/src/utils/add-linting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ import { Linter, lintProjectGenerator } from '@nx/linter';
import {
addDependenciesToPackageJson,
GeneratorCallback,
joinPathFragments,
runTasksInSerial,
Tree,
updateJson,
} from '@nx/devkit';
import { extraEslintDependencies } from '@nx/react/src/utils/lint';
import {
extendReactEslintJson,
extraEslintDependencies,
} from '@nx/react/src/utils/lint';
import type { Linter as ESLintLinter } from 'eslint';
addExtendsToLintConfig,
addIgnoresToLintConfig,
} from '@nx/linter/src/generators/utils/eslint-file';

interface NormalizedSchema {
linter?: Linter;
Expand Down Expand Up @@ -39,24 +37,13 @@ export async function addLinting(host: Tree, options: NormalizedSchema) {

tasks.push(lintTask);

updateJson(
host,
joinPathFragments(options.projectRoot, '.eslintrc.json'),
(json: ESLintLinter.Config) => {
json = extendReactEslintJson(json);

json.ignorePatterns = [
...json.ignorePatterns,
'.expo',
'node_modules',
'web-build',
'cache',
'dist',
];

return json;
}
);
addExtendsToLintConfig(host, options.projectRoot, 'plugin:@nx/react');
addIgnoresToLintConfig(host, options.projectRoot, [
'.expo',
'web-build',
'cache',
'dist',
]);

if (!options.skipPackageJson) {
const installTask = await addDependenciesToPackageJson(
Expand Down
5 changes: 3 additions & 2 deletions packages/playwright/src/utils/add-linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import {
readProjectConfiguration,
runTasksInSerial,
Tree,
updateJson,
} from '@nx/devkit';
import { Linter, lintProjectGenerator } from '@nx/linter';
import { javaScriptOverride } from '@nx/linter/src/generators/init/global-eslint-config';
import { eslintPluginPlaywrightVersion } from './versions';
import {
addExtendsToLintConfig,
addOverrideToLintConfig,
findEslintFile,
} from '@nx/linter/src/generators/utils/eslint-file';

export interface PlaywrightLinterOptions {
Expand All @@ -39,7 +39,8 @@ export async function addLinterToPlaywrightProject(
const tasks: GeneratorCallback[] = [];
const projectConfig = readProjectConfiguration(tree, options.project);

if (!tree.exists(joinPathFragments(projectConfig.root, '.eslintrc.json'))) {
const eslintFile = findEslintFile(tree, projectConfig.root);
if (!eslintFile) {
tasks.push(
await lintProjectGenerator(tree, {
project: options.project,
Expand Down
13 changes: 3 additions & 10 deletions packages/react/src/generators/application/application.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
extendReactEslintJson,
extraEslintDependencies,
} from '../../utils/lint';
import { extraEslintDependencies } from '../../utils/lint';
import { NormalizedSchema, Schema } from './schema';
import { createApplicationFiles } from './lib/create-application-files';
import { updateSpecConfig } from './lib/update-jest-config';
Expand All @@ -22,7 +19,6 @@ import {
runTasksInSerial,
stripIndents,
Tree,
updateJson,
} from '@nx/devkit';

import reactInitGenerator from '../init/init';
Expand All @@ -39,6 +35,7 @@ import { addSwcDependencies } from '@nx/js/src/utils/swc/add-swc-dependencies';
import * as chalk from 'chalk';
import { showPossibleWarnings } from './lib/show-possible-warnings';
import { addE2e } from './lib/add-e2e';
import { addExtendsToLintConfig } from '@nx/linter/src/generators/utils/eslint-file';

async function addLinting(host: Tree, options: NormalizedSchema) {
const tasks: GeneratorCallback[] = [];
Expand All @@ -63,11 +60,7 @@ async function addLinting(host: Tree, options: NormalizedSchema) {
});
tasks.push(lintTask);

updateJson(
host,
joinPathFragments(options.appProjectRoot, '.eslintrc.json'),
extendReactEslintJson
);
addExtendsToLintConfig(host, options.appProjectRoot, 'plugin:@nx/react');

if (!options.skipPackageJson) {
const installTask = addDependenciesToPackageJson(
Expand Down
13 changes: 3 additions & 10 deletions packages/react/src/generators/library/lib/add-linting.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { Tree } from 'nx/src/generators/tree';
import { Linter, lintProjectGenerator } from '@nx/linter';
import { joinPathFragments } from 'nx/src/utils/path';
import { updateJson } from 'nx/src/generators/utils/json';
import { addDependenciesToPackageJson, runTasksInSerial } from '@nx/devkit';

import { NormalizedSchema } from '../schema';
import {
extendReactEslintJson,
extraEslintDependencies,
} from '../../../utils/lint';
import { extraEslintDependencies } from '../../../utils/lint';
import { addExtendsToLintConfig } from '@nx/linter/src/generators/utils/eslint-file';

export async function addLinting(host: Tree, options: NormalizedSchema) {
if (options.linter === Linter.EsLint) {
Expand All @@ -25,11 +22,7 @@ export async function addLinting(host: Tree, options: NormalizedSchema) {
setParserOptionsProject: options.setParserOptionsProject,
});

updateJson(
host,
joinPathFragments(options.projectRoot, '.eslintrc.json'),
extendReactEslintJson
);
addExtendsToLintConfig(host, options.projectRoot, 'plugin:@nx/react');

let installTask = () => {};
if (!options.skipPackageJson) {
Expand Down
16 changes: 7 additions & 9 deletions packages/react/src/utils/lint.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { offsetFromRoot } from '@nx/devkit';
import type { Linter } from 'eslint';
import {
eslintPluginImportVersion,
eslintPluginReactVersion,
Expand All @@ -17,11 +15,11 @@ export const extraEslintDependencies = {
},
};

export const extendReactEslintJson = (json: Linter.Config) => {
const { extends: pluginExtends, ...config } = json;
// export const extendReactEslintJson = (json: Linter.Config) => {
// const { extends: pluginExtends, ...config } = json;

return {
extends: ['plugin:@nx/react', ...(pluginExtends || [])],
...config,
};
};
// return {
// extends: ['plugin:@nx/react', ...(pluginExtends || [])],
// ...config,
// };
// };

0 comments on commit 14d600c

Please sign in to comment.