@@ -66,7 +66,6 @@ module.exports = {
66
66
67
67
function updateFile ( ) {
68
68
// change the file
69
- const lineEnd = ( / \r \n / . test ( testCase . code ) || ! / \n / . test ( testCase . code ) ) ? '\r\n' : '\n' ;
70
69
const separator = '\n\n----------------------------------------------------\n\n' ;
71
70
const pretty = TokenStreamTransformer . prettyprint ( tokenStream , '\t' ) ;
72
71
@@ -75,7 +74,9 @@ module.exports = {
75
74
content += separator + testCase . comment . trim ( ) ;
76
75
}
77
76
content += '\n' ;
78
- content = content . replace ( / \r ? \n / g, lineEnd ) ;
77
+
78
+ // convert line ends to the line ends of the file
79
+ content = content . replace ( / \r \n ? | \n / g, testCase . lineEndOnDisk ) ;
79
80
80
81
fs . writeFileSync ( filePath , content , 'utf-8' ) ;
81
82
}
@@ -202,14 +203,19 @@ module.exports = {
202
203
* @returns {ParsedTestCase }
203
204
*
204
205
* @typedef ParsedTestCase
206
+ * @property {string } lineEndOnDisk The EOL format used by the parsed file.
205
207
* @property {string } code
206
208
* @property {string } expectedJson
207
209
* @property {number } expectedLineOffset
208
210
* @property {Array | null } expectedTokenStream
209
211
* @property {string } comment
210
212
*/
211
213
parseTestCaseFile ( filePath ) {
212
- const testCaseSource = fs . readFileSync ( filePath , 'utf8' ) ;
214
+ let testCaseSource = fs . readFileSync ( filePath , 'utf8' ) ;
215
+ const lineEndOnDisk = ( / \r \n ? | \n / . exec ( testCaseSource ) || [ '\n' ] ) [ 0 ] ;
216
+ // normalize line ends to \r\n
217
+ testCaseSource = testCaseSource . replace ( / \r \n ? | \n / g, '\r\n' ) ;
218
+
213
219
const testCaseParts = testCaseSource . split ( / ^ - { 10 , } [ \t ] * $ / m) ;
214
220
215
221
if ( testCaseParts . length > 3 ) {
@@ -221,9 +227,10 @@ module.exports = {
221
227
const comment = ( testCaseParts [ 2 ] || '' ) . trimStart ( ) ;
222
228
223
229
const testCase = {
230
+ lineEndOnDisk,
224
231
code,
225
232
expectedJson : expected ,
226
- expectedLineOffset : code . split ( / \r \n ? | \n / g) . length ,
233
+ expectedLineOffset : code . split ( / \r \n / g) . length ,
227
234
expectedTokenStream : expected ? JSON . parse ( expected ) : null ,
228
235
comment
229
236
} ;
0 commit comments