Skip to content

Commit

Permalink
Support typescript paths for checking imports. Code actions, defs etc.
Browse files Browse the repository at this point in the history
still TODO
  • Loading branch information
Matsuuu committed Nov 12, 2023
1 parent 65e75d9 commit 03110ff
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
6 changes: 6 additions & 0 deletions lib/custom-elements-languageserver-core/src/cem/cem-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class CEMInstance {
// This is done due to the caching that we do with some CEM's. Currently needed only for local
// CEM. Dependency CEM is resolved as is.
public cemSourcePath: string | undefined;
public cemSourceFolderPath: string | undefined;
public cemFolderPath: string | undefined;
public cemPath: string | undefined;
public packagePath: string | undefined;
Expand Down Expand Up @@ -69,6 +70,11 @@ export class CEMInstance {
this.cemPath = builderData.cemPath;
this.cemFolderPath = this.cemPath.substring(0, this.cemPath.lastIndexOf("/"));
this.cemSourcePath = builderData.cemSourcePath || this.cemFolderPath;
// From aa/bb/custom-elements.json to aa/bb in cases where the json is appended. Ignore otherwise
// TODO: We really need to clean up these vars to a more clear format.
this.cemSourceFolderPath = this.cemSourcePath.endsWith(".json")
? this.cemSourcePath.substring(0, this.cemSourcePath.lastIndexOf("/"))
: this.cemSourcePath;
this.packagePath = builderData.packagePath;
this.packageName = builderData.packageName;
this.packageJsonPath = builderData.packageJsonPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getCEMData } from "../../cem/cem-cache.js";
import { findCustomElementDefinitionModule } from "../../cem/cem-helpers.js";
import { getCustomElementTagsInContext } from "../../scanners/tag-scanner.js";
import { getAllFilesAssociatedWithSourceFile, getSourceFile } from "../../ts/sourcefile.js";
import { SourceFile } from "typescript";
import ts, { SourceFile } from "typescript";
import { getFilePathFolder, resolveImportPath } from "./imports.js";
import { CODE_ACTIONS } from "../enum/code-actions.js";
import { getPathAsDtsFile, getPathAsJsFile, getPathAsTsFile } from "../../ts/filepath-transformers.js";
Expand Down Expand Up @@ -45,10 +45,13 @@ export function getImportDiagnostics(request: CustomElementsLanguageServiceReque
if (!definition) {
continue;
}

const cemInstanceRef = definition.cem;
const fullImportPath = normalizePath(`${cemInstanceRef.cemSourcePath}/${definition.path}`);
const moduleResolution = ts.resolveModuleName(`${cemInstanceRef.cemSourceFolderPath}/${definition.path}`, filePath, project.getCompilerOptions(), project.projectService.host);
const resolvedModuleFileName = moduleResolution.resolvedModule?.resolvedFileName;

if (!sourceFilesContainFilePath(sourceFileNames, fullImportPath)) {
if (!resolvedModuleFileName || !associatedFiles.includes(resolvedModuleFileName)) {

const relativeImportPath = resolveImportPath(fullImportPath, filePathWithoutFile);

Expand Down
20 changes: 14 additions & 6 deletions lib/custom-elements-languageserver-core/src/ts/sourcefile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,47 @@ export function getSourceFile(baseOrFullPath: string, classPath: string | undefi

export function getAllFilesAssociatedWithSourceFile(sourceFile: ts.SourceFile, basePath: string, project: tss.server.Project) {

const analyzedFiles: Record<string, ts.SourceFile> = {};
const analyzedFiles: Record<string, string[]> = {};

function processFileAndAddImports(currentSourceFile: ts.SourceFile) {
function processFileAndAddImports(currentSourceFile: ts.SourceFile, importAlias?: string) {
if (analyzedFiles[currentSourceFile.fileName]) {
return;
}
analyzedFiles[currentSourceFile.fileName] = currentSourceFile;
analyzedFiles[currentSourceFile.fileName] = [currentSourceFile.fileName];
if (importAlias) {
analyzedFiles[currentSourceFile.fileName].push(importAlias);
}
const fileInfo = ts.preProcessFile(currentSourceFile.getFullText());
const imports = fileInfo.importedFiles;

for (const importReference of imports) {
const moduleResolution = ts.resolveModuleName(importReference.fileName, currentSourceFile.fileName, project.getCompilerOptions(), project.projectService.host);
// TODO: for node modules, cache the found connections
const importFilePath = importReference.fileName
const importFilePath = moduleResolution.resolvedModule?.resolvedFileName ?? importReference.fileName
// No need for this anymore?
const absoluteImportPath = resolveAbsoluteFileToImport(importFilePath, basePath, currentSourceFile);

if (!absoluteImportPath) {
continue;
}
if (analyzedFiles[absoluteImportPath]) {
continue;
}

// Need for this anymore?
const importSourceFile = tryGetSourceFileForImport(absoluteImportPath, project);
if (!importSourceFile) {
continue;
}

processFileAndAddImports(importSourceFile);
const alias = importReference.fileName !== importSourceFile.fileName ? importReference.fileName : undefined;
processFileAndAddImports(importSourceFile, alias);
}
}

processFileAndAddImports(sourceFile);

return Object.keys(analyzedFiles);
return Object.values(analyzedFiles).flatMap(x => x);
}

function tryGetSourceFileForImport(absoluteImportPath: string, project: tss.server.Project) {
Expand Down

0 comments on commit 03110ff

Please sign in to comment.