diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c2202f14b55..64e2509f9f6 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4719,8 +4719,7 @@ namespace ts { } // Handle export default expressions if (isSourceFile(declaration)) { - Debug.assert(isJsonSourceFile(declaration)); - const jsonSourceFile = declaration; + const jsonSourceFile = cast(declaration, isJsonSourceFile); return links.type = jsonSourceFile.statements.length ? checkExpression(jsonSourceFile.statements[0].expression) : emptyObjectType; } if (declaration.kind === SyntaxKind.ExportAssignment) { diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 395291750ce..0f75d445c00 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -1076,6 +1076,8 @@ namespace ts { /** * Convert the json syntax tree into the json value and report errors + * This returns the json value (apart from checking errors) only if returnValue provided is true. + * Otherwise it just checks the errors and returns undefined */ /*@internal*/ export function convertToObjectWorker( diff --git a/src/compiler/program.ts b/src/compiler/program.ts index ed436718209..4f052581c20 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -2433,7 +2433,7 @@ namespace ts { switch (extension) { case Extension.Ts: case Extension.Dts: - case Extension.Json: + case Extension.Json: // Since module is resolved to json file only when --resolveJsonModule, we dont need further check // These are always allowed. return undefined; case Extension.Tsx: diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index f326840afa1..3f7affcc044 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -1076,26 +1076,20 @@ namespace ts { }); } - export function getTsConfigObjectLiteralExpression(tsConfigSourceFile: TsConfigSourceFile) { + export function getTsConfigObjectLiteralExpression(tsConfigSourceFile: TsConfigSourceFile | undefined) { if (tsConfigSourceFile && tsConfigSourceFile.statements.length) { const expression = tsConfigSourceFile.statements[0].expression; return isObjectLiteralExpression(expression) && expression; } } - export function getTsConfigPropArrayElementValue(tsConfigSourceFile: TsConfigSourceFile, propKey: string, elementValue: string): StringLiteral { + export function getTsConfigPropArrayElementValue(tsConfigSourceFile: TsConfigSourceFile | undefined, propKey: string, elementValue: string): StringLiteral | undefined { const jsonObjectLiteral = getTsConfigObjectLiteralExpression(tsConfigSourceFile); - if (jsonObjectLiteral) { - for (const property of getPropertyAssignment(jsonObjectLiteral, propKey)) { - if (isArrayLiteralExpression(property.initializer)) { - for (const element of property.initializer.elements) { - if (isStringLiteral(element) && element.text === elementValue) { - return element; - } - } - } - } - } + return jsonObjectLiteral && + firstDefined(getPropertyAssignment(jsonObjectLiteral, propKey), property => + isArrayLiteralExpression(property.initializer) ? + find(property.initializer.elements, (element): element is StringLiteral => isStringLiteral(element) && element.text === elementValue) : + undefined); } export function getContainingFunction(node: Node): SignatureDeclaration {