Skip to content

Conversation

jrencz
Copy link
Contributor

@jrencz jrencz commented Aug 2, 2016

What issue does this pull request address?
#6778

What changes did you make? (Give an overview)
I implemented symbol-description rule

Is there anything you'd like reviewers to focus on?

  • whether rule is supposed to be recommended or not
  • in which group (Best practices or ES6) should this rule be placed in

TODO:

@mention-bot
Copy link

@jrencz, thanks for your PR! By analyzing the annotation information on this pull request, we identified @nzakas, @alberto and @kaicataldo to be potential reviewers

@eslintbot
Copy link

LGTM

@eslintbot
Copy link

LGTM

@mysticatea mysticatea added the do not merge This pull request should not be merged yet label Aug 2, 2016
@mysticatea
Copy link
Member

mysticatea commented Aug 2, 2016

Thank you for this PR!

I added do not merge label for now since #6778 has not been accepted yet.
I predict the issue to be accepted soon.

@ferrante
Copy link

ferrante commented Aug 2, 2016

+1

@nzakas nzakas removed the do not merge This pull request should not be merged yet label Aug 2, 2016
@nzakas
Copy link
Member

nzakas commented Aug 2, 2016

Issue has been accepted.

@@ -0,0 +1,94 @@
# Require/Disallow Symbol Description (symbol-description)

The `Symbol` constructor may have optional description:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: it's not a constructor, just a function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Then no-new-symbol should be fixed as well - I based this one on that.

The Symbol constructor is not intended to be used with the new operator

I'll fix it as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrencz I would recommend fixing those docs in a separate issue/PR (if you weren't already thinking of doing that)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nzakas
Copy link
Member

nzakas commented Aug 2, 2016

This is a really great start @jrencz. My comments are mostly just fine-tuning what you already have here.


## When Not To Use It

This rule should not be used in ES3/5 environments and in case you don't want to enforce either presence or omission of `description`.
Copy link
Member

@platinumazure platinumazure Aug 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might be a little awkward as one sentence, and would do better broken up into two sentences.

For example:
This rule should not be used in ES3/5 environments. In addition, this rule can be safely turned off if you don't want to enforce either the presence or the omission of description when creating Symbols.

Not a huge deal-- your call on whether you want to change or not.

@platinumazure
Copy link
Member

platinumazure commented Aug 2, 2016

This is looking really good. Just two things from me:

  1. I left an inline comment about a config section with a long and potentially awkward sentence.
  2. I would love to see some tests around redefining or shadowing the Symbol variable:
    1. Redefine: Symbol = foo; Symbol(); should be valid even if rule is set to always
    2. Redefine: Symbol = foo; Symbol("stuff"); should be valid even if rule is set to never (may not apply if "never" option removed)
    3. Redefine (advanced): Symbol(); Symbol = foo; should be invalid if rule is set to always
    4. Shadow: function bar() { var Symbol = foo; Symbol(); } should be valid even if rule is set to always
    5. Shadow: function bar() { var Symbol = foo; Symbol("stuff"); } should be valid even if rule is set to never (may not apply if "never" option removed)

@eslintbot
Copy link

LGTM

@jrencz
Copy link
Contributor Author

jrencz commented Aug 2, 2016

@platinumazure @nzakas than you for the review.

I applied all of @nzakas comments (I have option removal commit ready yet not pushed).

I'll add tests @platinumazure asked for (but ii and v since never is removed).

@platinumazure
Copy link
Member

@jrencz Thanks! I'm wondering, though, maybe we should just remove the "always" option altogether and have schema: []? Seems it would simplify some things.

@eslintbot
Copy link

LGTM

1 similar comment
@eslintbot
Copy link

LGTM

@jrencz
Copy link
Contributor Author

jrencz commented Aug 2, 2016

State of the PR: I need to add rules suggested by @platinumazure. All other comments were applied.

> console.log(foo);
// Symbol(foo)

var bar = foo + '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually throws an error. Symbols cannot be coerced.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right

@eslintbot
Copy link

LGTM

2 similar comments
@eslintbot
Copy link

LGTM

@eslintbot
Copy link

LGTM


## Rule Details

This rule allows enforcing or disallowing symbol descriptions.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I like "allows" followed by "disallowing", seems a bit confusing. I think it's better to follow the language we used in other rules like arrow-body-style, func-name, etc. "This rule can enforce or disallow the use of braces around arrow function body."

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now it's more like, "This rules requires a description when creating symbols."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@ilyavolodin
Copy link
Member

Just a few minor comments about documentation, otherwise LGTM.

@@ -0,0 +1,61 @@
# Require Symbol Description (symbol-description)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These words should be all lowercase

@platinumazure
Copy link
Member

Friendly ping @jrencz, need any help?

@jrencz
Copy link
Contributor Author

jrencz commented Aug 19, 2016

@platinumazure I had no time to start. I'll try today

@jrencz jrencz force-pushed the symbol-description branch from d9ef644 to e0f7a71 Compare August 19, 2016 09:53
@eslintbot
Copy link

LGTM

1 similar comment
@eslintbot
Copy link

LGTM

@jrencz
Copy link
Contributor Author

jrencz commented Aug 19, 2016

@platinumazure I added the tests you suggested. It turns out my implementation (which is combined from no-new-symbol and radix) did covers all cases. Please review now.

There's one case which you didn't mention and i'm not sure if it can be covered:

Symbol = function () {};
Symbol();

If symbol is reassigned in the same scope as it's called but before it's called (you'r point 3 was about redefining after it's called). Symbol call in this example is a call of noop function assigned to Symbol identifier. It raises a warning although it shouldn't but I'n not sure if it's possible to detect if Symbol was reassigned. I think it may be checked in the same function scope but definitely not in general (in global scope).

@platinumazure
Copy link
Member

@mysticatea Is the issue which @jrencz raised here possible to catch via escope?

@mysticatea
Copy link
Member

I think the current implementation is good.
Several rules are using the same way (variable && variable.defs.length === 0) to detect redeclarations, but we have received no complaint about it.

We need dynamic analysis to detect completely the timing of the overwrites, but ESLint is a static analyzer 😄 .

}

return {
"Program:exit": () => {
Copy link
Member

@mysticatea mysticatea Aug 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel this : and => are extra.

"Program:exit"() {

(I'm sorry, very nit-picking.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should object-shorthand (which we have on in the codebase I think?) catch this? I guess it might not due to this working differently...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends on whether you treat it as a method that should use the object's this or a regular function that may only react to the this from the outer environment in the place of its definition.

Both could be used in the same project based on various circumstances.

@platinumazure
Copy link
Member

@mysticatea I'll defer to your opinion. If you think the current implementation is good and that we probably can't do better, feel free to merge (if enough people have looked at it, in your view, and enough time has passed). Thanks!

@mysticatea
Copy link
Member

mysticatea commented Aug 19, 2016

Oops, sorry, I had been asked about re-assignments, not redeclarations. Hmm, interesting.

variable && variable.references.some(r => r.isWrite())

This is a check of re-assignment (though it doesn't detect timing...). I think fine that we add the check. But if we don't add the check, it will be warned by no-undef or no-global-assign, so it's not bad. I don't have strong opinions here.

@jrencz
Copy link
Contributor Author

jrencz commented Aug 19, 2016

But if we don't add the check, it will be warned by no-undef or no-global-assign, so it's not bad.

I think it sticks to the policy that each rule should overlap with others as little as possible. Let's stick to what we have now then.

I'm going to squash it now and It looks like it's ready to be merged (at least from my point of view)

@jrencz jrencz force-pushed the symbol-description branch from eb52c28 to d0f0496 Compare August 19, 2016 16:11
@eslintbot
Copy link

LGTM

@nzakas
Copy link
Member

nzakas commented Aug 19, 2016

We don't need to check for reassignment, only that Symbol is a global reference. If people do weird things to that reference, it's on them. :)

@nzakas
Copy link
Member

nzakas commented Aug 19, 2016

Lgtm, just waiting another day for others to review.

@platinumazure
Copy link
Member

LGTM.

@vitorbal
Copy link
Member

LGTM

@ilyavolodin
Copy link
Member

LGTM. Thanks @jrencz for working on this!

@ilyavolodin ilyavolodin merged commit 0d268f1 into eslint:master Aug 23, 2016
@jrencz jrencz deleted the symbol-description branch August 23, 2016 15:40
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 6, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion
Projects
None yet
Development

Successfully merging this pull request may close these issues.