Skip to content

Commit 1e359e7

Browse files
authored
fix(parser): panic when parsing JSDoc @Satisfies with export assignments (#1647)
1 parent 6eff055 commit 1e359e7

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

internal/ast/ast.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,8 @@ func (n *Node) Initializer() *Node {
779779
return n.AsForInOrOfStatement().Initializer
780780
case KindJsxAttribute:
781781
return n.AsJsxAttribute().Initializer
782+
case KindCommonJSExport:
783+
return n.AsCommonJSExport().Initializer
782784
}
783785
panic("Unhandled case in Node.Initializer")
784786
}
@@ -806,6 +808,8 @@ func (m *mutableNode) SetInitializer(initializer *Node) {
806808
n.AsForInOrOfStatement().Initializer = initializer
807809
case KindJsxAttribute:
808810
n.AsJsxAttribute().Initializer = initializer
811+
case KindCommonJSExport:
812+
n.AsCommonJSExport().Initializer = initializer
809813
default:
810814
panic("Unhandled case in mutableNode.SetInitializer")
811815
}

internal/parser/reparser.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
357357
}
358358
}
359359
case ast.KindVariableDeclaration,
360-
ast.KindCommonJSExport, ast.KindExportAssignment, ast.KindJSExportAssignment,
360+
ast.KindCommonJSExport,
361361
ast.KindPropertyDeclaration, ast.KindPropertyAssignment, ast.KindShorthandPropertyAssignment:
362362
if parent.Initializer() != nil && tag.AsJSDocSatisfiesTag().TypeExpression != nil {
363363
parent.AsMutable().SetInitializer(p.makeNewCast(
@@ -366,7 +366,8 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
366366
false /*isAssertion*/))
367367
p.finishMutatedNode(parent)
368368
}
369-
case ast.KindReturnStatement, ast.KindParenthesizedExpression:
369+
case ast.KindReturnStatement, ast.KindParenthesizedExpression,
370+
ast.KindExportAssignment, ast.KindJSExportAssignment:
370371
if parent.Expression() != nil && tag.AsJSDocSatisfiesTag().TypeExpression != nil {
371372
parent.AsMutable().SetExpression(p.makeNewCast(
372373
p.factory.DeepCloneReparse(tag.AsJSDocSatisfiesTag().TypeExpression.Type()),
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [tests/cases/compiler/panicSatisfiesOnExportEqualsDeclaration.ts] ////
2+
3+
=== panicSatisfiesOnExportEqualsDeclaration.js ===
4+
/**
5+
* @satisfies {Record<string, never>}
6+
*/
7+
module.exports = {};
8+
>module.exports : Symbol(export=, Decl(panicSatisfiesOnExportEqualsDeclaration.js, 0, 0))
9+
>module : Symbol(module.exports)
10+
>exports : Symbol(export=, Decl(panicSatisfiesOnExportEqualsDeclaration.js, 0, 0))
11+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [tests/cases/compiler/panicSatisfiesOnExportEqualsDeclaration.ts] ////
2+
3+
=== panicSatisfiesOnExportEqualsDeclaration.js ===
4+
/**
5+
* @satisfies {Record<string, never>}
6+
*/
7+
module.exports = {};
8+
>module.exports = {} : {}
9+
>module.exports : {}
10+
>module : { "export=": {}; }
11+
>exports : {}
12+
>{} : {}
13+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @allowJs: true
2+
// @checkJs: true
3+
// @noEmit: true
4+
// @module: commonjs
5+
// @lib: esnext,dom,dom.iterable
6+
// @Filename: panicSatisfiesOnExportEqualsDeclaration.js
7+
8+
/**
9+
* @satisfies {Record<string, never>}
10+
*/
11+
module.exports = {};

0 commit comments

Comments
 (0)