Skip to content

Commit

Permalink
Merge pull request #131 from hatena/support-project-service
Browse files Browse the repository at this point in the history
typescript-eslint の projectService をオプトインできるようにする
  • Loading branch information
susisu authored Sep 4, 2024
2 parents 3f91088 + 0fa98bc commit fe9abf9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
21 changes: 19 additions & 2 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,33 @@ module.exports = {

### TypeScript

TypeScript に関連した設定はデフォルトで有効になっているため、特別に設定を追加する必要はありません。
TypeScript に関連した設定はデフォルトで有効になっているため、特別に設定を追加する必要はありません。TypeScript の設定ファイルはデフォルトでは `./tsconfig.json` が使われます。

TypeScript の設定ファイルはデフォルトでは `./tsconfig.json` が使われます。このファイルを変更する場合は `tsProject` オプションを使用してください。
ESLint を実行するディレクトリが `tsconfig.json` が配置されているディレクトリと異なり、うまく `tsconfig.json` を解決できない場合は、`tsconfigRootDir` を指定してください。

```javascript
export default config({
tsconfigRootDir: import.meta.dirname,
});
```

参照する TypeScript の設定ファイルを変更する場合は `tsProject` オプションを使用してください。ただし、この設定をすると ESLint 以外のツールとの間で型情報の不一致が生まれる可能性があるため、非推奨です。

```javascript
export default config({
tsProject: './tsconfig.lint.json',
});
```

typescript-eslint v8 から追加された [`projectService` オプション](https://typescript-eslint.io/packages/parser#projectservice)は、現在オプトインとして提供しています。使用する場合は `tsProjectService` を有効にしてください (`tsProject` よりも優先されます)。

```javascript
export default config({
tsProjectService: true,
tsconfigRootDir: import.meta.dirname,
});
```

### React

React に関連した設定やルールを有効化するには、`react` オプションを有効化してください。
Expand Down
17 changes: 14 additions & 3 deletions lib/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ const rules = require('../rules/index.js');

/**
* @typedef ConfigOptions
* @property {boolean | string | string[] | undefined} [tsProject]
* TypeScript の設定ファイル (languageOptions.parserOptions.project)
* @property {import('@typescript-eslint/parser').ParserOptions['project']} [tsProject]
* TypeScript の設定ファイル (https://typescript-eslint.io/packages/parser#project)
* デフォルト: true (lint 対象のファイルに最も近い tsconfig.json を利用する)
* @property {import('@typescript-eslint/parser').ParserOptions['projectService']} [tsProjectService]
* TypeScript のプロジェクトの設定 (https://typescript-eslint.io/packages/parser#projectservice)
* デフォルト: false (無効)
* @property {import('@typescript-eslint/parser').ParserOptions['tsconfigRootDir']} [tsconfigRootDir]
* tsconfig.json の探索先のルートディレクトリ (https://typescript-eslint.io/packages/parser#tsconfigrootdir)
* デフォルト: undefined
* @property {boolean | undefined} [react]
* React を使用するか. true の場合, React に関連する設定を有効にする
* デフォルト: false
Expand All @@ -30,6 +36,8 @@ const rules = require('../rules/index.js');
*/
function config(options, configs) {
const tsProject = options?.tsProject ?? true;
const tsProjectService = options?.tsProjectService ?? false;
const tsconfigRootDir = options?.tsconfigRootDir ?? undefined;
const react = options?.react ?? false;
const prettier = options?.prettier ?? true;

Expand Down Expand Up @@ -75,7 +83,10 @@ function config(options, configs) {
languageOptions: {
sourceType: 'module',
parser: /** @type {import('eslint').Linter.Parser} */ (tsEslint.parser),
parserOptions: tsProject ? { project: tsProject } : {},
parserOptions: {
...(tsProjectService ? { projectService: tsProjectService } : tsProject ? { project: tsProject } : {}),
tsconfigRootDir,
},
},
settings: {
...importPlugin.configs.typescript.settings,
Expand Down

0 comments on commit fe9abf9

Please sign in to comment.