-
-
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
Add no-mutable-exports #290
Conversation
Thanks for this, great rule. Use case makes a lot of sense. Can you add a reference to the README under "Helpful warnings" and add a note to the change log? (See recent closed rule PRs for an example of the latter) |
'ExportNamedDeclaration': function (node) { | ||
const kind = node.declaration.kind | ||
if (kind === 'var' || kind === 'let') { | ||
context.report(node, `Exporting mutable '${kind}' binding, use const instead.`) |
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.
For consistency, could you wrap const
in quotes too?
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's some odd indentation here, do you mind fixing it?
Thanks for the rule @josh! :) Quick question (I don't think I have a project set up to test it): is this valid? const foo = 2;
export foo; If so, would be nice to track the type of variables and error when |
@jfmengels is that valid syntax? Should be |
That's my question exactly. Figured out I could test it on |
To that end, would be nice-to-have if Might not be too hard, though; could capture all the declarations in the @jfmengels, what's your take on doing that pre- vs. post-merge of this? should handling non-inline declarations be handled before publishing this at all? (I think that's the primary question for when to merge, should be publish-ready before hitting |
If that is valid code-wise but invalid exports-wise, it should be covered. If @josh is fine with working on this and handling this case, we should wait until all cases are covered. I don't know how semver applies with adding new errors in existing rules. The further addition of this case would probably be akin to a bug fix in the case, so a patch, but if we can ship this all in one go, I'd much prefer it. @josh Do you mind continuing to work on this? |
Added test coverage for null declarations. I'm not sure my declaration tracing implementation is ideal. Any better ideas? |
@josh tests look good so I'm cool with what you've done. I'm 0% familiar with the scope object, thus my suggestion. I think yours is likely more robust. A few considerations still:
|
@benmosher great suggestions! Updated the PR. Let me know if I still missed some cases. |
this is great! I was about to ask for it and now I don't have to. Looking forward to the next version. |
It would be good if that was checked as well, e.g. in a no-export-mutate rule |
@lukeapage actually, I hadn't thought about it before, but this rule could check *I'm not 100% positive this is true, but IIRC it is. @josh would you be up for implementing that? i.e. for export class x {}
// later: (or earlier, given hoisting)
x = "whatever" // report this reassignment to `x`
// need same for:
export function y() {}
y = "foo, or bar, or baz, or..." // report this reassignment too
// and even
function z() {}
export { z as a }
z = function someOtherZ() {} |
Ha, is "pass" an acceptable answer? |
Eslint does already have a no-func-assign and no-class-assign so you could also either reccomend those in connection with this or look at their source for inspiration.. |
Fair enough, on both counts. I think I'll merge and save function and class identifier checks for later. Will just make a note in the doc. |
Doesn't merge anymore :( |
Rebased and merged! 😄 |
BTW am I committing a GitHub faux pas by rebasing and merging PRs? I noticed it doesn't mark the PR merged but I do my best to keep the original author metadata on the commits when moving them around. |
Ive been involved in lots of projects and no its not unusual.
|
@benmosher no it's fine IMO. When you simply want to fix something without having a second commit, I would do the opposite though: merge, and then rebase/fix, that way contributors (especially the new ones) can see their work as properly accepted and merged. Lodash does it all the time, and does it almost all of the time to have a clean history without merge commits (might be less rebases now that GH has a button for this), but it helps that there's one main maintainer on it (like here), as it may force contributors to rebase (which you'd like to avoid as much as possibl obviously). |
❤️
If you use the Squash button UI on the PR it will close it correctly. Otherwise if your doing things by hand, just put |
Hey @benmosher, this plugin is pretty rad 🤘.
I added a rule to prevent the use of
export let
andexport var
. ES module export bindings are supposed to be live which is a pretty interesting concept, but tricky to shim correctly. Most CJS or AMD transpilers don't cover these cases correctly. So I'm just trying to avoid them in our code base until native implementations are finally here.export const
is usually what you want anyway.http://www.2ality.com/2014/09/es6-modules-final.html
http://stackoverflow.com/questions/32558514/javascript-es6-export-const-vs-export-let