Skip to content

Commit 01cbcdd

Browse files
CopilotRyanCavanaughweswigham
authored
Fix crash: JSTypeAliasDeclaration passed to GetTypeOfDeclaration during CJS declaration emit (#4663)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com> Co-authored-by: weswigham <2932786+weswigham@users.noreply.github.com>
1 parent 42bb833 commit 01cbcdd

6 files changed

Lines changed: 164 additions & 1 deletion

internal/checker/nodebuilderimpl.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2160,7 +2160,15 @@ func (b *NodeBuilderImpl) indexInfoToIndexSignatureDeclarationHelper(indexInfo *
21602160
}
21612161

21622162
func hasTypeAnnotation(declaration *ast.Declaration) bool {
2163-
return declaration != nil && declaration.Type() != nil
2163+
if declaration == nil || declaration.Type() == nil {
2164+
return false
2165+
}
2166+
// Type alias declarations have a .Type() that is their type definition, not a type annotation on a value.
2167+
// Exclude them so callers don't mistake them for annotated value declarations.
2168+
if ast.IsTypeAliasDeclaration(declaration) || ast.IsJSTypeAliasDeclaration(declaration) {
2169+
return false
2170+
}
2171+
return true
21642172
}
21652173

21662174
/**
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
local-lib/ModuleGraphConnection.js(3,1): error TS2309: An export assignment cannot be used in a module with other exported elements.
2+
local-lib/ModuleGraphConnection.js(4,16): error TS2339: Property 'T' does not exist on type 'typeof ModuleGraphConnection'.
3+
4+
5+
==== local-lib/ModuleGraphConnection.js (2 errors) ====
6+
/** @typedef {typeof T} T */
7+
const T = Symbol();
8+
module.exports = class ModuleGraphConnection {};
9+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10+
!!! error TS2309: An export assignment cannot be used in a module with other exported elements.
11+
module.exports.T = T;
12+
~
13+
!!! error TS2339: Property 'T' does not exist on type 'typeof ModuleGraphConnection'.
14+
15+
==== repro.js (0 errors) ====
16+
'use strict';
17+
/** @typedef {import('./local-lib/ModuleGraphConnection')} ImportedType */
18+
/** @type {ImportedType} */
19+
module.exports = class Repro {};
20+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//// [tests/cases/compiler/jsTypedefMergedWithModuleExportProperty.ts] ////
2+
3+
//// [ModuleGraphConnection.js]
4+
/** @typedef {typeof T} T */
5+
const T = Symbol();
6+
module.exports = class ModuleGraphConnection {};
7+
module.exports.T = T;
8+
9+
//// [repro.js]
10+
'use strict';
11+
/** @typedef {import('./local-lib/ModuleGraphConnection')} ImportedType */
12+
/** @type {ImportedType} */
13+
module.exports = class Repro {};
14+
15+
16+
//// [ModuleGraphConnection.js]
17+
"use strict";
18+
/** @typedef {typeof T} T */
19+
const T = Symbol();
20+
module.exports = class ModuleGraphConnection {
21+
};
22+
module.exports.T = T;
23+
//// [repro.js]
24+
'use strict';
25+
/** @typedef {import('./local-lib/ModuleGraphConnection')} ImportedType */
26+
/** @type {ImportedType} */
27+
module.exports = class Repro {
28+
};
29+
30+
31+
//// [ModuleGraphConnection.d.ts]
32+
export = ModuleGraphConnection;
33+
declare class ModuleGraphConnection {
34+
}
35+
declare namespace ModuleGraphConnection {
36+
const _exported: typeof T;
37+
export { _exported as T };
38+
}
39+
export type T = typeof T;
40+
/** @typedef {typeof T} T */
41+
declare const T: unique symbol;
42+
//// [repro.d.ts]
43+
export = Repro;
44+
/** @typedef {import('./local-lib/ModuleGraphConnection')} ImportedType */
45+
/** @type {ImportedType} */
46+
declare class Repro {
47+
}
48+
export type ImportedType = import('./local-lib/ModuleGraphConnection');
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//// [tests/cases/compiler/jsTypedefMergedWithModuleExportProperty.ts] ////
2+
3+
=== local-lib/ModuleGraphConnection.js ===
4+
/** @typedef {typeof T} T */
5+
const T = Symbol();
6+
>T : Symbol(T, Decl(ModuleGraphConnection.js, 1, 5), Decl(ModuleGraphConnection.js, 0, 4))
7+
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
8+
9+
module.exports = class ModuleGraphConnection {};
10+
>module.exports : Symbol(exports, Decl(ModuleGraphConnection.js, 0, 0))
11+
>module : Symbol(module, Decl(ModuleGraphConnection.js, 0, 0))
12+
>exports : Symbol(exports, Decl(ModuleGraphConnection.js, 0, 0))
13+
>ModuleGraphConnection : Symbol(ModuleGraphConnection, Decl(ModuleGraphConnection.js, 2, 16))
14+
15+
module.exports.T = T;
16+
>module.exports : Symbol(exports, Decl(ModuleGraphConnection.js, 0, 0))
17+
>module : Symbol(module, Decl(ModuleGraphConnection.js, 0, 0))
18+
>exports : Symbol(exports, Decl(ModuleGraphConnection.js, 0, 0))
19+
>T : Symbol(T, Decl(ModuleGraphConnection.js, 1, 5), Decl(ModuleGraphConnection.js, 0, 4))
20+
21+
=== repro.js ===
22+
'use strict';
23+
/** @typedef {import('./local-lib/ModuleGraphConnection')} ImportedType */
24+
/** @type {ImportedType} */
25+
module.exports = class Repro {};
26+
>module.exports : Symbol(exports, Decl(repro.js, 0, 0))
27+
>module : Symbol(module, Decl(repro.js, 0, 0))
28+
>exports : Symbol(exports, Decl(repro.js, 0, 0))
29+
>Repro : Symbol(Repro, Decl(repro.js, 3, 16))
30+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//// [tests/cases/compiler/jsTypedefMergedWithModuleExportProperty.ts] ////
2+
3+
=== local-lib/ModuleGraphConnection.js ===
4+
/** @typedef {typeof T} T */
5+
const T = Symbol();
6+
>T : unique symbol
7+
>Symbol() : unique symbol
8+
>Symbol : SymbolConstructor
9+
10+
module.exports = class ModuleGraphConnection {};
11+
>module.exports = class ModuleGraphConnection {} : typeof ModuleGraphConnection
12+
>module.exports : typeof ModuleGraphConnection
13+
>module : { exports: typeof ModuleGraphConnection; }
14+
>exports : typeof ModuleGraphConnection
15+
>class ModuleGraphConnection {} : typeof ModuleGraphConnection
16+
>ModuleGraphConnection : typeof ModuleGraphConnection
17+
18+
module.exports.T = T;
19+
>module.exports.T = T : unique symbol
20+
>module.exports.T : any
21+
>module.exports : typeof ModuleGraphConnection
22+
>module : { exports: typeof ModuleGraphConnection; }
23+
>exports : typeof ModuleGraphConnection
24+
>T : any
25+
>T : unique symbol
26+
27+
=== repro.js ===
28+
'use strict';
29+
>'use strict' : "use strict"
30+
31+
/** @typedef {import('./local-lib/ModuleGraphConnection')} ImportedType */
32+
/** @type {ImportedType} */
33+
module.exports = class Repro {};
34+
>module.exports = class Repro {} : typeof Repro
35+
>module.exports : typeof Repro
36+
>module : { exports: typeof Repro; }
37+
>exports : typeof Repro
38+
>class Repro {} : typeof Repro
39+
>Repro : typeof Repro
40+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// @allowJs: true
2+
// @checkJs: true
3+
// @target: es2015
4+
// @outDir: ./out
5+
// @declaration: true
6+
7+
// @filename: local-lib/ModuleGraphConnection.js
8+
/** @typedef {typeof T} T */
9+
const T = Symbol();
10+
module.exports = class ModuleGraphConnection {};
11+
module.exports.T = T;
12+
13+
// @filename: repro.js
14+
'use strict';
15+
/** @typedef {import('./local-lib/ModuleGraphConnection')} ImportedType */
16+
/** @type {ImportedType} */
17+
module.exports = class Repro {};

0 commit comments

Comments
 (0)