Skip to content

Commit

Permalink
fix: match enablePaths by whole path components (#918)
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn authored Sep 12, 2023
1 parent 6122bb2 commit a5dffc3
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions typescript-deno-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import type { PluginSettings, Settings } from "../../client/src/shared_types";
import type * as ts from "../node_modules/typescript/lib/tsserverlibrary";
import * as path from "path";
import * as os from "os";

/** Extract the return type from a maybe function. */
// deno-lint-ignore no-explicit-any
Expand Down Expand Up @@ -90,10 +92,10 @@ class Plugin implements ts.server.PluginModule {
const settings = projectSettings.get(this.#projectName);
if (settings?.enabledPaths) {
const paths = settings.enabledPaths.find(({ workspace }) =>
fileName.startsWith(workspace)
pathStartsWith(fileName, workspace)
)?.paths;
if (paths && paths.length) {
return paths.some((path) => fileName.startsWith(path));
return paths.some((path) => pathStartsWith(fileName, path));
}
}
// TODO(@kitsonk): rework all of this to be more like the workspace folders
Expand Down Expand Up @@ -408,4 +410,17 @@ function init(): ts.server.PluginModule {
return new Plugin();
}

const PARENT_RELATIVE_REGEX = os.platform() === "win32"
? /\.\.(?:[/\\]|$)/
: /\.\.(?:\/|$)/;

/** Checks if `parent` is an ancestor of `child`. */
function pathStartsWith(child: string, parent: string) {
if (path.isAbsolute(child) !== path.isAbsolute(parent)) {
return false;
}
const relative = path.relative(parent, child);
return !relative.match(PARENT_RELATIVE_REGEX);
}

export = init;

0 comments on commit a5dffc3

Please sign in to comment.