[Duplicate imports] Fix issue that would cause duplicate imports#300
Open
rcancro wants to merge 2 commits intopinterest:masterfrom
Open
[Duplicate imports] Fix issue that would cause duplicate imports#300rcancro wants to merge 2 commits intopinterest:masterfrom
rcancro wants to merge 2 commits intopinterest:masterfrom
Conversation
In the given schema file, we can see the class references itself:
```
{
"id": "board_activity.json",
"title": "board_activity",
"description": "Schema definition of a BoardActivity",
"$schema": "http://json-schema.org/schema#",
"type": "object",
"properties": {
"ref_board_activity": { "$ref": "board_activity.json" },
},
}
```
This would cause multiple `BoardActivity` imports in `BoardActivity.m`. This was caused by calling `ObjCIR.Root.imports` `renderImplementation` method. This is how the imports were being generated:
```
case .imports(let classNames, let myName, _):
return [classNames.union(Set([myName]))
.sorted()
.map { $0.trimmingCharacters(in: .whitespaces) }
.map { ObjCIR.fileImportStmt($0, headerPrefix: params[GenerationParameterType.headerPrefix]) }
.joined(separator: "\n")]
```
Using the schema above, `classNames` would include `“BoardActivity “`. Note the space. `myName` would be `”BoardActivity”`. Note the lack of space.
These two get unioned together in a set. The result is `[“BoardActivity ", "BoardActivity”]`. We sort this set, then remove any trailing spaces. But at this point it is too late! We are no longer a set, we are an array which can have multiple of equivalent objects. At this point our array is `[“BoardActivity", "BoardActivity”]`. This leads to duplicate import statements.
A solution would be to trim these strings BEFORE the union:
```
case .imports(let classNames, let myName, _):
let strippedClassNames = Set(classNames.map { $0.trimmingCharacters(in: .whitespaces) })
return [strippedClassNames.union(Set([myName.trimmingCharacters(in: .whitespaces)]))
.sorted()
.map { ObjCIR.fileImportStmt($0, headerPrefix: params[GenerationParameterType.headerPrefix]) }
.joined(separator: "\n")]
```
I created `strippedClassNames` which holds the trimmed `classNames`. This gets unioned that with the stripped version of `myName` to get the list of imports. Now we end up with the array only have one entry `[“BoardActivity”]`.
Note: I also made a change in `ObjectiveCNSCodingExtension.swift` because the compiler was telling me that the return value was too complicated for it to figure out.
woshimaliang
approved these changes
Sep 6, 2024
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
In the given schema file, we can see the class
board_activityreferences itself:This causes multiple
BoardActivityimports inBoardActivity.m. The issue is inObjCIR.Root.importsrenderImplementationmethod. This is how the imports are being generated:Using the schema above,
classNameswould include“BoardActivity “. Note the space.myNamewould be”BoardActivity”. Note the lack of space.These two get unioned together in a set. The result is
["BoardActivity ", "BoardActivity"]. We sort this set, then remove any trailing spaces. But at this point we are an array, and we now have two duplicate entries in our array:["BoardActivity", "BoardActivity"]. This leads plank to render#import "BoardActivity.h"twice in our implementation file.A solution would be to trim these strings BEFORE the union:
I created
strippedClassNameswhich holds the trimmedclassNames. This gets unioned that with the stripped version ofmyNameto get the list of imports. Now we end up with the array only have one entry["BoardActivity"].Note: I also made a change in
ObjectiveCNSCodingExtension.swiftbecause the compiler was telling me that the return value was too complicated for it to figure out.