@@ -66,7 +66,6 @@ module.exports = {
6666
6767 function updateFile ( ) {
6868 // change the file
69- const lineEnd = ( / \r \n / . test ( testCase . code ) || ! / \n / . test ( testCase . code ) ) ? '\r\n' : '\n' ;
7069 const separator = '\n\n----------------------------------------------------\n\n' ;
7170 const pretty = TokenStreamTransformer . prettyprint ( tokenStream , '\t' ) ;
7271
@@ -75,7 +74,9 @@ module.exports = {
7574 content += separator + testCase . comment . trim ( ) ;
7675 }
7776 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 ) ;
7980
8081 fs . writeFileSync ( filePath , content , 'utf-8' ) ;
8182 }
@@ -202,14 +203,19 @@ module.exports = {
202203 * @returns {ParsedTestCase }
203204 *
204205 * @typedef ParsedTestCase
206+ * @property {string } lineEndOnDisk The EOL format used by the parsed file.
205207 * @property {string } code
206208 * @property {string } expectedJson
207209 * @property {number } expectedLineOffset
208210 * @property {Array | null } expectedTokenStream
209211 * @property {string } comment
210212 */
211213 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+
213219 const testCaseParts = testCaseSource . split ( / ^ - { 10 , } [ \t ] * $ / m) ;
214220
215221 if ( testCaseParts . length > 3 ) {
@@ -221,9 +227,10 @@ module.exports = {
221227 const comment = ( testCaseParts [ 2 ] || '' ) . trimStart ( ) ;
222228
223229 const testCase = {
230+ lineEndOnDisk,
224231 code,
225232 expectedJson : expected ,
226- expectedLineOffset : code . split ( / \r \n ? | \n / g) . length ,
233+ expectedLineOffset : code . split ( / \r \n / g) . length ,
227234 expectedTokenStream : expected ? JSON . parse ( expected ) : null ,
228235 comment
229236 } ;
0 commit comments