Skip to content

Commit

Permalink
Fix: object-curly-newline for flow code (#9458)
Browse files Browse the repository at this point in the history
* Fix: object-curly-newline for flow code (fixes babel-eslint/513)

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.

* Fix object-curly-newline for flow type literals & added tests

- Added tests for `object-curly-newline` in code annotated with flow types
- Fixed incorrect behaviour of object-curly-newline for literal object types

* Replace brace counting to find matching brace with direct lookup

As per #9458 (comment)
  • Loading branch information
TiddoLangerak authored and ilyavolodin committed Nov 6, 2017
1 parent 9064b9c commit e5a37ce
Show file tree
Hide file tree
Showing 6 changed files with 2,923 additions and 3 deletions.
11 changes: 9 additions & 2 deletions lib/rules/object-curly-newline.js
Expand Up @@ -131,8 +131,15 @@ module.exports = {
*/
function check(node) {
const options = normalizedOptions[node.type];
const openBrace = sourceCode.getFirstToken(node);
const closeBrace = sourceCode.getLastToken(node);
const openBrace = sourceCode.getFirstToken(node, token => token.value === "{");
let closeBrace;

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

let first = sourceCode.getTokenAfter(openBrace, { includeComments: true });
let last = sourceCode.getTokenBefore(closeBrace, { includeComments: true });
const needsLinebreaks = (
Expand Down

0 comments on commit e5a37ce

Please sign in to comment.