From 4895d166450e2affed31281265a06947c1820d39 Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Sat, 28 Oct 2017 20:06:33 +0200 Subject: [PATCH] convertToObject: handle negative numbers in JSON Fixes: #19551 --- src/compiler/commandLineParser.ts | 7 +++++++ .../convertCompilerOptionsFromJson.ts | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index f050c4a5d16..b7003fbffd9 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -1138,6 +1138,13 @@ namespace ts { reportInvalidOptionValue(option && option.type !== "number"); return Number((valueExpression).text); + case SyntaxKind.PrefixUnaryExpression: + if ((valueExpression).operator !== SyntaxKind.MinusToken || (valueExpression).operand.kind !== SyntaxKind.NumericLiteral) { + break; // not valid JSON syntax + } + reportInvalidOptionValue(option && option.type !== "number"); + return -Number(((valueExpression).operand).text); + case SyntaxKind.ObjectLiteralExpression: reportInvalidOptionValue(option && option.type !== "object"); const objectLiteralExpression = valueExpression; diff --git a/src/harness/unittests/convertCompilerOptionsFromJson.ts b/src/harness/unittests/convertCompilerOptionsFromJson.ts index c55dee26dc4..09659762288 100644 --- a/src/harness/unittests/convertCompilerOptionsFromJson.ts +++ b/src/harness/unittests/convertCompilerOptionsFromJson.ts @@ -421,6 +421,24 @@ namespace ts { ); }); + it("Convert negative numbers in tsconfig.json ", () => { + assertCompilerOptions( + { + "compilerOptions": { + "allowJs": true, + "maxNodeModuleJsDepth": -1 + } + }, "tsconfig.json", + { + compilerOptions: { + allowJs: true, + maxNodeModuleJsDepth: -1 + }, + errors: [] + } + ); + }); + // jsconfig.json it("Convert correctly format jsconfig.json to compiler-options ", () => { assertCompilerOptions(