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

New: Add object options to object-property-newline for JSCS. #9581

Closed
wants to merge 18 commits into from
Closed

New: Add object options to object-property-newline for JSCS. #9581

wants to merge 18 commits into from

Conversation

jrpool
Copy link
Contributor

@jrpool jrpool commented Nov 4, 2017

New options make the rule compatible with JSCS rule requireObjectKeysOnNewLine.
Need for noCommaFirst option, instead of comma-style, explained in the rule document. Partly fixes issue #6251.

What is the purpose of this pull request? (put an "X" next to item)

[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[X] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

What rule do you want to change?
object-property-newline

Does this change cause the rule to produce more or fewer warnings?
No.

How will the change be implemented? (New option, new default behavior, etc.)?
2 new options.

Please provide some example code that this change will affect:

const newObject = {
    a: 1, [
        process.argv[4]
    ]: '01'
};

What does the rule currently do for this code?
Treats it as invalid.

What will the rule do after it's changed?
Treat it as valid if the treatComputedPropertiesLikeJSCS object option is set to true.

What changes did you make? (Give an overview)
Added 2 options to object-property-newline for compatibility with JSCS rule requireObjectKeysOnNewLine.

Is there anything you'd like reviewers to focus on?
Determine whether my judgment is correct that the comma-style rule cannot be relied on to achieve compatibility with the JSCS rule and therefore the noCommaFirst object option is appropriate. See conversation in PR #9522.

@aladdin-add aladdin-add added enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules labels Nov 5, 2017
Copy link
Member

@platinumazure platinumazure left a comment

Choose a reason for hiding this comment

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

I've reviewed the documentation and rule implementation, but skipped the tests since I had a number of questions about the first two.

I apologize if I missed some earlier conversation about the direction you've decided to take here, and I appreciate any context you can give me that might help me overcome any misunderstandings I might be exhibiting here. Thanks!

### Optional Exception
### Optional Exceptions

The rule offers three object option.
Copy link
Member

Choose a reason for hiding this comment

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

Typo: option --> options

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you, @platinumazure, for the review and suggestions. I’ll attend to these.

The rule offers one object option, `allowMultiplePropertiesPerLine`. If you set it to `true`, object literals such as the first two above, with all property specifications on the same line, will be permitted, but one like
#### `allowMultiplePropertiesPerLine`

If you set `allowMultiplePropertiesPerLine` to `true`, object literals such as the first two above, with all property specifications on the same line, will be permitted, but one like
Copy link
Member

Choose a reason for hiding this comment

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

In other rule documentation, we like to use patterns like the below:

Examples of **correct** code with the `allowMultiplePropertiesPerLine: true` option:

```js
/* examples here */
```

Examples of **incorrect** code with the `allowMultiplePropertiesPerLine: true` option:

```js
/* examples here */
```

Referring to other examples ("the first two above") makes the documentation harder to read. It's better to repeat examples than to refer to examples elsewhere in the document.

```

will be prohibited, because two properties, but not all properties, appear on the same line.

#### `treatComputedPropertiesLikeJSCS`

If you set `treatComputedPropertiesLikeJSCS` to `true`, an object literal such as the one below will be permitted:
Copy link
Member

Choose a reason for hiding this comment

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

See above comment for info on our example conventions.

```

will be prohibited, because two properties, but not all properties, appear on the same line.

#### `treatComputedPropertiesLikeJSCS`
Copy link
Member

Choose a reason for hiding this comment

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

I think this option name should be more generic and not reference JSCS directly (although the option description can certainly reference JSCS). Maybe something like ignoreBracketsInComputedProperties?


This object option makes the rule stricter by prohibiting one of the patterns by which you could comply with the rule. Specifically, the comma between two property specifications may not appear before the second one on the same line. The JSCS rule `requireObjectKeysOnNewLine` treats commas this way, so this object option makes ESLint compatible with JSCS in this respect.

You can use the `comma-style` rule instead of this option to achieve partial JSCS compatibility, but not in combination with the `treatComputedPropertiesLikeJSCS` object option. Using the `comma-style` rule for the sole purpose of JSCS compatibility would also require you to enumerate 9 exceptions, leaving only `ObjectExpression` subject to the rule.
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 understand this part. Why can't comma-style be used in combination with treatComputedPropertiesLikeJSCS?

Should this be valid or invalid:

/* eslint comma-style: ["error", "first"], object-property-newline: ["error", { treatComputedPropertiesLikeJSCS: true }] */
({
    foo: "bar", [
        "computed"
    ]: true
    , baz: "woo"     // <--- this leading comma here: valid or invalid?
});

const sourceCode = context.getSourceCode();

/**
* Returns whether a property begins with a bracket that ends its line.
* @param {token} myToken0 Current property’s first token.
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I think firstToken or propertyFirstToken would be a better variable name.


/**
* Returns whether a comma precedes a property on the same line.
* @param {token} myToken0 Current property’s first token.
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I think firstToken or propertyFirstToken would be a better variable name.

validPerLeadingComma(firstTokenOfCurrentProperty, sourceCode, currentPropertyStartLine);
} else if (allowOpenBracket && denyCommaFirst) {

// A line containing only “, [” is valid here.
Copy link
Member

Choose a reason for hiding this comment

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

Why should this be valid? Is that how JSCS handles this case? Would it make more sense to augment comma-style with an option to allow computed properties after commas in "last" mode?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, JSCS treats such a line as valid per its requireObjectKeysOnNewLine rule. One might presume that if JSCS has such a rule then there is some demand for it.

Given that ESLint wants to establish JSCS compatibility, it seems to me more user-friendly to tell JSCS users how to do that for a single JSCS rule with instructions for configuring a single ESLint rule, rather than making them piece together 2 ESLint rules.

Also, adding an option to comma-style could cover this case but not the case of a line with a lone comma on it. That would need another new option.

Also, this may be due to my inadequate knowledge, but I don’t understand how the comma-style rule can be simultaneously applied oppositely to 2 or more of the 10 node types that it applies to. If my impression is correct, then it seems questionable to load additional options onto a rule that already has so many.

I have added what I think is a thorough explanation of the (in)applicability of comma-style to the noCommaFirst section of docs/rules/object-property-newline. I hope it at least shows why comma-style as it now exists can’t do the job. The other points above lead me to believe that confining these alterations to object-property-newline is, on balance, wiser than giving part of the job to comma-style, but there may be reasons for the latter that I’m not aware of.

jrpool and others added 18 commits January 8, 2018 16:47
New options make the rule compatible with JSCS rule requireObjectKeysOnNewLine.
Need for noCommaFirst option, instead of comma-style, explained in the rule document.
…#9819)

This updates `Linter` to treat malformed configuration comments as linting errors rather than fatal issues that crash the process. Previously, this could cause surprising behavior because it was the only known instance where a problem in the source code (given a valid configuration) could cause an error to be thrown.

For example, this also fixes eslint/archive-website#413.
@jrpool
Copy link
Contributor Author

jrpool commented Jan 10, 2018

I believe I have completed the responses to all above comments and change requests.

@kaicataldo
Copy link
Member

Apologies - it looks like we lost track of this. @eslint/eslint-team Can we give this a look?

@aladdin-add
Copy link
Member

@platinumazure have your comments being addressed?

this is a jscs-compatible issue, labelled accepted.

@aladdin-add aladdin-add added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Jul 21, 2018
@nzakas
Copy link
Member

nzakas commented Dec 7, 2018

@platinumazure have your comments been addressed? We should either work to merge this or close it out as this PR is now over a year old.

@kaicataldo
Copy link
Member

One more friendly ping here

@platinumazure
Copy link
Member

I'm still confused about the special case where, if comma first is not allowed but open bracket could be on its own line, why , [ is valid on its own line. I had suggested that maybe this rule should not try to worry about comma placement and that the comma-style rule should try to handle that case.

I'd have to dig into this again more deeply to try to figure out a more concrete proposal for how this should be handled specifically.

@jrpool
Copy link
Contributor Author

jrpool commented Jan 20, 2019

Thank you, @kaicataldo and @platinumazure, for noticing this long-incomplete thread. There doesn't seem to exist a consensus or decision yet on whether compatibility with the JSCS rule requireObjectKeysOnNewLine may and should require modifications to both object-property-newline and comma-style. Or is there possibly a reason not to insist on 100% compatibility with JSCS?

@platinumazure
Copy link
Member

@jrpool My take is that we want to provide a way for any JSCS settings to be achieved (except for any prudent decisions we might make on environment-specific options, etc., which go against ESLint's design principles of trying not to favor libraries and runtimes).

However, I don't think one JSCS rule absolutely needs to map to one ESLint rule. In my comment above, I think my suggestion was we should try to let comma-style and other rules related to commas handle the comma logic, and let this rule focus on newlines around/between object properties. I think we can reasonably note, in rule documentation and/or a hypothetical JSCS migration guide, that for some particular setting, a user might need to configure 2 ESLint rules to get the desired behavior.

That's just my take. @kaicataldo Do you have any thoughts?

@kaicataldo
Copy link
Member

Agreed with @platinumazure! My understanding of "parity with JSCS" is that we want to be able to enforce styles that you can in JSCS, but the actual API doesn't have to match.

@ilyavolodin
Copy link
Member

Since this PR has been sitting here without any movement for a long while now, I'm going to go ahead and close it. @jrpool thanks again for your effort to push this through!

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Nov 22, 2019
@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 Nov 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants