Skip to content

enforceLineBreak now handles export type correctly #488

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

Merged
merged 3 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/rules/enforceLineBreak.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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',
},
],
};