Skip to content

Commit 6252b70

Browse files
authored
fix(lint): do not output warning about react on non-react projec… (#308)
fix(lint): do not output warning about react on non-react projects
2 parents ffd8733 + b6c3153 commit 6252b70

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

src/createEslintConfig.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
11
import fs from 'fs';
22
import path from 'path';
33
import { CLIEngine } from 'eslint';
4+
import { PackageJson } from './types';
5+
import { getReactVersion } from './utils';
46

57
interface CreateEslintConfigArgs {
8+
pkg: PackageJson;
69
rootDir: string;
710
writeFile: boolean;
811
}
912
export function createEslintConfig({
13+
pkg,
1014
rootDir,
1115
writeFile,
1216
}: CreateEslintConfigArgs): CLIEngine.Options['baseConfig'] {
17+
const isReactLibrary = Boolean(getReactVersion(pkg));
18+
1319
const config = {
1420
extends: [
1521
'react-app',
1622
'prettier/@typescript-eslint',
1723
'plugin:prettier/recommended',
1824
],
25+
settings: {
26+
react: {
27+
// Fix for https://github.com/jaredpalmer/tsdx/issues/279
28+
version: isReactLibrary ? 'detect' : '999.999.999',
29+
},
30+
},
1931
};
2032

2133
if (writeFile) {

src/index.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,13 @@ import { concatAllArray } from 'jpjs';
3333
import getInstallCmd from './getInstallCmd';
3434
import getInstallArgs from './getInstallArgs';
3535
import { Input, Select } from 'enquirer';
36-
import { TsdxOptions } from './types';
36+
import { PackageJson, TsdxOptions } from './types';
3737
import { createProgressEstimator } from './createProgressEstimator';
3838
const pkg = require('../package.json');
3939

4040
const prog = sade('tsdx');
4141

42-
let appPackageJson: {
43-
name: string;
44-
source?: string;
45-
jest?: any;
46-
eslint?: any;
47-
};
42+
let appPackageJson: PackageJson;
4843

4944
try {
5045
appPackageJson = fs.readJSONSync(resolveApp('package.json'));
@@ -606,6 +601,7 @@ prog
606601
const cli = new CLIEngine({
607602
baseConfig: {
608603
...createEslintConfig({
604+
pkg: appPackageJson,
609605
rootDir: paths.appRoot,
610606
writeFile: opts['write-file'],
611607
}),

src/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,12 @@ export interface TsdxOptions {
1818
// Is this the very first rollup config (and thus should one-off metadata be extracted)?
1919
writeMeta?: boolean;
2020
}
21+
22+
export interface PackageJson {
23+
name: string;
24+
source?: string;
25+
jest?: any;
26+
eslint?: any;
27+
dependencies?: { [packageName: string]: string };
28+
devDependencies?: { [packageName: string]: string };
29+
}

src/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from 'fs-extra';
22
import path from 'path';
33
import camelCase from 'camelcase';
4+
import { PackageJson } from './types';
45

56
// Remove the package name scope if it exists
67
export const removeScope = (name: string) => name.replace(/^@.*\//, '');
@@ -35,3 +36,13 @@ export function clearConsole() {
3536
process.platform === 'win32' ? '\x1B[2J\x1B[0f' : '\x1B[2J\x1B[3J\x1B[H'
3637
);
3738
}
39+
40+
export function getReactVersion({
41+
dependencies,
42+
devDependencies,
43+
}: PackageJson) {
44+
return (
45+
(dependencies && dependencies.react) ||
46+
(devDependencies && devDependencies.react)
47+
);
48+
}

0 commit comments

Comments
 (0)