Skip to content

Commit

Permalink
Pass Path To PHPCS (#17)
Browse files Browse the repository at this point in the history
Since some sniffs check the file path it makes sense for us to send
the path using PHPCS' `phpcs_input_file: [path]` feature. When
prepended to the content this line is used by PHPCS to set the
filename of STDIN files.
  • Loading branch information
ObliviousHarmony authored Mar 2, 2021
1 parent bd834c1 commit 5f8ee49
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [1.0.0] - 2021-03-01
### Fixed
- Pass file path to `phpcs` for use in sniffs.
- Handle Uri schemes other than `'file'`.

## [0.4.1] - 2021-02-22
Expand Down
2 changes: 2 additions & 0 deletions src/phpcs-report/__tests__/worker-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('Worker/WorkerPool Integration', () => {
.then((worker) => {
const request: Request<ReportType.Diagnostic> = {
type: ReportType.Diagnostic,
documentPath: 'Test.php',
documentContent: '<?php class Test {}',
options: {
workingDirectory: __dirname,
Expand Down Expand Up @@ -74,6 +75,7 @@ describe('Worker/WorkerPool Integration', () => {
.then((worker) => {
const request: Request<ReportType.Diagnostic> = {
type: ReportType.Diagnostic,
documentPath: 'Test.php',
documentContent: '<?php class Test {}',
options: {
workingDirectory: __dirname,
Expand Down
6 changes: 6 additions & 0 deletions src/phpcs-report/__tests__/worker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('Worker', () => {

const request: Request<ReportType.Diagnostic> = {
type: ReportType.Diagnostic,
documentPath: 'Test.php',
documentContent: '',
options: {
workingDirectory: __dirname,
Expand All @@ -65,6 +66,7 @@ describe('Worker', () => {

const request: Request<ReportType.Diagnostic> = {
type: ReportType.Diagnostic,
documentPath: 'Test.php',
documentContent: '<?php class Test {}',
options: {
workingDirectory: __dirname,
Expand All @@ -88,6 +90,7 @@ describe('Worker', () => {

const request: Request<ReportType.CodeAction> = {
type: ReportType.CodeAction,
documentPath: 'Test.php',
documentContent: '<?php class Test {}',
options: {
workingDirectory: __dirname,
Expand Down Expand Up @@ -127,6 +130,7 @@ describe('Worker', () => {

const request: Request<ReportType.Format> = {
type: ReportType.Format,
documentPath: 'Test.php',
documentContent: '<?php class Test {}',
options: {
workingDirectory: __dirname,
Expand All @@ -148,6 +152,7 @@ describe('Worker', () => {

const request: Request<ReportType.Diagnostic> = {
type: ReportType.Diagnostic,
documentPath: 'Test.php',
documentContent: '<?php class Test {}',
options: {
workingDirectory: __dirname,
Expand All @@ -173,6 +178,7 @@ describe('Worker', () => {

const request: Request<ReportType.Diagnostic> = {
type: ReportType.Diagnostic,
documentPath: 'Test.php',
documentContent: '<?php class Test {}',
options: {
workingDirectory: __dirname,
Expand Down
5 changes: 5 additions & 0 deletions src/phpcs-report/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ export interface Request<T extends ReportType> {
*/
type: T;

/**
* The filesystem path for the document that the report is being generated for.
*/
documentPath: string;

/**
* The content for the document that the report is being generated for.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/phpcs-report/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ export class Worker {

// Send the document to be handled.
if (phpcsProcess.stdin.writable) {
// Write the input file path before the content so PHPCS can utilize it.
phpcsProcess.stdin.write('phpcs_input_file: ' + request.documentPath + '\n');
phpcsProcess.stdin.end(request.documentContent);
}

Expand Down
1 change: 1 addition & 0 deletions src/services/code-action-edit-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class CodeActionEditResolver extends WorkerService {
// Use the worker to make a request for a code action report.
const request: Request<ReportType.CodeAction> = {
type: ReportType.CodeAction,
documentPath: document.uri.fsPath,
documentContent: document.getText(),
options: {
workingDirectory: config.workingDirectory,
Expand Down
1 change: 1 addition & 0 deletions src/services/diagnostic-updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export class DiagnosticUpdater extends WorkerService {
// Use the worker to make a request for a diagnostic report.
const request: Request<ReportType.Diagnostic> = {
type: ReportType.Diagnostic,
documentPath: document.uri.fsPath,
documentContent: document.getText(),
options: {
workingDirectory: config.workingDirectory,
Expand Down
1 change: 1 addition & 0 deletions src/services/document-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class DocumentFormatter extends WorkerService {
// Use the worker to make a request for a format report.
const request: Request<ReportType.Format> = {
type: ReportType.Format,
documentPath: document.uri.fsPath,
documentContent: document.getText(),
options: {
workingDirectory: config.workingDirectory,
Expand Down

0 comments on commit 5f8ee49

Please sign in to comment.