æ°ã«ãªã£ãã®ã§èª¿ã¹ã¦ã¿ã¾ããã
tsconfig.jsonã¨æ®éã®JSONã®å¤§ããªéã
tsconfig.jsonã«ã¯ãã³ã¡ã³ããæ¸ãã¾ãã
tsc --init
ããæã«çæãããtsconfig.jsonã«ã大éã«ã³ã¡ã³ããä»ãããã¦ããã®ã§ãããã«æ°ä»ããã¨ã¨æãã¾ãã
ä¾)
{ "compilerOptions": { "target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */, "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */, /* Strict Type-Checking Options */ "strict": true /* Enable all strict type-checking options. */, "noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */ } }
ããã«ã¤ãã¦æ·±ãèãããã¨ãç¡ããã©ããã§ãã㯠JSON5 ã ã¨è¨ãåãè¦ã¦ãã*1ã®ã§ããã ã¨æã£ã¦ãã¾ããã
ã¨ãããã試ãã¦ã¿ãã¨ããJSON5ãªã使ããã¯ãã®è±å¯ãªææ³ãå ¨ç¶ä½¿ããªãã£ãããJSONã¨JSON5ã«ããã¦ããformatOnSaveã®è¨å®ãå¹ããªãã£ãã®ã§ãéãã¨ãããã¨ã«æ°ä»ãã¾ããã
tsconfig.jsonã«Prettierãããããªã¼ã¨æã£ããã©ãããããtsconfig.jsonã¯JSONãããªãã£ãäºãæãåºããã
— syumai (@__syumai) 2020å¹´3æ29æ¥
tsconfig.jsonã®æ£ä½ã¯ä¸ä½ä½ãªã®ã
çµè«ããå ¥ãã¾ãããã©ããããã㯠JSON with Comments (JSONC) ã¨è¨ããã©ã¼ãããã®ããã§ãã
å°ãªãã¨ããVS Codeã¯tsconfig.jsonãJSON with Commentsã¨ãã¦è§£éãã¦ãã¾ãã
å³ä¸ã«æ³¨ç®ãã ããã
èªåçã«JSON with Commentsã®ç·¨éã¢ã¼ãã«ãªã£ã¦ãããã¨ã確èªåºæ¥ãã¨æãã¾ãã
JSON with Commentsã¨ã¯
VS Codeã®ããã¥ã¡ã³ãã§ã¯ãJSON with Commentsã«ã¤ãã¦ä¸è¨ã®ããã«èª¬æããã¦ãã¾ãã
JSON with Comments
In addition to the default JSON mode following the JSON specification, VS Code also has a JSON with Comments (jsonc) mode. This mode is used for the VS Code configuration files such as settings.json, tasks.json, or launch.json. When in the JSON with Comments mode, you can use single line (//) as well as block comments (/* */) as used in JavaScript.
https://code.visualstudio.com/docs/languages/json#_json-with-comments
ã©ããããVS Codeã®è¨å®ãã¡ã¤ã«ãè¨è¿°ããããã«ä½¿ããã¦ãããã©ã¼ãããã®ããã§ãã
VS Codeã¯ãå
é¨ã§ node-jsonc-parser
ã¨è¨ãpackageãå©ç¨ãã¦JSON with Commentsã解æãã¦ããããã§ããã
node-jsonc-parserã®ä½¿ãæ¹
node-jsonc-parserã¯ã parse
é¢æ°ãæä¾ãã¦ãããJSON with Commentsãä¸è¨ã®ããã«ç°¡åã«Objectã¨ãã¦èªã¿åããã¨ãåºæ¥ã¾ãã
import { parse } from "jsonc-parser"; import { promises as fsPromises } from "fs"; (async () => { const buf = await fsPromises.readFile("./data.jsonc"); const data = parse(buf.toString()); console.log(data); // => çµæã®Objectãåºåããã })();
parseé¢æ°ã¯ãParseOptionsãæä¾ãã¦ãããããã«ä¸è¨ã®ããã« disallowComments
ãå«ã¾ãã¦ãã¾ããã©ããããVS Codeã¯é常ã®JSONããã®parserã®Optionã使ãåã解æãã¦ããããã§ããã*2
/** * Parses the given text and returns the object the JSON content represents. On invalid input, the parser tries to be as fault tolerant as possible, but still return a result. * Therefore, always check the errors list to find out if the input was valid. */ export declare const parse: (text: string, errors?: ParseError[], options?: ParseOptions) => any;
export interface ParseOptions { disallowComments?: boolean; allowTrailingComma?: boolean; allowEmptyContent?: boolean; }
ããã« allowTrailingComma
ãå«ã¾ãã¦ãããã¨ããããããããã ããã¨æãã¾ããã ãªã㨠JSONC
ã§ã¯trailing commaã使ãã¾ãã ãããJSONã¨ã®å¤§ããªéãã§ããã
allowEmptyContent
ã®ä½¿ãæ¹ã«ã¤ãã¦ã¯èª¿ã¹ããã¦ãã¾ããã
TypeScriptæ¬ä½ã¯ã©ã®ããã«ãã¦tsconfig.jsonããã¼ã¹ãã¦ããã®ã
ã§ã¯ãTypeScriptæ¬ä½ããã® node-jsonc-parser
ãå©ç¨ãã¦ããã®ãã¨è¨ãã¨ããªãã¸ããªå
ã«ãã®packageã¸ã®ä¾åãè¦ä»ããããããã§ã¯ãªãããã§ããã
è¦ã¤ãã£ãã®ã¯ä¸è¨ã® parseJsonText
ã¨è¨ãé¢æ°ã§ãããã使ã£ã¦JSONã解æãã¦ããããã§ããã
createSourceFile
ã«æ¸¡ãã¦ãããªãã·ã§ã³ãè¦ãéããTypeScriptã¯JSONãES2015ã®JavaScriptã¨ãã¦è§£æãã¦ããããã«è¦ãã¾ããã
export function parseJsonText(fileName: string, sourceText: string, languageVersion: ScriptTarget = ScriptTarget.ES2015, syntaxCursor?: IncrementalParser.SyntaxCursor, setParentNodes?: boolean): JsonSourceFile { initializeState(sourceText, languageVersion, syntaxCursor, ScriptKind.JSON); // Set source file so that errors will be reported with this file name sourceFile = createSourceFile(fileName, ScriptTarget.ES2015, ScriptKind.JSON, /*isDeclaration*/ false); sourceFile.flags = contextFlags;
ããã«ç¶ãã®ã¯ãTypeScriptãJSONã¨ãã¦åãä»ãããããã¼ã¯ã³ãåå²ããswitchæã§ãåãä»ãã¦ããã®ã¯ é
åãbooleanãnullãNumber (æ£è² )ãæåå
ã§ããããã®ã©ãã«ãå½ã¦ã¯ã¾ããªãã£ããObjectã¨ãã¦è§£æãããã¨è¨ã風ã«è¦åãããã¾ããã
switch (token()) { case SyntaxKind.OpenBracketToken: statement.expression = parseArrayLiteralExpression(); break; case SyntaxKind.TrueKeyword: case SyntaxKind.FalseKeyword: case SyntaxKind.NullKeyword: statement.expression = parseTokenNode<BooleanLiteral | NullLiteral>(); break; case SyntaxKind.MinusToken: if (lookAhead(() => nextToken() === SyntaxKind.NumericLiteral && nextToken() !== SyntaxKind.ColonToken)) { statement.expression = parsePrefixUnaryExpression() as JsonMinusNumericLiteral; } else { statement.expression = parseObjectLiteralExpression(); } break; case SyntaxKind.NumericLiteral: case SyntaxKind.StringLiteral: if (lookAhead(() => nextToken() !== SyntaxKind.ColonToken)) { statement.expression = parseLiteralNode() as StringLiteral | NumericLiteral; break; } // falls through default: statement.expression = parseObjectLiteralExpression(); break; }
ããããæ³åããã®ã¯ã TypeScriptã¯ES2015ã®Objectã®ä¸é¨ææ³ãå¶éãã¦JSONCã表ç¾ãã¦ãã ã¨ãããã®ã§ãããããã¯ãã³ã¡ã³ãã¨trailing comma以å¤ã®è¦ç´ ã¯å«ãããã¨ãåºæ¥ãã追ããããããªãã£ãã®ã§æ念ãã¾ããã
誰ãç¥ã£ã¦ããæãã¦ãã ããã
調ã¹ãæããtsconfig.jsonã¨ç´ä»ããå½¢ã§JSON with Commentsã«è§¦ãã¦ããæ å ±ã½ã¼ã¹ã¯ããªãå°ãªããTypeScriptæ¬ä½ãç¹ã«è¨åãã¦ããªãã®ã§æ å ±åéãã¨ã¦ãå°é£ã§ããã
JSON with Commentsã¨è¨ãè¦æ ¼ã«ã¤ãã¦
ãã®è¦æ ¼ã¯ãã©ããæ¨æºåããã¦ããªãæ°ããã¾ãã
JSONCã§æ¤ç´¢ãã¦è¦ã¤ããã®ã¯ãGoã§å®è£ ãããéè¯Parserã®ãããªãã®ã°ããã§ããã
node-jsonc-parserã代表çãªå®è£ ãªã®ããªã¨æã£ã¦ãã¾ãã
è¦ã¤ãã£ãParserã¨ã
- Goã®JSONCãã¼ãµã¼ GitHub - muhammadmuzzammil1998/jsonc: JSON with comments for Go!
- 3å¹´åã¨ãã«ä½ãããGoã®JSONCãã¼ãµã¼ GitHub - komkom/jsonc: json config.
- Rust製ã®Code formatterã®JSONCãã©ã°ã¤ã³(ããã¯TSã§æ¸ããã¦ãã£ã½ã) dprint/packages/dprint-plugin-jsonc at master · dsherret/dprint · GitHub
ä¸è¨ã®è¨äºãè¦ãéããWindows Terminalã®è¨å®ãã¡ã¤ã«ã¨ãã§ã使ããã¦ããããã§ãã
tsconfig.jsonããVS Codeã®settings.jsonãªã©ãããã¾ã§åºã使ããã¦ãããã®ã®ãã©ã¼ãããããããã¯ãã¨ãã¦ããããã«æããããã®ã¯ãä½ã ãé¢ç½ããªãã¨æã£ã話ã§ããã
*1:.babelrcã¯JSON5ããã https://babeljs.io/docs/en/config-files#supported-file-extensions
*2:VS Codeã®ãªãã¸ããªå ã«ãJSON Language Serverãjsonc-parserãå©ç¨ãã¦ãããã¨ã«ã¤ãã¦è¨è¿°ãããã¾ã https://github.com/microsoft/vscode/blob/1a55cd072acf651a321cdd7d94d324c186eb5af7/extensions/json-language-features/server/README.md#participate