Skip to content

Commit c40937a

Browse files
author
Brian Chen
authored
fix: enforceLineBreak now handles export type correctly (#488)
* fix enforce breakline with export type * add case for exports with comments * switch to using comments before func
1 parent 79267bb commit c40937a

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

src/rules/enforceLineBreak.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,31 @@ const create = (context) => {
1212
return;
1313
}
1414

15+
const exportedType = node.parent.type === 'ExportNamedDeclaration';
16+
const leadingComments = sourceCode.getCommentsBefore(exportedType ? node.parent : node);
17+
const hasLeadingComments = leadingComments.length > 0;
18+
1519
if (node.loc.start.line !== 1) {
16-
if (node.leadingComments && node.leadingComments[0].loc.start.line !== 1) {
17-
const lineAboveComment = sourceCode.lines[node.leadingComments[0].loc.start.line - 2];
20+
if (hasLeadingComments && leadingComments[0].loc.start.line !== 1) {
21+
const lineAboveComment = sourceCode.lines[leadingComments[0].loc.start.line - 2];
1822
if (lineAboveComment !== '') {
1923
context.report({
2024
fix (fixer) {
21-
return fixer.insertTextBeforeRange(node.leadingComments[0].range, '\n');
25+
return fixer.insertTextBeforeRange(leadingComments[0].range, '\n');
2226
},
2327
message: breakLineMessage('above'),
2428
node,
2529
});
2630
}
27-
} else if (!node.leadingComments) {
31+
} else if (!hasLeadingComments) {
2832
const isLineAbove = sourceCode.lines[node.loc.start.line - 2];
2933
if (isLineAbove !== '') {
3034
context.report({
3135
fix (fixer) {
32-
return fixer.insertTextBefore(node, '\n');
36+
return fixer.insertTextBefore(
37+
exportedType ? node.parent : node,
38+
'\n',
39+
);
3340
},
3441
message: breakLineMessage('above'),
3542
node,

tests/rules/assertions/enforceLineBreak.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,36 @@ export default {
3636
],
3737
output: 'type hello = 34;\n\nconst som = "jes";\n\ntype fed = "hed";\n',
3838
},
39+
{
40+
code: 'const a = 5;\nexport type hello = 34;\n',
41+
errors: [
42+
{message: 'New line required above type declaration'},
43+
],
44+
output: 'const a = 5;\n\nexport type hello = 34;\n',
45+
},
46+
{
47+
code: 'const a = 5;\n// a comment\nexport type hello = 34;\n',
48+
errors: [
49+
{message: 'New line required above type declaration'},
50+
],
51+
output: 'const a = 5;\n\n// a comment\nexport type hello = 34;\n',
52+
},
53+
{
54+
code: `const a = 5;
55+
/**
56+
* a jsdoc block
57+
*/
58+
type hello = 34;`,
59+
errors: [
60+
{message: 'New line required above type declaration'},
61+
],
62+
output: `const a = 5;
63+
64+
/**
65+
* a jsdoc block
66+
*/
67+
type hello = 34;`,
68+
},
3969
],
4070
valid: [
4171
{
@@ -77,5 +107,8 @@ type Props = {
77107
78108
type RoadT = "grass" | "gravel" | "cement";`,
79109
},
110+
{
111+
code: '// @flow\ntype A = string',
112+
},
80113
],
81114
};

0 commit comments

Comments
 (0)