Skip to content

Commit

Permalink
Cherry-pick PR #36996 into release-3.8 (#37002)
Browse files Browse the repository at this point in the history
Component commits:
888ebf4 Fix crash on aliased,exported @enum tag in jsdoc
THe code to bind `@enum` and `@typedef` didn't handle the case that the
`@enum` was on a property assignment to an alias of module.exports.
Specifically, `x` needs to be correctly aliased to the file's symbol in
the example below:

```
var x = module.exports = {};
/** @enum {string} */
x.E = {
  A: "A"
};
```

Co-authored-by: Nathan Shively-Sanders <[email protected]>
  • Loading branch information
TypeScript Bot and sandersn authored Feb 25, 2020
1 parent af614cc commit 98fc075
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2113,7 +2113,9 @@ namespace ts {
container = (declName.parent.expression as PropertyAccessExpression).name;
break;
case AssignmentDeclarationKind.Property:
container = isPropertyAccessExpression(declName.parent.expression) ? declName.parent.expression.name : declName.parent.expression;
container = isExportsOrModuleExportsOrAlias(file, declName.parent.expression) ? file
: isPropertyAccessExpression(declName.parent.expression) ? declName.parent.expression.name
: declName.parent.expression;
break;
case AssignmentDeclarationKind.None:
return Debug.fail("Shouldn't have detected typedef or enum on non-assignment declaration");
Expand Down
18 changes: 18 additions & 0 deletions tests/baselines/reference/exportedAliasedEnumTag.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/conformance/jsdoc/exportedAliasedEnumTag.js ===
var middlewarify = module.exports = {};
>middlewarify : Symbol(middlewarify, Decl(exportedAliasedEnumTag.js, 0, 3))
>module.exports : Symbol("tests/cases/conformance/jsdoc/exportedAliasedEnumTag", Decl(exportedAliasedEnumTag.js, 0, 0))
>module : Symbol(module, Decl(exportedAliasedEnumTag.js, 0, 18))
>exports : Symbol("tests/cases/conformance/jsdoc/exportedAliasedEnumTag", Decl(exportedAliasedEnumTag.js, 0, 0))

/** @enum */
middlewarify.Type = {
>middlewarify.Type : Symbol(Type, Decl(exportedAliasedEnumTag.js, 0, 39), Decl(exportedAliasedEnumTag.js, 3, 13), Decl(exportedAliasedEnumTag.js, 2, 4))
>middlewarify : Symbol(middlewarify, Decl(exportedAliasedEnumTag.js, 0, 3))
>Type : Symbol(Type, Decl(exportedAliasedEnumTag.js, 0, 39), Decl(exportedAliasedEnumTag.js, 3, 13), Decl(exportedAliasedEnumTag.js, 2, 4))

BEFORE: 'before'
>BEFORE : Symbol(BEFORE, Decl(exportedAliasedEnumTag.js, 3, 21))

};

23 changes: 23 additions & 0 deletions tests/baselines/reference/exportedAliasedEnumTag.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/conformance/jsdoc/exportedAliasedEnumTag.js ===
var middlewarify = module.exports = {};
>middlewarify : typeof import("tests/cases/conformance/jsdoc/exportedAliasedEnumTag")
>module.exports = {} : typeof import("tests/cases/conformance/jsdoc/exportedAliasedEnumTag")
>module.exports : typeof import("tests/cases/conformance/jsdoc/exportedAliasedEnumTag")
>module : { "\"tests/cases/conformance/jsdoc/exportedAliasedEnumTag\"": typeof import("tests/cases/conformance/jsdoc/exportedAliasedEnumTag"); }
>exports : typeof import("tests/cases/conformance/jsdoc/exportedAliasedEnumTag")
>{} : {}

/** @enum */
middlewarify.Type = {
>middlewarify.Type = { BEFORE: 'before'} : { BEFORE: string; }
>middlewarify.Type : { BEFORE: string; }
>middlewarify : typeof import("tests/cases/conformance/jsdoc/exportedAliasedEnumTag")
>Type : { BEFORE: string; }
>{ BEFORE: 'before'} : { BEFORE: string; }

BEFORE: 'before'
>BEFORE : string
>'before' : "before"

};

9 changes: 9 additions & 0 deletions tests/cases/conformance/jsdoc/exportedAliasedEnumTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @noemit: true
// @allowjs: true
// @filename: exportedAliasedEnumTag.js
var middlewarify = module.exports = {};

/** @enum */
middlewarify.Type = {
BEFORE: 'before'
};

0 comments on commit 98fc075

Please sign in to comment.