-
Notifications
You must be signed in to change notification settings - Fork 55
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
Useful changes from my setup which I think may be beneficial to others #506
Changes from all commits
6246b9a
e713147
b063c3f
3b4e10e
1a7ee44
2c2f69e
0fd3a6d
e3a4b11
52f9e0f
9d3567e
7bceb5b
124a792
60fc13f
f64afde
89f5e4b
6b8657c
ed94946
4689499
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import sqlite3 from 'sqlite3'; | ||
|
||
import lastUpdate from './lastUpdate'; | ||
|
||
const MAX_CHUNK_SIZE = 100; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -243,7 +243,8 @@ export default class ImportStatements { | |
importsArray, | ||
(importStatement: ImportStatement): boolean => | ||
!importStatement.isParsedAndUntouched(), | ||
); | ||
).reverse(); | ||
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. This will have to go too. 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. #448 |
||
|
||
result = flattenDeep(result); | ||
|
||
if (this.config.get('sortImports')) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,13 @@ it('knows about dynamic keys', () => { | |
`))).toEqual(new Set(['bar'])); | ||
}); | ||
|
||
it('knows about aliases in destructured objects', () => { | ||
expect(findUndefinedIdentifiers(parse(` | ||
const foo = { 'theValueOfMyConst': 123 }; | ||
const { [MY_CONST]: someAlias } = foo; | ||
`))).toEqual(new Set(['MY_CONST'])); | ||
}); | ||
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. Nice. |
||
|
||
it('knows about jsx', () => { | ||
expect(findUndefinedIdentifiers(parse(` | ||
export default <FooBar foo={bar} />; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
const KEYS_USED_FOR_ASSIGNMENT = new Set(['id', 'imported', 'local', 'params']); | ||
const KEYS_USED_IN_REFERENCE_TO_OBJECTS = new Set(['property']); | ||
|
||
function normalizeNode(node, context) { | ||
const { key, parent } = context; | ||
|
@@ -54,7 +53,7 @@ function normalizeNode(node, context) { | |
} | ||
|
||
const isAssignment = KEYS_USED_FOR_ASSIGNMENT.has(key) || | ||
(key === 'key' && parent.parent.type === 'ObjectPattern') || | ||
(key === 'key' && !parent.computed && parent.parent.type === 'ObjectPattern') || | ||
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. Can you come up with a test case demonstrating what this is supposed to fix? 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. added tests |
||
(key === 'left' && parent.type === 'AssignmentPattern') || | ||
(key === 'elements' && parent.type === 'ArrayPattern') || | ||
(key === 'value' && | ||
|
@@ -64,7 +63,7 @@ function normalizeNode(node, context) { | |
context.definedInScope.add(node.name); | ||
} | ||
|
||
const isReference = KEYS_USED_IN_REFERENCE_TO_OBJECTS.has(key) || | ||
const isReference = (key === 'property' && !parent.computed && parent.parent.type !== 'ObjectPattern') || | ||
(key === 'key' && !parent.computed && parent.parent.type !== 'ObjectPattern'); | ||
|
||
return { | ||
|
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.
This change will modify sorting of imports. From experience, I know there will be heated discussions around this. Would you mind moving this out of the PR and pushing it as a separate PR? Or better yet, a PR adding support for better user-control over sorting (and grouping).
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.
With this sorting, the node_modules imports will be at the top with them being sorted by package name.
After them will be the rest of the imports which will also be sorted by filename.
I really don't see any other possible sorting as viable. Sorting by the imports themselves and not where they come from doesn't make any sense. This will produce many unnecessary/confusing merge conflicts. Example: instead of only removing a named export, the change will also include the movement of the imports for that file due to the sorting. The same scenario will be for appending named imports.
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.
There has been plenty of discussion around sorting and grouping, and it's pretty clear that we need more flexibility:
#508
#454
#448
#284
I think the best thing we could do is to look at what the sort-imports eslint plugin does and make things configurable the same way.
I'm not going to argue much with what you think is the right order. What's important here is that we can either introduce a breaking change (that doesn't fix the problem) or keep the current sorting until we have a non-breaking and flexible solution. Introducing a breaking change on import sort order right now would mean introducing churn for every project that has been using import-js for a while.
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.
Two of those issues are things that I fixed myself - the empty line between imports being removed and the newly added imports being appended instead of prepended.
The other two issues are from people which want exactly the same sort order as I have currently implemented in this PR.
So I don't really see any conflicts of interest... Everyone wants the same thing.
Those issue links just highlight what I have told you in my previous comment.
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.
Let me present you with a few possible sorting options that are all equally viable:
For groups, any combination of the following seems equally viable:
The right thing here would be to make things configurable, so that you can choose any sorting and any grouping. If we make the default config produce the same sorting and grouping result as what we have today by default, I'll be happy to merge.
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.
First option:
This is not viable, because of I what I wrote in the first comment...
Since you haven't read it I will quote it here...
Second option:
This is what I have currently implemented...
For the grouping, it is very simple - exports from outside of the project, after which - local exports. I think everyone will find that as a reasonable default.
If you find any value in adding a custom sorting/grouping function that will benefit anyone (since I haven't seen one such person in the above mentioned issues by you),
then do that yourself. I have done enough work, I am happy with what I've got and I consider it to be the best default behavior for the project.
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.
I currently have a few projects using the current default sorting and grouping rules and I agree that this shouldn't be changed until it's configurable. Otherwise I'll need to turn it off and manually sort and group again, or deal with PR churn for the foreseeable future. I'm sure there are others using the default behaviour that we don't hear from.
Because this is an area where it's down to preference, and an existing default has been in use for a fairly long time, it does not make sense to change the default and push a different preference on those already using the default behaviour.
Now @trotzig has not asked you to implement custom sorting configuration, only to keep the existing defaults so it doesn't create churn for other users.
I understand the frustration of having a PR held up when you feel that you have done a good job, but lets keep some perspective.
Also, you have done a great job here. As a user I always appreciate when someone makes an effort to improve OSS tools for the community
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.
The current setup doesn't make sense for the reason I have pointed out above. That is why I changed it to something that does. You simply don't want the sorting to be changed because that is what you have used in existing projects. I consider this argument invalid. Keeping wrong code because it is what people have used so far is not correct. Please give me an argument that supports the claim that the current sorting is in any way better than the one I have implemented.
If you can't, someone else will have to implement these features listed here, because I got tired of this.