-
-
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
Proposal: default-import-match-filename
#325
Comments
Makes sense to me. Probably needs a little more detail/scope to build out. How would you propose packages be handled? I.E. import React from 'react' is idiomatic, but not sure how one would divine this from just the import path. |
I would propose to have a separate rule for overseeing module imports and separate rule for importing files, i.e. |
Got it, so this rule's scope would be only internal relative imports, as you've described? |
We've had a similar discussion on I have trouble seeing this as something that can be generalized enough. For example, how would you enforce / generalize the following? import parseFloat from './parse/float';
import parseDate from './parse/date';
import formatFloat from './format/float';
import formatDate from './format/date'; or import closeToFoo from './foo';
function foo(...) { ... }
export foo; |
I would think that such a folder layout simply couldn't use the rule that @gajus has described. It would be workable as he has described if it stays as simple as "default import identifiers should match an imported file's name". |
First of all, if you have: import parseFloat from './parse/float';
import parseDate from './parse/date';
import formatFloat from './format/float';
import formatDate from './format/date'; This is specifically the design flaw that I am describing. What is the reason you are naming your variables different than the files? In this specific context, it is because variable name makes more sense in the code than simply "float" or "date". Then what is the reason you are naming a file containing "parseFloat" function as "float"? It makes no sense. When considering a file name, file path should have no weight. With this rule, you would be forced to restructure your code as: import parseFloat from './parse/parseFloat';
import parseDate from './parse/parseDate';
import formatFloat from './format/formatFloat';
import formatDate from './format/formatDate'; (or choose not to use the rule). |
Hm, I'd like to disagree. I don't see why the file path should have no weight. If I have my
That is of course an acceptable solution ;) I have no issue with this issue being in the repo at all. I just have a concern that if you generalize this to external modules (it is similar, but I guess that it is done for different purposes here, so one could argue one way or the other IMO), then you'd have issues all over the place for handling it one way or another, have exceptions, etc., and that could be a huge burden on the maintainer. |
I can think of many examples. However, style is one of those things that it is easy to find arguments in both directions. To quickly summarize my position:
There are a few more arguments (such as promoting code re-usability), though this is getting off-topic. This rule is opt-in and it will benefit developers who prefer explicit code naming conventions. |
I think we need Example // MyComponent.js
// pass
export default function MyComponent() { return ... }
// fail
export default function NotMyComponent() { return ... } |
I think this rule (both export & import) can be configured by
Then we can fit all requirements from different projects above, and leave the option to users. For import parseFloat from './parse/float';
import parseDate from './parse/date';
import formatFloat from './format/float';
import formatDate from './format/date'; It's valid for "full path" match, but not "base file name" match. |
I'm not sure about your latter example - is I think the rule should only ever use the base filename, or the base dirname if it's an index (the latter could be configurable, but should be on by default). The rule would need to handle the most common cases; it's not practical for it to attempt to cover every conceivable pattern. |
@ljharb Matching base filename is not always good, especially for big project which has complicated features which is grouped by folders. As an example, there is one file named And, There maybe another configuration for all exceptions like |
Any argument for |
Is there any movement on this? Or was it dropped :( I was looking around to see if this was a thing and stumbled upon this thread, would be awesome if even the rule was "imported value should have the same name as the export if the export is named" |
Hello! I’m looking for a similar rule but with a different approach:
(I’ve checked the current released rules, #1143 and #1476 and none of them seem to cover that) I was wondering if implementing this proposal, means that #1041 won’t be implemented? Or can the 2 rules coexist? |
@CarinaChenot I'd personally want to enforce that the filename matches the default export name, but I'd never want to enforce that users import it with the same name. However, many do want to impose that on their codebases. Since the logic for "filename → export name" would need to be shared, it could be done as two separate rules, or as two options on one rule. Either way, I'm not interested in adding the import-side restriction unless the export-side one exists first/at the same time. |
@golopot How about publishing this rule as its own package? |
I am not planning on it. But I am fine If anyone wants to create a package based on my pull request. |
Hi For educational purpose, below is the link to my custom plugin: I made it abundantly clear in the README and in the code who the authors of the code are. Would you mind if I publish it to npm and apply the rules to my company projects? Thanks again |
I am glad my work helps people! Surly you can publish it to npm. |
I often see things such as:
The disparity between the default import name (
parseDate
) and file name (date
) often indicates that one or the other is named inappropriately. In the first case, thats the file name. In the second case, thats the local variable name.Valid code:
Invalid code:
There is no way to enforce a right name, e.g. user could just as well end up with:
However, I would argue that these issues are easy to notice in the code. Having this rule would prevent lazy developers from using a better variable name in a local file and ignore the need to rename the file and other references.
Furthermore, this enables easier code inspection (because it is easy to recognise what a variable represents when it is being used consistently between many files).
The text was updated successfully, but these errors were encountered: