-
-
Notifications
You must be signed in to change notification settings - Fork 37
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
Resolve export default expression name collision #268
Conversation
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.
Hi @Atrue,
I'm sorry for late reply. Thanks for your contribution to address such a "fundamental" issue of the tool. I really appreciate it!
I haven't debugged the new code yet so I have a few clarification questions.
const renamedDeclarations = node.declarationList.declarations.map((declaration, i) => { | ||
const name = renaming[i]; |
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.
Is it a coincidence that iterating over renaming
with i
that is an index in declarations works because we find the first item? Can you clarify the relation between items (their positions) in renaming
array and order of items in node.declarationList.declarations
?
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.
Sure. Let’s take this statement as an example:
declare const a = 1, b = 2
(*)
this statement has 2 variable declarations:
a = 1
and b = 2
.
Let’s assume then we need to rename b
variable name to b$2
, so according to TS api we need to need to create variable statement with 2 variable declarations (a = 1
and b$2 = 2
)
The statement (*) goes to substituteNode
and gets the renaming
array for each statement inside declarations using getStatementRenaming
.
dts-bundle-generator/src/bundle-generator.ts
Line 206 in 6c033c0
return statement.declarationList.declarations.map(declaration => { |
[undefined, 'b$2']
.
Then it goes inside custom createVariableStatement
function and updates the name for each non-undefined value in the array:
declaration a = 1
will be the same as it has no renaming for index = 0
declaration b = 2
will be renamed to b$ = 2
with ts.factory.updateVariableDeclaration
using a new name b$2
for index = 1.
@@ -279,6 +279,8 @@ function getExportsForName( | |||
|
|||
export type ModifiersMap = Record<ts.ModifierSyntaxKind, boolean>; | |||
|
|||
export type StatementRenaming = (string | undefined)[]; |
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.
is it (string | undefined)[]
or string[]
? what does it mean if an item is undefined
?
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.
See #268 (comment)
if (modifiersMap[ts.SyntaxKind.DeclareKeyword]) { | ||
renaming = helpers.getStatementRenaming(statement); |
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.
is it condition true for every node with declare
keyword or it should be a more specific check here?
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.
looks like it will be applicable to statements like export declare interface Foo {}
as well? if so, how it would work with declaration merging?
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.
looks like in situations like this the renaming would be applicable to a variable declaration only which would lead to a incorrect output:
file1.ts:
export const MergedSymbol = '';
export interface MergedSymbol {
test(): void
};
file2.ts:
export const MergedSymbol = '';
export interface MergedSymbol {
test2(): void
};
index.ts:
export { MergedSymbol as MS1 } from './file1';
export { MergedSymbol as MS2 } from './file2';
Expected output:
declare const MergedSymbol = '';
interface MergedSymbol {
test(): void
};
declare const MergedSymbol$2 = '';
interface MergedSymbol$2 {
test2(): void
};
export {
MergedSymbol as MS1,
MergedSymbol$2 as MS2,
};
Actual output:
declare const MergedSymbol = '';
interface MergedSymbol {
test(): void
};
declare const MergedSymbol$2 = '';
interface MergedSymbol { // note that this symbol isn't renamed but should be
test2(): void
};
export {
MergedSymbol as MS1,
MergedSymbol$2 as MS2,
};
Even if the output compiles logically it is incorrect as it leads to wrong declaration merging.
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.
Honestly, it’s first time I see the interface statement with declare keyword export declare interface Test {}
You are right, this statement is also covered by modifiersMap[ts.SyntaxKind.DeclareKeyword]
but getStatementRenaming
has the additional check for ts.isVariableStatement(statement)
so interface Test
will never be renamed (it returns an empty array)
dts-bundle-generator/src/bundle-generator.ts
Line 205 in 6c033c0
if (ts.isVariableStatement(statement)) { |
Note: A type/interface can not be renamed like this as it has much more problems: all types are reusable so they might be used in another type, function, class or something. It means it requires much more logic to check every related type in recreateRootLevelNodeWithModifiersImpl
. So my goal was to fix only export default
issue.
I heard about ts-morph
library that allows to do such renaming in one call, so if you’re interested in it https://ts-morph.com/manipulation/
Hi @Atrue, Sorry for a very long delay. First of all, I want to thank you for your contribution and the fact that you jumped into a new project and were able to address such a complex problem at least deserves a recognition. Secondly, your contribution kicked me off to address the names collision problem in general (not just for default names) and it seems that I have a solution that at least passed all tests that the tool currently have, including yours and several others that I've added during the implementation. It took a while to make it work... As the next step, I'll be finishing the implementation (I need to write a couple more tests and run the tool against some well-known consumers of the tool to make sure that I'm not missing anything) and publishing a PR. Unfortunately, it seems that none of your changes in the tool preserved (tests are fine tho) after my changes (I had to do big and a touch painful refactoring), but to pay respect to your contribution and to make sure you'll be in the list of contributors to the tool I'm going to merge your PR right before my changes anyway. Please keep it open until then. After everything above is completed, I think it would be a preparation for the next major version (as amount of changes is enormous). I hope that is alright with you and my long silence didn't cause too much problem for you. |
If you're interested, I just created a draft PR #270 (mostly to keep track of packages I need to validate these changes against, but feel free to take a look/review/pull and check for your use case). |
The fix has been released in v9.0.0. |
Fixes #116Issue
exporting default unnamed expression bundles the same name
result:
See 3be37c9 for reference
Suggestion
Using a
nameCollision
map to detect and rename variable collisions.See 6c033c0 for reference
Restrictions
There are the same restrictions as with the current unnamed class or function statements behaviour
export default class {}
.It can not be used as reference for other declarations:
Similar issues
It looks like it also fixes this #116