Skip to content

Commit

Permalink
refactor: Use direct path mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
matz3 committed Mar 25, 2024
1 parent 7fb5036 commit 3d163b0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 48 deletions.
48 changes: 17 additions & 31 deletions src/detectors/typeChecker/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fs from "node:fs/promises";
import {createRequire} from "node:module";
const require = createRequire(import.meta.url);

const SAPUI5_TYPES_FILES = [
const SAPUI5_TYPES_LIBS = [
"sap.apf",
"sap.chart",
"sap.ui.codeeditor",
Expand Down Expand Up @@ -70,6 +70,19 @@ const SAPUI5_TYPES_FILES = [
"sap.ui.webc.main",
];

function addSapui5TypesMappingToCompilerOptions(options: ts.CompilerOptions) {
const paths = options.paths ?? (options.paths = {});
SAPUI5_TYPES_LIBS.forEach((lib) => {
if (lib === "sap.ui.core") {
// No need to add a mapping for sap.ui.core, as it is loaded by default
return;
}
const namespace = lib.replace(/\./g, "/") + "/*";
const pathsEntry = paths[namespace] ?? (paths[namespace] = []);
pathsEntry.push(`/types/@sapui5/types/types/${lib}.d.ts`);
});
}

interface PackageJson {
dependencies: Record<string, string>;
}
Expand Down Expand Up @@ -121,6 +134,8 @@ export async function createVirtualCompilerHost(
"/types/@sapui5/types/types/sap.ui.core.d.ts",
];

addSapui5TypesMappingToCompilerOptions(options);

// Create regex matching all path mapping keys
const pathMappingRegex = new RegExp(
`^\\/types\\/(${Array.from(typePathMappings.keys()).join("|").replaceAll("/", "\\/")})\\/(.*)`);
Expand Down Expand Up @@ -193,9 +208,6 @@ export async function createVirtualCompilerHost(
if (fsPath) {
return ts.sys.directoryExists(fsPath);
}
if (directory.startsWith("/types/@ui5/linter/dynamic-types/")) {
return true;
}
}
return false;
},
Expand All @@ -210,9 +222,6 @@ export async function createVirtualCompilerHost(
if (fsPath) {
return ts.sys.fileExists(fsPath);
}
if (fileName.startsWith("/types/@ui5/linter/dynamic-types/") && fileName.endsWith(".d.ts")) {
return true;
}
}
return false;
},
Expand Down Expand Up @@ -244,30 +253,7 @@ export async function createVirtualCompilerHost(
if (sourceFileCache.has(fileName)) {
return sourceFileCache.get(fileName);
}

let sourceText: string | undefined = undefined;

if (fileName.startsWith("/types/@ui5/linter/dynamic-types/") && fileName.endsWith(".d.ts")) {
const moduleName = fileName.match(/\/types\/@ui5\/linter\/dynamic-types\/(.*)\.d\.ts/)?.[1];
if (moduleName) {
const libraryNameCheck = moduleName?.replace(/\//g, ".");
if (libraryNameCheck.startsWith("sap.ui.core.") || !libraryNameCheck.startsWith("sap.")) {
// sap.ui.core is loaded by default
return;
}
const libraryName = SAPUI5_TYPES_FILES.find(($) => libraryNameCheck.startsWith($ + "."));
if (libraryName) {
sourceText = `/// <reference path="/types/@sapui5/types/types/${libraryName}.d.ts"/>`;
} else {
// Can happen e.g. for sap/ui/base/Event.d.ts, but sap.ui.core.d.ts is loaded by default
return;
}
}
}

if (!sourceText) {
sourceText = getFile(fileName);
}
const sourceText = getFile(fileName);
if (sourceText === undefined) {
throw new Error(`File not found: ${fileName}`);
}
Expand Down
18 changes: 1 addition & 17 deletions src/detectors/typeChecker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,8 @@ export class TsProjectDetector extends ProjectBasedDetector {

const namespace = project.getNamespace();
this.#projectBasePath = `/resources/${namespace}/`;

const namespacePathMapping = [
`${this.#projectBasePath}*`,
];

// Special handling when linting sap/* namespaces (UI5 framework libraries):
// Add mapping for dynamic type lookup. Otherwise types within the sap library itself
// can't be resolved.
if (namespace.startsWith("sap/")) {
namespacePathMapping.push(`/types/@ui5/linter/dynamic-types/${namespace}/*`);
}

this.compilerOptions.paths = {
"sap/*": ["/types/@ui5/linter/dynamic-types/sap/*"],
[`${namespace}/*`]: namespacePathMapping,
[`${namespace}/*`]: [`${this.#projectBasePath}*`],
};
}

Expand Down Expand Up @@ -276,9 +263,6 @@ export class TsFileDetector extends FileBasedDetector {
const options: ts.CompilerOptions = {
...DEFAULT_OPTIONS,
rootDir: this.rootDir,
paths: {
"sap/*": ["/types/@ui5/linter/dynamic-types/sap/*"],
},
};

const resources = new Map<string, string>();
Expand Down

0 comments on commit 3d163b0

Please sign in to comment.