Skip to content

Commit

Permalink
feat(linter): add flat support to react-native
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Aug 9, 2023
1 parent 53df1c7 commit f76a5ec
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 29 deletions.
30 changes: 30 additions & 0 deletions packages/linter/src/generators/utils/eslint-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import { useFlatConfig } from '../../utils/flat-config';
import {
addBlockToFlatConfigExport,
addCompatToFlatConfig,
generateAst,
generateFlatOverride,
generatePluginExtendsElement,
mapFilePath,
} from './flat-config/ast-utils';
import ts = require('typescript');

export const eslintConfigFileWhitelist = [
'.eslintrc',
Expand Down Expand Up @@ -83,3 +86,30 @@ export function addExtendsToLintConfig(
});
}
}

export function addIgnoresToLintConfig(
tree: Tree,
root: string,
ignorePatterns: string[]
) {
if (useFlatConfig()) {
const fileName = joinPathFragments(root, 'eslint.config.js');
const block = generateAst<ts.ObjectLiteralExpression>({
ignores: ignorePatterns.map((path) => mapFilePath(path, root)),
});
tree.write(
fileName,
addBlockToFlatConfigExport(tree.read(fileName, 'utf8'), block)
);
} else {
const fileName = joinPathFragments(root, '.eslintrc.json');
updateJson(tree, fileName, (json) => {
const ignoreSet = new Set([
...(json.ignorePatterns ?? []),
...ignorePatterns,
]);
json.ignorePatterns = Array.from(ignoreSet);
return json;
});
}
}
30 changes: 7 additions & 23 deletions packages/react-native/src/utils/add-linting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ import { Linter, lintProjectGenerator } from '@nx/linter';
import {
addDependenciesToPackageJson,
GeneratorCallback,
joinPathFragments,
runTasksInSerial,
Tree,
updateJson,
} from '@nx/devkit';
import {
extendReactEslintJson,
extraEslintDependencies,
} from '@nx/react/src/utils/lint';
import type { Linter as ESLintLinter } from 'eslint';
import { extraEslintDependencies } from '@nx/react/src/utils/lint';
import { addIgnoresToLintConfig } from '@nx/linter/src/generators/utils/eslint-file';

interface NormalizedSchema {
linter?: Linter;
Expand Down Expand Up @@ -39,22 +34,11 @@ 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,
'public',
'.cache',
'node_modules',
];

return json;
}
);
addIgnoresToLintConfig(host, options.projectRoot, [
'public',
'.cache',
'node_modules',
]);

if (!options.skipPackageJson) {
const installTask = await addDependenciesToPackageJson(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import {
import { StorybookConfigureSchema } from '../schema';
import { UiFramework7 } from '../../../utils/models';
import { nxVersion } from '../../../utils/versions';
import ts = require('typescript');
import {
addOverrideToLintConfig,
findEslintFile,
} from '@nx/linter/src/generators/utils/eslint-file';

const DEFAULT_PORT = 4400;

Expand Down Expand Up @@ -173,7 +176,7 @@ export function createStorybookTsconfigFile(
if (tree.exists(oldStorybookTsConfigPath)) {
logger.warn(`.storybook/tsconfig.json already exists for this project`);
logger.warn(
`It will be renamed and moved to tsconfig.storybook.json.
`It will be renamed and moved to tsconfig.storybook.json.
Please make sure all settings look correct after this change.
Also, please make sure to use "nx migrate" to move from one version of Nx to another.
`
Expand Down Expand Up @@ -382,6 +385,10 @@ export function updateLintConfig(tree: Tree, schema: StorybookConfigureSchema) {
]);
});

if (!findEslintFile(tree)) {
return;
}

if (tree.exists(join(root, '.eslintrc.json'))) {
updateJson(tree, join(root, '.eslintrc.json'), (json) => {
if (typeof json.parserOptions?.project === 'string') {
Expand Down Expand Up @@ -629,15 +636,15 @@ export function rootFileIsTs(
): boolean {
if (tree.exists(`.storybook/${rootFileName}.ts`) && !tsConfiguration) {
logger.info(
`The root Storybook configuration is in TypeScript,
so Nx will generate TypeScript Storybook configuration files
`The root Storybook configuration is in TypeScript,
so Nx will generate TypeScript Storybook configuration files
in this project's .storybook folder as well.`
);
return true;
} else if (tree.exists(`.storybook/${rootFileName}.js`) && tsConfiguration) {
logger.info(
`The root Storybook configuration is in JavaScript,
so Nx will generate JavaScript Storybook configuration files
`The root Storybook configuration is in JavaScript,
so Nx will generate JavaScript Storybook configuration files
in this project's .storybook folder as well.`
);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function moveProjectFiles(
'tsconfig.spec.json',
'.babelrc',
'.eslintrc.json',
'eslint.config.js',
/^jest\.config\.(app|lib)\.[jt]s$/,
'vite.config.ts',
/^webpack.*\.js$/,
Expand Down

0 comments on commit f76a5ec

Please sign in to comment.