File tree Expand file tree Collapse file tree 2 files changed +45
-5
lines changed Expand file tree Collapse file tree 2 files changed +45
-5
lines changed Original file line number Diff line number Diff line change @@ -12,24 +12,31 @@ const create = (context) => {
12
12
return ;
13
13
}
14
14
15
+ const exportedType = node . parent . type === 'ExportNamedDeclaration' ;
16
+ const leadingComments = sourceCode . getCommentsBefore ( exportedType ? node . parent : node ) ;
17
+ const hasLeadingComments = leadingComments . length > 0 ;
18
+
15
19
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 ] ;
18
22
if ( lineAboveComment !== '' ) {
19
23
context . report ( {
20
24
fix ( fixer ) {
21
- return fixer . insertTextBeforeRange ( node . leadingComments [ 0 ] . range , '\n' ) ;
25
+ return fixer . insertTextBeforeRange ( leadingComments [ 0 ] . range , '\n' ) ;
22
26
} ,
23
27
message : breakLineMessage ( 'above' ) ,
24
28
node,
25
29
} ) ;
26
30
}
27
- } else if ( ! node . leadingComments ) {
31
+ } else if ( ! hasLeadingComments ) {
28
32
const isLineAbove = sourceCode . lines [ node . loc . start . line - 2 ] ;
29
33
if ( isLineAbove !== '' ) {
30
34
context . report ( {
31
35
fix ( fixer ) {
32
- return fixer . insertTextBefore ( node , '\n' ) ;
36
+ return fixer . insertTextBefore (
37
+ exportedType ? node . parent : node ,
38
+ '\n' ,
39
+ ) ;
33
40
} ,
34
41
message : breakLineMessage ( 'above' ) ,
35
42
node,
Original file line number Diff line number Diff line change @@ -36,6 +36,36 @@ export default {
36
36
] ,
37
37
output : 'type hello = 34;\n\nconst som = "jes";\n\ntype fed = "hed";\n' ,
38
38
} ,
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
+ } ,
39
69
] ,
40
70
valid : [
41
71
{
@@ -77,5 +107,8 @@ type Props = {
77
107
78
108
type RoadT = "grass" | "gravel" | "cement";` ,
79
109
} ,
110
+ {
111
+ code : '// @flow\ntype A = string' ,
112
+ } ,
80
113
] ,
81
114
} ;
You can’t perform that action at this time.
0 commit comments