Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new rule enforce-line-break (#484)
* feat: add new rule enforce-line-break * feat: edit new rule enforce line break - don't add line if comments above type declaration is on the 1st line, update tests * fix: clean up code * fix: change message - enforceLineBreak Co-authored-by: Angelica Bocanegra <angelica.bocanegra@tabcorp.com.au>
- Loading branch information
1 parent
d2f6d9b
commit 417068c
Showing
7 changed files
with
231 additions
and
0 deletions.
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,5 @@ | ||
### `enforce-line-break` | ||
|
||
This rule enforces line breaks between type definitions. | ||
|
||
<!-- assertions enforceLineBreak --> |
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
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,63 @@ | ||
const schema = []; | ||
|
||
const breakLineMessage = (direction) => { | ||
return `New line required ${direction} type declaration`; | ||
}; | ||
|
||
const create = (context) => { | ||
return { | ||
TypeAlias (node) { | ||
const sourceCode = context.getSourceCode(); | ||
if (sourceCode.lines.length === 1) { | ||
return; | ||
} | ||
|
||
if (node.loc.start.line !== 1) { | ||
if (node.leadingComments && node.leadingComments[0].loc.start.line !== 1) { | ||
const lineAboveComment = sourceCode.lines[node.leadingComments[0].loc.start.line - 2]; | ||
if (lineAboveComment !== '') { | ||
context.report({ | ||
fix (fixer) { | ||
return fixer.insertTextBeforeRange(node.leadingComments[0].range, '\n'); | ||
}, | ||
message: breakLineMessage('above'), | ||
node, | ||
}); | ||
} | ||
} else if (!node.leadingComments) { | ||
const isLineAbove = sourceCode.lines[node.loc.start.line - 2]; | ||
if (isLineAbove !== '') { | ||
context.report({ | ||
fix (fixer) { | ||
return fixer.insertTextBefore(node, '\n'); | ||
}, | ||
message: breakLineMessage('above'), | ||
node, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
if (sourceCode.lines.length !== node.loc.end.line) { | ||
const isLineBelow = sourceCode.lines[node.loc.end.line]; | ||
if (isLineBelow !== '') { | ||
context.report({ | ||
fix (fixer) { | ||
return fixer.insertTextAfter(node, '\n'); | ||
}, | ||
message: breakLineMessage('below'), | ||
node, | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}; | ||
|
||
export default { | ||
create, | ||
meta: { | ||
fixable: 'code', | ||
}, | ||
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,81 @@ | ||
export default { | ||
invalid: [ | ||
{ | ||
code: 'type baz = 6;\nconst hi = 2;', | ||
errors: [{ | ||
message: 'New line required below type declaration', | ||
}], | ||
output: 'type baz = 6;\n\nconst hi = 2;', | ||
}, | ||
{ | ||
code: 'const foo = 6;\ntype hi = 2;\n', | ||
errors: [ | ||
{message: 'New line required above type declaration'}, | ||
], | ||
output: 'const foo = 6;\n\ntype hi = 2;\n', | ||
}, | ||
{ | ||
code: 'const som = "jes";\n// a comment\ntype fed = "hed";\n', | ||
errors: [ | ||
{message: 'New line required above type declaration'}, | ||
], | ||
output: 'const som = "jes";\n\n// a comment\ntype fed = "hed";\n', | ||
}, | ||
{ | ||
code: 'type som = "jes";\n// a comment\nconst fed = "hed";\n', | ||
errors: [ | ||
{message: 'New line required below type declaration'}, | ||
], | ||
output: 'type som = "jes";\n\n// a comment\nconst fed = "hed";\n', | ||
}, | ||
{ | ||
code: 'type hello = 34;\nconst som = "jes";\ntype fed = "hed";\n', | ||
errors: [ | ||
{message: 'New line required below type declaration'}, | ||
{message: 'New line required above type declaration'}, | ||
], | ||
output: 'type hello = 34;\n\nconst som = "jes";\n\ntype fed = "hed";\n', | ||
}, | ||
], | ||
valid: [ | ||
{ | ||
code: 'type gjs = 6;', | ||
}, | ||
{ | ||
code: 'type gjs = 6;\n\ntype hi = 2;\n', | ||
}, | ||
{ | ||
code: | ||
`type X = 4; | ||
const red = "serpent"; | ||
console.log("hello"); | ||
// number or string | ||
type Y = string | number; | ||
// resting + sleep | ||
type snooze = "dreaming" | "";`, | ||
}, | ||
{ | ||
code: | ||
`type Props = { | ||
accountBalance: string | number, | ||
accountNumber: string | number, | ||
};`, | ||
}, | ||
{ | ||
code: | ||
`const x = 4; | ||
const y = 489; | ||
// Some Comment | ||
type Props = { | ||
accountBalance: string | number, | ||
accountNumber: string | number, | ||
}; | ||
type RoadT = "grass" | "gravel" | "cement";`, | ||
}, | ||
], | ||
}; |
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