Skip to content
Draft
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
14 changes: 5 additions & 9 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const path = require("path");
const fs = require("fs");
const os = require("os");
const cp = require("child_process");
const { findFiles } = require("./lib/utils");
const { findFiles, resolveWorkspacePath } = require("./lib/utils");
const TmpDir = os.tmpdir();

class PHPCBF {
Expand Down Expand Up @@ -62,15 +62,11 @@ class PHPCBF {

this.standard = config.get("standard", null);

// Resolve ${workspaceFolder} / ${workspaceRoot} in the standard path.
if (this.standard && configUri) {
const folder = workspace.getWorkspaceFolder(configUri);
// Resolve variables and relative paths in the standard path.
if (this.standard) {
const folder = configUri ? workspace.getWorkspaceFolder(configUri) : null;
const rootPath = folder ? folder.uri.fsPath : null;
if (rootPath) {
this.standard = this.standard
.replace("${workspaceFolder}", rootPath)
.replace("${workspaceRoot}", rootPath);
}
this.standard = resolveWorkspacePath(this.standard, rootPath);
}

this.documentFormattingProvider = config.get(
Expand Down
40 changes: 39 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";
const path = require("path");
const fs = require("fs");
const os = require("os");

/**
* Walk up the directory tree from `parent/directory` looking for any file
Expand Down Expand Up @@ -32,4 +33,41 @@ function findFiles(parent, directory, name) {
return null;
}

module.exports = { findFiles };
/**
* Resolve variable substitutions and relative paths in a path string.
*
* Handles (in order):
* - `~` expansion to the home directory
* - `${workspaceFolder}` and `${workspaceRoot}` substitution
* - Relative paths resolved against `rootPath` (when supplied)
*
* @param {string} inputPath - Path string to resolve.
* @param {string|null} [rootPath] - Workspace root directory; required for
* variable substitution and relative path resolution.
* @returns {string} The resolved path.
*/
function resolveWorkspacePath(inputPath, rootPath) {
if (!inputPath) return inputPath;
let resolved = inputPath;

// Expand ~ to home directory.
if (resolved.startsWith("~/") || resolved === "~") {
resolved = resolved.replace(/^~(?=\/|$)/, os.homedir());
}

if (rootPath) {
// Substitute ${workspaceFolder} and ${workspaceRoot}.
resolved = resolved
.replace(/\$\{workspaceFolder\}/g, rootPath)
.replace(/\$\{workspaceRoot\}/g, rootPath);

// Resolve relative paths against the workspace root.
if (!path.isAbsolute(resolved)) {
resolved = path.resolve(rootPath, resolved);
}
}

return resolved;
}

module.exports = { findFiles, resolveWorkspacePath };
62 changes: 61 additions & 1 deletion test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const path = require("path");
const fs = require("fs");
const os = require("os");

const { findFiles } = require("../lib/utils");
const { findFiles, resolveWorkspacePath } = require("../lib/utils");

// ---------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -104,3 +104,63 @@ describe("findFiles", () => {
assert.equal(result, expected);
});
});

// ---------------------------------------------------------------------------
// resolveWorkspacePath
// ---------------------------------------------------------------------------

describe("resolveWorkspacePath", () => {
const root = path.join(os.tmpdir(), "phpcbf-resolve-test");

test("returns null/undefined as-is", () => {
assert.equal(resolveWorkspacePath(null, root), null);
assert.equal(resolveWorkspacePath(undefined, root), undefined);
assert.equal(resolveWorkspacePath("", root), "");
});

test("substitutes ${workspaceFolder}", () => {
const result = resolveWorkspacePath("${workspaceFolder}/phpcs.xml", root);
assert.equal(result, path.join(root, "phpcs.xml"));
});

test("substitutes ${workspaceRoot}", () => {
const result = resolveWorkspacePath("${workspaceRoot}/phpcs.xml", root);
assert.equal(result, path.join(root, "phpcs.xml"));
});

test("resolves relative path starting with ./", () => {
const result = resolveWorkspacePath("./ruleset.xml", root);
assert.equal(result, path.resolve(root, "ruleset.xml"));
});

test("resolves relative path without leading ./", () => {
const result = resolveWorkspacePath("vendor/standard/ruleset.xml", root);
assert.equal(result, path.resolve(root, "vendor/standard/ruleset.xml"));
});

test("leaves absolute path unchanged when rootPath supplied", () => {
const abs = path.join(os.tmpdir(), "absolute.xml");
const result = resolveWorkspacePath(abs, root);
assert.equal(result, abs);
});

test("expands ~ to home directory", () => {
const result = resolveWorkspacePath("~/.phpcs.xml", root);
assert.equal(result, path.join(os.homedir(), ".phpcs.xml"));
});

test("expands ~ alone to home directory", () => {
const result = resolveWorkspacePath("~", root);
assert.equal(result, os.homedir());
});

test("returns original path unchanged when rootPath is null", () => {
const result = resolveWorkspacePath("./relative.xml", null);
assert.equal(result, "./relative.xml");
});

test("substitutes ${workspaceFolder} inside a path segment", () => {
const result = resolveWorkspacePath("${workspaceFolder}/sub/ruleset.xml", root);
assert.equal(result, path.join(root, "sub", "ruleset.xml"));
});
});