-
-
Notifications
You must be signed in to change notification settings - Fork 153
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
feat: Object curly newline #490
Open
Brianzchen
wants to merge
5
commits into
gajus:master
Choose a base branch
from
Brianzchen:object-curly-newline
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
### `object-type-curly-newline` | ||
|
||
_The `--fix` option on the command line automatically fixes problems reported by this rule._ | ||
|
||
This rule enforce consistent line breaks after opening and before closing braces of type objects. | ||
|
||
#### Options | ||
|
||
The rule has a string option: | ||
|
||
* `"never"`: disallows multiline type objects. | ||
* `"always"`: requires multiline type objects. | ||
* `"multiline"`(default): acts as `never` until there is a multiline, then acts as `always` | ||
|
||
<!-- assertions objectTypeCurlyNewline --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import {spacingFixers} from '../utilities'; | ||
|
||
const schema = [ | ||
{ | ||
enum: ['always', 'never', 'multiline'], | ||
type: 'string', | ||
}, | ||
]; | ||
|
||
const meta = { | ||
fixable: 'code', | ||
}; | ||
|
||
const create = (context) => { | ||
const option = context?.options[0] ?? 'multiline'; | ||
|
||
return { | ||
ObjectTypeAnnotation (node) { | ||
const { | ||
properties, | ||
} = node; | ||
|
||
if (properties.length === 0) { | ||
return; | ||
} | ||
|
||
const sourceCode = context.getSourceCode(); | ||
|
||
if (option === 'always' || option === 'multiline') { | ||
// If option is multiline act as never but if there are any multi lines | ||
// act as always | ||
if (option === 'multiline' && node.loc.start.line === node.loc.end.line) { | ||
return; | ||
} | ||
|
||
if (properties[0].loc.start.line === node.loc.start.line) { | ||
context.report({ | ||
fix: (fixer) => { | ||
return fixer.insertTextAfter( | ||
sourceCode.getFirstToken(node), | ||
'\n', | ||
); | ||
}, | ||
message: 'There should be a newline after opening curly brace', | ||
node, | ||
}); | ||
} | ||
|
||
if (properties[properties.length - 1].loc.end.line === node.loc.end.line) { | ||
context.report({ | ||
fix: (fixer) => { | ||
return fixer.insertTextBefore( | ||
sourceCode.getLastToken(node), | ||
'\n', | ||
); | ||
}, | ||
message: 'There should be a newline before closing curly brace', | ||
node, | ||
}); | ||
} | ||
} else if (option === 'never') { | ||
if (properties[0].loc.start.line !== node.loc.start.line) { | ||
const openBrace = sourceCode.getFirstToken(node); | ||
context.report({ | ||
fix: spacingFixers.stripSpacesAfter(openBrace, 1), | ||
message: 'There should not be a newline after opening curly brace', | ||
node, | ||
}); | ||
} | ||
|
||
if (properties[properties.length - 1].loc.end.line !== node.loc.end.line) { | ||
const closingBrace = sourceCode.getLastToken(node); | ||
context.report({ | ||
fix: spacingFixers.stripSpacesBefore(closingBrace, 1), | ||
message: 'There should not be a newline before closing curly brace', | ||
node, | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}; | ||
|
||
export default { | ||
create, | ||
meta, | ||
schema, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
export default { | ||
invalid: [ | ||
// Never | ||
{ | ||
code: 'type obj = { "foo": "bar"\n}', | ||
errors: [ | ||
{message: 'There should not be a newline before closing curly brace'}, | ||
], | ||
options: ['never'], | ||
output: 'type obj = { "foo": "bar"}', | ||
}, | ||
{ | ||
code: 'type obj = {\n"foo": "bar" }', | ||
errors: [ | ||
{message: 'There should not be a newline after opening curly brace'}, | ||
], | ||
options: ['never'], | ||
output: 'type obj = {"foo": "bar" }', | ||
}, | ||
{ | ||
code: 'type obj = {\n"foo": "bar"\n}', | ||
errors: [ | ||
{message: 'There should not be a newline after opening curly brace'}, | ||
{message: 'There should not be a newline before closing curly brace'}, | ||
], | ||
options: ['never'], | ||
output: 'type obj = {"foo": "bar"}', | ||
}, | ||
|
||
// Always | ||
{ | ||
code: 'type obj = {"foo": "bar"\n}', | ||
errors: [ | ||
{message: 'There should be a newline after opening curly brace'}, | ||
], | ||
options: ['always'], | ||
output: 'type obj = {\n"foo": "bar"\n}', | ||
}, | ||
{ | ||
code: 'type obj = {\n"foo": "bar"}', | ||
errors: [ | ||
{message: 'There should be a newline before closing curly brace'}, | ||
], | ||
options: ['always'], | ||
output: 'type obj = {\n"foo": "bar"\n}', | ||
}, | ||
{ | ||
code: 'type obj = {"foo": "bar"}', | ||
errors: [ | ||
{message: 'There should be a newline after opening curly brace'}, | ||
{message: 'There should be a newline before closing curly brace'}, | ||
], | ||
options: ['always'], | ||
output: 'type obj = {\n"foo": "bar"\n}', | ||
}, | ||
|
||
// Multiline | ||
{ | ||
code: 'type obj = { "foo": "bar"\n}', | ||
errors: [ | ||
{message: 'There should be a newline after opening curly brace'}, | ||
], | ||
options: ['multiline'], | ||
output: 'type obj = {\n "foo": "bar"\n}', | ||
}, | ||
{ | ||
code: 'type obj = {\n"foo": "bar"}', | ||
errors: [ | ||
{message: 'There should be a newline before closing curly brace'}, | ||
], | ||
options: ['multiline'], | ||
output: 'type obj = {\n"foo": "bar"\n}', | ||
}, | ||
], | ||
valid: [ | ||
{ | ||
code: 'type obj = { test: a }', | ||
options: ['never'], | ||
}, | ||
{ | ||
code: 'type obj = {\ntest: a\n}', | ||
options: ['always'], | ||
}, | ||
{ | ||
code: 'type obj = { test: a }', | ||
options: ['multiline'], | ||
}, | ||
{ | ||
code: 'type obj = {\ntest: a\n}', | ||
options: ['multiline'], | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It would be good to have some tests when there object has multiple properties
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.
Yes I can, just to note though this rule does not cater to multi-lining multiple properties, that'll be handled by a separate rule hopefully in the near future just like standard objects https://eslint.org/docs/rules/object-property-newline.
Edit: found some bug behaviour, will fix