Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion transforms/magic-redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
memberExpression,
} from 'jscodeshift'
import { getParsedFile } from '../utils/parse'
import { getOptions } from '../utils/recastOptions'
import { recursiveParent } from '../utils/recursiveParent'

const unifiedMagicString = (path: ASTPath<CallExpression>, projectRequestName: string) => {
Expand Down Expand Up @@ -51,5 +52,5 @@ export default function transformer(file: FileInfo, _api: API) {
})
.map((path) => unifiedMagicString(path, recursiveParent(path.parentPath) || 'req'))

return parsedFile.toSource()
return parsedFile.toSource(getOptions(file.source))
}
3 changes: 2 additions & 1 deletion transforms/pluralized-methods.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { API, FileInfo } from 'jscodeshift'
import { Identifier, identifier } from 'jscodeshift'
import { getParsedFile } from '../utils/parse'
import { getOptions } from '../utils/recastOptions'

export default function transformer(file: FileInfo, _api: API): string {
const parsedFile = getParsedFile(file)
Expand All @@ -17,5 +18,5 @@ export default function transformer(file: FileInfo, _api: API): string {
.replaceWith(() => identifier(plural))
}

return parsedFile.toSource()
return parsedFile.toSource(getOptions(file.source))
}
3 changes: 2 additions & 1 deletion transforms/req-param.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { API, FileInfo } from 'jscodeshift'
import { CallExpression, identifier, memberExpression, withParser } from 'jscodeshift'
import { getOptions } from '../utils/recastOptions'
import { recursiveParent } from '../utils/recursiveParent'

export default function transformer(file: FileInfo, _api: API): string {
Expand Down Expand Up @@ -40,5 +41,5 @@ export default function transformer(file: FileInfo, _api: API): string {

return path
})
.toSource()
.toSource(getOptions(file.source))
}
3 changes: 2 additions & 1 deletion transforms/v4-deprecated-signatures.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { API, ASTPath, FileInfo } from 'jscodeshift'
import { CallExpression, callExpression, identifier, memberExpression, withParser } from 'jscodeshift'
import { getOptions } from '../utils/recastOptions'
import { recursiveParent } from '../utils/recursiveParent'

const separateStatusAndBody = (path: ASTPath<CallExpression>, calleePropertyName: string) => {
Expand Down Expand Up @@ -153,5 +154,5 @@ export default function transformer(file: FileInfo, _api: API): string {
return path
})

return parsedFile.toSource()
return parsedFile.toSource(getOptions(file.source))
}
17 changes: 17 additions & 0 deletions utils/recastOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* By default, jscodeshift(recast) uses the line terminator of the OS the code runs on.
* This is often not desired, so we instead try to detect it from the input.
* If there is at least one Windows-style linebreak (CRLF) in the input and
* no Unix-style linebreak (LF), use that. In all other cases, use Unix-style (LF).
* @return '\n' or '\r\n'
*/
export function getOptions(code: string) {
return { lineTerminator: detectLineTerminator(code) }
}

function detectLineTerminator(code: string) {
const hasCRLF = /\r\n/.test(code)
const hasLF = /[^\r]\n/.test(code)

return hasCRLF && !hasLF ? '\r\n' : '\n'
}