Skip to content

Commit 2742e7f

Browse files
committed
Convert exports named "default" last
Closes #1795
1 parent 2d6e49d commit 2742e7f

File tree

5 files changed

+81
-54
lines changed

5 files changed

+81
-54
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Unreleased
22

3+
### Bug Fixes
4+
5+
- If file exports a symbol both under it's real name and as `default`, the `default` export will now always be the renamed symbol, #1795.
6+
37
## v0.22.9 (2021-11-14)
48

59
### Features

src/lib/converter/converter.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -467,14 +467,16 @@ function getExports(
467467
node: ts.SourceFile | ts.ModuleBlock,
468468
symbol: ts.Symbol | undefined
469469
): ts.Symbol[] {
470+
let result: ts.Symbol[];
471+
470472
// The generated docs aren't great, but you really ought not be using
471473
// this in the first place... so it's better than nothing.
472474
const exportEq = symbol?.exports?.get("export=" as ts.__String);
473475
if (exportEq) {
474476
// JS users might also have exported types here.
475477
// We need to filter for types because otherwise static methods can show up as both
476478
// members of the export= class and as functions if a class is directly exported.
477-
return [exportEq].concat(
479+
result = [exportEq].concat(
478480
context.checker
479481
.getExportsOfModule(symbol!)
480482
.filter(
@@ -485,23 +487,33 @@ function getExports(
485487
)
486488
)
487489
);
488-
}
489-
490-
if (symbol) {
491-
return context.checker
490+
} else if (symbol) {
491+
result = context.checker
492492
.getExportsOfModule(symbol)
493493
.filter((s) => !hasAllFlags(s.flags, ts.SymbolFlags.Prototype));
494+
} else {
495+
// Global file with no inferred top level symbol, get all symbols declared in this file.
496+
const sourceFile = node.getSourceFile();
497+
result = context.checker
498+
.getSymbolsInScope(node, ts.SymbolFlags.ModuleMember)
499+
.filter((s) =>
500+
s
501+
.getDeclarations()
502+
?.some((d) => d.getSourceFile() === sourceFile)
503+
);
494504
}
495505

496-
// Global file with no inferred top level symbol, get all symbols declared in this file.
497-
const sourceFile = node.getSourceFile();
498-
const globalSymbols = context.checker
499-
.getSymbolsInScope(node, ts.SymbolFlags.ModuleMember)
500-
.filter((s) =>
501-
s.getDeclarations()?.some((d) => d.getSourceFile() === sourceFile)
502-
);
506+
// Put symbols named "default" last, #1795
507+
result.sort((a, b) => {
508+
if (a.name === "default") {
509+
return 1;
510+
} else if (b.name === "default") {
511+
return -1;
512+
}
513+
return 0;
514+
});
503515

504-
return globalSymbols;
516+
return result;
505517
}
506518

507519
function isDirectExport(symbol: ts.Symbol, file: ts.SourceFile): boolean {

src/test/converter/exports/specs.json

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"kind": 16777216,
1818
"kindString": "Reference",
1919
"flags": {},
20-
"target": 37
20+
"target": 35
2121
},
2222
{
2323
"id": 39,
@@ -44,23 +44,23 @@
4444
"kind": 16777216,
4545
"kindString": "Reference",
4646
"flags": {},
47-
"target": 30
47+
"target": 36
4848
},
4949
{
5050
"id": 45,
5151
"name": "ThisModule",
5252
"kind": 16777216,
5353
"kindString": "Reference",
5454
"flags": {},
55-
"target": 36
55+
"target": 34
5656
},
5757
{
5858
"id": 42,
5959
"name": "a",
6060
"kind": 16777216,
6161
"kindString": "Reference",
6262
"flags": {},
63-
"target": 32
63+
"target": 30
6464
},
6565
{
6666
"id": 43,
@@ -71,25 +71,25 @@
7171
"comment": {
7272
"shortText": "An export of a local under a different name."
7373
},
74-
"target": 33
74+
"target": 31
7575
},
7676
{
7777
"id": 38,
7878
"name": "c",
7979
"kind": 16777216,
8080
"kindString": "Reference",
8181
"flags": {},
82-
"target": 32
82+
"target": 30
8383
},
8484
{
85-
"id": 23,
85+
"id": 20,
8686
"name": "GH1453",
8787
"kind": 4,
8888
"kindString": "Namespace",
8989
"flags": {},
9090
"children": [
9191
{
92-
"id": 28,
92+
"id": 25,
9393
"name": "Foo",
9494
"kind": 4194304,
9595
"kindString": "Type alias",
@@ -101,7 +101,7 @@
101101
}
102102
},
103103
{
104-
"id": 26,
104+
"id": 23,
105105
"name": "Member",
106106
"kind": 32,
107107
"kindString": "Variable",
@@ -113,7 +113,7 @@
113113
"defaultValue": "Mod.a"
114114
},
115115
{
116-
"id": 24,
116+
"id": 21,
117117
"name": "Module",
118118
"kind": 32,
119119
"kindString": "Variable",
@@ -126,7 +126,7 @@
126126
"defaultValue": "Mod"
127127
},
128128
{
129-
"id": 27,
129+
"id": 24,
130130
"name": "TypedMember",
131131
"kind": 32,
132132
"kindString": "Variable",
@@ -139,7 +139,7 @@
139139
"defaultValue": "Mod.a"
140140
},
141141
{
142-
"id": 25,
142+
"id": 22,
143143
"name": "TypedModule",
144144
"kind": 32,
145145
"kindString": "Variable",
@@ -157,37 +157,37 @@
157157
"title": "Type aliases",
158158
"kind": 4194304,
159159
"children": [
160-
28
160+
25
161161
]
162162
},
163163
{
164164
"title": "Variables",
165165
"kind": 32,
166166
"children": [
167-
26,
167+
23,
168+
21,
168169
24,
169-
27,
170-
25
170+
22
171171
]
172172
}
173173
]
174174
},
175175
{
176-
"id": 18,
176+
"id": 15,
177177
"name": "add",
178178
"kind": 64,
179179
"kindString": "Function",
180180
"flags": {},
181181
"signatures": [
182182
{
183-
"id": 19,
183+
"id": 16,
184184
"name": "add",
185185
"kind": 4096,
186186
"kindString": "Call signature",
187187
"flags": {},
188188
"parameters": [
189189
{
190-
"id": 20,
190+
"id": 17,
191191
"name": "x",
192192
"kind": 32768,
193193
"kindString": "Parameter",
@@ -198,7 +198,7 @@
198198
}
199199
},
200200
{
201-
"id": 21,
201+
"id": 18,
202202
"name": "y",
203203
"kind": 32768,
204204
"kindString": "Parameter",
@@ -217,21 +217,21 @@
217217
]
218218
},
219219
{
220-
"id": 15,
220+
"id": 26,
221221
"name": "default",
222222
"kind": 64,
223223
"kindString": "Function",
224224
"flags": {},
225225
"signatures": [
226226
{
227-
"id": 16,
227+
"id": 27,
228228
"name": "default",
229229
"kind": 4096,
230230
"kindString": "Call signature",
231231
"flags": {},
232232
"parameters": [
233233
{
234-
"id": 17,
234+
"id": 28,
235235
"name": "a",
236236
"kind": 32768,
237237
"kindString": "Parameter",
@@ -269,15 +269,15 @@
269269
"title": "Namespaces",
270270
"kind": 4,
271271
"children": [
272-
23
272+
20
273273
]
274274
},
275275
{
276276
"title": "Functions",
277277
"kind": 64,
278278
"children": [
279-
18,
280-
15
279+
15,
280+
26
281281
]
282282
}
283283
]
@@ -466,37 +466,37 @@
466466
},
467467
"children": [
468468
{
469-
"id": 36,
469+
"id": 34,
470470
"name": "ThisModule",
471471
"kind": 16777216,
472472
"kindString": "Reference",
473473
"flags": {},
474474
"target": 29
475475
},
476476
{
477-
"id": 33,
477+
"id": 31,
478478
"name": "b",
479479
"kind": 16777216,
480480
"kindString": "Reference",
481481
"flags": {},
482482
"comment": {
483483
"shortText": "An export of a local under a different name."
484484
},
485-
"target": 32
485+
"target": 30
486486
},
487487
{
488-
"id": 34,
488+
"id": 32,
489489
"name": "c",
490490
"kind": 16777216,
491491
"kindString": "Reference",
492492
"flags": {},
493493
"comment": {
494494
"shortText": "An export with a module specifier that comes from this file."
495495
},
496-
"target": 32
496+
"target": 30
497497
},
498498
{
499-
"id": 37,
499+
"id": 35,
500500
"name": "GH1453Helper",
501501
"kind": 4194304,
502502
"kindString": "Type alias",
@@ -507,7 +507,7 @@
507507
}
508508
},
509509
{
510-
"id": 32,
510+
"id": 30,
511511
"name": "a",
512512
"kind": 32,
513513
"kindString": "Variable",
@@ -522,14 +522,14 @@
522522
"defaultValue": "1"
523523
},
524524
{
525-
"id": 30,
525+
"id": 36,
526526
"name": "default",
527527
"kind": 64,
528528
"kindString": "Function",
529529
"flags": {},
530530
"signatures": [
531531
{
532-
"id": 31,
532+
"id": 37,
533533
"name": "default",
534534
"kind": 4096,
535535
"kindString": "Call signature",
@@ -550,30 +550,30 @@
550550
"title": "References",
551551
"kind": 16777216,
552552
"children": [
553-
36,
554-
33,
555-
34
553+
34,
554+
31,
555+
32
556556
]
557557
},
558558
{
559559
"title": "Type aliases",
560560
"kind": 4194304,
561561
"children": [
562-
37
562+
35
563563
]
564564
},
565565
{
566566
"title": "Variables",
567567
"kind": 32,
568568
"children": [
569-
32
569+
30
570570
]
571571
},
572572
{
573573
"title": "Functions",
574574
"kind": 64,
575575
"children": [
576-
30
576+
36
577577
]
578578
}
579579
]

src/test/converter2/issues/gh1795.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export default function foo() {}
2+
export { foo };

src/test/issueTests.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,4 +281,13 @@ export const issueTests: {
281281
)
282282
);
283283
},
284+
285+
gh1795(project) {
286+
equal(
287+
project.children?.map((c) => c.name),
288+
["default", "foo"]
289+
);
290+
ok(project.children![0].kind === ReflectionKind.Reference);
291+
ok(project.children![1].kind !== ReflectionKind.Reference);
292+
},
284293
};

0 commit comments

Comments
 (0)