Skip to content

Fix crash in JS declaration emit #38508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5949,6 +5949,25 @@ namespace ts {
}
}

function isEffectiveClassSymbol(symbol: Symbol) {
if (!(symbol.flags & SymbolFlags.Class)) {
return false;
}
if (isInJSFile(symbol.valueDeclaration) && !isClassLike(symbol.valueDeclaration)) {
// For a symbol that isn't syntactically a `class` in a JS file we have heuristics
// that detect prototype assignments that indicate the symbol is *probably* a class.
// Filter out any prototype assignments for non-class symbols, i.e.
//
// let A;
// A = {};
// A.prototype.b = {};
const type = getTypeOfSymbol(symbol);
return some(getSignaturesOfType(type, SignatureKind.Construct))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, thinking about it, is there a way to adjust the binder to only apply the Class flag if the thing is actually constructable?

Alternatively, should we still emit this as a class, but with a private constructor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Binder has no access to type information, so there's no way to be sure because of augmentations, merging globals, etc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the private constructor suggestion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can look into it.

|| some(getSignaturesOfType(type, SignatureKind.Call));
}
return true;
}

// Synthesize declarations for a symbol - might be an Interface, a Class, a Namespace, a Type, a Variable (const, let, or var), an Alias
// or a merge of some number of those.
// An interesting challenge is ensuring that when classes merge with namespaces and interfaces, is keeping
Expand Down Expand Up @@ -5990,14 +6009,14 @@ namespace ts {
if (symbol.flags & (SymbolFlags.BlockScopedVariable | SymbolFlags.FunctionScopedVariable | SymbolFlags.Property)
&& symbol.escapedName !== InternalSymbolName.ExportEquals
&& !(symbol.flags & SymbolFlags.Prototype)
&& !(symbol.flags & SymbolFlags.Class)
&& !isEffectiveClassSymbol(symbol)
&& !isConstMergedWithNSPrintableAsSignatureMerge) {
serializeVariableOrProperty(symbol, symbolName, isPrivate, needsPostExportDefault, propertyAsAlias, modifierFlags);
}
if (symbol.flags & SymbolFlags.Enum) {
serializeEnum(symbol, symbolName, modifierFlags);
}
if (symbol.flags & SymbolFlags.Class) {
if (isEffectiveClassSymbol(symbol)) {
if (symbol.flags & SymbolFlags.Property && isBinaryExpression(symbol.valueDeclaration.parent) && isClassExpression(symbol.valueDeclaration.parent.right)) {
// Looks like a `module.exports.Sub = class {}` - if we serialize `symbol` as a class, the result will have no members,
// since the classiness is actually from the target of the effective alias the symbol is. yes. A BlockScopedVariable|Class|Property
Expand Down Expand Up @@ -6317,7 +6336,9 @@ namespace ts {
const baseTypes = getBaseTypes(classType);
const implementsTypes = getImplementsTypes(classType);
const staticType = getTypeOfSymbol(symbol);
const staticBaseType = getBaseConstructorTypeOfClass(staticType as InterfaceType);
const staticBaseType = staticType.symbol?.valueDeclaration && isClassLike(staticType.symbol.valueDeclaration)
? getBaseConstructorTypeOfClass(staticType as InterfaceType)
: anyType;
const heritageClauses = [
...!length(baseTypes) ? [] : [createHeritageClause(SyntaxKind.ExtendsKeyword, map(baseTypes, b => serializeBaseType(b, staticBaseType, localName)))],
...!length(implementsTypes) ? [] : [createHeritageClause(SyntaxKind.ImplementsKeyword, map(implementsTypes, b => serializeBaseType(b, staticBaseType, localName)))]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
tests/cases/conformance/jsdoc/declarations/index.js(4,3): error TS2339: Property 'prototype' does not exist on type '{}'.


==== tests/cases/conformance/jsdoc/declarations/index.js (1 errors) ====
// https://github.com/microsoft/TypeScript/issues/35801
let A;
A = {};
A.prototype.b = {};
~~~~~~~~~
!!! error TS2339: Property 'prototype' does not exist on type '{}'.
15 changes: 15 additions & 0 deletions tests/baselines/reference/jsDeclarationsClassLikeHeuristic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//// [index.js]
// https://github.com/microsoft/TypeScript/issues/35801
let A;
A = {};
A.prototype.b = {};

//// [index.js]
// https://github.com/microsoft/TypeScript/issues/35801
var A;
A = {};
A.prototype.b = {};


//// [index.d.ts]
declare let A: any;
13 changes: 13 additions & 0 deletions tests/baselines/reference/jsDeclarationsClassLikeHeuristic.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
=== tests/cases/conformance/jsdoc/declarations/index.js ===
// https://github.com/microsoft/TypeScript/issues/35801
let A;
>A : Symbol(A, Decl(index.js, 1, 3))

A = {};
>A : Symbol(A, Decl(index.js, 1, 3))

A.prototype.b = {};
>A.prototype : Symbol(A.b, Decl(index.js, 2, 7))
>A : Symbol(A, Decl(index.js, 1, 3))
>b : Symbol(A.b, Decl(index.js, 2, 7))

19 changes: 19 additions & 0 deletions tests/baselines/reference/jsDeclarationsClassLikeHeuristic.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
=== tests/cases/conformance/jsdoc/declarations/index.js ===
// https://github.com/microsoft/TypeScript/issues/35801
let A;
>A : any

A = {};
>A = {} : {}
>A : any
>{} : {}

A.prototype.b = {};
>A.prototype.b = {} : {}
>A.prototype.b : any
>A.prototype : any
>A : {}
>prototype : any
>b : any
>{} : {}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @allowJs: true
// @checkJs: true
// @target: es5
// @outDir: ./out
// @declaration: true
// @filename: index.js
// https://github.com/microsoft/TypeScript/issues/35801
let A;
A = {};
A.prototype.b = {};