-
Notifications
You must be signed in to change notification settings - Fork 74
feat: create fixes on c-like and php files #869
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,3 +73,5 @@ codecov.exe | |
| .coverage | ||
| # File genrated as part of XCode tests | ||
| coverage-report-test.json | ||
|
|
||
| *.coverage.txt | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import fs from 'fs' | ||
| import readline from 'readline' | ||
|
|
||
| import { getAllFiles } from './files' | ||
| import { UploadLogger } from './logger' | ||
|
|
||
| export const FIXES_HEADER = '# path=fixes\n' | ||
|
|
||
| export async function generateFixes(projectRoot: string): Promise<string> { | ||
| // Fake out the UploaderArgs as they are not needed | ||
| const allFiles = await getAllFiles(projectRoot, projectRoot, { | ||
| flags: '', | ||
| slug: '', | ||
| upstream: '', | ||
| }) | ||
|
|
||
| const allAdjustments: string[] = [] | ||
| const EMPTYLINE = /^\s*$/mg | ||
| // { or } | ||
| const SYNTAXBRACKET = /^\s*[{}]\s*(\/\/.*)?$/m | ||
| // [ or ] | ||
| const SYNTAXLIST = /^\s*[[\]]\s*(\/\/.*)?$/m | ||
|
|
||
| for (const file of allFiles) { | ||
| let lineAdjustments: string[] = [] | ||
|
|
||
| if ( | ||
| file.match(/\.c$/) || | ||
| file.match(/\.cpp$/) || | ||
| file.match(/\.h$/) || | ||
| file.match(/\.hpp$/) || | ||
| file.match(/\.m$/) || | ||
| file.match(/\.swift$/) || | ||
| file.match(/\.vala$/) | ||
| ) { | ||
| lineAdjustments = await getMatchedLines(file, [EMPTYLINE, SYNTAXBRACKET]) | ||
| } else if ( | ||
| file.match(/\.php$/) | ||
| ) { | ||
| lineAdjustments = await getMatchedLines(file, [SYNTAXBRACKET, SYNTAXLIST]) | ||
| } | ||
|
|
||
| if (lineAdjustments.length > 0) { | ||
| UploadLogger.verbose(`Matched file ${file} for adjustments: ${lineAdjustments.join(',')}`) | ||
| allAdjustments.push(`${file}:${lineAdjustments.join(',')}\n`) | ||
| } | ||
| } | ||
| return allAdjustments.join('') | ||
| } | ||
|
|
||
| async function getMatchedLines(file: string, matchers: RegExp[]): Promise<string[]> { | ||
| const fileStream = fs.createReadStream(file) | ||
| const rl = readline.createInterface({ | ||
| input: fileStream, | ||
| crlfDelay: Infinity | ||
| }); | ||
|
|
||
| const matchedLines: string[] = [] | ||
| let lineNumber = 1 | ||
|
|
||
| for await (const line of rl) { | ||
| for (const matcher of matchers) { | ||
| if (line.match(matcher)) { | ||
| matchedLines.push(lineNumber.toString()) | ||
| } | ||
| } | ||
| lineNumber++ | ||
| } | ||
| return matchedLines | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php namespace Example; | ||
|
|
||
| class Example | ||
| { | ||
| public static function go() | ||
| { | ||
| $test_array = [ | ||
| 1, | ||
| 2, | ||
| 3, | ||
| ] | ||
|
|
||
| if (false) { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
|
|
||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import td from 'testdouble' | ||
| import childProcess from 'child_process' | ||
|
|
||
| import * as fixesHelpers from '../../src/helpers/fixes' | ||
|
|
||
| describe('Fixes Helpers', () => { | ||
| afterEach(() => { | ||
| td.reset() | ||
| }) | ||
|
|
||
| it('provides no fixes if none are applicable', async () => { | ||
| const files = ['package.json'] | ||
| td.replace(childProcess, 'spawnSync', () => { | ||
| return { | ||
| stdout: files.join('\n'), | ||
| status: 0, | ||
| error: undefined, | ||
| } | ||
| }) | ||
| expect( | ||
| await fixesHelpers.generateFixes('.') | ||
| ).toBe('') | ||
| }) | ||
|
|
||
| it('provides proper fixes for c-like files', async () => { | ||
| const files = ['test/fixtures/gcov/main.c'] | ||
| td.replace(childProcess, 'spawnSync', () => { | ||
| return { | ||
| stdout: files.join('\n'), | ||
| status: 0, | ||
| error: undefined, | ||
| } | ||
| }) | ||
| expect( | ||
| await fixesHelpers.generateFixes('.') | ||
| ).toBe('test/fixtures/gcov/main.c:2,4,11,12,13,14,16,20\n') | ||
| }) | ||
|
|
||
| it('provides proper fixes for php-like files', async () => { | ||
| const files = ['test/fixtures/fixes/example.php'] | ||
| td.replace(childProcess, 'spawnSync', () => { | ||
| return { | ||
| stdout: files.join('\n'), | ||
| status: 0, | ||
| error: undefined, | ||
| } | ||
| }) | ||
| expect( | ||
| await fixesHelpers.generateFixes('.') | ||
| ).toBe('test/fixtures/fixes/example.php:4,6,11,15,19,20\n') | ||
| }) | ||
|
|
||
| it('provides multiple fixes for files', async () => { | ||
| const files = ['test/fixtures/fixes/example.php', 'test/fixtures/gcov/main.c'] | ||
| td.replace(childProcess, 'spawnSync', () => { | ||
| return { | ||
| stdout: files.join('\n'), | ||
| status: 0, | ||
| error: undefined, | ||
| } | ||
| }) | ||
| expect( | ||
| await fixesHelpers.generateFixes('.') | ||
| ).toBe('test/fixtures/fixes/example.php:4,6,11,15,19,20\ntest/fixtures/gcov/main.c:2,4,11,12,13,14,16,20\n') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A future PR should move these promises into Promise.all or Promise.allSettled.