Skip to content

Commit

Permalink
fix: enforceLineBreak now handles export type correctly (#488)
Browse files Browse the repository at this point in the history
* fix enforce breakline with export type

* add case for exports with comments

* switch to using comments before func
  • Loading branch information
Brian Chen committed Jul 26, 2021
1 parent 79267bb commit c40937a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/rules/enforceLineBreak.js
Expand Up @@ -12,24 +12,31 @@ const create = (context) => {
return;
}

const exportedType = node.parent.type === 'ExportNamedDeclaration';
const leadingComments = sourceCode.getCommentsBefore(exportedType ? node.parent : node);
const hasLeadingComments = leadingComments.length > 0;

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 (hasLeadingComments && leadingComments[0].loc.start.line !== 1) {
const lineAboveComment = sourceCode.lines[leadingComments[0].loc.start.line - 2];
if (lineAboveComment !== '') {
context.report({
fix (fixer) {
return fixer.insertTextBeforeRange(node.leadingComments[0].range, '\n');
return fixer.insertTextBeforeRange(leadingComments[0].range, '\n');
},
message: breakLineMessage('above'),
node,
});
}
} else if (!node.leadingComments) {
} else if (!hasLeadingComments) {
const isLineAbove = sourceCode.lines[node.loc.start.line - 2];
if (isLineAbove !== '') {
context.report({
fix (fixer) {
return fixer.insertTextBefore(node, '\n');
return fixer.insertTextBefore(
exportedType ? node.parent : node,
'\n',
);
},
message: breakLineMessage('above'),
node,
Expand Down
33 changes: 33 additions & 0 deletions tests/rules/assertions/enforceLineBreak.js
Expand Up @@ -36,6 +36,36 @@ export default {
],
output: 'type hello = 34;\n\nconst som = "jes";\n\ntype fed = "hed";\n',
},
{
code: 'const a = 5;\nexport type hello = 34;\n',
errors: [
{message: 'New line required above type declaration'},
],
output: 'const a = 5;\n\nexport type hello = 34;\n',
},
{
code: 'const a = 5;\n// a comment\nexport type hello = 34;\n',
errors: [
{message: 'New line required above type declaration'},
],
output: 'const a = 5;\n\n// a comment\nexport type hello = 34;\n',
},
{
code: `const a = 5;
/**
* a jsdoc block
*/
type hello = 34;`,
errors: [
{message: 'New line required above type declaration'},
],
output: `const a = 5;
/**
* a jsdoc block
*/
type hello = 34;`,
},
],
valid: [
{
Expand Down Expand Up @@ -77,5 +107,8 @@ type Props = {
type RoadT = "grass" | "gravel" | "cement";`,
},
{
code: '// @flow\ntype A = string',
},
],
};

0 comments on commit c40937a

Please sign in to comment.