Skip to content

Commit

Permalink
Convert exports named "default" last
Browse files Browse the repository at this point in the history
Closes #1795
  • Loading branch information
Gerrit0 committed Nov 22, 2021
1 parent 2d6e49d commit 2742e7f
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 54 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# Unreleased

### Bug Fixes

- 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.

## v0.22.9 (2021-11-14)

### Features
Expand Down
38 changes: 25 additions & 13 deletions src/lib/converter/converter.ts
Expand Up @@ -467,14 +467,16 @@ function getExports(
node: ts.SourceFile | ts.ModuleBlock,
symbol: ts.Symbol | undefined
): ts.Symbol[] {
let result: ts.Symbol[];

// The generated docs aren't great, but you really ought not be using
// this in the first place... so it's better than nothing.
const exportEq = symbol?.exports?.get("export=" as ts.__String);
if (exportEq) {
// JS users might also have exported types here.
// We need to filter for types because otherwise static methods can show up as both
// members of the export= class and as functions if a class is directly exported.
return [exportEq].concat(
result = [exportEq].concat(
context.checker
.getExportsOfModule(symbol!)
.filter(
Expand All @@ -485,23 +487,33 @@ function getExports(
)
)
);
}

if (symbol) {
return context.checker
} else if (symbol) {
result = context.checker
.getExportsOfModule(symbol)
.filter((s) => !hasAllFlags(s.flags, ts.SymbolFlags.Prototype));
} else {
// Global file with no inferred top level symbol, get all symbols declared in this file.
const sourceFile = node.getSourceFile();
result = context.checker
.getSymbolsInScope(node, ts.SymbolFlags.ModuleMember)
.filter((s) =>
s
.getDeclarations()
?.some((d) => d.getSourceFile() === sourceFile)
);
}

// Global file with no inferred top level symbol, get all symbols declared in this file.
const sourceFile = node.getSourceFile();
const globalSymbols = context.checker
.getSymbolsInScope(node, ts.SymbolFlags.ModuleMember)
.filter((s) =>
s.getDeclarations()?.some((d) => d.getSourceFile() === sourceFile)
);
// Put symbols named "default" last, #1795
result.sort((a, b) => {
if (a.name === "default") {
return 1;
} else if (b.name === "default") {
return -1;
}
return 0;
});

return globalSymbols;
return result;
}

function isDirectExport(symbol: ts.Symbol, file: ts.SourceFile): boolean {
Expand Down
82 changes: 41 additions & 41 deletions src/test/converter/exports/specs.json
Expand Up @@ -17,7 +17,7 @@
"kind": 16777216,
"kindString": "Reference",
"flags": {},
"target": 37
"target": 35
},
{
"id": 39,
Expand All @@ -44,23 +44,23 @@
"kind": 16777216,
"kindString": "Reference",
"flags": {},
"target": 30
"target": 36
},
{
"id": 45,
"name": "ThisModule",
"kind": 16777216,
"kindString": "Reference",
"flags": {},
"target": 36
"target": 34
},
{
"id": 42,
"name": "a",
"kind": 16777216,
"kindString": "Reference",
"flags": {},
"target": 32
"target": 30
},
{
"id": 43,
Expand All @@ -71,25 +71,25 @@
"comment": {
"shortText": "An export of a local under a different name."
},
"target": 33
"target": 31
},
{
"id": 38,
"name": "c",
"kind": 16777216,
"kindString": "Reference",
"flags": {},
"target": 32
"target": 30
},
{
"id": 23,
"id": 20,
"name": "GH1453",
"kind": 4,
"kindString": "Namespace",
"flags": {},
"children": [
{
"id": 28,
"id": 25,
"name": "Foo",
"kind": 4194304,
"kindString": "Type alias",
Expand All @@ -101,7 +101,7 @@
}
},
{
"id": 26,
"id": 23,
"name": "Member",
"kind": 32,
"kindString": "Variable",
Expand All @@ -113,7 +113,7 @@
"defaultValue": "Mod.a"
},
{
"id": 24,
"id": 21,
"name": "Module",
"kind": 32,
"kindString": "Variable",
Expand All @@ -126,7 +126,7 @@
"defaultValue": "Mod"
},
{
"id": 27,
"id": 24,
"name": "TypedMember",
"kind": 32,
"kindString": "Variable",
Expand All @@ -139,7 +139,7 @@
"defaultValue": "Mod.a"
},
{
"id": 25,
"id": 22,
"name": "TypedModule",
"kind": 32,
"kindString": "Variable",
Expand All @@ -157,37 +157,37 @@
"title": "Type aliases",
"kind": 4194304,
"children": [
28
25
]
},
{
"title": "Variables",
"kind": 32,
"children": [
26,
23,
21,
24,
27,
25
22
]
}
]
},
{
"id": 18,
"id": 15,
"name": "add",
"kind": 64,
"kindString": "Function",
"flags": {},
"signatures": [
{
"id": 19,
"id": 16,
"name": "add",
"kind": 4096,
"kindString": "Call signature",
"flags": {},
"parameters": [
{
"id": 20,
"id": 17,
"name": "x",
"kind": 32768,
"kindString": "Parameter",
Expand All @@ -198,7 +198,7 @@
}
},
{
"id": 21,
"id": 18,
"name": "y",
"kind": 32768,
"kindString": "Parameter",
Expand All @@ -217,21 +217,21 @@
]
},
{
"id": 15,
"id": 26,
"name": "default",
"kind": 64,
"kindString": "Function",
"flags": {},
"signatures": [
{
"id": 16,
"id": 27,
"name": "default",
"kind": 4096,
"kindString": "Call signature",
"flags": {},
"parameters": [
{
"id": 17,
"id": 28,
"name": "a",
"kind": 32768,
"kindString": "Parameter",
Expand Down Expand Up @@ -269,15 +269,15 @@
"title": "Namespaces",
"kind": 4,
"children": [
23
20
]
},
{
"title": "Functions",
"kind": 64,
"children": [
18,
15
15,
26
]
}
]
Expand Down Expand Up @@ -466,37 +466,37 @@
},
"children": [
{
"id": 36,
"id": 34,
"name": "ThisModule",
"kind": 16777216,
"kindString": "Reference",
"flags": {},
"target": 29
},
{
"id": 33,
"id": 31,
"name": "b",
"kind": 16777216,
"kindString": "Reference",
"flags": {},
"comment": {
"shortText": "An export of a local under a different name."
},
"target": 32
"target": 30
},
{
"id": 34,
"id": 32,
"name": "c",
"kind": 16777216,
"kindString": "Reference",
"flags": {},
"comment": {
"shortText": "An export with a module specifier that comes from this file."
},
"target": 32
"target": 30
},
{
"id": 37,
"id": 35,
"name": "GH1453Helper",
"kind": 4194304,
"kindString": "Type alias",
Expand All @@ -507,7 +507,7 @@
}
},
{
"id": 32,
"id": 30,
"name": "a",
"kind": 32,
"kindString": "Variable",
Expand All @@ -522,14 +522,14 @@
"defaultValue": "1"
},
{
"id": 30,
"id": 36,
"name": "default",
"kind": 64,
"kindString": "Function",
"flags": {},
"signatures": [
{
"id": 31,
"id": 37,
"name": "default",
"kind": 4096,
"kindString": "Call signature",
Expand All @@ -550,30 +550,30 @@
"title": "References",
"kind": 16777216,
"children": [
36,
33,
34
34,
31,
32
]
},
{
"title": "Type aliases",
"kind": 4194304,
"children": [
37
35
]
},
{
"title": "Variables",
"kind": 32,
"children": [
32
30
]
},
{
"title": "Functions",
"kind": 64,
"children": [
30
36
]
}
]
Expand Down
2 changes: 2 additions & 0 deletions src/test/converter2/issues/gh1795.ts
@@ -0,0 +1,2 @@
export default function foo() {}
export { foo };
9 changes: 9 additions & 0 deletions src/test/issueTests.ts
Expand Up @@ -281,4 +281,13 @@ export const issueTests: {
)
);
},

gh1795(project) {
equal(
project.children?.map((c) => c.name),
["default", "foo"]
);
ok(project.children![0].kind === ReflectionKind.Reference);
ok(project.children![1].kind !== ReflectionKind.Reference);
},
};

0 comments on commit 2742e7f

Please sign in to comment.