Prettier backfill

This commit is contained in:
Matt Rubens
2025-01-17 14:11:28 -05:00
parent 3bcb4ff8c5
commit 60a0a824b9
174 changed files with 15715 additions and 15428 deletions

View File

@@ -132,10 +132,10 @@ export class DiffViewProvider {
// Apply the final content
const finalEdit = new vscode.WorkspaceEdit()
finalEdit.replace(document.uri, new vscode.Range(0, 0, document.lineCount, 0), accumulatedContent)
await vscode.workspace.applyEdit(finalEdit)
// Clear all decorations at the end (after applying final edit)
this.fadedOverlayController.clear()
this.activeLineController.clear()
await vscode.workspace.applyEdit(finalEdit)
// Clear all decorations at the end (after applying final edit)
this.fadedOverlayController.clear()
this.activeLineController.clear()
}
}
@@ -352,4 +352,4 @@ export class DiffViewProvider {
this.streamedLines = []
this.preDiagnostics = []
}
}
}

View File

@@ -1,8 +1,8 @@
import { DiffViewProvider } from '../DiffViewProvider';
import * as vscode from 'vscode';
import { DiffViewProvider } from "../DiffViewProvider"
import * as vscode from "vscode"
// Mock vscode
jest.mock('vscode', () => ({
jest.mock("vscode", () => ({
workspace: {
applyEdit: jest.fn(),
},
@@ -19,34 +19,34 @@ jest.mock('vscode', () => ({
TextEditorRevealType: {
InCenter: 2,
},
}));
}))
// Mock DecorationController
jest.mock('../DecorationController', () => ({
jest.mock("../DecorationController", () => ({
DecorationController: jest.fn().mockImplementation(() => ({
setActiveLine: jest.fn(),
updateOverlayAfterLine: jest.fn(),
clear: jest.fn(),
})),
}));
}))
describe('DiffViewProvider', () => {
let diffViewProvider: DiffViewProvider;
const mockCwd = '/mock/cwd';
let mockWorkspaceEdit: { replace: jest.Mock; delete: jest.Mock };
describe("DiffViewProvider", () => {
let diffViewProvider: DiffViewProvider
const mockCwd = "/mock/cwd"
let mockWorkspaceEdit: { replace: jest.Mock; delete: jest.Mock }
beforeEach(() => {
jest.clearAllMocks();
jest.clearAllMocks()
mockWorkspaceEdit = {
replace: jest.fn(),
delete: jest.fn(),
};
(vscode.WorkspaceEdit as jest.Mock).mockImplementation(() => mockWorkspaceEdit);
}
;(vscode.WorkspaceEdit as jest.Mock).mockImplementation(() => mockWorkspaceEdit)
diffViewProvider = new DiffViewProvider(mockCwd);
diffViewProvider = new DiffViewProvider(mockCwd)
// Mock the necessary properties and methods
(diffViewProvider as any).relPath = 'test.txt';
(diffViewProvider as any).activeDiffEditor = {
;(diffViewProvider as any).relPath = "test.txt"
;(diffViewProvider as any).activeDiffEditor = {
document: {
uri: { fsPath: `${mockCwd}/test.txt` },
getText: jest.fn(),
@@ -58,43 +58,39 @@ describe('DiffViewProvider', () => {
},
edit: jest.fn().mockResolvedValue(true),
revealRange: jest.fn(),
};
(diffViewProvider as any).activeLineController = { setActiveLine: jest.fn(), clear: jest.fn() };
(diffViewProvider as any).fadedOverlayController = { updateOverlayAfterLine: jest.fn(), clear: jest.fn() };
});
}
;(diffViewProvider as any).activeLineController = { setActiveLine: jest.fn(), clear: jest.fn() }
;(diffViewProvider as any).fadedOverlayController = { updateOverlayAfterLine: jest.fn(), clear: jest.fn() }
})
describe('update method', () => {
it('should preserve empty last line when original content has one', async () => {
(diffViewProvider as any).originalContent = 'Original content\n';
await diffViewProvider.update('New content', true);
describe("update method", () => {
it("should preserve empty last line when original content has one", async () => {
;(diffViewProvider as any).originalContent = "Original content\n"
await diffViewProvider.update("New content", true)
expect(mockWorkspaceEdit.replace).toHaveBeenCalledWith(
expect.anything(),
expect.anything(),
'New content\n'
);
});
"New content\n",
)
})
it('should not add extra newline when accumulated content already ends with one', async () => {
(diffViewProvider as any).originalContent = 'Original content\n';
await diffViewProvider.update('New content\n', true);
it("should not add extra newline when accumulated content already ends with one", async () => {
;(diffViewProvider as any).originalContent = "Original content\n"
await diffViewProvider.update("New content\n", true)
expect(mockWorkspaceEdit.replace).toHaveBeenCalledWith(
expect.anything(),
expect.anything(),
'New content\n'
);
});
"New content\n",
)
})
it('should not add newline when original content does not end with one', async () => {
(diffViewProvider as any).originalContent = 'Original content';
await diffViewProvider.update('New content', true);
it("should not add newline when original content does not end with one", async () => {
;(diffViewProvider as any).originalContent = "Original content"
await diffViewProvider.update("New content", true)
expect(mockWorkspaceEdit.replace).toHaveBeenCalledWith(
expect.anything(),
expect.anything(),
'New content'
);
});
});
});
expect(mockWorkspaceEdit.replace).toHaveBeenCalledWith(expect.anything(), expect.anything(), "New content")
})
})
})

View File

@@ -1,6 +1,6 @@
import { detectCodeOmission } from '../detect-omission'
import { detectCodeOmission } from "../detect-omission"
describe('detectCodeOmission', () => {
describe("detectCodeOmission", () => {
const originalContent = `function example() {
// Some code
const x = 1;
@@ -10,124 +10,132 @@ describe('detectCodeOmission', () => {
const generateLongContent = (commentLine: string, length: number = 90) => {
return `${commentLine}
${Array.from({ length }, (_, i) => `const x${i} = ${i};`).join('\n')}
${Array.from({ length }, (_, i) => `const x${i} = ${i};`).join("\n")}
const y = 2;`
}
it('should skip comment checks for files under 100 lines', () => {
it("should skip comment checks for files under 100 lines", () => {
const newContent = `// Lines 1-50 remain unchanged
const z = 3;`
const predictedLineCount = 50
expect(detectCodeOmission(originalContent, newContent, predictedLineCount)).toBe(false)
})
it('should not detect regular comments without omission keywords', () => {
const newContent = generateLongContent('// Adding new functionality')
it("should not detect regular comments without omission keywords", () => {
const newContent = generateLongContent("// Adding new functionality")
const predictedLineCount = 150
expect(detectCodeOmission(originalContent, newContent, predictedLineCount)).toBe(false)
})
it('should not detect when comment is part of original content', () => {
it("should not detect when comment is part of original content", () => {
const originalWithComment = `// Content remains unchanged
${originalContent}`
const newContent = generateLongContent('// Content remains unchanged')
const newContent = generateLongContent("// Content remains unchanged")
const predictedLineCount = 150
expect(detectCodeOmission(originalWithComment, newContent, predictedLineCount)).toBe(false)
})
it('should not detect code that happens to contain omission keywords', () => {
it("should not detect code that happens to contain omission keywords", () => {
const newContent = generateLongContent(`const remains = 'some value';
const unchanged = true;`)
const predictedLineCount = 150
expect(detectCodeOmission(originalContent, newContent, predictedLineCount)).toBe(false)
})
it('should detect suspicious single-line comment when content is more than 20% shorter', () => {
const newContent = generateLongContent('// Previous content remains here\nconst x = 1;')
it("should detect suspicious single-line comment when content is more than 20% shorter", () => {
const newContent = generateLongContent("// Previous content remains here\nconst x = 1;")
const predictedLineCount = 150
expect(detectCodeOmission(originalContent, newContent, predictedLineCount)).toBe(true)
})
it('should not flag suspicious single-line comment when content is less than 20% shorter', () => {
const newContent = generateLongContent('// Previous content remains here', 130)
it("should not flag suspicious single-line comment when content is less than 20% shorter", () => {
const newContent = generateLongContent("// Previous content remains here", 130)
const predictedLineCount = 150
expect(detectCodeOmission(originalContent, newContent, predictedLineCount)).toBe(false)
})
it('should detect suspicious Python-style comment when content is more than 20% shorter', () => {
const newContent = generateLongContent('# Previous content remains here\nconst x = 1;')
it("should detect suspicious Python-style comment when content is more than 20% shorter", () => {
const newContent = generateLongContent("# Previous content remains here\nconst x = 1;")
const predictedLineCount = 150
expect(detectCodeOmission(originalContent, newContent, predictedLineCount)).toBe(true)
})
it('should not flag suspicious Python-style comment when content is less than 20% shorter', () => {
const newContent = generateLongContent('# Previous content remains here', 130)
it("should not flag suspicious Python-style comment when content is less than 20% shorter", () => {
const newContent = generateLongContent("# Previous content remains here", 130)
const predictedLineCount = 150
expect(detectCodeOmission(originalContent, newContent, predictedLineCount)).toBe(false)
})
it('should detect suspicious multi-line comment when content is more than 20% shorter', () => {
const newContent = generateLongContent('/* Previous content remains the same */\nconst x = 1;')
it("should detect suspicious multi-line comment when content is more than 20% shorter", () => {
const newContent = generateLongContent("/* Previous content remains the same */\nconst x = 1;")
const predictedLineCount = 150
expect(detectCodeOmission(originalContent, newContent, predictedLineCount)).toBe(true)
})
it('should not flag suspicious multi-line comment when content is less than 20% shorter', () => {
const newContent = generateLongContent('/* Previous content remains the same */', 130)
it("should not flag suspicious multi-line comment when content is less than 20% shorter", () => {
const newContent = generateLongContent("/* Previous content remains the same */", 130)
const predictedLineCount = 150
expect(detectCodeOmission(originalContent, newContent, predictedLineCount)).toBe(false)
})
it('should detect suspicious JSX comment when content is more than 20% shorter', () => {
const newContent = generateLongContent('{/* Rest of the code remains the same */}\nconst x = 1;')
it("should detect suspicious JSX comment when content is more than 20% shorter", () => {
const newContent = generateLongContent("{/* Rest of the code remains the same */}\nconst x = 1;")
const predictedLineCount = 150
expect(detectCodeOmission(originalContent, newContent, predictedLineCount)).toBe(true)
})
it('should not flag suspicious JSX comment when content is less than 20% shorter', () => {
const newContent = generateLongContent('{/* Rest of the code remains the same */}', 130)
it("should not flag suspicious JSX comment when content is less than 20% shorter", () => {
const newContent = generateLongContent("{/* Rest of the code remains the same */}", 130)
const predictedLineCount = 150
expect(detectCodeOmission(originalContent, newContent, predictedLineCount)).toBe(false)
})
it('should detect suspicious HTML comment when content is more than 20% shorter', () => {
const newContent = generateLongContent('<!-- Existing content unchanged -->\nconst x = 1;')
it("should detect suspicious HTML comment when content is more than 20% shorter", () => {
const newContent = generateLongContent("<!-- Existing content unchanged -->\nconst x = 1;")
const predictedLineCount = 150
expect(detectCodeOmission(originalContent, newContent, predictedLineCount)).toBe(true)
})
it('should not flag suspicious HTML comment when content is less than 20% shorter', () => {
const newContent = generateLongContent('<!-- Existing content unchanged -->', 130)
it("should not flag suspicious HTML comment when content is less than 20% shorter", () => {
const newContent = generateLongContent("<!-- Existing content unchanged -->", 130)
const predictedLineCount = 150
expect(detectCodeOmission(originalContent, newContent, predictedLineCount)).toBe(false)
})
it('should detect suspicious square bracket notation when content is more than 20% shorter', () => {
const newContent = generateLongContent('[Previous content from line 1-305 remains exactly the same]\nconst x = 1;')
it("should detect suspicious square bracket notation when content is more than 20% shorter", () => {
const newContent = generateLongContent(
"[Previous content from line 1-305 remains exactly the same]\nconst x = 1;",
)
const predictedLineCount = 150
expect(detectCodeOmission(originalContent, newContent, predictedLineCount)).toBe(true)
})
it('should not flag suspicious square bracket notation when content is less than 20% shorter', () => {
const newContent = generateLongContent('[Previous content from line 1-305 remains exactly the same]', 130)
it("should not flag suspicious square bracket notation when content is less than 20% shorter", () => {
const newContent = generateLongContent("[Previous content from line 1-305 remains exactly the same]", 130)
const predictedLineCount = 150
expect(detectCodeOmission(originalContent, newContent, predictedLineCount)).toBe(false)
})
it('should not flag content very close to predicted length', () => {
const newContent = generateLongContent(`const x = 1;
it("should not flag content very close to predicted length", () => {
const newContent = generateLongContent(
`const x = 1;
const y = 2;
// This is a legitimate comment that remains here`, 130)
// This is a legitimate comment that remains here`,
130,
)
const predictedLineCount = 150
expect(detectCodeOmission(originalContent, newContent, predictedLineCount)).toBe(false)
})
it('should not flag when content is longer than predicted', () => {
const newContent = generateLongContent(`const x = 1;
it("should not flag when content is longer than predicted", () => {
const newContent = generateLongContent(
`const x = 1;
const y = 2;
// Previous content remains here but we added more
const z = 3;
const w = 4;`, 160)
const w = 4;`,
160,
)
const predictedLineCount = 150
expect(detectCodeOmission(originalContent, newContent, predictedLineCount)).toBe(false)
})

View File

@@ -8,7 +8,7 @@
export function detectCodeOmission(
originalFileContent: string,
newFileContent: string,
predictedLineCount: number
predictedLineCount: number,
): boolean {
// Skip all checks if predictedLineCount is less than 100
if (!predictedLineCount || predictedLineCount < 100) {
@@ -20,7 +20,17 @@ export function detectCodeOmission(
const originalLines = originalFileContent.split("\n")
const newLines = newFileContent.split("\n")
const omissionKeywords = ["remain", "remains", "unchanged", "rest", "previous", "existing", "content", "same", "..."]
const omissionKeywords = [
"remain",
"remains",
"unchanged",
"rest",
"previous",
"existing",
"content",
"same",
"...",
]
const commentPatterns = [
/^\s*\/\//, // Single-line comment for most languages
@@ -39,7 +49,7 @@ export function detectCodeOmission(
if (omissionKeywords.some((keyword) => words.includes(keyword))) {
if (!originalLines.includes(line)) {
// For files with 100+ lines, only flag if content is more than 20% shorter
if (lengthRatio <= 0.80) {
if (lengthRatio <= 0.8) {
return true
}
}
@@ -48,4 +58,4 @@ export function detectCodeOmission(
}
return false
}
}