Skip to content
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

Add ESLint rules from client codebase #1894

Open
wants to merge 1 commit into
base: master
from

Conversation

Projects
None yet
3 participants
@dstillman
Copy link
Member

dstillman commented Feb 26, 2019

So this is what the config would be if we added all rules from the client. See #1847 (comment) for context. I don't know whether we care about having this level of conformity in translators.

The warning/error distinction is also fairly arbitrary right now. Some things, particularly spacing rules, are warnings because they appear all throughout the existing client codebase, but conflicts are less of an issue here, so we could just make those errors and fix them, hopefully mostly with --fix. Or we might want to reserve errors for things that are more likely to cause bugs, but I'm not really sure what the point of style rules are if we're not going to enforce them consistently.

@retorquere

This comment has been minimized.

Copy link
Contributor

retorquere commented Feb 26, 2019

Just a side-note -- --fix doesn't work for the translators because of eslint/eslint#5121.

(of course a gnarly workaround can be made by physically splitting the translators into header and code files, linting them separately and recombining them if --fix is ran, but that's a little uglier than I'm comfortable with. So far, with the use of vim, bringing code in line was a few minutes per translator (hence the barrage of PRs))

@retorquere

This comment has been minimized.

Copy link
Contributor

retorquere commented Feb 26, 2019

Oh and personally I'm all for a high level of encouraged consistency in the translators. Given that most new translators seem to be copy-and-change jobs from existing translators, enforcing rules would be low-impact (since they'd be copying fairly clean code after the first major sweep) and it'd not further proliferate bad code styles.

"no-return-await": "error",
"no-script-url": "error",
"no-self-compare": "error",
"no-sequences": "error",
"no-shadow": "off",

This comment has been minimized.

@retorquere

retorquere Feb 26, 2019

Contributor

I can't assess how impactful/useful this would be for Zotero, but this one has saved me on occasion from some confusion.

@dstillman

This comment has been minimized.

Copy link
Member Author

dstillman commented Feb 26, 2019

Just a side-note -- --fix doesn't work for the translators because of eslint/eslint#5121.

Oh, that's annoying. I guess not making these proper JS files was shortsighted, but we're at least a decade out from that decision, so we're probably stuck with it.

of course a gnarly workaround can be made by physically splitting the translators into header and code files, linting them separately and recombining them if --fix is ran, but that's a little uglier than I'm comfortable with.

I don't know — it seems fairly trivial to just have a script that splits a translator, runs --fix on a temp file with the code part, and merges back to the original file.

@retorquere

This comment has been minimized.

Copy link
Contributor

retorquere commented Feb 26, 2019

It is annoying, and we're also stuck with it. I dunno -- it wouldn't be hard to make a script that just does what the preprocessor does and undoes it after. But it'd be changing files on disk, inside the repo. If it bombs out, it would leave changed files which could accidentally be checked in.

@dstillman

This comment has been minimized.

Copy link
Member Author

dstillman commented Feb 26, 2019

I'm not really concerned with accidental commits — anyone committing should be reviewing the diff first, and --fix itself has to be reviewed anyway — but it should be easy to make the script fairly safe.

@retorquere

This comment has been minimized.

Copy link
Contributor

retorquere commented Feb 26, 2019

I can have a stab at that.

@retorquere

This comment has been minimized.

Copy link
Contributor

retorquere commented Feb 26, 2019

I'll just do the split to a separate directory and stick that in .gitignore, that should minimize the risk.

@retorquere

This comment has been minimized.

Copy link
Contributor

retorquere commented Feb 26, 2019

I'll wait for #1895 to clear though, it would build on some of those changes.

@dstillman

This comment has been minimized.

Copy link
Member Author

dstillman commented Feb 26, 2019

I'll just do the split to a separate directory and stick that in .gitignore, that should minimize the risk.

That's not necessary. The header can just stay in memory, and the code can go in a temp file, and then they can be recombined back into the original file. The temp file can be cleaned up in an exit trap in case something goes wrong.

@retorquere

This comment has been minimized.

Copy link
Contributor

retorquere commented Feb 26, 2019

You mean by running eslint from the script itself? Wouldn't this split off the messages about the header from the rest of the eslint output? Just adding the temp dir to .gitignore would achieve much the same and we could just run eslint as usual.

@dstillman

This comment has been minimized.

Copy link
Member Author

dstillman commented Feb 26, 2019

Yes, we'd just have an eslint-fix script in .ci that took a filename. This is just for running --fix, so the header checks wouldn't be relevant.

@retorquere

This comment has been minimized.

Copy link
Contributor

retorquere commented Feb 26, 2019

I don't completely follow, but I think we agree on what we want to achieve and roughly how to do it.

@retorquere

This comment has been minimized.

Copy link
Contributor

retorquere commented Feb 26, 2019

Oh, that's annoying. I guess not making these proper JS files was shortsighted, but we're at least a decade out from that decision, so we're probably stuck with it.

Are we though? What would be the harm from optionally allowing a translator to start with const ZOTERO_TRANSLATOR_INFO = and stripping this exact string from the start of translators when they are loaded into Zotero? Old translators would continue to work, and an ESLint rule to detect that it's missing would be easy enough to do.

@dstillman

This comment has been minimized.

Copy link
Member Author

dstillman commented Feb 27, 2019

I don't think it's worth it. We'd have to make changes in many places in many repos, we'd have to either cut off translator updates or send backported translators to older versions, and we'd risk bugs in critical infrastructure, all with very little to be gained. We already have your postprocessor to get ESLint checks working, and making a --fix wrapper would take about 10 minutes.

@retorquere

This comment has been minimized.

Copy link
Contributor

retorquere commented Feb 27, 2019

I have a wrapperless solution that I'm testing now.

@retorquere

This comment has been minimized.

Copy link
Contributor

retorquere commented Feb 27, 2019

Alright, the results are at https://github.com/retorquere/translators/tree/eslint-split-merge

It looks a little baroque, I know, but the current implementation achieves a few goals I think are interesting:

  • First of all, it allows --fix
  • It does this while retaining the processor. Retaining the processor means that vscode et al can load the eslint config -- otherwise they would error out on the header.
  • No temporary files
  • Monkey-patches notice/notice so it can fix a missing license comment

Fix inside editors is not (currently) possible because they must load the plugin processor.

@retorquere

This comment has been minimized.

Copy link
Contributor

retorquere commented Feb 27, 2019

Are we though?

I had forgotten that there are more environments than just the Zotero client that load the translators.

@retorquere

This comment has been minimized.

Copy link
Contributor

retorquere commented Feb 27, 2019

Are the test cases at the end required to be valid JSON or just valid JS?

@dstillman

This comment has been minimized.

Copy link
Member Author

dstillman commented Feb 28, 2019

Are the test cases at the end required to be valid JSON or just valid JS?

JSON, from the looks of it, which doesn't really make sense, but they're only ever generated by Scaffold anyway.

@retorquere

This comment has been minimized.

Copy link
Contributor

retorquere commented Feb 28, 2019

That's ok but then the fixer workaround needs to protect them. I'll add that.

@retorquere

This comment has been minimized.

Copy link
Contributor

retorquere commented Feb 28, 2019

It's not that bad an idea -- JSON can be parsed safely using JSON.parse. Otherwise you'd have to get fancy with either sandboxes or esprima or whatnot.

I've merged the changes into #1895, and added two test for testCases. Fix now works without touching the header or the testCases.

@retorquere

This comment has been minimized.

Copy link
Contributor

retorquere commented Mar 5, 2019

I have some time available this week and I'd love to plow through the translators based on this. I've done some test and after fix (which I did mostly manually so far), the linter reports quite a few genuine errors.

@adam3smith

This comment has been minimized.

Copy link
Collaborator

adam3smith commented Mar 5, 2019

My weekend looks (reasonably) good, so I can help speed-review & merge.

@retorquere

This comment has been minimized.

Copy link
Contributor

retorquere commented Mar 5, 2019

I take it it is still best if I do one pr per translator? Or do you want them batched at any size?

@adam3smith

This comment has been minimized.

Copy link
Collaborator

adam3smith commented Mar 5, 2019

Unless it's much more work, one per translator is easiest in case there are blockers/questions, so if it doesn't make a difference to you, do that. If you find it takes extra time, batches of 5 are fine, too. And thanks!

@retorquere

This comment has been minimized.

Copy link
Contributor

retorquere commented Mar 5, 2019

Nope, I have a few scripts to manage this process, one per is no problem whatsoever.

@retorquere

This comment has been minimized.

Copy link
Contributor

retorquere commented Mar 5, 2019

What about the existing PRs? Squash or submit new ones?

@adam3smith

This comment has been minimized.

Copy link
Collaborator

adam3smith commented Mar 5, 2019

squash if you don't mind. (you don't even need to squash now that github allows me to do that on merge)

@retorquere

This comment has been minimized.

Copy link
Contributor

retorquere commented Mar 5, 2019

No problem. I know what to do now.

@retorquere

This comment has been minimized.

Copy link
Contributor

retorquere commented Mar 5, 2019

Oh if I don't have to... that saves me updating my scripts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.