toSource() outputs CRLF newline when input file only had LF
Author: binkiCreated Jun 8, 2018Updated May 20, 2025
Labelsbug
The README claims that it will do its best to preserve whitespace/formatting. But instead it adds CRLF when I wasn’t expecting it. It looks like I can work around it by doing something like:
module.exports = function (fileInfo, {
jscodeshift,
}, options) {
const isUnix = fileInfo.source.indexOf('\r\n') === -1;
const dom = jscodeshift(fileInfo.source);
return dom.toSource().replace(/\r\n/g, isUnix ? '\n' : '\r\n');
};However, that behavior should probably just be provided out of the box instead of requiring this hack.
Source: facebook/jscodeshift