-
Notifications
You must be signed in to change notification settings - Fork 34
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
Autofix replaces leading comments #16
Comments
Hmm, this is tricky... generally the copyright statement should be the first thing in the file. It also appears that the My preferred solution would be to move the flow comment from the first line. Otherwise this plugin will have to become far more complex, both detecting, and fixing. What do you think? |
Exactly like you said, I meant that the What I want is this: // @flow
console.log(1); to become this: // Copyright 2018, My Company
// @flow
console.log(1); but I would also make sure that this: // Copyright 2017, My Company
console.log(1); becomes this: // Copyright 2018, My Company
console.log(1); instead of this: // Copyright 2018, My Company
// Copyright 2017, My Company
console.log(1); I would achieve this by comparing similarities, it would not specifically look for If this is something that you want, I'll gladly start working on it. |
That becomes a harder problem. How would you compare similarities? What's to differentiate The similarity between strings is usually measured by hamming distance, which is pretty expensive to calculate for each header in a large codebase. Besides the time, I think settling on some arbitrary hamming distance threshold to decide what behaviour should occur is odd. A better solution IMO might be to have a configuration option where you can define your behaviour (think of something like a I don't think the use case is common enough (and if it come up, can usually be fixed with a one time 'Find and Replace') to warrant adding all that complexity to this plugin |
Similarity would be calculated only if a file starts with a string that isn't equal to the header, and matching for equality is cheap.
Sorry, I lost you here. Could you explain with examples of configuration and result? I don't see an easy way to solve this problem, all I know is that autofixing this: // this function does something
function doSomething() { } into this: // Copyright 2018, My Company
function doSomething() { } isn't correct. I'll take some more time to think about it. |
@silvenon ah I see! I wonder if we can actually special case a few comments. The one that come to mind are The alternative, more generic solution is not doing anything in "fix" mode when there is a comment at the top of the file. e,g, console.log(1); would get the header prepended, but // anything
console.log(1); would not. It would then be up to users to manually fix up these cases. We could have an option to turn on "aggressive" mode, if you do want to replace all comments. |
Whitelisting comments is something that crossed my mind as well, but the example in #16 (comment) wouldn't cover it, and it's a common case. I like the idea of not autofixing if the file starts with a comment. On the other hand, it would be a shame to not be able to update your headers using autofix… Ugh, complicated stuff. 😜 |
@Stuk I see what you meant by identifying special comments and prepending the header in those cases. Is it ok if I proceed with that?
The downside is that you would not be able to update your headers using autofix, but often you can do that with a simple search & replace, no? |
I must say, I don't like the idea of a whitelist of specific comments that warrant prepending the comment. I think it makes this harder to document, and increases the cost to the developer who now has to think about and realise that the plugin does that. I'm not a big fan of increasing developer cost (both in terms of configuration and non-intuitive convention). Anyway, that's just my 2 cents on having a whitelist. A lot of files will tend to start with unrecognized comments, as coding style at a lot of companies involves a necessary documentation line for the class/function being exported. The only exception here obviously is that you might have Anyway, on the second point, I think you've already recognised it too, and so like you said, simply using find and replace should do the trick. Then again, if that's the case, this plugin feels to me like it effectively only becomes useful for a one time use. Someone can set it up, run If all of this is only because the autofix removes |
The user wouldn't have to think about a whitelist, I think the plugin itself would have it predefined. Concerning the rest of what you said, sure, the user could manually add the deleted comments back, but:
Many files start with comments (either Flow, JSDoc or something else), so this behavior could use some rethinking. What do you think? |
Hmm, I guess the whitelist could work. I'm not a big fan, but it might be the best solution and atleast better than always deleting Just to throw other ideas out there, what do you think about supporting something like this; Headers generated by autofix will be at the top of the file, and will end with something like this. /**
* ...
* END ESLINT PLUGIN HEADER
/* It will always be prepended to the top of the file, and will never replace existing comments. Also, if the header is changed in the future from configuration, the plugin only has the authority to change things before that line. So: // @flow
import React from 'react'; becomes /**
* Copyright 2018
* END ESLINT-PLUGIN-HEADER
* The above header is autogenerated and should not be manually modified.
*/
// @flow
import React from 'react'; and /**
* Copyright 2016
* END ESLINT-PLUGIN-HEADER
* The above header is autogenerated and should not be manually modified.
*/
// @flow
import React from 'react'; becomes this /**
* Copyright 2018
* END ESLINT-PLUGIN-HEADER
* The above header is autogenerated and should not be manually modified.
*/
// @flow
import React from 'react'; |
Yep, that could work! It's not ideal that the comment depends on the technology used, but it seems to be the most stable option so far. |
I got the same issue, but my codes are starts with |
I'd like to also add that we've started using this in our TypeScript project and files without any imports are having their leading doc comments stripped. So say I have this file for the header: /*!
* put this in the file
*/ and this code in the file /**
* This interface does something and should be used with {@link MyInterface2}
*/
export interface Test {
// Interface code
} Running the fixer will generate this output. /*!
* put this in the file
*/
export interface Test {
// Interface code
} In the above scenario, I would not expect the comment to be dropped. We use the |
It looks like the new eslint suggestions feature would help with this. When a leading comment is found we could suggest either adding the header before the existing comment or replacing it. |
That's great! It would be nice to be able to run |
Actually, it can autofix in the case that the header comment matches I noticed that currently it's not possible to configure the rule to retrieve the header comment from a file and set the |
Sorry for offtop, but I just wanted to say that one more nice thing that would be great to have is a blank line after the generated header. |
My file starts with
// @flow
, but that comment gets erased with autofix.I'll gladly fix this, I'm just wondering if it's ok that I completely remove the replacing behavior. I think it was meant to replace wrong headers, but in reality it's replacing any first non-shebang comment, which is not always what we want.
Or maybe we could measure how closely the header is matching given pattern using string-similarity and decide if it's a wrong header or an unrelated comment?
Update
tl;dr for anyone who doesn't feel like reading all of these comments, basically the plugin cannot know whether the existing header comment is an old header that should be replaced (e.g. the year changed) or an unrelated comment. This issue was raised before v3 was released, i.e. before the
pattern
option, so now we're discussing how we can use it, and ESLint suggestions, to our advantage.The text was updated successfully, but these errors were encountered: