Skip to content

Commit

Permalink
✨ env 규칙 추가 (#33)
Browse files Browse the repository at this point in the history
resolved #29
  • Loading branch information
woohm402 authored Jun 22, 2024
1 parent 7fbcb09 commit 209b49d
Show file tree
Hide file tree
Showing 25 changed files with 758 additions and 73 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
"author": "Hyunmin Woo <[email protected]> (https://github.com/woohm402)",
"scripts": {
"test": "turbo run test",
"build": "turbo run build",
"prettier": "prettier . --check",
"publish:base": "yarn workspace @woohm402/eslint-config-base publish",
"publish:react": "yarn workspace @woohm402/eslint-config-react publish"
"publish:base": "yarn build && yarn workspace @woohm402/eslint-config-base publish",
"publish:react": "yarn build && yarn workspace @woohm402/eslint-config-react publish"
},
"devDependencies": {
"prettier": "3.2.5",
Expand Down
1 change: 1 addition & 0 deletions packages/base/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/dist
8 changes: 6 additions & 2 deletions packages/base/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Add the following to your `eslint.config.js` file:
```js
import woohm402EslintConfigBase from '@woohm402/eslint-config-base';

export default [...woohm402EslintConfigBase];
export default [...woohm402EslintConfigBase()];
```

<br />
Expand All @@ -39,7 +39,11 @@ It also includes the following additional rules:
- `'@eslint-community/eslint-comments/no-use': ['error', { allow: [] }]`
- This rule disallows the use of eslint comments e.g. `// eslint-disable-next-line`.
- If you want to disable a rule, you should do so in the configuration file.
- `'no-restricted-syntax': ['error', ...]`
- `'no-restricted-syntax': ['error', ...]` about `console.log`
- This rule disallows usage of `console.log`.
- Usually console.log is used for debugging purposes and should be removed before committing code.
- If you intended to print to the console, use alternative methods like `console.debug` or `console.error`.
- `'no-restricted-syntax': ['error', ...]` about env
- This rule disallows usage of `process.env` in too many places.
- Env variables are hard to track and should be used sparingly.
- You might need to use env variable, so pass `envAllowedFiles` option to this rule. They will be ignored.
2 changes: 2 additions & 0 deletions packages/base/__tests__/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import config from '../dist/index.js';
export default config();
2 changes: 1 addition & 1 deletion packages/base/__tests__/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ESLint } from 'eslint';
import { describe, it, expect } from 'vitest';
import path from 'path';

const eslint = new ESLint({ overrideConfigFile: path.resolve(__dirname, '../index.js') });
const eslint = new ESLint({ overrideConfigFile: path.resolve(__dirname, '_config.js') });

describe('@woohm402/eslint-config-base', function () {
it('right', async function () {
Expand Down
5 changes: 0 additions & 5 deletions packages/base/index.d.ts

This file was deleted.

25 changes: 0 additions & 25 deletions packages/base/index.js

This file was deleted.

13 changes: 8 additions & 5 deletions packages/base/package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
{
"name": "@woohm402/eslint-config-base",
"version": "0.1.0",
"version": "0.2.5",
"description": "Base config for TypeScript",
"private": false,
"scripts": {
"build": "tsup",
"test": "vitest run"
},
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"index.js",
"index.d.ts"
"src/index.ts",
"dist/index.js",
"dist/index.d.ts"
],
"publishConfig": {
"access": "public"
Expand Down Expand Up @@ -47,6 +49,7 @@
"@types/eslint": "8.56.10",
"@types/node": "20.12.13",
"eslint": "9.3.0",
"tsup": "8.1.0",
"typescript": "5.4.5",
"vitest": "1.6.0"
}
Expand Down
64 changes: 64 additions & 0 deletions packages/base/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import ESLintPluginESLintCommentsConfigs from '@eslint-community/eslint-plugin-eslint-comments/configs';
import simpleImportSort from 'eslint-plugin-simple-import-sort';
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';
import eslint from '@eslint/js';
import tseslint, { ConfigWithExtends } from 'typescript-eslint';

const consoleLogRestrictSyntax = [
{
selector: "CallExpression[callee.object.name='console'][callee.property.name='log']",
message:
'console.log() 가 main 에 머지되는 것은 실수일 수 있습니다. 의도하지 않았다면 제거하고, 의도했다면 console.debug() 등 다른 메서드를 사용해주세요.',
},
];

const envRestrictSyntax = [
{
selector: 'MemberExpression[object.name=process][property.name=env]',
message:
'환경변수를 여러 곳에서 접근하는 것은 위험할 수 있습니다. envAllowedFiles 에 소수의 파일들을 등록하고 그곳에서만 접근해주세요.',
},
{
selector: 'MemberExpression[object.meta.name=import][object.property.name=meta][property.name=env]',
message:
'환경변수를 여러 곳에서 접근하는 것은 위험할 수 있습니다. envAllowedFiles 에 소수의 파일들을 등록하고 그곳에서만 접근해주세요.',
},
];

const baseRules: ConfigWithExtends = {
plugins: {
'simple-import-sort': simpleImportSort,
'@eslint-community/eslint-comments':
ESLintPluginESLintCommentsConfigs.recommended.plugins['@eslint-community/eslint-comments'],
},
rules: {
'simple-import-sort/imports': 'error',
'@eslint-community/eslint-comments/no-use': ['error', { allow: [] }],
},
};

const noRestrictedSyntaxForEnvNotAllowedFiles = (options?: Options): ConfigWithExtends => ({
ignores: options?.envAllowedFiles ?? [],
rules: {
'no-restricted-syntax': ['error', ...consoleLogRestrictSyntax, ...envRestrictSyntax],
},
});

const noRestrictedSyntaxForEnvAllowedFiles = (options?: Options): ConfigWithExtends => ({
files: options?.envAllowedFiles ?? [],
rules: {
'no-restricted-syntax': ['error', ...consoleLogRestrictSyntax],
},
});

type Options = { envAllowedFiles?: string[] };

export default (options?: Options) =>
tseslint.config(
eslint.configs.recommended,
eslintPluginPrettierRecommended,
...tseslint.configs.recommended,
baseRules,
noRestrictedSyntaxForEnvNotAllowedFiles(options),
...(options?.envAllowedFiles && options.envAllowedFiles.length > 0 ? [noRestrictedSyntaxForEnvAllowedFiles(options)] : []),
);
7 changes: 7 additions & 0 deletions packages/base/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare module '@eslint-community/eslint-plugin-eslint-comments/configs' {
export const recommended: any;
}

declare module '@eslint/js' {
export const configs: any;
}
9 changes: 7 additions & 2 deletions packages/base/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
"compilerOptions": {
"types": ["node"],
"strict": true,
"esModuleInterop": true
"esModuleInterop": true,
"module": "ESNext",
"moduleResolution": "Bundler",
"skipLibCheck": true,
"allowJs": true,
"noEmit": true
},
"include": ["__tests__/**/*"],
"include": ["__tests__", "src"],
"exclude": ["node_modules"]
}
7 changes: 7 additions & 0 deletions packages/base/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'tsup';

export default defineConfig({
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
dts: true,
});
1 change: 1 addition & 0 deletions packages/react/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/dist
2 changes: 1 addition & 1 deletion packages/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Add the following to your `eslint.config.js` file:
```js
import woohm402EslintConfigReact from '@woohm402/eslint-config-react';

export default [...woohm402EslintConfigReact];
export default [...woohm402EslintConfigReact()];
```

<br />
Expand Down
2 changes: 2 additions & 0 deletions packages/react/__tests__/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import config from '../dist/index.js';
export default config();
2 changes: 1 addition & 1 deletion packages/react/__tests__/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ESLint } from 'eslint';
import { describe, it, expect } from 'vitest';
import path from 'path';

const eslint = new ESLint({ overrideConfigFile: path.resolve(__dirname, '../index.js') });
const eslint = new ESLint({ overrideConfigFile: path.resolve(__dirname, '_config.js') });

describe('@woohm402/eslint-config-base', function () {
it('right', async function () {
Expand Down
5 changes: 0 additions & 5 deletions packages/react/index.d.ts

This file was deleted.

15 changes: 9 additions & 6 deletions packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
{
"name": "@woohm402/eslint-config-react",
"version": "0.0.1",
"version": "0.0.3",
"description": "Base config for TypeScript React",
"private": false,
"scripts": {
"build": "tsup",
"test": "vitest run"
},
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"index.js",
"index.d.ts"
"src/index.ts",
"dist/index.js",
"dist/index.d.ts"
],
"publishConfig": {
"access": "public"
Expand All @@ -33,7 +35,7 @@
"homepage": "https://github.com/woohm402/eslint-config-woohm402#readme",
"dependencies": {
"@eslint/compat": "1.0.3",
"@woohm402/eslint-config-base": "0.1.0",
"@woohm402/eslint-config-base": "0.2.5",
"eslint-plugin-react-hooks": "4.6.2"
},
"peerDependencies": {
Expand All @@ -48,6 +50,7 @@
"@types/node": "20.12.13",
"@types/react": "18.3.3",
"eslint": "9.3.0",
"tsup": "8.1.0",
"typescript": "5.4.5",
"vitest": "1.6.0"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/react/index.js → packages/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import woohm402 from '@woohm402/eslint-config-base';
import eslintPluginReactHooks from 'eslint-plugin-react-hooks';
import { fixupPluginRules } from '@eslint/compat';

export default [
...woohm402,
export default (options?: Parameters<typeof woohm402>[0]) => [
...woohm402(options),
{
plugins: {
'react-hooks': fixupPluginRules(eslintPluginReactHooks),
Expand Down
7 changes: 7 additions & 0 deletions packages/react/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare module 'eslint-plugin-react-hooks' {
export const configs: any;
}

declare module '@eslint/compat' {
export const fixupPluginRules: any;
}
9 changes: 7 additions & 2 deletions packages/react/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
"types": ["node"],
"strict": true,
"esModuleInterop": true,
"jsx": "react-jsx"
"module": "ESNext",
"moduleResolution": "Bundler",
"skipLibCheck": true,
"jsx": "react-jsx",
"allowJs": true,
"noEmit": true
},
"include": ["__tests__/**/*"],
"include": ["__tests__/**/*", "src"],
"exclude": ["node_modules"]
}
7 changes: 7 additions & 0 deletions packages/react/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'tsup';

export default defineConfig({
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
dts: true,
});
3 changes: 2 additions & 1 deletion turbo.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"$schema": "https://turbo.build/schema.json",
"pipeline": {
"test": {},
"build": { "outputs": ["dist"] },
"test": { "dependsOn": ["build"] },
"prettier": {}
}
}
Loading

0 comments on commit 209b49d

Please sign in to comment.