diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 4f2fb6129c3e7..d1c782d6155fa 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -281,6 +281,10 @@ namespace ts { return InternalSymbolName.Index; case SyntaxKind.ExportDeclaration: return InternalSymbolName.ExportStar; + case SyntaxKind.SourceFile: + // json file should behave as + // module.exports = ... + return InternalSymbolName.ExportEquals; case SyntaxKind.BinaryExpression: if (getSpecialPropertyAssignmentKind(node as BinaryExpression) === SpecialPropertyAssignmentKind.ModuleExports) { // module.exports = ... @@ -2234,6 +2238,13 @@ namespace ts { if (isExternalModule(file)) { bindSourceFileAsExternalModule(); } + else if (isJsonSourceFile(file)) { + bindSourceFileAsExternalModule(); + // Create symbol equivalent for the module.exports = {} + const originalSymbol = file.symbol; + declareSymbol(file.symbol.exports, file.symbol, file, SymbolFlags.Property, SymbolFlags.All); + file.symbol = originalSymbol; + } } function bindSourceFileAsExternalModule() { diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 62fc1a7cc418f..64e2509f9f6d0 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -2207,7 +2207,7 @@ namespace ts { } // May be an untyped module. If so, ignore resolutionDiagnostic. - if (resolvedModule && !extensionIsTypeScript(resolvedModule.extension) && resolutionDiagnostic === undefined || resolutionDiagnostic === Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type) { + if (resolvedModule && !resolutionExtensionIsTypeScriptOrJson(resolvedModule.extension) && resolutionDiagnostic === undefined || resolutionDiagnostic === Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type) { if (isForAugmentation) { const diag = Diagnostics.Invalid_module_name_in_augmentation_Module_0_resolves_to_an_untyped_module_at_1_which_cannot_be_augmented; error(errorNode, diag, moduleReference, resolvedModule.resolvedFileName); @@ -4718,6 +4718,10 @@ namespace ts { return links.type = anyType; } // Handle export default expressions + if (isSourceFile(declaration)) { + const jsonSourceFile = cast(declaration, isJsonSourceFile); + return links.type = jsonSourceFile.statements.length ? checkExpression(jsonSourceFile.statements[0].expression) : emptyObjectType; + } if (declaration.kind === SyntaxKind.ExportAssignment) { return links.type = checkExpression((declaration).expression); } @@ -15597,7 +15601,7 @@ namespace ts { const contextualType = getApparentTypeOfContextualType(node); const contextualTypeHasPattern = contextualType && contextualType.pattern && (contextualType.pattern.kind === SyntaxKind.ObjectBindingPattern || contextualType.pattern.kind === SyntaxKind.ObjectLiteralExpression); - const isInJSFile = isInJavaScriptFile(node); + const isInJSFile = isInJavaScriptFile(node) && !isInJsonFile(node); const isJSObjectLiteral = !contextualType && isInJSFile; let typeFlags: TypeFlags = 0; let patternWithComputedProperties = false; diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index fc86c63a5c649..9710b0c12621e 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -501,6 +501,12 @@ namespace ts { category: Diagnostics.Advanced_Options, description: Diagnostics.Enable_tracing_of_the_name_resolution_process }, + { + name: "resolveJsonModule", + type: "boolean", + category: Diagnostics.Advanced_Options, + description: Diagnostics.Include_modules_imported_with_json_extension + }, { name: "listFiles", type: "boolean", @@ -953,9 +959,9 @@ namespace ts { * Read tsconfig.json file * @param fileName The path to the config file */ - export function readJsonConfigFile(fileName: string, readFile: (path: string) => string | undefined): JsonSourceFile { + export function readJsonConfigFile(fileName: string, readFile: (path: string) => string | undefined): TsConfigSourceFile { const textOrDiagnostic = tryReadFile(fileName, readFile); - return isString(textOrDiagnostic) ? parseJsonText(fileName, textOrDiagnostic) : { parseDiagnostics: [textOrDiagnostic] }; + return isString(textOrDiagnostic) ? parseJsonText(fileName, textOrDiagnostic) : { parseDiagnostics: [textOrDiagnostic] }; } function tryReadFile(fileName: string, readFile: (path: string) => string | undefined): string | Diagnostic { @@ -973,58 +979,62 @@ namespace ts { return arrayToMap(options, option => option.name); } - let _tsconfigRootOptions: Map; + let _tsconfigRootOptions: TsConfigOnlyOption; function getTsconfigRootOptionsMap() { if (_tsconfigRootOptions === undefined) { - _tsconfigRootOptions = commandLineOptionsToMap([ - { - name: "compilerOptions", - type: "object", - elementOptions: commandLineOptionsToMap(optionDeclarations), - extraKeyDiagnosticMessage: Diagnostics.Unknown_compiler_option_0 - }, - { - name: "typingOptions", - type: "object", - elementOptions: commandLineOptionsToMap(typeAcquisitionDeclarations), - extraKeyDiagnosticMessage: Diagnostics.Unknown_type_acquisition_option_0 - }, - { - name: "typeAcquisition", - type: "object", - elementOptions: commandLineOptionsToMap(typeAcquisitionDeclarations), - extraKeyDiagnosticMessage: Diagnostics.Unknown_type_acquisition_option_0 - }, - { - name: "extends", - type: "string" - }, - { - name: "files", - type: "list", - element: { - name: "files", + _tsconfigRootOptions = { + name: undefined, // should never be needed since this is root + type: "object", + elementOptions: commandLineOptionsToMap([ + { + name: "compilerOptions", + type: "object", + elementOptions: commandLineOptionsToMap(optionDeclarations), + extraKeyDiagnosticMessage: Diagnostics.Unknown_compiler_option_0 + }, + { + name: "typingOptions", + type: "object", + elementOptions: commandLineOptionsToMap(typeAcquisitionDeclarations), + extraKeyDiagnosticMessage: Diagnostics.Unknown_type_acquisition_option_0 + }, + { + name: "typeAcquisition", + type: "object", + elementOptions: commandLineOptionsToMap(typeAcquisitionDeclarations), + extraKeyDiagnosticMessage: Diagnostics.Unknown_type_acquisition_option_0 + }, + { + name: "extends", type: "string" - } - }, - { - name: "include", - type: "list", - element: { + }, + { + name: "files", + type: "list", + element: { + name: "files", + type: "string" + } + }, + { name: "include", - type: "string" - } - }, - { - name: "exclude", - type: "list", - element: { + type: "list", + element: { + name: "include", + type: "string" + } + }, + { name: "exclude", - type: "string" - } - }, - compileOnSaveCommandLineOption - ]); + type: "list", + element: { + name: "exclude", + type: "string" + } + }, + compileOnSaveCommandLineOption + ]) + }; } return _tsconfigRootOptions; } @@ -1061,23 +1071,30 @@ namespace ts { * Convert the json syntax tree into the json value */ export function convertToObject(sourceFile: JsonSourceFile, errors: Push): any { - return convertToObjectWorker(sourceFile, errors, /*knownRootOptions*/ undefined, /*jsonConversionNotifier*/ undefined); + return convertToObjectWorker(sourceFile, errors, /*returnValue*/ true, /*knownRootOptions*/ undefined, /*jsonConversionNotifier*/ undefined); } /** - * Convert the json syntax tree into the json value + * 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 */ - function convertToObjectWorker( + /*@internal*/ + export function convertToObjectWorker( sourceFile: JsonSourceFile, errors: Push, - knownRootOptions: Map | undefined, + returnValue: boolean, + knownRootOptions: CommandLineOption | undefined, jsonConversionNotifier: JsonConversionNotifier | undefined): any { - if (!sourceFile.jsonObject) { - return {}; + if (!sourceFile.statements.length) { + return returnValue ? {} : undefined; } - return convertObjectLiteralExpressionToJson(sourceFile.jsonObject, knownRootOptions, - /*extraKeyDiagnosticMessage*/ undefined, /*parentOption*/ undefined); + return convertPropertyValueToJson(sourceFile.statements[0].expression, knownRootOptions); + + function isRootOptionMap(knownOptions: Map | undefined) { + return knownRootOptions && (knownRootOptions as TsConfigOnlyOption).elementOptions === knownOptions; + } function convertObjectLiteralExpressionToJson( node: ObjectLiteralExpression, @@ -1085,7 +1102,7 @@ namespace ts { extraKeyDiagnosticMessage: DiagnosticMessage | undefined, parentOption: string | undefined ): any { - const result: any = {}; + const result: any = returnValue ? {} : undefined; for (const element of node.properties) { if (element.kind !== SyntaxKind.PropertyAssignment) { errors.push(createDiagnosticForNodeInSourceFile(sourceFile, element, Diagnostics.Property_assignment_expected)); @@ -1106,11 +1123,13 @@ namespace ts { } const value = convertPropertyValueToJson(element.initializer, option); if (typeof keyText !== "undefined") { - result[keyText] = value; + if (returnValue) { + result[keyText] = value; + } // Notify key value set, if user asked for it if (jsonConversionNotifier && // Current callbacks are only on known parent option or if we are setting values in the root - (parentOption || knownOptions === knownRootOptions)) { + (parentOption || isRootOptionMap(knownOptions))) { const isValidOptionValue = isCompilerOptionsValue(option, value); if (parentOption) { if (isValidOptionValue) { @@ -1118,7 +1137,7 @@ namespace ts { jsonConversionNotifier.onSetValidOptionKeyValueInParent(parentOption, option, value); } } - else if (knownOptions === knownRootOptions) { + else if (isRootOptionMap(knownOptions)) { if (isValidOptionValue) { // Notify about the valid root key value being set jsonConversionNotifier.onSetValidOptionKeyValueInRoot(keyText, element.name, value, element.initializer); @@ -1137,8 +1156,8 @@ namespace ts { function convertArrayLiteralExpressionToJson( elements: NodeArray, elementOption: CommandLineOption | undefined - ): any[] { - return elements.map(element => convertPropertyValueToJson(element, elementOption)); + ): any[] | void { + return (returnValue ? elements.map : elements.forEach).call(elements, (element: Expression) => convertPropertyValueToJson(element, elementOption)); } function convertPropertyValueToJson(valueExpression: Expression, option: CommandLineOption): any { @@ -1433,12 +1452,12 @@ namespace ts { * @param basePath A root directory to resolve relative path entries in the config * file to. e.g. outDir */ - export function parseJsonSourceFileConfigFileContent(sourceFile: JsonSourceFile, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray): ParsedCommandLine { + export function parseJsonSourceFileConfigFileContent(sourceFile: TsConfigSourceFile, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray): ParsedCommandLine { return parseJsonConfigFileContentWorker(/*json*/ undefined, sourceFile, host, basePath, existingOptions, configFileName, resolutionStack, extraFileExtensions); } /*@internal*/ - export function setConfigFileInOptions(options: CompilerOptions, configFile: JsonSourceFile) { + export function setConfigFileInOptions(options: CompilerOptions, configFile: TsConfigSourceFile) { if (configFile) { Object.defineProperty(options, "configFile", { enumerable: false, writable: false, value: configFile }); } @@ -1466,7 +1485,7 @@ namespace ts { */ function parseJsonConfigFileContentWorker( json: any, - sourceFile: JsonSourceFile, + sourceFile: TsConfigSourceFile, host: ParseConfigHost, basePath: string, existingOptions: CompilerOptions = {}, @@ -1587,7 +1606,7 @@ namespace ts { */ function parseConfig( json: any, - sourceFile: JsonSourceFile, + sourceFile: TsConfigSourceFile, host: ParseConfigHost, basePath: string, configFileName: string, @@ -1664,7 +1683,7 @@ namespace ts { } function parseOwnConfigOfJsonSourceFile( - sourceFile: JsonSourceFile, + sourceFile: TsConfigSourceFile, host: ParseConfigHost, basePath: string, configFileName: string | undefined, @@ -1711,7 +1730,7 @@ namespace ts { } } }; - const json = convertToObjectWorker(sourceFile, errors, getTsconfigRootOptionsMap(), optionsIterator); + const json = convertToObjectWorker(sourceFile, errors, /*returnValue*/ true, getTsconfigRootOptionsMap(), optionsIterator); if (!typeAcquisition) { if (typingOptionstypeAcquisition) { typeAcquisition = (typingOptionstypeAcquisition.enableAutoDiscovery !== undefined) ? @@ -1754,7 +1773,7 @@ namespace ts { } function getExtendedConfig( - sourceFile: JsonSourceFile, + sourceFile: TsConfigSourceFile, extendedConfigPath: string, host: ParseConfigHost, basePath: string, @@ -2006,7 +2025,7 @@ namespace ts { host: ParseConfigHost, errors: Push, extraFileExtensions: ReadonlyArray, - jsonSourceFile: JsonSourceFile + jsonSourceFile: TsConfigSourceFile ): ExpandResult { basePath = normalizePath(basePath); let validatedIncludeSpecs: ReadonlyArray, validatedExcludeSpecs: ReadonlyArray; @@ -2107,7 +2126,7 @@ namespace ts { }; } - function validateSpecs(specs: ReadonlyArray, errors: Push, allowTrailingRecursion: boolean, jsonSourceFile: JsonSourceFile, specKey: string): ReadonlyArray { + function validateSpecs(specs: ReadonlyArray, errors: Push, allowTrailingRecursion: boolean, jsonSourceFile: TsConfigSourceFile, specKey: string): ReadonlyArray { return specs.filter(spec => { const diag = specToDiagnostic(spec, allowTrailingRecursion); if (diag !== undefined) { @@ -2117,18 +2136,10 @@ namespace ts { }); function createDiagnostic(message: DiagnosticMessage, spec: string): Diagnostic { - if (jsonSourceFile && jsonSourceFile.jsonObject) { - for (const property of getPropertyAssignment(jsonSourceFile.jsonObject, specKey)) { - if (isArrayLiteralExpression(property.initializer)) { - for (const element of property.initializer.elements) { - if (isStringLiteral(element) && element.text === spec) { - return createDiagnosticForNodeInSourceFile(jsonSourceFile, element, message, spec); - } - } - } - } - } - return createCompilerDiagnostic(message, spec); + const element = getTsConfigPropArrayElementValue(jsonSourceFile, specKey, spec); + return element ? + createDiagnosticForNodeInSourceFile(jsonSourceFile, element, message, spec) : + createCompilerDiagnostic(message, spec); } } diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 72b45385c9370..c5e9a75b4ee07 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -2966,7 +2966,7 @@ namespace ts { } } - const extensionsToRemove = [Extension.Dts, Extension.Ts, Extension.Js, Extension.Tsx, Extension.Jsx]; + const extensionsToRemove = [Extension.Dts, Extension.Ts, Extension.Js, Extension.Tsx, Extension.Jsx, Extension.Json]; export function removeFileExtension(path: string): string { for (const ext of extensionsToRemove) { const extensionless = tryRemoveExtension(path, ext); @@ -3307,6 +3307,10 @@ namespace ts { return ext === Extension.Ts || ext === Extension.Tsx || ext === Extension.Dts; } + export function resolutionExtensionIsTypeScriptOrJson(ext: Extension) { + return extensionIsTypeScript(ext) || ext === Extension.Json; + } + /** * Gets the extension from a path. * Path must have a valid extension. diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index ec4cbc25c4fc9..c12e0a9931b9b 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2806,6 +2806,10 @@ "category": "Error", "code": 5069 }, + "Option '--resolveJsonModule' cannot be specified without 'node' module resolution strategy.": { + "category": "Error", + "code": 5070 + }, "Generates a sourcemap for each corresponding '.d.ts' file.": { "category": "Message", @@ -3543,6 +3547,11 @@ "code": 6196, "reportsUnnecessary": true }, + "Include modules imported with '.json' extension": { + "category": "Message", + "code": 6197 + }, + "Variable '{0}' implicitly has an '{1}' type.": { "category": "Error", "code": 7005 diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index ed61d20610c1b..dfc6549ee066f 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -67,6 +67,10 @@ namespace ts { // For TypeScript, the only time to emit with a '.jsx' extension, is on JSX input, and JsxEmit.Preserve /* @internal */ export function getOutputExtension(sourceFile: SourceFile, options: CompilerOptions): Extension { + if (isJsonSourceFile(sourceFile)) { + return Extension.Json; + } + if (options.jsx === JsxEmit.Preserve) { if (isSourceFileJavaScript(sourceFile)) { if (fileExtensionIs(sourceFile.fileName, Extension.Jsx)) { @@ -1676,7 +1680,9 @@ namespace ts { function emitExpressionStatement(node: ExpressionStatement) { emitExpression(node.expression); - writeSemicolon(); + if (!isJsonSourceFile(currentSourceFile)) { + writeSemicolon(); + } } function emitIfStatement(node: IfStatement) { diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index 8a7ddf0bc52bd..735de930ae9c0 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -24,10 +24,8 @@ namespace ts { if (!elements || elements === emptyArray) { elements = []; } - else { - if (isNodeArray(elements)) { - return elements; - } + else if (isNodeArray(elements)) { + return elements; } const array = >elements; diff --git a/src/compiler/moduleNameResolver.ts b/src/compiler/moduleNameResolver.ts index 9ede13888e8b7..ca282da18b8d6 100644 --- a/src/compiler/moduleNameResolver.ts +++ b/src/compiler/moduleNameResolver.ts @@ -45,6 +45,7 @@ namespace ts { enum Extensions { TypeScript, /** '.ts', '.tsx', or '.d.ts' */ JavaScript, /** '.js' or '.jsx' */ + Json, /** '.json' */ DtsOnly /** Only '.d.ts' */ } @@ -746,7 +747,11 @@ namespace ts { const failedLookupLocations: string[] = []; const state: ModuleResolutionState = { compilerOptions, host, traceEnabled }; - const result = jsOnly ? tryResolve(Extensions.JavaScript) : (tryResolve(Extensions.TypeScript) || tryResolve(Extensions.JavaScript)); + const result = jsOnly ? + tryResolve(Extensions.JavaScript) : + (tryResolve(Extensions.TypeScript) || + tryResolve(Extensions.JavaScript) || + (compilerOptions.resolveJsonModule ? tryResolve(Extensions.Json) : undefined)); if (result && result.value) { const { resolved, originalPath, isExternalLibraryImport } = result.value; return createResolvedModuleWithFailedLookupLocations(resolved, originalPath, isExternalLibraryImport, failedLookupLocations); @@ -898,6 +903,11 @@ namespace ts { * in cases when we know upfront that all load attempts will fail (because containing folder does not exists) however we still need to record all failed lookup locations. */ function loadModuleFromFile(extensions: Extensions, candidate: string, failedLookupLocations: Push, onlyRecordFailures: boolean, state: ModuleResolutionState): PathAndExtension | undefined { + if (extensions === Extensions.Json) { + const extensionLess = tryRemoveExtension(candidate, Extension.Json); + return extensionLess && tryAddingExtensions(extensionLess, extensions, failedLookupLocations, onlyRecordFailures, state); + } + // First, try adding an extension. An import of "foo" could be matched by a file "foo.ts", or "foo.js" by "foo.js.ts" const resolvedByAddingExtension = tryAddingExtensions(candidate, extensions, failedLookupLocations, onlyRecordFailures, state); if (resolvedByAddingExtension) { @@ -933,6 +943,8 @@ namespace ts { return tryExtension(Extension.Ts) || tryExtension(Extension.Tsx) || tryExtension(Extension.Dts); case Extensions.JavaScript: return tryExtension(Extension.Js) || tryExtension(Extension.Jsx); + case Extensions.Json: + return tryExtension(Extension.Json); } function tryExtension(ext: Extension): PathAndExtension | undefined { @@ -1030,7 +1042,7 @@ namespace ts { } function loadModuleFromPackageJson(jsonContent: PackageJsonPathFields, extensions: Extensions, candidate: string, failedLookupLocations: Push, state: ModuleResolutionState): PathAndExtension | undefined { - const file = tryReadPackageJsonFields(extensions !== Extensions.JavaScript, jsonContent, candidate, state); + const file = tryReadPackageJsonFields(extensions !== Extensions.JavaScript && extensions !== Extensions.Json, jsonContent, candidate, state); if (!file) { return undefined; } @@ -1069,6 +1081,8 @@ namespace ts { switch (extensions) { case Extensions.JavaScript: return extension === Extension.Js || extension === Extension.Jsx; + case Extensions.Json: + return extension === Extension.Json; case Extensions.TypeScript: return extension === Extension.Ts || extension === Extension.Tsx || extension === Extension.Dts; case Extensions.DtsOnly: @@ -1144,7 +1158,7 @@ namespace ts { if (packageResult) { return packageResult; } - if (extensions !== Extensions.JavaScript) { + if (extensions !== Extensions.JavaScript && extensions !== Extensions.Json) { const nodeModulesAtTypes = combinePaths(nodeModulesFolder, "@types"); let nodeModulesAtTypesExists = nodeModulesFolderExists; if (nodeModulesFolderExists && !directoryProbablyExists(nodeModulesAtTypes, state.host)) { diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 32e7ca19dd313..b84c8ca004f04 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -670,6 +670,13 @@ namespace ts { export function parseSourceFile(fileName: string, sourceText: string, languageVersion: ScriptTarget, syntaxCursor: IncrementalParser.SyntaxCursor, setParentNodes?: boolean, scriptKind?: ScriptKind): SourceFile { scriptKind = ensureScriptKind(fileName, scriptKind); + if (scriptKind === ScriptKind.JSON) { + const result = parseJsonText(fileName, sourceText, languageVersion, syntaxCursor, setParentNodes); + convertToObjectWorker(result, result.parseDiagnostics, /*returnValue*/ false, /*knownRootOptions*/ undefined, /*jsonConversionNotifier*/ undefined); + result.typeReferenceDirectives = emptyArray; + result.amdDependencies = emptyArray; + return result; + } initializeState(sourceText, languageVersion, syntaxCursor, scriptKind); @@ -691,27 +698,59 @@ namespace ts { return isInvalid ? entityName : undefined; } - export function parseJsonText(fileName: string, sourceText: string): JsonSourceFile { - initializeState(sourceText, ScriptTarget.ES2015, /*syntaxCursor*/ undefined, ScriptKind.JSON); + 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); - const result = sourceFile; // Prime the scanner. nextToken(); + const pos = getNodePos(); if (token() === SyntaxKind.EndOfFileToken) { + sourceFile.statements = createNodeArray([], pos, pos); sourceFile.endOfFileToken = parseTokenNode(); } - else if (token() === SyntaxKind.OpenBraceToken || - lookAhead(() => token() === SyntaxKind.StringLiteral)) { - result.jsonObject = parseObjectLiteralExpression(); + else { + const statement = createNode(SyntaxKind.ExpressionStatement) as JsonObjectExpressionStatement; + switch (token()) { + case SyntaxKind.OpenBracketToken: + statement.expression = parseArrayLiteralExpression(); + break; + case SyntaxKind.TrueKeyword: + case SyntaxKind.FalseKeyword: + case SyntaxKind.NullKeyword: + statement.expression = parseTokenNode(); + 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; + } + finishNode(statement); + sourceFile.statements = createNodeArray([statement], pos); sourceFile.endOfFileToken = parseExpectedToken(SyntaxKind.EndOfFileToken, Diagnostics.Unexpected_token); } - else { - parseExpected(SyntaxKind.OpenBraceToken); + + if (setParentNodes) { + fixupParentReferences(sourceFile); } sourceFile.parseDiagnostics = parseDiagnostics; + const result = sourceFile as JsonSourceFile; clearState(); return result; } @@ -739,9 +778,11 @@ namespace ts { switch (scriptKind) { case ScriptKind.JS: case ScriptKind.JSX: - case ScriptKind.JSON: contextFlags = NodeFlags.JavaScriptFile; break; + case ScriptKind.JSON: + contextFlags = NodeFlags.JavaScriptFile | NodeFlags.JsonFile; + break; default: contextFlags = NodeFlags.None; break; diff --git a/src/compiler/program.ts b/src/compiler/program.ts old mode 100755 new mode 100644 index db630d8ca22cf..4f052581c2028 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1988,7 +1988,7 @@ namespace ts { } const isFromNodeModulesSearch = resolution.isExternalLibraryImport; - const isJsFile = !extensionIsTypeScript(resolution.extension); + const isJsFile = !resolutionExtensionIsTypeScriptOrJson(resolution.extension); const isJsFileFromNodeModules = isFromNodeModulesSearch && isJsFile; const resolvedFileName = resolution.resolvedFileName; @@ -2200,6 +2200,12 @@ namespace ts { } } + if (options.resolveJsonModule) { + if (getEmitModuleResolutionKind(options) !== ModuleResolutionKind.NodeJs) { + createDiagnosticForOptionName(Diagnostics.Option_resolveJsonModule_cannot_be_specified_without_node_module_resolution_strategy, "resolveJsonModule"); + } + } + // there has to be common source directory if user specified --outdir || --sourceRoot // if user specified --mapRoot, there needs to be common source directory if there would be multiple files being emitted if (options.outDir || // there is --outDir specified @@ -2355,8 +2361,9 @@ namespace ts { function getCompilerOptionsObjectLiteralSyntax() { if (_compilerOptionsObjectLiteralSyntax === undefined) { _compilerOptionsObjectLiteralSyntax = null; // tslint:disable-line:no-null-keyword - if (options.configFile && options.configFile.jsonObject) { - for (const prop of getPropertyAssignment(options.configFile.jsonObject, "compilerOptions")) { + const jsonObjectLiteral = getTsConfigObjectLiteralExpression(options.configFile); + if (jsonObjectLiteral) { + for (const prop of getPropertyAssignment(jsonObjectLiteral, "compilerOptions")) { if (isObjectLiteralExpression(prop.initializer)) { _compilerOptionsObjectLiteralSyntax = prop.initializer; break; @@ -2426,6 +2433,7 @@ namespace ts { switch (extension) { case Extension.Ts: case Extension.Dts: + 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/types.ts b/src/compiler/types.ts index d59ea7dfbfd3b..4f48fe96a8727 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -505,6 +505,7 @@ namespace ts { JSDoc = 1 << 21, // If node was parsed inside jsdoc /* @internal */ Ambient = 1 << 22, // If node was inside an ambient context -- a declaration file, or inside something with the `declare` modifier. /* @internal */ InWithStatement = 1 << 23, // If any ancestor of node was the `statement` of a WithStatement (not the `expression`) + JsonFile = 1 << 24, // If node was parsed in a Json BlockScoped = Let | Const, @@ -2620,10 +2621,23 @@ namespace ts { } export interface JsonSourceFile extends SourceFile { - jsonObject?: ObjectLiteralExpression; + statements: NodeArray; + } + + export interface TsConfigSourceFile extends JsonSourceFile { extendedSourceFiles?: string[]; } + export interface JsonMinusNumericLiteral extends PrefixUnaryExpression { + kind: SyntaxKind.PrefixUnaryExpression; + operator: SyntaxKind.MinusToken; + operand: NumericLiteral; + } + + export interface JsonObjectExpressionStatement extends ExpressionStatement { + expression: ObjectLiteralExpression | ArrayLiteralExpression | JsonMinusNumericLiteral | NumericLiteral | StringLiteral | BooleanLiteral | NullLiteral; + } + export interface ScriptReferenceHost { getCompilerOptions(): CompilerOptions; getSourceFile(fileName: string): SourceFile | undefined; @@ -4176,7 +4190,7 @@ namespace ts { checkJs?: boolean; /* @internal */ configFilePath?: string; /** configFile is set as non enumerable property so as to avoid checking of json source files */ - /* @internal */ readonly configFile?: JsonSourceFile; + /* @internal */ readonly configFile?: TsConfigSourceFile; declaration?: boolean; declarationMap?: boolean; emitDeclarationOnly?: boolean; @@ -4250,6 +4264,7 @@ namespace ts { /* @internal */ suppressOutputPathCheck?: boolean; target?: ScriptTarget; traceResolution?: boolean; + resolveJsonModule?: boolean; types?: string[]; /** Paths used to compute primary types search locations */ typeRoots?: string[]; @@ -4257,7 +4272,7 @@ namespace ts { /*@internal*/ watch?: boolean; esModuleInterop?: boolean; - [option: string]: CompilerOptionsValue | JsonSourceFile | undefined; + [option: string]: CompilerOptionsValue | TsConfigSourceFile | undefined; } export interface TypeAcquisition { diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index ae6bfb5ac135e..3f7affcc04444 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -724,6 +724,11 @@ namespace ts { return (file.externalModuleIndicator || file.commonJsModuleIndicator) !== undefined; } + + export function isJsonSourceFile(file: SourceFile): file is JsonSourceFile { + return file.scriptKind === ScriptKind.JSON; + } + export function isConstEnumDeclaration(node: Node): boolean { return node.kind === SyntaxKind.EnumDeclaration && isConst(node); } @@ -1071,6 +1076,22 @@ namespace ts { }); } + 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 | undefined, propKey: string, elementValue: string): StringLiteral | undefined { + const jsonObjectLiteral = getTsConfigObjectLiteralExpression(tsConfigSourceFile); + 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 { return findAncestor(node.parent, isFunctionLike); } @@ -1467,6 +1488,10 @@ namespace ts { return node && !!(node.flags & NodeFlags.JavaScriptFile); } + export function isInJsonFile(node: Node | undefined): boolean { + return node && !!(node.flags & NodeFlags.JsonFile); + } + export function isInJSDoc(node: Node | undefined): boolean { return node && !!(node.flags & NodeFlags.JSDoc); } diff --git a/src/harness/compiler.ts b/src/harness/compiler.ts index 8308074c44466..59d636303f9c0 100644 --- a/src/harness/compiler.ts +++ b/src/harness/compiler.ts @@ -70,7 +70,7 @@ namespace compiler { const dts = this.dts = new collections.SortedMap({ comparer: this.vfs.stringComparer, sort: "insertion" }); const maps = this.maps = new collections.SortedMap({ comparer: this.vfs.stringComparer, sort: "insertion" }); for (const document of this.host.outputs) { - if (vpath.isJavaScript(document.file)) { + if (vpath.isJavaScript(document.file) || ts.fileExtensionIs(document.file, ts.Extension.Json)) { js.set(document.file, document); } else if (vpath.isDeclaration(document.file)) { @@ -245,4 +245,4 @@ namespace compiler { const errors = ts.getPreEmitDiagnostics(program); return new CompilationResult(host, compilerOptions, program, emitResult, errors); } -} \ No newline at end of file +} diff --git a/src/harness/harness.ts b/src/harness/harness.ts index 53ca42d2d70a1..d73bfd2ed63f1 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -1217,7 +1217,7 @@ namespace Harness { } const useCaseSensitiveFileNames = options.useCaseSensitiveFileNames !== undefined ? options.useCaseSensitiveFileNames : true; - const programFileNames = inputFiles.map(file => file.unitName); + const programFileNames = inputFiles.map(file => file.unitName).filter(fileName => !ts.fileExtensionIs(fileName, ts.Extension.Json)); // Files from built\local that are requested by test "@includeBuiltFiles" to be in the context. // Treat them as library files, so include them in build, but not in baselines. diff --git a/src/harness/unittests/tsconfigParsing.ts b/src/harness/unittests/tsconfigParsing.ts index 500204fbde84b..f1b4d5265816b 100644 --- a/src/harness/unittests/tsconfigParsing.ts +++ b/src/harness/unittests/tsconfigParsing.ts @@ -10,12 +10,6 @@ namespace ts { assert.equal(JSON.stringify(parsed), JSON.stringify(expectedConfigObject)); } - function assertParseError(jsonText: string) { - const parsed = parseConfigFileTextToJson("/apath/tsconfig.json", jsonText); - assert.deepEqual(parsed.config, {}); - assert.isTrue(undefined !== parsed.error); - } - function assertParseErrorWithExcludesKeyword(jsonText: string) { { const parsed = parseConfigFileTextToJson("/apath/tsconfig.json", jsonText); @@ -138,7 +132,14 @@ namespace ts { }); it("returns object with error when json is invalid", () => { - assertParseError("invalid"); + const parsed = parseConfigFileTextToJson("/apath/tsconfig.json", "invalid"); + assert.deepEqual(parsed.config, { invalid: undefined }); + const expected = createCompilerDiagnostic(Diagnostics._0_expected, "{"); + assert.equal(parsed.error.messageText, expected.messageText); + assert.equal(parsed.error.category, expected.category); + assert.equal(parsed.error.code, expected.code); + assert.equal(parsed.error.start, 0); + assert.equal(parsed.error.length, "invalid".length); }); it("returns object when users correctly specify library", () => { diff --git a/src/server/protocol.ts b/src/server/protocol.ts index b933d88af7e63..819a4823d08b1 100644 --- a/src/server/protocol.ts +++ b/src/server/protocol.ts @@ -2741,6 +2741,7 @@ namespace ts.server.protocol { suppressImplicitAnyIndexErrors?: boolean; target?: ScriptTarget | ts.ScriptTarget; traceResolution?: boolean; + resolveJsonModule?: boolean; types?: string[]; /** Paths used to used to compute primary types search locations */ typeRoots?: string[]; diff --git a/src/services/getEditsForFileRename.ts b/src/services/getEditsForFileRename.ts index 741ba977184cf..a5f1c5ed4f447 100644 --- a/src/services/getEditsForFileRename.ts +++ b/src/services/getEditsForFileRename.ts @@ -15,21 +15,13 @@ namespace ts { } function updateTsconfigFiles(program: Program, changeTracker: textChanges.ChangeTracker, oldFilePath: string, newFilePath: string): void { - const cfg = program.getCompilerOptions().configFile; - if (!cfg) return; - const oldFile = cfg.jsonObject && getFilesEntry(cfg.jsonObject, oldFilePath); + const configFile = program.getCompilerOptions().configFile; + const oldFile = getTsConfigPropArrayElementValue(configFile, "files", oldFilePath); if (oldFile) { - changeTracker.replaceRangeWithText(cfg, createStringRange(oldFile, cfg), newFilePath); + changeTracker.replaceRangeWithText(configFile, createStringRange(oldFile, configFile), newFilePath); } } - function getFilesEntry(cfg: ObjectLiteralExpression, fileName: string): StringLiteral | undefined { - const filesProp = find(cfg.properties, (prop): prop is PropertyAssignment => - isPropertyAssignment(prop) && isStringLiteral(prop.name) && prop.name.text === "files"); - const files = filesProp && filesProp.initializer; - return files && isArrayLiteralExpression(files) ? find(files.elements, (e): e is StringLiteral => isStringLiteral(e) && e.text === fileName) : undefined; - } - interface ToUpdate { readonly sourceFile: SourceFile; readonly toUpdate: StringLiteralLike | FileReference; diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 62a916e732a62..5b7acb4d87d78 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -415,6 +415,7 @@ declare namespace ts { ThisNodeOrAnySubNodesHasError = 131072, HasAggregatedChildData = 262144, JSDoc = 2097152, + JsonFile = 16777216, BlockScoped = 3, ReachabilityCheckFlags = 384, ReachabilityAndEmitFlags = 1408, @@ -1647,9 +1648,19 @@ declare namespace ts { sourceFiles: ReadonlyArray; } interface JsonSourceFile extends SourceFile { - jsonObject?: ObjectLiteralExpression; + statements: NodeArray; + } + interface TsConfigSourceFile extends JsonSourceFile { extendedSourceFiles?: string[]; } + interface JsonMinusNumericLiteral extends PrefixUnaryExpression { + kind: SyntaxKind.PrefixUnaryExpression; + operator: SyntaxKind.MinusToken; + operand: NumericLiteral; + } + interface JsonObjectExpressionStatement extends ExpressionStatement { + expression: ObjectLiteralExpression | ArrayLiteralExpression | JsonMinusNumericLiteral | NumericLiteral | StringLiteral | BooleanLiteral | NullLiteral; + } interface ScriptReferenceHost { getCompilerOptions(): CompilerOptions; getSourceFile(fileName: string): SourceFile | undefined; @@ -2395,11 +2406,12 @@ declare namespace ts { suppressImplicitAnyIndexErrors?: boolean; target?: ScriptTarget; traceResolution?: boolean; + resolveJsonModule?: boolean; types?: string[]; /** Paths used to compute primary types search locations */ typeRoots?: string[]; esModuleInterop?: boolean; - [option: string]: CompilerOptionsValue | JsonSourceFile | undefined; + [option: string]: CompilerOptionsValue | TsConfigSourceFile | undefined; } interface TypeAcquisition { enableAutoDiscovery?: boolean; @@ -4220,7 +4232,7 @@ declare namespace ts { * Read tsconfig.json file * @param fileName The path to the config file */ - function readJsonConfigFile(fileName: string, readFile: (path: string) => string | undefined): JsonSourceFile; + function readJsonConfigFile(fileName: string, readFile: (path: string) => string | undefined): TsConfigSourceFile; /** * Convert the json syntax tree into the json value */ @@ -4240,7 +4252,7 @@ declare namespace ts { * @param basePath A root directory to resolve relative path entries in the config * file to. e.g. outDir */ - function parseJsonSourceFileConfigFileContent(sourceFile: JsonSourceFile, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray): ParsedCommandLine; + function parseJsonSourceFileConfigFileContent(sourceFile: TsConfigSourceFile, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray): ParsedCommandLine; function convertCompilerOptionsFromJson(jsonOptions: any, basePath: string, configFileName?: string): { options: CompilerOptions; errors: Diagnostic[]; @@ -7542,6 +7554,7 @@ declare namespace ts.server.protocol { suppressImplicitAnyIndexErrors?: boolean; target?: ScriptTarget | ts.ScriptTarget; traceResolution?: boolean; + resolveJsonModule?: boolean; types?: string[]; /** Paths used to used to compute primary types search locations */ typeRoots?: string[]; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index e6ad7e5f3e801..82ba11f6f2060 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -415,6 +415,7 @@ declare namespace ts { ThisNodeOrAnySubNodesHasError = 131072, HasAggregatedChildData = 262144, JSDoc = 2097152, + JsonFile = 16777216, BlockScoped = 3, ReachabilityCheckFlags = 384, ReachabilityAndEmitFlags = 1408, @@ -1647,9 +1648,19 @@ declare namespace ts { sourceFiles: ReadonlyArray; } interface JsonSourceFile extends SourceFile { - jsonObject?: ObjectLiteralExpression; + statements: NodeArray; + } + interface TsConfigSourceFile extends JsonSourceFile { extendedSourceFiles?: string[]; } + interface JsonMinusNumericLiteral extends PrefixUnaryExpression { + kind: SyntaxKind.PrefixUnaryExpression; + operator: SyntaxKind.MinusToken; + operand: NumericLiteral; + } + interface JsonObjectExpressionStatement extends ExpressionStatement { + expression: ObjectLiteralExpression | ArrayLiteralExpression | JsonMinusNumericLiteral | NumericLiteral | StringLiteral | BooleanLiteral | NullLiteral; + } interface ScriptReferenceHost { getCompilerOptions(): CompilerOptions; getSourceFile(fileName: string): SourceFile | undefined; @@ -2395,11 +2406,12 @@ declare namespace ts { suppressImplicitAnyIndexErrors?: boolean; target?: ScriptTarget; traceResolution?: boolean; + resolveJsonModule?: boolean; types?: string[]; /** Paths used to compute primary types search locations */ typeRoots?: string[]; esModuleInterop?: boolean; - [option: string]: CompilerOptionsValue | JsonSourceFile | undefined; + [option: string]: CompilerOptionsValue | TsConfigSourceFile | undefined; } interface TypeAcquisition { enableAutoDiscovery?: boolean; @@ -4220,7 +4232,7 @@ declare namespace ts { * Read tsconfig.json file * @param fileName The path to the config file */ - function readJsonConfigFile(fileName: string, readFile: (path: string) => string | undefined): JsonSourceFile; + function readJsonConfigFile(fileName: string, readFile: (path: string) => string | undefined): TsConfigSourceFile; /** * Convert the json syntax tree into the json value */ @@ -4240,7 +4252,7 @@ declare namespace ts { * @param basePath A root directory to resolve relative path entries in the config * file to. e.g. outDir */ - function parseJsonSourceFileConfigFileContent(sourceFile: JsonSourceFile, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray): ParsedCommandLine; + function parseJsonSourceFileConfigFileContent(sourceFile: TsConfigSourceFile, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray): ParsedCommandLine; function convertCompilerOptionsFromJson(jsonOptions: any, basePath: string, configFileName?: string): { options: CompilerOptions; errors: Diagnostic[]; diff --git a/tests/baselines/reference/requireOfJsonFile.js b/tests/baselines/reference/requireOfJsonFile.js new file mode 100644 index 0000000000000..a93a52b5e3fa6 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFile.js @@ -0,0 +1,32 @@ +//// [tests/cases/compiler/requireOfJsonFile.ts] //// + +//// [file1.ts] +import b1 = require('./b.json'); +let x = b1.a; +import b2 = require('./b.json'); +if (x) { + let b = b2.b; + x = (b1.b === b); +} + +//// [b.json] +{ + "a": true, + "b": "hello" +} + +//// [out/b.json] +{ + "a": true, + "b": "hello" +} +//// [out/file1.js] +"use strict"; +exports.__esModule = true; +var b1 = require("./b.json"); +var x = b1.a; +var b2 = require("./b.json"); +if (x) { + var b = b2.b; + x = (b1.b === b); +} diff --git a/tests/baselines/reference/requireOfJsonFile.symbols b/tests/baselines/reference/requireOfJsonFile.symbols new file mode 100644 index 0000000000000..a8f82b6b14590 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFile.symbols @@ -0,0 +1,38 @@ +=== tests/cases/compiler/file1.ts === +import b1 = require('./b.json'); +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) + +let x = b1.a; +>x : Symbol(x, Decl(file1.ts, 1, 3)) +>b1.a : Symbol("a", Decl(b.json, 0, 1)) +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) +>a : Symbol("a", Decl(b.json, 0, 1)) + +import b2 = require('./b.json'); +>b2 : Symbol(b2, Decl(file1.ts, 1, 13)) + +if (x) { +>x : Symbol(x, Decl(file1.ts, 1, 3)) + + let b = b2.b; +>b : Symbol(b, Decl(file1.ts, 4, 7)) +>b2.b : Symbol("b", Decl(b.json, 1, 14)) +>b2 : Symbol(b2, Decl(file1.ts, 1, 13)) +>b : Symbol("b", Decl(b.json, 1, 14)) + + x = (b1.b === b); +>x : Symbol(x, Decl(file1.ts, 1, 3)) +>b1.b : Symbol("b", Decl(b.json, 1, 14)) +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) +>b : Symbol("b", Decl(b.json, 1, 14)) +>b : Symbol(b, Decl(file1.ts, 4, 7)) +} + +=== tests/cases/compiler/b.json === +{ + "a": true, +>"a" : Symbol("a", Decl(b.json, 0, 1)) + + "b": "hello" +>"b" : Symbol("b", Decl(b.json, 1, 14)) +} diff --git a/tests/baselines/reference/requireOfJsonFile.types b/tests/baselines/reference/requireOfJsonFile.types new file mode 100644 index 0000000000000..c718c429408b4 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFile.types @@ -0,0 +1,45 @@ +=== tests/cases/compiler/file1.ts === +import b1 = require('./b.json'); +>b1 : { "a": boolean; "b": string; } + +let x = b1.a; +>x : boolean +>b1.a : boolean +>b1 : { "a": boolean; "b": string; } +>a : boolean + +import b2 = require('./b.json'); +>b2 : { "a": boolean; "b": string; } + +if (x) { +>x : boolean + + let b = b2.b; +>b : string +>b2.b : string +>b2 : { "a": boolean; "b": string; } +>b : string + + x = (b1.b === b); +>x = (b1.b === b) : boolean +>x : boolean +>(b1.b === b) : boolean +>b1.b === b : boolean +>b1.b : string +>b1 : { "a": boolean; "b": string; } +>b : string +>b : string +} + +=== tests/cases/compiler/b.json === +{ +>{ "a": true, "b": "hello"} : { "a": boolean; "b": string; } + + "a": true, +>"a" : boolean +>true : true + + "b": "hello" +>"b" : string +>"hello" : "hello" +} diff --git a/tests/baselines/reference/requireOfJsonFileNonRelative.js b/tests/baselines/reference/requireOfJsonFileNonRelative.js new file mode 100644 index 0000000000000..35253e04c9ab7 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileNonRelative.js @@ -0,0 +1,33 @@ +//// [tests/cases/compiler/requireOfJsonFileNonRelative.ts] //// + +//// [file1.ts] +import b1 = require('b.json'); +let x = b1.a; +import b2 = require('c.json'); +if (x) { + let b = b2.b; + x = (b1.b === b); +} + +//// [b.json] +{ + "a": true, + "b": "hello" +} + +//// [c.json] +{ + "a": true, + "b": "hello" +} + +//// [out/file1.js] +"use strict"; +exports.__esModule = true; +var b1 = require("b.json"); +var x = b1.a; +var b2 = require("c.json"); +if (x) { + var b = b2.b; + x = (b1.b === b); +} diff --git a/tests/baselines/reference/requireOfJsonFileNonRelative.symbols b/tests/baselines/reference/requireOfJsonFileNonRelative.symbols new file mode 100644 index 0000000000000..4a38120620494 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileNonRelative.symbols @@ -0,0 +1,47 @@ +=== /src/projects/file1.ts === +import b1 = require('b.json'); +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) + +let x = b1.a; +>x : Symbol(x, Decl(file1.ts, 1, 3)) +>b1.a : Symbol("a", Decl(b.json, 0, 1)) +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) +>a : Symbol("a", Decl(b.json, 0, 1)) + +import b2 = require('c.json'); +>b2 : Symbol(b2, Decl(file1.ts, 1, 13)) + +if (x) { +>x : Symbol(x, Decl(file1.ts, 1, 3)) + + let b = b2.b; +>b : Symbol(b, Decl(file1.ts, 4, 7)) +>b2.b : Symbol("b", Decl(c.json, 1, 14)) +>b2 : Symbol(b2, Decl(file1.ts, 1, 13)) +>b : Symbol("b", Decl(c.json, 1, 14)) + + x = (b1.b === b); +>x : Symbol(x, Decl(file1.ts, 1, 3)) +>b1.b : Symbol("b", Decl(b.json, 1, 14)) +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) +>b : Symbol("b", Decl(b.json, 1, 14)) +>b : Symbol(b, Decl(file1.ts, 4, 7)) +} + +=== /src/projects/node_modules/b.json === +{ + "a": true, +>"a" : Symbol("a", Decl(b.json, 0, 1)) + + "b": "hello" +>"b" : Symbol("b", Decl(b.json, 1, 14)) +} + +=== /src/node_modules/c.json === +{ + "a": true, +>"a" : Symbol("a", Decl(c.json, 0, 1)) + + "b": "hello" +>"b" : Symbol("b", Decl(c.json, 1, 14)) +} diff --git a/tests/baselines/reference/requireOfJsonFileNonRelative.types b/tests/baselines/reference/requireOfJsonFileNonRelative.types new file mode 100644 index 0000000000000..8f15e2282d96f --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileNonRelative.types @@ -0,0 +1,58 @@ +=== /src/projects/file1.ts === +import b1 = require('b.json'); +>b1 : { "a": boolean; "b": string; } + +let x = b1.a; +>x : boolean +>b1.a : boolean +>b1 : { "a": boolean; "b": string; } +>a : boolean + +import b2 = require('c.json'); +>b2 : { "a": boolean; "b": string; } + +if (x) { +>x : boolean + + let b = b2.b; +>b : string +>b2.b : string +>b2 : { "a": boolean; "b": string; } +>b : string + + x = (b1.b === b); +>x = (b1.b === b) : boolean +>x : boolean +>(b1.b === b) : boolean +>b1.b === b : boolean +>b1.b : string +>b1 : { "a": boolean; "b": string; } +>b : string +>b : string +} + +=== /src/projects/node_modules/b.json === +{ +>{ "a": true, "b": "hello"} : { "a": boolean; "b": string; } + + "a": true, +>"a" : boolean +>true : true + + "b": "hello" +>"b" : string +>"hello" : "hello" +} + +=== /src/node_modules/c.json === +{ +>{ "a": true, "b": "hello"} : { "a": boolean; "b": string; } + + "a": true, +>"a" : boolean +>true : true + + "b": "hello" +>"b" : string +>"hello" : "hello" +} diff --git a/tests/baselines/reference/requireOfJsonFileNonRelativeWithoutExtension.errors.txt b/tests/baselines/reference/requireOfJsonFileNonRelativeWithoutExtension.errors.txt new file mode 100644 index 0000000000000..13660f8b83648 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileNonRelativeWithoutExtension.errors.txt @@ -0,0 +1,29 @@ +/src/projects/file1.ts(1,20): error TS2307: Cannot find module 'd.json'. +/src/projects/file1.ts(2,20): error TS2307: Cannot find module 'e'. + + +==== /src/projects/file1.ts (2 errors) ==== + import d = require("d.json"); // Should fail + ~~~~~~~~ +!!! error TS2307: Cannot find module 'd.json'. + import e = require("e"); // Should fail + ~~~ +!!! error TS2307: Cannot find module 'e'. + +==== /src/projects/node_modules/b.json (0 errors) ==== + { + "a": true, + "b": "hello" + } + +==== /src/projects/node_modules/e.json (0 errors) ==== + { + "a": true, + "b": "hello" + } + +==== /src/node_modules/c.json (0 errors) ==== + { + "a": true, + "b": "hello" + } \ No newline at end of file diff --git a/tests/baselines/reference/requireOfJsonFileNonRelativeWithoutExtension.js b/tests/baselines/reference/requireOfJsonFileNonRelativeWithoutExtension.js new file mode 100644 index 0000000000000..7a85dff350116 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileNonRelativeWithoutExtension.js @@ -0,0 +1,27 @@ +//// [tests/cases/compiler/requireOfJsonFileNonRelativeWithoutExtension.ts] //// + +//// [file1.ts] +import d = require("d.json"); // Should fail +import e = require("e"); // Should fail + +//// [b.json] +{ + "a": true, + "b": "hello" +} + +//// [e.json] +{ + "a": true, + "b": "hello" +} + +//// [c.json] +{ + "a": true, + "b": "hello" +} + +//// [out/file1.js] +"use strict"; +exports.__esModule = true; diff --git a/tests/baselines/reference/requireOfJsonFileNonRelativeWithoutExtension.symbols b/tests/baselines/reference/requireOfJsonFileNonRelativeWithoutExtension.symbols new file mode 100644 index 0000000000000..0eedb44c3dca5 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileNonRelativeWithoutExtension.symbols @@ -0,0 +1,7 @@ +=== /src/projects/file1.ts === +import d = require("d.json"); // Should fail +>d : Symbol(d, Decl(file1.ts, 0, 0)) + +import e = require("e"); // Should fail +>e : Symbol(e, Decl(file1.ts, 0, 29)) + diff --git a/tests/baselines/reference/requireOfJsonFileNonRelativeWithoutExtension.types b/tests/baselines/reference/requireOfJsonFileNonRelativeWithoutExtension.types new file mode 100644 index 0000000000000..ec891ba628056 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileNonRelativeWithoutExtension.types @@ -0,0 +1,7 @@ +=== /src/projects/file1.ts === +import d = require("d.json"); // Should fail +>d : any + +import e = require("e"); // Should fail +>e : any + diff --git a/tests/baselines/reference/requireOfJsonFileNonRelativeWithoutExtensionResolvesToTs.js b/tests/baselines/reference/requireOfJsonFileNonRelativeWithoutExtensionResolvesToTs.js new file mode 100644 index 0000000000000..03dd4ab513cf8 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileNonRelativeWithoutExtensionResolvesToTs.js @@ -0,0 +1,23 @@ +//// [tests/cases/compiler/requireOfJsonFileNonRelativeWithoutExtensionResolvesToTs.ts] //// + +//// [file1.ts] +import f = require("f"); // should work to f.ts +let fnumber: number = f; + +//// [f.json] +{ + "a": true, + "b": "hello" +} + +//// [f.ts] +export = 10; + +//// [out/node_modules/f.js] +"use strict"; +module.exports = 10; +//// [out/projects/file1.js] +"use strict"; +exports.__esModule = true; +var f = require("f"); // should work to f.ts +var fnumber = f; diff --git a/tests/baselines/reference/requireOfJsonFileNonRelativeWithoutExtensionResolvesToTs.symbols b/tests/baselines/reference/requireOfJsonFileNonRelativeWithoutExtensionResolvesToTs.symbols new file mode 100644 index 0000000000000..cf903d2fcfb7c --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileNonRelativeWithoutExtensionResolvesToTs.symbols @@ -0,0 +1,11 @@ +=== /src/projects/file1.ts === +import f = require("f"); // should work to f.ts +>f : Symbol(f, Decl(file1.ts, 0, 0)) + +let fnumber: number = f; +>fnumber : Symbol(fnumber, Decl(file1.ts, 1, 3)) +>f : Symbol(f, Decl(file1.ts, 0, 0)) + +=== /src/node_modules/f.ts === +export = 10; +No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/requireOfJsonFileNonRelativeWithoutExtensionResolvesToTs.types b/tests/baselines/reference/requireOfJsonFileNonRelativeWithoutExtensionResolvesToTs.types new file mode 100644 index 0000000000000..75c2db9bae9f1 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileNonRelativeWithoutExtensionResolvesToTs.types @@ -0,0 +1,11 @@ +=== /src/projects/file1.ts === +import f = require("f"); // should work to f.ts +>f : 10 + +let fnumber: number = f; +>fnumber : number +>f : 10 + +=== /src/node_modules/f.ts === +export = 10; +No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/requireOfJsonFileTypes.js b/tests/baselines/reference/requireOfJsonFileTypes.js new file mode 100644 index 0000000000000..e84f684187a10 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileTypes.js @@ -0,0 +1,85 @@ +//// [tests/cases/compiler/requireOfJsonFileTypes.ts] //// + +//// [file1.ts] +import b = require('./b.json'); +import c = require('./c.json'); +import d = require('./d.json'); +import e = require('./e.json'); +import f = require('./f.json'); +import g = require('./g.json'); + +let booleanLiteral: boolean, nullLiteral: null; +let stringLiteral: string; +let numberLiteral: number; + +booleanLiteral = b.a; +stringLiteral = b.b; +nullLiteral = b.c; +booleanLiteral = b.d; +const stringOrNumberOrNull: string | number | null = c[0]; +stringLiteral = d; +numberLiteral = e; +numberLiteral = f[0]; +booleanLiteral = g[0]; + +//// [b.json] +{ + "a": true, + "b": "hello", + "c": null, + "d": false +} + +//// [c.json] +["a", null, "string"] + +//// [d.json] +"dConfig" + +//// [e.json] +-10 + +//// [f.json] +[-10, 30] + +//// [g.json] +[true, false] + +//// [out/b.json] +{ + "a": true, + "b": "hello", + "c": null, + "d": false +} +//// [out/c.json] +["a", null, "string"] +//// [out/d.json] +"dConfig" +//// [out/e.json] +-10 +//// [out/f.json] +[-10, 30] +//// [out/g.json] +[true, false] +//// [out/file1.js] +"use strict"; +exports.__esModule = true; +var b = require("./b.json"); +var c = require("./c.json"); +var d = require("./d.json"); +var e = require("./e.json"); +var f = require("./f.json"); +var g = require("./g.json"); +var booleanLiteral, nullLiteral; +var stringLiteral; +var numberLiteral; +booleanLiteral = b.a; +stringLiteral = b.b; +nullLiteral = b.c; +booleanLiteral = b.d; +var stringOrNumberOrNull = c[0]; +stringLiteral = d; +numberLiteral = e; +numberLiteral = f[0]; +booleanLiteral = g[0]; diff --git a/tests/baselines/reference/requireOfJsonFileTypes.symbols b/tests/baselines/reference/requireOfJsonFileTypes.symbols new file mode 100644 index 0000000000000..01b0f4a1dcc64 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileTypes.symbols @@ -0,0 +1,103 @@ +=== tests/cases/compiler/file1.ts === +import b = require('./b.json'); +>b : Symbol(b, Decl(file1.ts, 0, 0)) + +import c = require('./c.json'); +>c : Symbol(c, Decl(file1.ts, 0, 31)) + +import d = require('./d.json'); +>d : Symbol(d, Decl(file1.ts, 1, 31)) + +import e = require('./e.json'); +>e : Symbol(e, Decl(file1.ts, 2, 31)) + +import f = require('./f.json'); +>f : Symbol(f, Decl(file1.ts, 3, 31)) + +import g = require('./g.json'); +>g : Symbol(g, Decl(file1.ts, 4, 31)) + +let booleanLiteral: boolean, nullLiteral: null; +>booleanLiteral : Symbol(booleanLiteral, Decl(file1.ts, 7, 3)) +>nullLiteral : Symbol(nullLiteral, Decl(file1.ts, 7, 28)) + +let stringLiteral: string; +>stringLiteral : Symbol(stringLiteral, Decl(file1.ts, 8, 3)) + +let numberLiteral: number; +>numberLiteral : Symbol(numberLiteral, Decl(file1.ts, 9, 3)) + +booleanLiteral = b.a; +>booleanLiteral : Symbol(booleanLiteral, Decl(file1.ts, 7, 3)) +>b.a : Symbol("a", Decl(b.json, 0, 1)) +>b : Symbol(b, Decl(file1.ts, 0, 0)) +>a : Symbol("a", Decl(b.json, 0, 1)) + +stringLiteral = b.b; +>stringLiteral : Symbol(stringLiteral, Decl(file1.ts, 8, 3)) +>b.b : Symbol("b", Decl(b.json, 1, 14)) +>b : Symbol(b, Decl(file1.ts, 0, 0)) +>b : Symbol("b", Decl(b.json, 1, 14)) + +nullLiteral = b.c; +>nullLiteral : Symbol(nullLiteral, Decl(file1.ts, 7, 28)) +>b.c : Symbol("c", Decl(b.json, 2, 17)) +>b : Symbol(b, Decl(file1.ts, 0, 0)) +>c : Symbol("c", Decl(b.json, 2, 17)) + +booleanLiteral = b.d; +>booleanLiteral : Symbol(booleanLiteral, Decl(file1.ts, 7, 3)) +>b.d : Symbol("d", Decl(b.json, 3, 14)) +>b : Symbol(b, Decl(file1.ts, 0, 0)) +>d : Symbol("d", Decl(b.json, 3, 14)) + +const stringOrNumberOrNull: string | number | null = c[0]; +>stringOrNumberOrNull : Symbol(stringOrNumberOrNull, Decl(file1.ts, 15, 5)) +>c : Symbol(c, Decl(file1.ts, 0, 31)) + +stringLiteral = d; +>stringLiteral : Symbol(stringLiteral, Decl(file1.ts, 8, 3)) +>d : Symbol(d, Decl(file1.ts, 1, 31)) + +numberLiteral = e; +>numberLiteral : Symbol(numberLiteral, Decl(file1.ts, 9, 3)) +>e : Symbol(e, Decl(file1.ts, 2, 31)) + +numberLiteral = f[0]; +>numberLiteral : Symbol(numberLiteral, Decl(file1.ts, 9, 3)) +>f : Symbol(f, Decl(file1.ts, 3, 31)) + +booleanLiteral = g[0]; +>booleanLiteral : Symbol(booleanLiteral, Decl(file1.ts, 7, 3)) +>g : Symbol(g, Decl(file1.ts, 4, 31)) + +=== tests/cases/compiler/b.json === +{ + "a": true, +>"a" : Symbol("a", Decl(b.json, 0, 1)) + + "b": "hello", +>"b" : Symbol("b", Decl(b.json, 1, 14)) + + "c": null, +>"c" : Symbol("c", Decl(b.json, 2, 17)) + + "d": false +>"d" : Symbol("d", Decl(b.json, 3, 14)) +} + +=== tests/cases/compiler/c.json === +["a", null, "string"] +No type information for this code. +No type information for this code.=== tests/cases/compiler/d.json === +"dConfig" +No type information for this code. +No type information for this code.=== tests/cases/compiler/e.json === +-10 +No type information for this code. +No type information for this code.=== tests/cases/compiler/f.json === +[-10, 30] +No type information for this code. +No type information for this code.=== tests/cases/compiler/g.json === +[true, false] +No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/requireOfJsonFileTypes.types b/tests/baselines/reference/requireOfJsonFileTypes.types new file mode 100644 index 0000000000000..d9cd38cd58fdd --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileTypes.types @@ -0,0 +1,139 @@ +=== tests/cases/compiler/file1.ts === +import b = require('./b.json'); +>b : { "a": boolean; "b": string; "c": null; "d": boolean; } + +import c = require('./c.json'); +>c : (string | null)[] + +import d = require('./d.json'); +>d : "dConfig" + +import e = require('./e.json'); +>e : -10 + +import f = require('./f.json'); +>f : number[] + +import g = require('./g.json'); +>g : boolean[] + +let booleanLiteral: boolean, nullLiteral: null; +>booleanLiteral : boolean +>nullLiteral : null +>null : null + +let stringLiteral: string; +>stringLiteral : string + +let numberLiteral: number; +>numberLiteral : number + +booleanLiteral = b.a; +>booleanLiteral = b.a : boolean +>booleanLiteral : boolean +>b.a : boolean +>b : { "a": boolean; "b": string; "c": null; "d": boolean; } +>a : boolean + +stringLiteral = b.b; +>stringLiteral = b.b : string +>stringLiteral : string +>b.b : string +>b : { "a": boolean; "b": string; "c": null; "d": boolean; } +>b : string + +nullLiteral = b.c; +>nullLiteral = b.c : null +>nullLiteral : null +>b.c : null +>b : { "a": boolean; "b": string; "c": null; "d": boolean; } +>c : null + +booleanLiteral = b.d; +>booleanLiteral = b.d : boolean +>booleanLiteral : boolean +>b.d : boolean +>b : { "a": boolean; "b": string; "c": null; "d": boolean; } +>d : boolean + +const stringOrNumberOrNull: string | number | null = c[0]; +>stringOrNumberOrNull : string | number | null +>null : null +>c[0] : string | null +>c : (string | null)[] +>0 : 0 + +stringLiteral = d; +>stringLiteral = d : "dConfig" +>stringLiteral : string +>d : "dConfig" + +numberLiteral = e; +>numberLiteral = e : -10 +>numberLiteral : number +>e : -10 + +numberLiteral = f[0]; +>numberLiteral = f[0] : number +>numberLiteral : number +>f[0] : number +>f : number[] +>0 : 0 + +booleanLiteral = g[0]; +>booleanLiteral = g[0] : boolean +>booleanLiteral : boolean +>g[0] : boolean +>g : boolean[] +>0 : 0 + +=== tests/cases/compiler/b.json === +{ +>{ "a": true, "b": "hello", "c": null, "d": false} : { "a": boolean; "b": string; "c": null; "d": boolean; } + + "a": true, +>"a" : boolean +>true : true + + "b": "hello", +>"b" : string +>"hello" : "hello" + + "c": null, +>"c" : null +>null : null + + "d": false +>"d" : boolean +>false : false +} + +=== tests/cases/compiler/c.json === +["a", null, "string"] +>["a", null, "string"] : (string | null)[] +>"a" : "a" +>null : null +>"string" : "string" + +=== tests/cases/compiler/d.json === +"dConfig" +>"dConfig" : "dConfig" + +=== tests/cases/compiler/e.json === +-10 +>-10 : -10 +>10 : 10 + +=== tests/cases/compiler/f.json === +[-10, 30] +>[-10, 30] : number[] +>-10 : -10 +>10 : 10 +>30 : 30 + +=== tests/cases/compiler/g.json === +[true, false] +>[true, false] : boolean[] +>true : true +>false : false + diff --git a/tests/baselines/reference/requireOfJsonFileWithAmd.errors.txt b/tests/baselines/reference/requireOfJsonFileWithAmd.errors.txt new file mode 100644 index 0000000000000..2c43e1d530179 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithAmd.errors.txt @@ -0,0 +1,24 @@ +error TS5070: Option '--resolveJsonModule' cannot be specified without 'node' module resolution strategy. +tests/cases/compiler/file1.ts(1,21): error TS2307: Cannot find module './b'. +tests/cases/compiler/file1.ts(3,21): error TS2307: Cannot find module './b.json'. + + +!!! error TS5070: Option '--resolveJsonModule' cannot be specified without 'node' module resolution strategy. +==== tests/cases/compiler/file1.ts (2 errors) ==== + import b1 = require('./b'); + ~~~~~ +!!! error TS2307: Cannot find module './b'. + let x = b1.a; + import b2 = require('./b.json'); + ~~~~~~~~~~ +!!! error TS2307: Cannot find module './b.json'. + if (x) { + let b = b2.b; + x = (b1.b === b); + } + +==== tests/cases/compiler/b.json (0 errors) ==== + { + "a": true, + "b": "hello" + } \ No newline at end of file diff --git a/tests/baselines/reference/requireOfJsonFileWithAmd.js b/tests/baselines/reference/requireOfJsonFileWithAmd.js new file mode 100644 index 0000000000000..6e4928174c2d8 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithAmd.js @@ -0,0 +1,27 @@ +//// [tests/cases/compiler/requireOfJsonFileWithAmd.ts] //// + +//// [file1.ts] +import b1 = require('./b'); +let x = b1.a; +import b2 = require('./b.json'); +if (x) { + let b = b2.b; + x = (b1.b === b); +} + +//// [b.json] +{ + "a": true, + "b": "hello" +} + +//// [out/file1.js] +define(["require", "exports", "./b", "./b.json"], function (require, exports, b1, b2) { + "use strict"; + exports.__esModule = true; + var x = b1.a; + if (x) { + var b = b2.b; + x = (b1.b === b); + } +}); diff --git a/tests/baselines/reference/requireOfJsonFileWithAmd.symbols b/tests/baselines/reference/requireOfJsonFileWithAmd.symbols new file mode 100644 index 0000000000000..1729461ef07bb --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithAmd.symbols @@ -0,0 +1,24 @@ +=== tests/cases/compiler/file1.ts === +import b1 = require('./b'); +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) + +let x = b1.a; +>x : Symbol(x, Decl(file1.ts, 1, 3)) +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) + +import b2 = require('./b.json'); +>b2 : Symbol(b2, Decl(file1.ts, 1, 13)) + +if (x) { +>x : Symbol(x, Decl(file1.ts, 1, 3)) + + let b = b2.b; +>b : Symbol(b, Decl(file1.ts, 4, 7)) +>b2 : Symbol(b2, Decl(file1.ts, 1, 13)) + + x = (b1.b === b); +>x : Symbol(x, Decl(file1.ts, 1, 3)) +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) +>b : Symbol(b, Decl(file1.ts, 4, 7)) +} + diff --git a/tests/baselines/reference/requireOfJsonFileWithAmd.types b/tests/baselines/reference/requireOfJsonFileWithAmd.types new file mode 100644 index 0000000000000..7984743550dae --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithAmd.types @@ -0,0 +1,33 @@ +=== tests/cases/compiler/file1.ts === +import b1 = require('./b'); +>b1 : any + +let x = b1.a; +>x : any +>b1.a : any +>b1 : any +>a : any + +import b2 = require('./b.json'); +>b2 : any + +if (x) { +>x : any + + let b = b2.b; +>b : any +>b2.b : any +>b2 : any +>b : any + + x = (b1.b === b); +>x = (b1.b === b) : boolean +>x : any +>(b1.b === b) : boolean +>b1.b === b : boolean +>b1.b : any +>b1 : any +>b : any +>b : any +} + diff --git a/tests/baselines/reference/requireOfJsonFileWithEmptyObject.js b/tests/baselines/reference/requireOfJsonFileWithEmptyObject.js new file mode 100644 index 0000000000000..ad6694b525696 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithEmptyObject.js @@ -0,0 +1,25 @@ +//// [tests/cases/compiler/requireOfJsonFileWithEmptyObject.ts] //// + +//// [file1.ts] +import b1 = require('./b.json'); +let x = b1; +import b2 = require('./b.json'); +if (x) { + x = b2; +} + +//// [b.json] +{ +} + +//// [out/b.json] +{} +//// [out/file1.js] +"use strict"; +exports.__esModule = true; +var b1 = require("./b.json"); +var x = b1; +var b2 = require("./b.json"); +if (x) { + x = b2; +} diff --git a/tests/baselines/reference/requireOfJsonFileWithEmptyObject.symbols b/tests/baselines/reference/requireOfJsonFileWithEmptyObject.symbols new file mode 100644 index 0000000000000..84f4cc98c5b41 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithEmptyObject.symbols @@ -0,0 +1,23 @@ +=== tests/cases/compiler/file1.ts === +import b1 = require('./b.json'); +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) + +let x = b1; +>x : Symbol(x, Decl(file1.ts, 1, 3)) +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) + +import b2 = require('./b.json'); +>b2 : Symbol(b2, Decl(file1.ts, 1, 11)) + +if (x) { +>x : Symbol(x, Decl(file1.ts, 1, 3)) + + x = b2; +>x : Symbol(x, Decl(file1.ts, 1, 3)) +>b2 : Symbol(b2, Decl(file1.ts, 1, 11)) +} + +=== tests/cases/compiler/b.json === +{ +No type information for this code.} +No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/requireOfJsonFileWithEmptyObject.types b/tests/baselines/reference/requireOfJsonFileWithEmptyObject.types new file mode 100644 index 0000000000000..5cbc36a7e548e --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithEmptyObject.types @@ -0,0 +1,24 @@ +=== tests/cases/compiler/file1.ts === +import b1 = require('./b.json'); +>b1 : {} + +let x = b1; +>x : {} +>b1 : {} + +import b2 = require('./b.json'); +>b2 : {} + +if (x) { +>x : {} + + x = b2; +>x = b2 : {} +>x : {} +>b2 : {} +} + +=== tests/cases/compiler/b.json === +{ +>{} : {} +} diff --git a/tests/baselines/reference/requireOfJsonFileWithEmptyObjectWithErrors.errors.txt b/tests/baselines/reference/requireOfJsonFileWithEmptyObjectWithErrors.errors.txt new file mode 100644 index 0000000000000..0f10e81f8149b --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithEmptyObjectWithErrors.errors.txt @@ -0,0 +1,23 @@ +tests/cases/compiler/file1.ts(2,12): error TS2339: Property 'a' does not exist on type '{}'. +tests/cases/compiler/file1.ts(5,16): error TS2339: Property 'b' does not exist on type '{}'. +tests/cases/compiler/file1.ts(6,13): error TS2339: Property 'b' does not exist on type '{}'. + + +==== tests/cases/compiler/file1.ts (3 errors) ==== + import b1 = require('./b.json'); + let x = b1.a; + ~ +!!! error TS2339: Property 'a' does not exist on type '{}'. + import b2 = require('./b.json'); + if (x) { + let b = b2.b; + ~ +!!! error TS2339: Property 'b' does not exist on type '{}'. + x = (b1.b === b); + ~ +!!! error TS2339: Property 'b' does not exist on type '{}'. + } + +==== tests/cases/compiler/b.json (0 errors) ==== + { + } \ No newline at end of file diff --git a/tests/baselines/reference/requireOfJsonFileWithEmptyObjectWithErrors.js b/tests/baselines/reference/requireOfJsonFileWithEmptyObjectWithErrors.js new file mode 100644 index 0000000000000..ae50a6ea0e580 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithEmptyObjectWithErrors.js @@ -0,0 +1,27 @@ +//// [tests/cases/compiler/requireOfJsonFileWithEmptyObjectWithErrors.ts] //// + +//// [file1.ts] +import b1 = require('./b.json'); +let x = b1.a; +import b2 = require('./b.json'); +if (x) { + let b = b2.b; + x = (b1.b === b); +} + +//// [b.json] +{ +} + +//// [out/b.json] +{} +//// [out/file1.js] +"use strict"; +exports.__esModule = true; +var b1 = require("./b.json"); +var x = b1.a; +var b2 = require("./b.json"); +if (x) { + var b = b2.b; + x = (b1.b === b); +} diff --git a/tests/baselines/reference/requireOfJsonFileWithEmptyObjectWithErrors.symbols b/tests/baselines/reference/requireOfJsonFileWithEmptyObjectWithErrors.symbols new file mode 100644 index 0000000000000..819d0c1aabd79 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithEmptyObjectWithErrors.symbols @@ -0,0 +1,28 @@ +=== tests/cases/compiler/file1.ts === +import b1 = require('./b.json'); +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) + +let x = b1.a; +>x : Symbol(x, Decl(file1.ts, 1, 3)) +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) + +import b2 = require('./b.json'); +>b2 : Symbol(b2, Decl(file1.ts, 1, 13)) + +if (x) { +>x : Symbol(x, Decl(file1.ts, 1, 3)) + + let b = b2.b; +>b : Symbol(b, Decl(file1.ts, 4, 7)) +>b2 : Symbol(b2, Decl(file1.ts, 1, 13)) + + x = (b1.b === b); +>x : Symbol(x, Decl(file1.ts, 1, 3)) +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) +>b : Symbol(b, Decl(file1.ts, 4, 7)) +} + +=== tests/cases/compiler/b.json === +{ +No type information for this code.} +No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/requireOfJsonFileWithEmptyObjectWithErrors.types b/tests/baselines/reference/requireOfJsonFileWithEmptyObjectWithErrors.types new file mode 100644 index 0000000000000..b0eb30cd30778 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithEmptyObjectWithErrors.types @@ -0,0 +1,37 @@ +=== tests/cases/compiler/file1.ts === +import b1 = require('./b.json'); +>b1 : {} + +let x = b1.a; +>x : any +>b1.a : any +>b1 : {} +>a : any + +import b2 = require('./b.json'); +>b2 : {} + +if (x) { +>x : any + + let b = b2.b; +>b : any +>b2.b : any +>b2 : {} +>b : any + + x = (b1.b === b); +>x = (b1.b === b) : boolean +>x : any +>(b1.b === b) : boolean +>b1.b === b : boolean +>b1.b : any +>b1 : {} +>b : any +>b : any +} + +=== tests/cases/compiler/b.json === +{ +>{} : {} +} diff --git a/tests/baselines/reference/requireOfJsonFileWithErrors.errors.txt b/tests/baselines/reference/requireOfJsonFileWithErrors.errors.txt new file mode 100644 index 0000000000000..bac48b2f2c052 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithErrors.errors.txt @@ -0,0 +1,19 @@ +tests/cases/compiler/b.json(2,5): error TS1327: String literal with double quotes expected. + + +==== tests/cases/compiler/file1.ts (0 errors) ==== + import b1 = require('./b.json'); + let x = b1.a; + import b2 = require('./b.json'); + if (x) { + let b = b2.b; + x = (b1.b === b); + } + +==== tests/cases/compiler/b.json (1 errors) ==== + { + 'a': true, + ~~~ +!!! error TS1327: String literal with double quotes expected. + "b": "hello" + } \ No newline at end of file diff --git a/tests/baselines/reference/requireOfJsonFileWithErrors.js b/tests/baselines/reference/requireOfJsonFileWithErrors.js new file mode 100644 index 0000000000000..a51687c66b017 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithErrors.js @@ -0,0 +1,32 @@ +//// [tests/cases/compiler/requireOfJsonFileWithErrors.ts] //// + +//// [file1.ts] +import b1 = require('./b.json'); +let x = b1.a; +import b2 = require('./b.json'); +if (x) { + let b = b2.b; + x = (b1.b === b); +} + +//// [b.json] +{ + 'a': true, + "b": "hello" +} + +//// [out/b.json] +{ + 'a': true, + "b": "hello" +} +//// [out/file1.js] +"use strict"; +exports.__esModule = true; +var b1 = require("./b.json"); +var x = b1.a; +var b2 = require("./b.json"); +if (x) { + var b = b2.b; + x = (b1.b === b); +} diff --git a/tests/baselines/reference/requireOfJsonFileWithErrors.symbols b/tests/baselines/reference/requireOfJsonFileWithErrors.symbols new file mode 100644 index 0000000000000..43e877cbed209 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithErrors.symbols @@ -0,0 +1,38 @@ +=== tests/cases/compiler/file1.ts === +import b1 = require('./b.json'); +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) + +let x = b1.a; +>x : Symbol(x, Decl(file1.ts, 1, 3)) +>b1.a : Symbol('a', Decl(b.json, 0, 1)) +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) +>a : Symbol('a', Decl(b.json, 0, 1)) + +import b2 = require('./b.json'); +>b2 : Symbol(b2, Decl(file1.ts, 1, 13)) + +if (x) { +>x : Symbol(x, Decl(file1.ts, 1, 3)) + + let b = b2.b; +>b : Symbol(b, Decl(file1.ts, 4, 7)) +>b2.b : Symbol("b", Decl(b.json, 1, 14)) +>b2 : Symbol(b2, Decl(file1.ts, 1, 13)) +>b : Symbol("b", Decl(b.json, 1, 14)) + + x = (b1.b === b); +>x : Symbol(x, Decl(file1.ts, 1, 3)) +>b1.b : Symbol("b", Decl(b.json, 1, 14)) +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) +>b : Symbol("b", Decl(b.json, 1, 14)) +>b : Symbol(b, Decl(file1.ts, 4, 7)) +} + +=== tests/cases/compiler/b.json === +{ + 'a': true, +>'a' : Symbol('a', Decl(b.json, 0, 1)) + + "b": "hello" +>"b" : Symbol("b", Decl(b.json, 1, 14)) +} diff --git a/tests/baselines/reference/requireOfJsonFileWithErrors.types b/tests/baselines/reference/requireOfJsonFileWithErrors.types new file mode 100644 index 0000000000000..35893282bee2f --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithErrors.types @@ -0,0 +1,45 @@ +=== tests/cases/compiler/file1.ts === +import b1 = require('./b.json'); +>b1 : { 'a': boolean; "b": string; } + +let x = b1.a; +>x : boolean +>b1.a : boolean +>b1 : { 'a': boolean; "b": string; } +>a : boolean + +import b2 = require('./b.json'); +>b2 : { 'a': boolean; "b": string; } + +if (x) { +>x : boolean + + let b = b2.b; +>b : string +>b2.b : string +>b2 : { 'a': boolean; "b": string; } +>b : string + + x = (b1.b === b); +>x = (b1.b === b) : boolean +>x : boolean +>(b1.b === b) : boolean +>b1.b === b : boolean +>b1.b : string +>b1 : { 'a': boolean; "b": string; } +>b : string +>b : string +} + +=== tests/cases/compiler/b.json === +{ +>{ 'a': true, "b": "hello"} : { 'a': boolean; "b": string; } + + 'a': true, +>'a' : boolean +>true : true + + "b": "hello" +>"b" : string +>"hello" : "hello" +} diff --git a/tests/baselines/reference/requireOfJsonFileWithNoContent.errors.txt b/tests/baselines/reference/requireOfJsonFileWithNoContent.errors.txt new file mode 100644 index 0000000000000..e08b474466c92 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithNoContent.errors.txt @@ -0,0 +1,22 @@ +tests/cases/compiler/file1.ts(2,12): error TS2339: Property 'a' does not exist on type '{}'. +tests/cases/compiler/file1.ts(5,16): error TS2339: Property 'b' does not exist on type '{}'. +tests/cases/compiler/file1.ts(6,13): error TS2339: Property 'b' does not exist on type '{}'. + + +==== tests/cases/compiler/file1.ts (3 errors) ==== + import b1 = require('./b.json'); + let x = b1.a; + ~ +!!! error TS2339: Property 'a' does not exist on type '{}'. + import b2 = require('./b.json'); + if (x) { + let b = b2.b; + ~ +!!! error TS2339: Property 'b' does not exist on type '{}'. + x = (b1.b === b); + ~ +!!! error TS2339: Property 'b' does not exist on type '{}'. + } + +==== tests/cases/compiler/b.json (0 errors) ==== + \ No newline at end of file diff --git a/tests/baselines/reference/requireOfJsonFileWithNoContent.js b/tests/baselines/reference/requireOfJsonFileWithNoContent.js new file mode 100644 index 0000000000000..4addb7f597c6c --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithNoContent.js @@ -0,0 +1,25 @@ +//// [tests/cases/compiler/requireOfJsonFileWithNoContent.ts] //// + +//// [file1.ts] +import b1 = require('./b.json'); +let x = b1.a; +import b2 = require('./b.json'); +if (x) { + let b = b2.b; + x = (b1.b === b); +} + +//// [b.json] + + +//// [out/b.json] +//// [out/file1.js] +"use strict"; +exports.__esModule = true; +var b1 = require("./b.json"); +var x = b1.a; +var b2 = require("./b.json"); +if (x) { + var b = b2.b; + x = (b1.b === b); +} diff --git a/tests/baselines/reference/requireOfJsonFileWithNoContent.symbols b/tests/baselines/reference/requireOfJsonFileWithNoContent.symbols new file mode 100644 index 0000000000000..07d1466a57d87 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithNoContent.symbols @@ -0,0 +1,27 @@ +=== tests/cases/compiler/file1.ts === +import b1 = require('./b.json'); +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) + +let x = b1.a; +>x : Symbol(x, Decl(file1.ts, 1, 3)) +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) + +import b2 = require('./b.json'); +>b2 : Symbol(b2, Decl(file1.ts, 1, 13)) + +if (x) { +>x : Symbol(x, Decl(file1.ts, 1, 3)) + + let b = b2.b; +>b : Symbol(b, Decl(file1.ts, 4, 7)) +>b2 : Symbol(b2, Decl(file1.ts, 1, 13)) + + x = (b1.b === b); +>x : Symbol(x, Decl(file1.ts, 1, 3)) +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) +>b : Symbol(b, Decl(file1.ts, 4, 7)) +} + +=== tests/cases/compiler/b.json === + +No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/requireOfJsonFileWithNoContent.types b/tests/baselines/reference/requireOfJsonFileWithNoContent.types new file mode 100644 index 0000000000000..e23845e073ec4 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithNoContent.types @@ -0,0 +1,36 @@ +=== tests/cases/compiler/file1.ts === +import b1 = require('./b.json'); +>b1 : {} + +let x = b1.a; +>x : any +>b1.a : any +>b1 : {} +>a : any + +import b2 = require('./b.json'); +>b2 : {} + +if (x) { +>x : any + + let b = b2.b; +>b : any +>b2.b : any +>b2 : {} +>b : any + + x = (b1.b === b); +>x = (b1.b === b) : boolean +>x : any +>(b1.b === b) : boolean +>b1.b === b : boolean +>b1.b : any +>b1 : {} +>b : any +>b : any +} + +=== tests/cases/compiler/b.json === + +No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/requireOfJsonFileWithoutAllowJs.js b/tests/baselines/reference/requireOfJsonFileWithoutAllowJs.js new file mode 100644 index 0000000000000..0513e2b62086e --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithoutAllowJs.js @@ -0,0 +1,32 @@ +//// [tests/cases/compiler/requireOfJsonFileWithoutAllowJs.ts] //// + +//// [file1.ts] +import b1 = require('./b.json'); +let x = b1.a; +import b2 = require('./b.json'); +if (x) { + let b = b2.b; + x = (b1.b === b); +} + +//// [b.json] +{ + "a": true, + "b": "hello" +} + +//// [out/b.json] +{ + "a": true, + "b": "hello" +} +//// [out/file1.js] +"use strict"; +exports.__esModule = true; +var b1 = require("./b.json"); +var x = b1.a; +var b2 = require("./b.json"); +if (x) { + var b = b2.b; + x = (b1.b === b); +} diff --git a/tests/baselines/reference/requireOfJsonFileWithoutAllowJs.symbols b/tests/baselines/reference/requireOfJsonFileWithoutAllowJs.symbols new file mode 100644 index 0000000000000..a8f82b6b14590 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithoutAllowJs.symbols @@ -0,0 +1,38 @@ +=== tests/cases/compiler/file1.ts === +import b1 = require('./b.json'); +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) + +let x = b1.a; +>x : Symbol(x, Decl(file1.ts, 1, 3)) +>b1.a : Symbol("a", Decl(b.json, 0, 1)) +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) +>a : Symbol("a", Decl(b.json, 0, 1)) + +import b2 = require('./b.json'); +>b2 : Symbol(b2, Decl(file1.ts, 1, 13)) + +if (x) { +>x : Symbol(x, Decl(file1.ts, 1, 3)) + + let b = b2.b; +>b : Symbol(b, Decl(file1.ts, 4, 7)) +>b2.b : Symbol("b", Decl(b.json, 1, 14)) +>b2 : Symbol(b2, Decl(file1.ts, 1, 13)) +>b : Symbol("b", Decl(b.json, 1, 14)) + + x = (b1.b === b); +>x : Symbol(x, Decl(file1.ts, 1, 3)) +>b1.b : Symbol("b", Decl(b.json, 1, 14)) +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) +>b : Symbol("b", Decl(b.json, 1, 14)) +>b : Symbol(b, Decl(file1.ts, 4, 7)) +} + +=== tests/cases/compiler/b.json === +{ + "a": true, +>"a" : Symbol("a", Decl(b.json, 0, 1)) + + "b": "hello" +>"b" : Symbol("b", Decl(b.json, 1, 14)) +} diff --git a/tests/baselines/reference/requireOfJsonFileWithoutAllowJs.types b/tests/baselines/reference/requireOfJsonFileWithoutAllowJs.types new file mode 100644 index 0000000000000..c718c429408b4 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithoutAllowJs.types @@ -0,0 +1,45 @@ +=== tests/cases/compiler/file1.ts === +import b1 = require('./b.json'); +>b1 : { "a": boolean; "b": string; } + +let x = b1.a; +>x : boolean +>b1.a : boolean +>b1 : { "a": boolean; "b": string; } +>a : boolean + +import b2 = require('./b.json'); +>b2 : { "a": boolean; "b": string; } + +if (x) { +>x : boolean + + let b = b2.b; +>b : string +>b2.b : string +>b2 : { "a": boolean; "b": string; } +>b : string + + x = (b1.b === b); +>x = (b1.b === b) : boolean +>x : boolean +>(b1.b === b) : boolean +>b1.b === b : boolean +>b1.b : string +>b1 : { "a": boolean; "b": string; } +>b : string +>b : string +} + +=== tests/cases/compiler/b.json === +{ +>{ "a": true, "b": "hello"} : { "a": boolean; "b": string; } + + "a": true, +>"a" : boolean +>true : true + + "b": "hello" +>"b" : string +>"hello" : "hello" +} diff --git a/tests/baselines/reference/requireOfJsonFileWithoutExtension.errors.txt b/tests/baselines/reference/requireOfJsonFileWithoutExtension.errors.txt new file mode 100644 index 0000000000000..799b78c26eec8 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithoutExtension.errors.txt @@ -0,0 +1,19 @@ +tests/cases/compiler/file1.ts(1,21): error TS2307: Cannot find module './b'. + + +==== tests/cases/compiler/file1.ts (1 errors) ==== + import b1 = require('./b'); // This should not resolve + ~~~~~ +!!! error TS2307: Cannot find module './b'. + let x = b1.a; + import b2 = require('./b.json'); + if (x) { + let b = b2.b; + x = (b1.b === b); + } + +==== tests/cases/compiler/b.json (0 errors) ==== + { + "a": true, + "b": "hello" + } \ No newline at end of file diff --git a/tests/baselines/reference/requireOfJsonFileWithoutExtension.js b/tests/baselines/reference/requireOfJsonFileWithoutExtension.js new file mode 100644 index 0000000000000..d3bbce8d371fa --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithoutExtension.js @@ -0,0 +1,32 @@ +//// [tests/cases/compiler/requireOfJsonFileWithoutExtension.ts] //// + +//// [file1.ts] +import b1 = require('./b'); // This should not resolve +let x = b1.a; +import b2 = require('./b.json'); +if (x) { + let b = b2.b; + x = (b1.b === b); +} + +//// [b.json] +{ + "a": true, + "b": "hello" +} + +//// [out/b.json] +{ + "a": true, + "b": "hello" +} +//// [out/file1.js] +"use strict"; +exports.__esModule = true; +var b1 = require("./b"); // This should not resolve +var x = b1.a; +var b2 = require("./b.json"); +if (x) { + var b = b2.b; + x = (b1.b === b); +} diff --git a/tests/baselines/reference/requireOfJsonFileWithoutExtension.symbols b/tests/baselines/reference/requireOfJsonFileWithoutExtension.symbols new file mode 100644 index 0000000000000..34125a48dd6d1 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithoutExtension.symbols @@ -0,0 +1,34 @@ +=== tests/cases/compiler/file1.ts === +import b1 = require('./b'); // This should not resolve +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) + +let x = b1.a; +>x : Symbol(x, Decl(file1.ts, 1, 3)) +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) + +import b2 = require('./b.json'); +>b2 : Symbol(b2, Decl(file1.ts, 1, 13)) + +if (x) { +>x : Symbol(x, Decl(file1.ts, 1, 3)) + + let b = b2.b; +>b : Symbol(b, Decl(file1.ts, 4, 7)) +>b2.b : Symbol("b", Decl(b.json, 1, 14)) +>b2 : Symbol(b2, Decl(file1.ts, 1, 13)) +>b : Symbol("b", Decl(b.json, 1, 14)) + + x = (b1.b === b); +>x : Symbol(x, Decl(file1.ts, 1, 3)) +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) +>b : Symbol(b, Decl(file1.ts, 4, 7)) +} + +=== tests/cases/compiler/b.json === +{ + "a": true, +>"a" : Symbol("a", Decl(b.json, 0, 1)) + + "b": "hello" +>"b" : Symbol("b", Decl(b.json, 1, 14)) +} diff --git a/tests/baselines/reference/requireOfJsonFileWithoutExtension.types b/tests/baselines/reference/requireOfJsonFileWithoutExtension.types new file mode 100644 index 0000000000000..5c09cd9063d12 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithoutExtension.types @@ -0,0 +1,45 @@ +=== tests/cases/compiler/file1.ts === +import b1 = require('./b'); // This should not resolve +>b1 : any + +let x = b1.a; +>x : any +>b1.a : any +>b1 : any +>a : any + +import b2 = require('./b.json'); +>b2 : { "a": boolean; "b": string; } + +if (x) { +>x : any + + let b = b2.b; +>b : string +>b2.b : string +>b2 : { "a": boolean; "b": string; } +>b : string + + x = (b1.b === b); +>x = (b1.b === b) : boolean +>x : any +>(b1.b === b) : boolean +>b1.b === b : boolean +>b1.b : any +>b1 : any +>b : any +>b : string +} + +=== tests/cases/compiler/b.json === +{ +>{ "a": true, "b": "hello"} : { "a": boolean; "b": string; } + + "a": true, +>"a" : boolean +>true : true + + "b": "hello" +>"b" : string +>"hello" : "hello" +} diff --git a/tests/baselines/reference/requireOfJsonFileWithoutExtensionResolvesToTs.js b/tests/baselines/reference/requireOfJsonFileWithoutExtensionResolvesToTs.js new file mode 100644 index 0000000000000..438a11834095b --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithoutExtensionResolvesToTs.js @@ -0,0 +1,44 @@ +//// [tests/cases/compiler/requireOfJsonFileWithoutExtensionResolvesToTs.ts] //// + +//// [file1.ts] +import c1 = require('./c'); // resolves to c.ts +let x2 = c1.a; +import c2 = require('./c.json'); // resolves to c.json +if (x2) { + let b = c2.b; + let x = (c1.b === b); +} + +//// [b.json] +{ + "a": true, + "b": "hello" +} + +//// [c.json] +{ + "a": true, + "b": "hello" +} + +//// [c.ts] +export = { a: true, b: "hello" }; + +//// [out/c.js] +"use strict"; +module.exports = { a: true, b: "hello" }; +//// [out/c.json] +{ + "a": true, + "b": "hello" +} +//// [out/file1.js] +"use strict"; +exports.__esModule = true; +var c1 = require("./c"); // resolves to c.ts +var x2 = c1.a; +var c2 = require("./c.json"); // resolves to c.json +if (x2) { + var b = c2.b; + var x = (c1.b === b); +} diff --git a/tests/baselines/reference/requireOfJsonFileWithoutExtensionResolvesToTs.symbols b/tests/baselines/reference/requireOfJsonFileWithoutExtensionResolvesToTs.symbols new file mode 100644 index 0000000000000..fc45f789eac8e --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithoutExtensionResolvesToTs.symbols @@ -0,0 +1,44 @@ +=== tests/cases/compiler/file1.ts === +import c1 = require('./c'); // resolves to c.ts +>c1 : Symbol(c1, Decl(file1.ts, 0, 0)) + +let x2 = c1.a; +>x2 : Symbol(x2, Decl(file1.ts, 1, 3)) +>c1.a : Symbol(a, Decl(c.ts, 0, 10)) +>c1 : Symbol(c1, Decl(file1.ts, 0, 0)) +>a : Symbol(a, Decl(c.ts, 0, 10)) + +import c2 = require('./c.json'); // resolves to c.json +>c2 : Symbol(c2, Decl(file1.ts, 1, 14)) + +if (x2) { +>x2 : Symbol(x2, Decl(file1.ts, 1, 3)) + + let b = c2.b; +>b : Symbol(b, Decl(file1.ts, 4, 7)) +>c2.b : Symbol("b", Decl(c.json, 1, 14)) +>c2 : Symbol(c2, Decl(file1.ts, 1, 14)) +>b : Symbol("b", Decl(c.json, 1, 14)) + + let x = (c1.b === b); +>x : Symbol(x, Decl(file1.ts, 5, 7)) +>c1.b : Symbol(b, Decl(c.ts, 0, 19)) +>c1 : Symbol(c1, Decl(file1.ts, 0, 0)) +>b : Symbol(b, Decl(c.ts, 0, 19)) +>b : Symbol(b, Decl(file1.ts, 4, 7)) +} + +=== tests/cases/compiler/c.json === +{ + "a": true, +>"a" : Symbol("a", Decl(c.json, 0, 1)) + + "b": "hello" +>"b" : Symbol("b", Decl(c.json, 1, 14)) +} + +=== tests/cases/compiler/c.ts === +export = { a: true, b: "hello" }; +>a : Symbol(a, Decl(c.ts, 0, 10)) +>b : Symbol(b, Decl(c.ts, 0, 19)) + diff --git a/tests/baselines/reference/requireOfJsonFileWithoutExtensionResolvesToTs.types b/tests/baselines/reference/requireOfJsonFileWithoutExtensionResolvesToTs.types new file mode 100644 index 0000000000000..a2faa9c834e09 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithoutExtensionResolvesToTs.types @@ -0,0 +1,53 @@ +=== tests/cases/compiler/file1.ts === +import c1 = require('./c'); // resolves to c.ts +>c1 : { a: boolean; b: string; } + +let x2 = c1.a; +>x2 : boolean +>c1.a : boolean +>c1 : { a: boolean; b: string; } +>a : boolean + +import c2 = require('./c.json'); // resolves to c.json +>c2 : { "a": boolean; "b": string; } + +if (x2) { +>x2 : boolean + + let b = c2.b; +>b : string +>c2.b : string +>c2 : { "a": boolean; "b": string; } +>b : string + + let x = (c1.b === b); +>x : boolean +>(c1.b === b) : boolean +>c1.b === b : boolean +>c1.b : string +>c1 : { a: boolean; b: string; } +>b : string +>b : string +} + +=== tests/cases/compiler/c.json === +{ +>{ "a": true, "b": "hello"} : { "a": boolean; "b": string; } + + "a": true, +>"a" : boolean +>true : true + + "b": "hello" +>"b" : string +>"hello" : "hello" +} + +=== tests/cases/compiler/c.ts === +export = { a: true, b: "hello" }; +>{ a: true, b: "hello" } : { a: boolean; b: string; } +>a : boolean +>true : true +>b : string +>"hello" : "hello" + diff --git a/tests/baselines/reference/requireOfJsonFileWithoutOutDir.errors.txt b/tests/baselines/reference/requireOfJsonFileWithoutOutDir.errors.txt new file mode 100644 index 0000000000000..07f0f1b84d76c --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithoutOutDir.errors.txt @@ -0,0 +1,20 @@ +error TS5055: Cannot write file 'tests/cases/compiler/b.json' because it would overwrite input file. + Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig. + + +!!! error TS5055: Cannot write file 'tests/cases/compiler/b.json' because it would overwrite input file. +!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig. +==== tests/cases/compiler/file1.ts (0 errors) ==== + import b1 = require('./b.json'); + let x = b1.a; + import b2 = require('./b.json'); + if (x) { + let b = b2.b; + x = (b1.b === b); + } + +==== tests/cases/compiler/b.json (0 errors) ==== + { + "a": true, + "b": "hello" + } \ No newline at end of file diff --git a/tests/baselines/reference/requireOfJsonFileWithoutOutDir.js b/tests/baselines/reference/requireOfJsonFileWithoutOutDir.js new file mode 100644 index 0000000000000..d456f1f8c9f40 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithoutOutDir.js @@ -0,0 +1,27 @@ +//// [tests/cases/compiler/requireOfJsonFileWithoutOutDir.ts] //// + +//// [file1.ts] +import b1 = require('./b.json'); +let x = b1.a; +import b2 = require('./b.json'); +if (x) { + let b = b2.b; + x = (b1.b === b); +} + +//// [b.json] +{ + "a": true, + "b": "hello" +} + +//// [tests/cases/compiler/file1.js] +"use strict"; +exports.__esModule = true; +var b1 = require("./b.json"); +var x = b1.a; +var b2 = require("./b.json"); +if (x) { + var b = b2.b; + x = (b1.b === b); +} diff --git a/tests/baselines/reference/requireOfJsonFileWithoutOutDir.symbols b/tests/baselines/reference/requireOfJsonFileWithoutOutDir.symbols new file mode 100644 index 0000000000000..a8f82b6b14590 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithoutOutDir.symbols @@ -0,0 +1,38 @@ +=== tests/cases/compiler/file1.ts === +import b1 = require('./b.json'); +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) + +let x = b1.a; +>x : Symbol(x, Decl(file1.ts, 1, 3)) +>b1.a : Symbol("a", Decl(b.json, 0, 1)) +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) +>a : Symbol("a", Decl(b.json, 0, 1)) + +import b2 = require('./b.json'); +>b2 : Symbol(b2, Decl(file1.ts, 1, 13)) + +if (x) { +>x : Symbol(x, Decl(file1.ts, 1, 3)) + + let b = b2.b; +>b : Symbol(b, Decl(file1.ts, 4, 7)) +>b2.b : Symbol("b", Decl(b.json, 1, 14)) +>b2 : Symbol(b2, Decl(file1.ts, 1, 13)) +>b : Symbol("b", Decl(b.json, 1, 14)) + + x = (b1.b === b); +>x : Symbol(x, Decl(file1.ts, 1, 3)) +>b1.b : Symbol("b", Decl(b.json, 1, 14)) +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) +>b : Symbol("b", Decl(b.json, 1, 14)) +>b : Symbol(b, Decl(file1.ts, 4, 7)) +} + +=== tests/cases/compiler/b.json === +{ + "a": true, +>"a" : Symbol("a", Decl(b.json, 0, 1)) + + "b": "hello" +>"b" : Symbol("b", Decl(b.json, 1, 14)) +} diff --git a/tests/baselines/reference/requireOfJsonFileWithoutOutDir.types b/tests/baselines/reference/requireOfJsonFileWithoutOutDir.types new file mode 100644 index 0000000000000..c718c429408b4 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithoutOutDir.types @@ -0,0 +1,45 @@ +=== tests/cases/compiler/file1.ts === +import b1 = require('./b.json'); +>b1 : { "a": boolean; "b": string; } + +let x = b1.a; +>x : boolean +>b1.a : boolean +>b1 : { "a": boolean; "b": string; } +>a : boolean + +import b2 = require('./b.json'); +>b2 : { "a": boolean; "b": string; } + +if (x) { +>x : boolean + + let b = b2.b; +>b : string +>b2.b : string +>b2 : { "a": boolean; "b": string; } +>b : string + + x = (b1.b === b); +>x = (b1.b === b) : boolean +>x : boolean +>(b1.b === b) : boolean +>b1.b === b : boolean +>b1.b : string +>b1 : { "a": boolean; "b": string; } +>b : string +>b : string +} + +=== tests/cases/compiler/b.json === +{ +>{ "a": true, "b": "hello"} : { "a": boolean; "b": string; } + + "a": true, +>"a" : boolean +>true : true + + "b": "hello" +>"b" : string +>"hello" : "hello" +} diff --git a/tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModule.errors.txt b/tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModule.errors.txt new file mode 100644 index 0000000000000..7c720cd70ac7d --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModule.errors.txt @@ -0,0 +1,19 @@ +tests/cases/compiler/file1.ts(1,21): error TS2307: Cannot find module './b.json'. +tests/cases/compiler/file1.ts(3,21): error TS2307: Cannot find module './b.json'. + + +==== tests/cases/compiler/file1.ts (2 errors) ==== + import b1 = require('./b.json'); // error + ~~~~~~~~~~ +!!! error TS2307: Cannot find module './b.json'. + let x = b1.a; + import b2 = require('./b.json'); // error + ~~~~~~~~~~ +!!! error TS2307: Cannot find module './b.json'. + if (x) { + let b = b2.b; + x = (b1.b === b); + } + +==== tests/cases/compiler/b.json (0 errors) ==== + contents Not read \ No newline at end of file diff --git a/tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModule.js b/tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModule.js new file mode 100644 index 0000000000000..1f01da4df4b30 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModule.js @@ -0,0 +1,24 @@ +//// [tests/cases/compiler/requireOfJsonFileWithoutResolveJsonModule.ts] //// + +//// [file1.ts] +import b1 = require('./b.json'); // error +let x = b1.a; +import b2 = require('./b.json'); // error +if (x) { + let b = b2.b; + x = (b1.b === b); +} + +//// [b.json] +contents Not read + +//// [out/file1.js] +"use strict"; +exports.__esModule = true; +var b1 = require("./b.json"); // error +var x = b1.a; +var b2 = require("./b.json"); // error +if (x) { + var b = b2.b; + x = (b1.b === b); +} diff --git a/tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModule.symbols b/tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModule.symbols new file mode 100644 index 0000000000000..75de9c447907c --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModule.symbols @@ -0,0 +1,24 @@ +=== tests/cases/compiler/file1.ts === +import b1 = require('./b.json'); // error +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) + +let x = b1.a; +>x : Symbol(x, Decl(file1.ts, 1, 3)) +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) + +import b2 = require('./b.json'); // error +>b2 : Symbol(b2, Decl(file1.ts, 1, 13)) + +if (x) { +>x : Symbol(x, Decl(file1.ts, 1, 3)) + + let b = b2.b; +>b : Symbol(b, Decl(file1.ts, 4, 7)) +>b2 : Symbol(b2, Decl(file1.ts, 1, 13)) + + x = (b1.b === b); +>x : Symbol(x, Decl(file1.ts, 1, 3)) +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) +>b : Symbol(b, Decl(file1.ts, 4, 7)) +} + diff --git a/tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModule.types b/tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModule.types new file mode 100644 index 0000000000000..b288772663fed --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModule.types @@ -0,0 +1,33 @@ +=== tests/cases/compiler/file1.ts === +import b1 = require('./b.json'); // error +>b1 : any + +let x = b1.a; +>x : any +>b1.a : any +>b1 : any +>a : any + +import b2 = require('./b.json'); // error +>b2 : any + +if (x) { +>x : any + + let b = b2.b; +>b : any +>b2.b : any +>b2 : any +>b : any + + x = (b1.b === b); +>x = (b1.b === b) : boolean +>x : any +>(b1.b === b) : boolean +>b1.b === b : boolean +>b1.b : any +>b1 : any +>b : any +>b : any +} + diff --git a/tests/cases/compiler/requireOfJsonFile.ts b/tests/cases/compiler/requireOfJsonFile.ts new file mode 100644 index 0000000000000..b1e8e3ff25dbc --- /dev/null +++ b/tests/cases/compiler/requireOfJsonFile.ts @@ -0,0 +1,20 @@ +// @module: commonjs +// @outdir: out/ +// @allowJs: true +// @fullEmitPaths: true +// @resolveJsonModule: true + +// @Filename: file1.ts +import b1 = require('./b.json'); +let x = b1.a; +import b2 = require('./b.json'); +if (x) { + let b = b2.b; + x = (b1.b === b); +} + +// @Filename: b.json +{ + "a": true, + "b": "hello" +} \ No newline at end of file diff --git a/tests/cases/compiler/requireOfJsonFileNonRelative.ts b/tests/cases/compiler/requireOfJsonFileNonRelative.ts new file mode 100644 index 0000000000000..0cfce0bdb1a0e --- /dev/null +++ b/tests/cases/compiler/requireOfJsonFileNonRelative.ts @@ -0,0 +1,26 @@ +// @module: commonjs +// @outdir: out/ +// @allowJs: true +// @fullEmitPaths: true +// @resolveJsonModule: true + +// @Filename: /src/projects/file1.ts +import b1 = require('b.json'); +let x = b1.a; +import b2 = require('c.json'); +if (x) { + let b = b2.b; + x = (b1.b === b); +} + +// @Filename: /src/projects/node_modules/b.json +{ + "a": true, + "b": "hello" +} + +// @Filename: /src/node_modules/c.json +{ + "a": true, + "b": "hello" +} \ No newline at end of file diff --git a/tests/cases/compiler/requireOfJsonFileNonRelativeWithoutExtension.ts b/tests/cases/compiler/requireOfJsonFileNonRelativeWithoutExtension.ts new file mode 100644 index 0000000000000..7e1637a40f9d9 --- /dev/null +++ b/tests/cases/compiler/requireOfJsonFileNonRelativeWithoutExtension.ts @@ -0,0 +1,27 @@ +// @module: commonjs +// @outdir: out/ +// @allowJs: true +// @fullEmitPaths: true +// @resolveJsonModule: true + +// @Filename: /src/projects/file1.ts +import d = require("d.json"); // Should fail +import e = require("e"); // Should fail + +// @Filename: /src/projects/node_modules/b.json +{ + "a": true, + "b": "hello" +} + +// @Filename: /src/projects/node_modules/e.json +{ + "a": true, + "b": "hello" +} + +// @Filename: /src/node_modules/c.json +{ + "a": true, + "b": "hello" +} \ No newline at end of file diff --git a/tests/cases/compiler/requireOfJsonFileNonRelativeWithoutExtensionResolvesToTs.ts b/tests/cases/compiler/requireOfJsonFileNonRelativeWithoutExtensionResolvesToTs.ts new file mode 100644 index 0000000000000..4c8181b7a1007 --- /dev/null +++ b/tests/cases/compiler/requireOfJsonFileNonRelativeWithoutExtensionResolvesToTs.ts @@ -0,0 +1,18 @@ +// @module: commonjs +// @outdir: out/ +// @allowJs: true +// @fullEmitPaths: true +// @resolveJsonModule: true + +// @Filename: /src/projects/file1.ts +import f = require("f"); // should work to f.ts +let fnumber: number = f; + +// @Filename: /src/node_modules/f.json +{ + "a": true, + "b": "hello" +} + +// @Filename: /src/node_modules/f.ts +export = 10; \ No newline at end of file diff --git a/tests/cases/compiler/requireOfJsonFileTypes.ts b/tests/cases/compiler/requireOfJsonFileTypes.ts new file mode 100644 index 0000000000000..ce2aed2e824f2 --- /dev/null +++ b/tests/cases/compiler/requireOfJsonFileTypes.ts @@ -0,0 +1,51 @@ +// @module: commonjs +// @outdir: out/ +// @allowJs: true +// @strictNullChecks: true +// @fullEmitPaths: true +// @resolveJsonModule: true + +// @Filename: file1.ts +import b = require('./b.json'); +import c = require('./c.json'); +import d = require('./d.json'); +import e = require('./e.json'); +import f = require('./f.json'); +import g = require('./g.json'); + +let booleanLiteral: boolean, nullLiteral: null; +let stringLiteral: string; +let numberLiteral: number; + +booleanLiteral = b.a; +stringLiteral = b.b; +nullLiteral = b.c; +booleanLiteral = b.d; +const stringOrNumberOrNull: string | number | null = c[0]; +stringLiteral = d; +numberLiteral = e; +numberLiteral = f[0]; +booleanLiteral = g[0]; + +// @Filename: b.json +{ + "a": true, + "b": "hello", + "c": null, + "d": false +} + +// @Filename: c.json +["a", null, "string"] + +// @Filename: d.json +"dConfig" + +// @Filename: e.json +-10 + +// @Filename: f.json +[-10, 30] + +// @Filename: g.json +[true, false] \ No newline at end of file diff --git a/tests/cases/compiler/requireOfJsonFileWithAmd.ts b/tests/cases/compiler/requireOfJsonFileWithAmd.ts new file mode 100644 index 0000000000000..3d5f44b61d6bb --- /dev/null +++ b/tests/cases/compiler/requireOfJsonFileWithAmd.ts @@ -0,0 +1,20 @@ +// @module: amd +// @outdir: out/ +// @allowJs: true +// @fullEmitPaths: true +// @resolveJsonModule: true + +// @Filename: file1.ts +import b1 = require('./b'); +let x = b1.a; +import b2 = require('./b.json'); +if (x) { + let b = b2.b; + x = (b1.b === b); +} + +// @Filename: b.json +{ + "a": true, + "b": "hello" +} \ No newline at end of file diff --git a/tests/cases/compiler/requireOfJsonFileWithEmptyObject.ts b/tests/cases/compiler/requireOfJsonFileWithEmptyObject.ts new file mode 100644 index 0000000000000..a098669973fe0 --- /dev/null +++ b/tests/cases/compiler/requireOfJsonFileWithEmptyObject.ts @@ -0,0 +1,17 @@ +// @module: commonjs +// @outdir: out/ +// @allowJs: true +// @fullEmitPaths: true +// @resolveJsonModule: true + +// @Filename: file1.ts +import b1 = require('./b.json'); +let x = b1; +import b2 = require('./b.json'); +if (x) { + x = b2; +} + +// @Filename: b.json +{ +} \ No newline at end of file diff --git a/tests/cases/compiler/requireOfJsonFileWithEmptyObjectWithErrors.ts b/tests/cases/compiler/requireOfJsonFileWithEmptyObjectWithErrors.ts new file mode 100644 index 0000000000000..e28fa58ba86a2 --- /dev/null +++ b/tests/cases/compiler/requireOfJsonFileWithEmptyObjectWithErrors.ts @@ -0,0 +1,18 @@ +// @module: commonjs +// @outdir: out/ +// @allowJs: true +// @fullEmitPaths: true +// @resolveJsonModule: true + +// @Filename: file1.ts +import b1 = require('./b.json'); +let x = b1.a; +import b2 = require('./b.json'); +if (x) { + let b = b2.b; + x = (b1.b === b); +} + +// @Filename: b.json +{ +} \ No newline at end of file diff --git a/tests/cases/compiler/requireOfJsonFileWithErrors.ts b/tests/cases/compiler/requireOfJsonFileWithErrors.ts new file mode 100644 index 0000000000000..e3d830a26d4de --- /dev/null +++ b/tests/cases/compiler/requireOfJsonFileWithErrors.ts @@ -0,0 +1,20 @@ +// @module: commonjs +// @outdir: out/ +// @allowJs: true +// @fullEmitPaths: true +// @resolveJsonModule: true + +// @Filename: file1.ts +import b1 = require('./b.json'); +let x = b1.a; +import b2 = require('./b.json'); +if (x) { + let b = b2.b; + x = (b1.b === b); +} + +// @Filename: b.json +{ + 'a': true, + "b": "hello" +} \ No newline at end of file diff --git a/tests/cases/compiler/requireOfJsonFileWithNoContent.ts b/tests/cases/compiler/requireOfJsonFileWithNoContent.ts new file mode 100644 index 0000000000000..9aed256e0bc0a --- /dev/null +++ b/tests/cases/compiler/requireOfJsonFileWithNoContent.ts @@ -0,0 +1,16 @@ +// @module: commonjs +// @outdir: out/ +// @allowJs: true +// @fullEmitPaths: true +// @resolveJsonModule: true + +// @Filename: file1.ts +import b1 = require('./b.json'); +let x = b1.a; +import b2 = require('./b.json'); +if (x) { + let b = b2.b; + x = (b1.b === b); +} + +// @Filename: b.json diff --git a/tests/cases/compiler/requireOfJsonFileWithoutAllowJs.ts b/tests/cases/compiler/requireOfJsonFileWithoutAllowJs.ts new file mode 100644 index 0000000000000..c6dc60e9e650f --- /dev/null +++ b/tests/cases/compiler/requireOfJsonFileWithoutAllowJs.ts @@ -0,0 +1,19 @@ +// @module: commonjs +// @outdir: out/ +// @fullEmitPaths: true +// @resolveJsonModule: true + +// @Filename: file1.ts +import b1 = require('./b.json'); +let x = b1.a; +import b2 = require('./b.json'); +if (x) { + let b = b2.b; + x = (b1.b === b); +} + +// @Filename: b.json +{ + "a": true, + "b": "hello" +} \ No newline at end of file diff --git a/tests/cases/compiler/requireOfJsonFileWithoutExtension.ts b/tests/cases/compiler/requireOfJsonFileWithoutExtension.ts new file mode 100644 index 0000000000000..a000f3404354a --- /dev/null +++ b/tests/cases/compiler/requireOfJsonFileWithoutExtension.ts @@ -0,0 +1,20 @@ +// @module: commonjs +// @outdir: out/ +// @allowJs: true +// @fullEmitPaths: true +// @resolveJsonModule: true + +// @Filename: file1.ts +import b1 = require('./b'); // This should not resolve +let x = b1.a; +import b2 = require('./b.json'); +if (x) { + let b = b2.b; + x = (b1.b === b); +} + +// @Filename: b.json +{ + "a": true, + "b": "hello" +} \ No newline at end of file diff --git a/tests/cases/compiler/requireOfJsonFileWithoutExtensionResolvesToTs.ts b/tests/cases/compiler/requireOfJsonFileWithoutExtensionResolvesToTs.ts new file mode 100644 index 0000000000000..30a2c193c239e --- /dev/null +++ b/tests/cases/compiler/requireOfJsonFileWithoutExtensionResolvesToTs.ts @@ -0,0 +1,29 @@ +// @module: commonjs +// @outdir: out/ +// @allowJs: true +// @fullEmitPaths: true +// @resolveJsonModule: true + +// @Filename: file1.ts +import c1 = require('./c'); // resolves to c.ts +let x2 = c1.a; +import c2 = require('./c.json'); // resolves to c.json +if (x2) { + let b = c2.b; + let x = (c1.b === b); +} + +// @Filename: b.json +{ + "a": true, + "b": "hello" +} + +// @Filename: c.json +{ + "a": true, + "b": "hello" +} + +// @Filename: c.ts +export = { a: true, b: "hello" }; \ No newline at end of file diff --git a/tests/cases/compiler/requireOfJsonFileWithoutOutDir.ts b/tests/cases/compiler/requireOfJsonFileWithoutOutDir.ts new file mode 100644 index 0000000000000..b25afcba49457 --- /dev/null +++ b/tests/cases/compiler/requireOfJsonFileWithoutOutDir.ts @@ -0,0 +1,18 @@ +// @module: commonjs +// @fullEmitPaths: true +// @resolveJsonModule: true + +// @Filename: file1.ts +import b1 = require('./b.json'); +let x = b1.a; +import b2 = require('./b.json'); +if (x) { + let b = b2.b; + x = (b1.b === b); +} + +// @Filename: b.json +{ + "a": true, + "b": "hello" +} \ No newline at end of file diff --git a/tests/cases/compiler/requireOfJsonFileWithoutResolveJsonModule.ts b/tests/cases/compiler/requireOfJsonFileWithoutResolveJsonModule.ts new file mode 100644 index 0000000000000..76af00dbc7a6d --- /dev/null +++ b/tests/cases/compiler/requireOfJsonFileWithoutResolveJsonModule.ts @@ -0,0 +1,16 @@ +// @module: commonjs +// @outdir: out/ +// @allowJs: true +// @fullEmitPaths: true + +// @Filename: file1.ts +import b1 = require('./b.json'); // error +let x = b1.a; +import b2 = require('./b.json'); // error +if (x) { + let b = b2.b; + x = (b1.b === b); +} + +// @Filename: b.json +contents Not read \ No newline at end of file