Skip to content

Commit

Permalink
Trim Quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
ObliviousHarmony committed Sep 17, 2024
1 parent b207031 commit 610bbcc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/phpcs-report/__tests__/worker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ describe('Worker', () => {
expect(onCompletion).toHaveBeenLastCalledWith(worker);
});

it('should support executables with spaces', async () => {
it('should support executables with spaces and quotes', async () => {
const worker = new Worker();

const request: Request<ReportType.Diagnostic> = {
Expand All @@ -212,7 +212,7 @@ describe('Worker', () => {
documentContent: '<?php class Test {}',
options: {
// Since we use custom reports, adding `-s` for sources won't break anything.
executable: phpcsPath + ' -s',
executable: '"' + phpcsPath + '" -s',
standard: 'PSR12',
autoloadPHPCSIntegration: false,
},
Expand Down
25 changes: 24 additions & 1 deletion src/phpcs-report/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,38 @@ export class Worker {
// We support the use of arguments in the executable option. This allows for
// users to build more complex commands such as those that should be ran
// in a container or with specific arguments.
const supportedQuotes = ["'", '"'];
const parsedExecutable = splitString(request.options.executable, {
quotes: ['"', "'"],
quotes: supportedQuotes,
brackets: false,
separator: ' ',
});
if (!parsedExecutable.length) {
throw new Error('No executable was given.');
}

// Trim quotes from the start/end of each argument since Node will
// handle quoting any arguments for us when we spawn the process.
for (const key in parsedExecutable) {
const segment = parsedExecutable[key];

const first = segment.at(0);
if (!first) {
continue;
}

if (!supportedQuotes.includes(first)) {
continue;
}

// Only remove matching quotes.
if (segment.at(-1) !== first) {
continue;
}

parsedExecutable[key] = segment.slice(1, -1);
}

// The first segment will always be the executable.
const executable = parsedExecutable.shift();
if (!executable) {
Expand Down

0 comments on commit 610bbcc

Please sign in to comment.