Avoid a crash with @typedef in a script file.

Scripts (as opposed to modules) do not have a symbol object. If a script
contains a `@typdef` defined on a namespace called `exports`, TypeScript
crashes because it attempts to create an exported symbol on the
(non-existent) symbol of the SourceFile.

This change avoids the crash by explicitly checking if the source file
has a symbol object, i.e. whether it is a module.
This commit is contained in:
Martin Probst
2019-10-07 14:18:15 +02:00
committed by Wesley Wigham
parent 07d3a2ec7e
commit 97dcbd3bb9
5 changed files with 58 additions and 1 deletions
+1 -1
View File
@@ -564,7 +564,7 @@ namespace ts {
// and this case is specially handled. Module augmentations should only be merged with original module definition
// and should never be merged directly with other augmentation, and the latter case would be possible if automatic merge is allowed.
if (isJSDocTypeAlias(node)) Debug.assert(isInJSFile(node)); // We shouldn't add symbols for JSDoc nodes if not in a JS file.
if ((!isAmbientModule(node) && (hasExportModifier || container.flags & NodeFlags.ExportContext)) || isJSDocTypeAlias(node)) {
if ((!isAmbientModule(node) && (hasExportModifier || container.flags & NodeFlags.ExportContext)) || (isJSDocTypeAlias(node) && container.symbol)) {
if (!container.locals || (hasModifier(node, ModifierFlags.Default) && !getDeclarationName(node))) {
return declareSymbol(container.symbol.exports!, container.symbol, node, symbolFlags, symbolExcludes); // No local symbol for an unnamed default!
}
@@ -0,0 +1,18 @@
//// [0.js]
// @ts-check
var exports = {};
/**
* @typedef {string}
*/
exports.SomeName;
//// [0.js]
// @ts-check
var exports = {};
/**
* @typedef {string}
*/
exports.SomeName;
@@ -0,0 +1,12 @@
=== tests/cases/conformance/jsdoc/0.js ===
// @ts-check
var exports = {};
>exports : Symbol(exports, Decl(0.js, 2, 3))
/**
* @typedef {string}
*/
exports.SomeName;
>exports : Symbol(exports, Decl(0.js, 2, 3))
@@ -0,0 +1,15 @@
=== tests/cases/conformance/jsdoc/0.js ===
// @ts-check
var exports = {};
>exports : {}
>{} : {}
/**
* @typedef {string}
*/
exports.SomeName;
>exports.SomeName : any
>exports : {}
>SomeName : any
@@ -0,0 +1,12 @@
// @allowJS: true
// @suppressOutputPathCheck: true
// @filename: 0.js
// @ts-check
var exports = {};
/**
* @typedef {string}
*/
exports.SomeName;