Skip to content

Commit

Permalink
Fixes symbol codefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanis committed Mar 9, 2020
1 parent 426f5a7 commit 944d972
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,11 +669,30 @@ namespace ts.codefix {
}
}

/**
* We only allow PnP to be used as a resolution strategy if TypeScript
* itself is executed under a PnP runtime (and we only allow it to access
* the current PnP runtime, not any on the disk). This ensures that we
* don't execute potentially malicious code that didn't already have a
* chance to be executed (if we're running within the runtime, it means
* that the runtime has already been executed).
* @internal
*/
function isPnpAvailable() {
// @ts-ignore
return process.versions.pnp;
}

function getPnpApi() {
// @ts-ignore
return require("pnpapi");
}

/**
* Don't include something from a `node_modules` that isn't actually reachable by a global import.
* A relative import to node_modules is usually a bad idea.
*/
function isImportablePath(fromPath: string, toPath: string, getCanonicalFileName: GetCanonicalFileName, globalCachePath?: string): boolean {
function isImportablePathNode(fromPath: string, toPath: string, getCanonicalFileName: GetCanonicalFileName, globalCachePath?: string): boolean {
// If it's in a `node_modules` but is not reachable from here via a global import, don't bother.
const toNodeModules = forEachAncestorDirectory(toPath, ancestor => getBaseFileName(ancestor) === "node_modules" ? ancestor : undefined);
const toNodeModulesParent = toNodeModules && getDirectoryPath(getCanonicalFileName(toNodeModules));
Expand All @@ -682,6 +701,30 @@ namespace ts.codefix {
|| (!!globalCachePath && startsWith(getCanonicalFileName(globalCachePath), toNodeModulesParent));
}

function isImportablePathPnp(fromPath: string, toPath: string): boolean {
const pnpApi = getPnpApi();

const fromLocator = pnpApi.findPackageLocator(fromPath);
const toLocator = pnpApi.findPackageLocator(toPath);

if (toLocator === null) {
return false;
}

const fromInfo = pnpApi.getPackageInformation(fromLocator);
const toReference = fromInfo.packageDependencies.get(toLocator.name);

return toReference === toLocator.reference;
}

function isImportablePath(fromPath: string, toPath: string, getCanonicalFileName: GetCanonicalFileName, globalCachePath?: string): boolean {
if (isPnpAvailable()) {
return isImportablePathPnp(fromPath, toPath);
} else {
return isImportablePathNode(fromPath, toPath, getCanonicalFileName, globalCachePath);
}
}

export function moduleSymbolToValidIdentifier(moduleSymbol: Symbol, target: ScriptTarget): string {
return moduleSpecifierToValidIdentifier(removeFileExtension(stripQuotes(moduleSymbol.name)), target);
}
Expand Down

0 comments on commit 944d972

Please sign in to comment.