Skip to content

Commit dda7cd8

Browse files
committed
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.
1 parent f1c0300 commit dda7cd8

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

src/compiler/binder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ namespace ts {
564564
// and this case is specially handled. Module augmentations should only be merged with original module definition
565565
// and should never be merged directly with other augmentation, and the latter case would be possible if automatic merge is allowed.
566566
if (isJSDocTypeAlias(node)) Debug.assert(isInJSFile(node)); // We shouldn't add symbols for JSDoc nodes if not in a JS file.
567-
if ((!isAmbientModule(node) && (hasExportModifier || container.flags & NodeFlags.ExportContext)) || isJSDocTypeAlias(node)) {
567+
if ((!isAmbientModule(node) && (hasExportModifier || container.flags & NodeFlags.ExportContext)) || (isJSDocTypeAlias(node) && container.symbol)) {
568568
if (!container.locals || (hasModifier(node, ModifierFlags.Default) && !getDeclarationName(node))) {
569569
return declareSymbol(container.symbol.exports!, container.symbol, node, symbolFlags, symbolExcludes); // No local symbol for an unnamed default!
570570
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [0.js]
2+
// @ts-check
3+
4+
var exports = {};
5+
6+
/**
7+
* @typedef {string}
8+
*/
9+
exports.SomeName;
10+
11+
12+
//// [0.js]
13+
// @ts-check
14+
var exports = {};
15+
/**
16+
* @typedef {string}
17+
*/
18+
exports.SomeName;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/conformance/jsdoc/0.js ===
2+
// @ts-check
3+
4+
var exports = {};
5+
>exports : Symbol(exports, Decl(0.js, 2, 3))
6+
7+
/**
8+
* @typedef {string}
9+
*/
10+
exports.SomeName;
11+
>exports : Symbol(exports, Decl(0.js, 2, 3))
12+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/conformance/jsdoc/0.js ===
2+
// @ts-check
3+
4+
var exports = {};
5+
>exports : {}
6+
>{} : {}
7+
8+
/**
9+
* @typedef {string}
10+
*/
11+
exports.SomeName;
12+
>exports.SomeName : any
13+
>exports : {}
14+
>SomeName : any
15+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @allowJS: true
2+
// @suppressOutputPathCheck: true
3+
4+
// @filename: 0.js
5+
// @ts-check
6+
7+
var exports = {};
8+
9+
/**
10+
* @typedef {string}
11+
*/
12+
exports.SomeName;

0 commit comments

Comments
 (0)