-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[TypeScript] Support default exports in TSExportAssignment. #1528
Merged
ljharb
merged 1 commit into
import-js:master
from
joaovieira:extend-ts-export-assignment
Apr 23, 2020
+112
−13
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -530,30 +530,50 @@ ExportMap.parse = function (path, content, context) { | |
|
||
// This doesn't declare anything, but changes what's being exported. | ||
if (n.type === 'TSExportAssignment') { | ||
const moduleDecls = ast.body.filter((bodyNode) => | ||
bodyNode.type === 'TSModuleDeclaration' && bodyNode.id.name === n.expression.name | ||
const exportedName = n.expression.name | ||
const declTypes = [ | ||
'VariableDeclaration', | ||
'ClassDeclaration', | ||
'TSDeclareFunction', | ||
'TSEnumDeclaration', | ||
'TSTypeAliasDeclaration', | ||
'TSInterfaceDeclaration', | ||
'TSAbstractClassDeclaration', | ||
'TSModuleDeclaration', | ||
] | ||
const exportedDecls = ast.body.filter(({ type, id, declarations }) => | ||
declTypes.includes(type) && | ||
(id && id.name === exportedName || declarations.find(d => d.id.name === exportedName)) | ||
) | ||
moduleDecls.forEach((moduleDecl) => { | ||
if (moduleDecl && moduleDecl.body && moduleDecl.body.body) { | ||
moduleDecl.body.body.forEach((moduleBlockNode) => { | ||
if (exportedDecls.length === 0) { | ||
// Export is not referencing any local declaration, must be re-exporting | ||
m.namespace.set('default', captureDoc(source, docStyleParsers, n)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about import { test } from './test';
export { foo }; // all good... |
||
return | ||
} | ||
exportedDecls.forEach((decl) => { | ||
if (decl.type === 'TSModuleDeclaration' && decl && decl.body && decl.body.body) { | ||
decl.body.body.forEach((moduleBlockNode) => { | ||
// Export-assignment exports all members in the namespace, explicitly exported or not. | ||
const exportedDecl = moduleBlockNode.type === 'ExportNamedDeclaration' ? | ||
const namespaceDecl = moduleBlockNode.type === 'ExportNamedDeclaration' ? | ||
moduleBlockNode.declaration : | ||
moduleBlockNode | ||
|
||
if (exportedDecl.type === 'VariableDeclaration') { | ||
exportedDecl.declarations.forEach((decl) => | ||
recursivePatternCapture(decl.id,(id) => m.namespace.set( | ||
if (namespaceDecl.type === 'VariableDeclaration') { | ||
namespaceDecl.declarations.forEach((d) => | ||
recursivePatternCapture(d.id, (id) => m.namespace.set( | ||
id.name, | ||
captureDoc(source, docStyleParsers, decl, exportedDecl, moduleBlockNode)) | ||
captureDoc(source, docStyleParsers, decl, namespaceDecl, moduleBlockNode)) | ||
) | ||
) | ||
} else { | ||
m.namespace.set( | ||
exportedDecl.id.name, | ||
namespaceDecl.id.name, | ||
captureDoc(source, docStyleParsers, moduleBlockNode)) | ||
} | ||
}) | ||
} else { | ||
// Export as default | ||
m.namespace.set('default', captureDoc(source, docStyleParsers, decl)) | ||
} | ||
}) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default function foobar() {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { getFoo } from './typescript'; | ||
export = getFoo; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export = foobar; | ||
|
||
declare const foobar: number; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export = foobar; | ||
|
||
declare function foobar(): void; | ||
declare namespace foobar { | ||
type MyType = string | ||
enum MyEnum { | ||
Foo, | ||
Bar, | ||
Baz | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey! Here
declarations
is undefined unlesstype === 'VariableDeclaration'
so it crashes the linter in some cases. I'll create another PR to fix it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!