microsoft / microsoft/TypeScript

API for parsing *.json config files into a complete config objects

オープン
#44,573 コメント 5 件 リアクション 8 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Suggestion

Dedicated public beginner-friendly API (ideally, an asynchronous function on the ts namespace) for parsing tsconfig.json / jsconfig.json files into a complete config object.

🔍 Search Terms

parse JSON config tsconfig tsconfig.json jsconfig.json

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

The suggestion is to have a single function that in its simplest form (overload) would take a string, – path to the config file (either tsconfig.json or jsconfig.json, shouldn't matter), and return (either synchronously or asynchronously) the fully compiled complete configuration object with all the properties initialized (either explicitly from the provided config(s) or implicitly, from known defaults).

See Motivating Example below for why I can't use existing functions on ts namespace.

The suggestion is related to issue #44516 and this StackOverflow question.

📃 Motivating Example

I know that there are already a couple of functions on the imported ts namespace whose purpose is to read/parse/compile config, given its filepath/stringified contents/parsed contents. These include:

However, they either provide incomplete functionality (e.g., ts.readConfigFile is just a glorified JSON.parse, it doesn't – for example – crawl parent configs) or are ridiculously complicated to use (e.g., ts.parseConfigFileTextToJson require 3-9 arguments, the third of which is some kind of host object, that has to have a readDirectory method, that in turn requires 4-5 arguments, and has to actually do something, which I honestly don't know why I can't just use fs.readdirSync).

I need a function that takes a path to json and returns a complete config object.

💻 Use Cases

I'm thinking of a couple of variants of the function (for the sake of an example, it is called readConfig):

// given these
type Path = string | Buffer | URL;
interface Config {
  include?: string[], exclude?: string[], files?: string[], compilerOptions: { /*...*/ }, /* other properties of *.json config files */
}
/**
 * Find file by path, parse it, extend parent config objects, fill in missing defaults, return the resulting config object
 * If an error occurs, throw it
 */
/* 1 */ function readConfigSync(path: Path): Config;
/**
 * Find file by path, parse it, extend parent config objects, fill in missing defaults, invoke `callback(null, <resulting config object>)`
 * If an error occurs, invoke `callback(<error>)`
 */
/* 2 */ function readConfig(path: Path, callback: (...args: [ err: unknown ] | [ err: null, config: Config ]) => void): void;
/**
 * Find file by path, parse it, extend parent config objects, fill in missing defaults, return promise that resolves to the default config object
 * If an error occurs, return promise that rejects with the error
 */
/* 3 */ function readConfig(path: Path): Promise<Config>;
/**
 * Return the default config object
 * If an error occurs, throw it
 */
/* 4 */ function readConfigSync(): Config;
/**
 * Invoke `callback(null, <default config object>)`
 * If an error occurs, invoke `callback(<error>)`
 */
/* 5 */ function readConfig(callback: (...args: [ err: unknown ] | [ err: null, config: Config ]) => void): void;
/**
 * Return promise that resolves to the default config object
 * If an error occurs, return promise that rejects with the error
 */
/* 6 */ function readConfig(): Promise<Config>;

Also, shout out to functions that return { config?: Config; error?: unknown }, which, however, are not idiomatic to Node.JS.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

src/compiler/commandLineParser.ts と、issue で名前が挙げられている既存の readConfigFile、parseConfigFileTextToJson、readJsonConfigFile、parseJsonConfigFileContent API から始めてください。公開 API の形を決める前に、関連する issue #44516 と文書化されているユースケースを確認してください。初心者にも使いやすいエントリーポイントを提供し、継承された設定とデフォルト値を含む完全に解析済みの設定を返せるようになれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, typescript
領域
api, tooling
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。