1
1
import { convertToLegalComments } from '../scripts/utils/comment-plugin.js' ;
2
2
3
3
describe ( 'convertToLegalComments' , ( ) => {
4
- it ( 'should convert matching comments to legal comments' , ( ) => {
5
- const source = '// Copyright\n// All rights reserved\n// 2024' ;
6
- const regex = / \/ \/ \s * C o p y r i g h t / ;
7
- const expected = '//! Copyright\n//! All rights reserved\n//! 2024' ;
4
+ it ( 'should convert single line comments with copyright' , ( ) => {
5
+ const input = `// This is a copyright notice
6
+ const foo = 'bar';` ;
8
7
9
- expect ( convertToLegalComments ( source , regex ) ) . toBe ( expected ) ;
8
+ const expected = `//! This is a copyright notice
9
+ const foo = 'bar';` ;
10
+
11
+ expect ( convertToLegalComments ( input ) ) . toEqual ( expected ) ;
12
+ } ) ;
13
+
14
+ it ( 'should convert multiple consecutive line comments following a copyright line' , ( ) => {
15
+ const input = `// This is a copyright notice
16
+ // This is a second line
17
+ // This is a third line
18
+ const foo = 'bar';` ;
19
+
20
+ const expected = `//! This is a copyright notice
21
+ //! This is a second line
22
+ //! This is a third line
23
+ const foo = 'bar';` ;
24
+
25
+ expect ( convertToLegalComments ( input ) ) . toEqual ( expected ) ;
26
+ } ) ;
27
+
28
+ it ( 'should stop converting after a non-comment line is encountered' , ( ) => {
29
+ const input = `// This is a copyright notice
30
+ // This is a second line
31
+ const foo = 'bar';
32
+ // This is a regular comment that should not be converted` ;
33
+
34
+ const expected = `//! This is a copyright notice
35
+ //! This is a second line
36
+ const foo = 'bar';
37
+ // This is a regular comment that should not be converted` ;
38
+
39
+ expect ( convertToLegalComments ( input ) ) . toEqual ( expected ) ;
40
+ } ) ;
41
+
42
+ it ( 'should handle multiple separate comment blocks' , ( ) => {
43
+ const input = `// This is a regular comment
44
+ const a = 1;
45
+
46
+ // This has copyright info
47
+ // And continues here
48
+ const b = 2;
49
+
50
+ // Another copyright notice
51
+ // With more details
52
+ // And even more info
53
+ const c = 3;` ;
54
+
55
+ const expected = `// This is a regular comment
56
+ const a = 1;
57
+
58
+ //! This has copyright info
59
+ //! And continues here
60
+ const b = 2;
61
+
62
+ //! Another copyright notice
63
+ //! With more details
64
+ //! And even more info
65
+ const c = 3;` ;
66
+
67
+ expect ( convertToLegalComments ( input ) ) . toEqual ( expected ) ;
68
+ } ) ;
69
+
70
+ it ( 'should handle indented comments' , ( ) => {
71
+ const input = `function test() {
72
+ // This has copyright info
73
+ // This is indented
74
+ return true;
75
+ }` ;
76
+
77
+ const expected = `function test() {
78
+ //! This has copyright info
79
+ //! This is indented
80
+ return true;
81
+ }` ;
82
+
83
+ expect ( convertToLegalComments ( input ) ) . toEqual ( expected ) ;
10
84
} ) ;
11
85
12
- it ( " should not convert comments that don't match the regex" , ( ) => {
13
- const source = '// Regular comment\n// Another comment' ;
14
- const regex = / \/ \/ \s * L i c e n s e / ;
86
+ it ( ' should handle block comments with copyright' , ( ) => {
87
+ const input = `/* This is a copyright block comment */
88
+ const foo = 'bar' ;
15
89
16
- expect ( convertToLegalComments ( source , regex ) ) . toBe ( source ) ;
90
+ /* This is a regular
91
+ multiline comment */` ;
92
+
93
+ const expected = `/*! This is a copyright block comment */
94
+ const foo = 'bar';
95
+
96
+ /* This is a regular
97
+ multiline comment */` ;
98
+
99
+ expect ( convertToLegalComments ( input ) ) . toEqual ( expected ) ;
17
100
} ) ;
18
101
19
- it ( 'should stop converting when encountering non-comment line' , ( ) => {
20
- const source = '// Copyright\n// All rights reserved\ncode line\n// Regular comment' ;
21
- const regex = / \/ \/ \s * C o p y r i g h t / ;
22
- const expected = '//! Copyright\n//! All rights reserved\ncode line\n// Regular comment' ;
102
+ it ( 'should handle mixed comment types' , ( ) => {
103
+ const input = `// This has copyright info
104
+ // This continues
105
+ /* This is a regular block comment */
106
+ const foo = 'bar';
107
+
108
+ /* This is a copyright block comment */
109
+ // This is a regular comment after a block` ;
110
+
111
+ const expected = `//! This has copyright info
112
+ //! This continues
113
+ /* This is a regular block comment */
114
+ const foo = 'bar';
115
+
116
+ /*! This is a copyright block comment */
117
+ // This is a regular comment after a block` ;
23
118
24
- expect ( convertToLegalComments ( source , regex ) ) . toBe ( expected ) ;
119
+ expect ( convertToLegalComments ( input ) ) . toEqual ( expected ) ;
25
120
} ) ;
26
121
27
- it ( 'should handle empty string input' , ( ) => {
28
- const regex = / \/ \/ \s * C o p y r i g h t / ;
122
+ it ( 'should handle block comments breaking line comment sequences' , ( ) => {
123
+ const input = `// This has copyright info
124
+ // This line should be converted
125
+ /* This block comment breaks the sequence */
126
+ // This line should NOT be converted
127
+ // Even though it follows another comment` ;
29
128
30
- expect ( convertToLegalComments ( '' , regex ) ) . toBe ( '' ) ;
129
+ const expected = `//! This has copyright info
130
+ //! This line should be converted
131
+ /* This block comment breaks the sequence */
132
+ // This line should NOT be converted
133
+ // Even though it follows another comment` ;
134
+
135
+ expect ( convertToLegalComments ( input ) ) . toEqual ( expected ) ;
31
136
} ) ;
32
137
33
- it ( 'should handle multiple comment blocks' , ( ) => {
34
- const source = '// Copyright\n// Notice\ncode\n// Copyright\n// Notice' ;
35
- const regex = / \/ \/ \s * C o p y r i g h t / ;
36
- const expected = '//! Copyright\n//! Notice\ncode\n//! Copyright\n//! Notice' ;
138
+ it ( 'should handle case insensitivity for "copyright"' , ( ) => {
139
+ const input = `// This has COPYRIGHT info
140
+ // This continues
141
+ const foo = 'bar';
142
+
143
+ // This has Copyright mixed case
144
+ // More comments
145
+ const baz = 'qux';` ;
146
+
147
+ const expected = `//! This has COPYRIGHT info
148
+ //! This continues
149
+ const foo = 'bar';
37
150
38
- expect ( convertToLegalComments ( source , regex ) ) . toBe ( expected ) ;
151
+ //! This has Copyright mixed case
152
+ //! More comments
153
+ const baz = 'qux';` ;
154
+
155
+ expect ( convertToLegalComments ( input ) ) . toEqual ( expected ) ;
39
156
} ) ;
40
157
41
- it ( 'should preserve leading spaces in comments' , ( ) => {
42
- const source = '// Copyright\n// All rights reserved' ;
43
- const regex = / \/ \/ \s * C o p y r i g h t / ;
44
- const expected = '//! Copyright\n//! All rights reserved' ;
158
+ it ( 'should not convert comments without copyright' , ( ) => {
159
+ const input = `// This is a regular comment
160
+ // Another regular comment
161
+ const foo = 'bar';` ;
162
+
163
+ const expected = `// This is a regular comment
164
+ // Another regular comment
165
+ const foo = 'bar';` ;
166
+
167
+ expect ( convertToLegalComments ( input ) ) . toEqual ( expected ) ;
168
+ } ) ;
169
+
170
+ it ( 'should handle code with no comments' , ( ) => {
171
+ const input = `const foo = 'bar';
172
+ function test() {
173
+ return true;
174
+ }
175
+ const obj = { key: 'value' };` ;
176
+
177
+ const expected = input ; // Should remain unchanged
178
+
179
+ expect ( convertToLegalComments ( input ) ) . toEqual ( expected ) ;
180
+ } ) ;
181
+
182
+ it ( 'should treat empty lines as non-comment lines that break the sequence' , ( ) => {
183
+ const input = `// This has copyright info
184
+ // This continues
185
+
186
+ // These comments should NOT be converted
187
+ // Because empty line breaks the sequence
188
+ const foo = 'bar';` ;
189
+
190
+ const expected = `//! This has copyright info
191
+ //! This continues
192
+
193
+ // These comments should NOT be converted
194
+ // Because empty line breaks the sequence
195
+ const foo = 'bar';` ;
45
196
46
- expect ( convertToLegalComments ( source , regex ) ) . toBe ( expected ) ;
197
+ expect ( convertToLegalComments ( input ) ) . toEqual ( expected ) ;
47
198
} ) ;
48
- } ) ;
199
+ } ) ;
0 commit comments