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

Fix: object-curly-newline for flow code #9458

Merged

Conversation

TiddoLangerak
Copy link
Contributor

@TiddoLangerak TiddoLangerak commented Oct 16, 2017

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

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

The object-curly-newline rule made the assumption that the last token
is always }. However, when an object is typed with flow this is not
the case. This causes false positives, as described in babel/babel-eslint#513.

What changes did you make? (Give an overview)

By adding a filter to the getLastToken function we skip over the flow
types, and get the actual closing brace. For symmetry the same filter is
added for the open brace, though I'm not aware of any issues it would
fix.

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

The `object-curly-newline` rule made the assumption that the last token
is always `}`. However, when an object is typed with flow this is not
the case. This causes false positives, as described in babel/babel-eslint#513.

By adding a filter to the `getLastToken` function we skip over the flow
types, and get the actual closing brace. For symmetry the same filter is
added for the open brace, though I'm not aware of any issues it would
fix.
@jsf-clabot
Copy link

jsf-clabot commented Oct 16, 2017

CLA assistant check
All committers have signed the CLA.

@eslintbot
Copy link

LGTM

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.

Hi @TiddoLangerak, thanks for the pull request.

Could you please add a test? We have some examples of test fixtures that represent different parsers-- not sure if we have any Flow ones but we have plenty of Typescript examples. Could you please add a test to this rule which uses a fixture you create that contains flow types in the AST, showing that the rule will correctly skip to the closing brace token? Thanks!

@aladdin-add aladdin-add added bug ESLint is working incorrectly evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules labels Oct 16, 2017
- Added tests for `object-curly-newline` in code annotated with flow types
- Fixed incorrect behaviour of object-curly-newline for literal object types
@eslintbot
Copy link

LGTM

@TiddoLangerak
Copy link
Contributor Author

TiddoLangerak commented Oct 17, 2017

@platinumazure I added tests similar to those found for object-curly-spacing. In the process I also discovered that my original fix wasn't sufficient, it would cause incorrect behaviour for objects with literal object types (e.g. { a : 3 } : { a : number }). I fixed this now, but as a result most of the original patch has changed.

@TiddoLangerak
Copy link
Contributor Author

I know you guys are busy so I don't want to pressure you, but does someone perhaps have time to take another look at this?

@aladdin-add
Copy link
Member

cc @eslint/eslint-team 😄


// With flow, an object can have the form of `{ a : 3 } : { a : number }`. In this form the
// closing brace isn't simply the last brace. In order to find the closing brace we actually
// need to traverse through the node and count the braces, to find the matching pair.
Copy link
Member

@btmills btmills Oct 31, 2017

Choose a reason for hiding this comment

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

Is there a way to do this that doesn't involve linearly traversing the node and counting braces? I'm imagining something like this:

if (node.typeAnnotation) {
    return sourceCode.getTokenBefore(node.typeAnnotation);
} else {
    return sourceCode.getLastToken(node);
}

Where node is the same node passed to check, instead of passing the openBrace to this 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.

I vaguely remember looking for that, but couldn't find it. Nevertheless, I'll look again and see if I can make this linear.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Welp, that exact snippet seems to have done the trick 😄 Thanks for helping me out!

@TiddoLangerak TiddoLangerak changed the title Fix object-curly-newline for flow code Fix: object-curly-newline for flow code Nov 1, 2017
const openBrace = sourceCode.getFirstToken(node, token => token.value === "{");
let closeBrace;

if (node.typeAnnotation) {
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should move this into astUtils. We have the same code in a bunch of rules already.

Copy link
Member

@btmills btmills left a comment

Choose a reason for hiding this comment

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

LGTM - nice work @TiddoLangerak!

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.

LGTM, thanks! Sorry for dropping the ball on this. Thanks for adding tests!

@ilyavolodin ilyavolodin 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 Nov 6, 2017
@ilyavolodin ilyavolodin merged commit e5a37ce into eslint:master Nov 6, 2017
@TiddoLangerak TiddoLangerak deleted the fix-object-curly-newline-for-flow branch November 6, 2017 18:46
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators May 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 May 6, 2018
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 bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants