Skip to content

Commit 5133c70

Browse files
authoredMay 4, 2018
Merge pull request #22167 from Microsoft/requireJson
Resolve the modue names with "modulename.json" to json files when doing node module resolution
2 parents ec19733 + c4143ae commit 5133c70

File tree

87 files changed

+2489
-139
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+2489
-139
lines changed
 

‎src/compiler/binder.ts

+11
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,10 @@ namespace ts {
281281
return InternalSymbolName.Index;
282282
case SyntaxKind.ExportDeclaration:
283283
return InternalSymbolName.ExportStar;
284+
case SyntaxKind.SourceFile:
285+
// json file should behave as
286+
// module.exports = ...
287+
return InternalSymbolName.ExportEquals;
284288
case SyntaxKind.BinaryExpression:
285289
if (getSpecialPropertyAssignmentKind(node as BinaryExpression) === SpecialPropertyAssignmentKind.ModuleExports) {
286290
// module.exports = ...
@@ -2234,6 +2238,13 @@ namespace ts {
22342238
if (isExternalModule(file)) {
22352239
bindSourceFileAsExternalModule();
22362240
}
2241+
else if (isJsonSourceFile(file)) {
2242+
bindSourceFileAsExternalModule();
2243+
// Create symbol equivalent for the module.exports = {}
2244+
const originalSymbol = file.symbol;
2245+
declareSymbol(file.symbol.exports, file.symbol, file, SymbolFlags.Property, SymbolFlags.All);
2246+
file.symbol = originalSymbol;
2247+
}
22372248
}
22382249

22392250
function bindSourceFileAsExternalModule() {

‎src/compiler/checker.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -2207,7 +2207,7 @@ namespace ts {
22072207
}
22082208

22092209
// May be an untyped module. If so, ignore resolutionDiagnostic.
2210-
if (resolvedModule && !extensionIsTypeScript(resolvedModule.extension) && resolutionDiagnostic === undefined || resolutionDiagnostic === Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type) {
2210+
if (resolvedModule && !resolutionExtensionIsTypeScriptOrJson(resolvedModule.extension) && resolutionDiagnostic === undefined || resolutionDiagnostic === Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type) {
22112211
if (isForAugmentation) {
22122212
const diag = Diagnostics.Invalid_module_name_in_augmentation_Module_0_resolves_to_an_untyped_module_at_1_which_cannot_be_augmented;
22132213
error(errorNode, diag, moduleReference, resolvedModule.resolvedFileName);
@@ -4718,6 +4718,10 @@ namespace ts {
47184718
return links.type = anyType;
47194719
}
47204720
// Handle export default expressions
4721+
if (isSourceFile(declaration)) {
4722+
const jsonSourceFile = cast(declaration, isJsonSourceFile);
4723+
return links.type = jsonSourceFile.statements.length ? checkExpression(jsonSourceFile.statements[0].expression) : emptyObjectType;
4724+
}
47214725
if (declaration.kind === SyntaxKind.ExportAssignment) {
47224726
return links.type = checkExpression((<ExportAssignment>declaration).expression);
47234727
}
@@ -15597,7 +15601,7 @@ namespace ts {
1559715601
const contextualType = getApparentTypeOfContextualType(node);
1559815602
const contextualTypeHasPattern = contextualType && contextualType.pattern &&
1559915603
(contextualType.pattern.kind === SyntaxKind.ObjectBindingPattern || contextualType.pattern.kind === SyntaxKind.ObjectLiteralExpression);
15600-
const isInJSFile = isInJavaScriptFile(node);
15604+
const isInJSFile = isInJavaScriptFile(node) && !isInJsonFile(node);
1560115605
const isJSObjectLiteral = !contextualType && isInJSFile;
1560215606
let typeFlags: TypeFlags = 0;
1560315607
let patternWithComputedProperties = false;

0 commit comments

Comments
 (0)