From c40937a4da3864ae94dc5190fd2b8adb4a9b2b41 Mon Sep 17 00:00:00 2001 From: Brian Chen Date: Tue, 27 Jul 2021 04:07:34 +1000 Subject: [PATCH] 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 --- src/rules/enforceLineBreak.js | 17 +++++++---- tests/rules/assertions/enforceLineBreak.js | 33 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/rules/enforceLineBreak.js b/src/rules/enforceLineBreak.js index e898bd8..3dc248f 100644 --- a/src/rules/enforceLineBreak.js +++ b/src/rules/enforceLineBreak.js @@ -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, diff --git a/tests/rules/assertions/enforceLineBreak.js b/tests/rules/assertions/enforceLineBreak.js index 291174a..8168d39 100644 --- a/tests/rules/assertions/enforceLineBreak.js +++ b/tests/rules/assertions/enforceLineBreak.js @@ -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: [ { @@ -77,5 +107,8 @@ type Props = { type RoadT = "grass" | "gravel" | "cement";`, }, + { + code: '// @flow\ntype A = string', + }, ], };