Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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))
}
68 changes: 68 additions & 0 deletions utils/__test__/recastOptions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { getOptions } from '../recastOptions'

describe('recastOptions', () => {
describe('getOptions', () => {
it('should return Unix line terminator for code with only LF', () => {
const code = 'const a = 1\nconst b = 2\n'
const result = getOptions(code)

expect(result).toEqual({ lineTerminator: '\n' })
})

it('should return Windows line terminator for code with only CRLF', () => {
const code = 'const a = 1\r\nconst b = 2\r\n'
const result = getOptions(code)

expect(result).toEqual({ lineTerminator: '\r\n' })
})

it('should return Unix line terminator for code with mixed line terminators', () => {
const code = 'const a = 1\r\nconst b = 2\nconst c = 3\r\n'
const result = getOptions(code)

expect(result).toEqual({ lineTerminator: '\n' })
})

it('should return Unix line terminator for code with no line terminators', () => {
const code = 'const a = 1'
const result = getOptions(code)

expect(result).toEqual({ lineTerminator: '\n' })
})

it('should return Unix line terminator for empty string', () => {
const code = ''
const result = getOptions(code)

expect(result).toEqual({ lineTerminator: '\n' })
})

it('should handle code with LF at beginning after CR', () => {
const code = '\r\nconst a = 1\nconst b = 2'
const result = getOptions(code)

expect(result).toEqual({ lineTerminator: '\n' })
})

it('should handle single CRLF without other line breaks', () => {
const code = 'const a = 1\r\nconst b = 2'
const result = getOptions(code)

expect(result).toEqual({ lineTerminator: '\r\n' })
})

it('should handle multiple CRLF without LF', () => {
const code = 'line1\r\nline2\r\nline3\r\n'
const result = getOptions(code)

expect(result).toEqual({ lineTerminator: '\r\n' })
})

it('should detect LF even when preceded by CR in different context', () => {
const code = 'const str = "\\r"\nconst a = 1\r\n'
const result = getOptions(code)

expect(result).toEqual({ lineTerminator: '\n' })
})
})
})
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'
}