Skip to content

Commit

Permalink
fix: Resolve types for framework libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
matz3 committed Mar 22, 2024
1 parent 664ad61 commit 7fb5036
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/detectors/typeChecker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,21 @@ 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 = {
[`${namespace}/*`]: [`${this.#projectBasePath}*`],
"sap/*": ["/types/@ui5/linter/dynamic-types/sap/*"],
[`${namespace}/*`]: namespacePathMapping,
};
}

Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/linter/projects/sap.f/src/sap/f/.library
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" ?>
<library xmlns="http://www.sap.com/sap.ui.library.xsd">
<name>sap.f</name>
<dependencies>
<dependency>
<libraryName>sap.ui.core</libraryName>
</dependency>
</dependencies>
</library>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// This project is used to test the linter with the namespace of an OpenUI5 project.
9 changes: 9 additions & 0 deletions test/fixtures/linter/projects/sap.f/test/sap/f/LinterTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// This project is used to test the linter with the namespace of an OpenUI5 project.

sap.ui.require([
"sap/f/Avatar",
"sap/m/DateTimeInput"
], (Avatar, DateTimeInput) => {
new Avatar();
new DateTimeInput();
});
18 changes: 18 additions & 0 deletions test/lib/linter/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,21 @@ test.serial("lint: All files of library.with.custom.paths", async (t) => {

t.snapshot(res);
});

test.serial("lint: All files of library with sap.f namespace", async (t) => {
const projectPath = path.join(fixturesProjectsPath, "sap.f");
const {lintProject} = t.context;

let res = await lintProject({
rootDir: projectPath,
filePaths: [],
reportCoverage: true,
messageDetails: true,
});

res = res.sort((a: {filePath: string}, b: {filePath: string}) => {
return a.filePath.localeCompare(b.filePath);
});

t.snapshot(res);
});
42 changes: 42 additions & 0 deletions test/lib/linter/snapshots/linter.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -981,3 +981,45 @@ Generated by [AVA](https://avajs.dev).
warningCount: 0,
},
]

## lint: All files of library with sap.f namespace

> Snapshot 1
[
{
coverageInfo: [],
errorCount: 0,
fatalErrorCount: 0,
filePath: 'src/sap/f/LinterTest.js',
messages: [],
warningCount: 0,
},
{
coverageInfo: [],
errorCount: 2,
fatalErrorCount: 0,
filePath: 'test/sap/f/LinterTest.js',
messages: [
{
column: 2,
fatal: undefined,
line: 4,
message: 'Import of deprecated module \'sap/f/Avatar\'',
messageDetails: 'Deprecated test message',
ruleId: 'ui5-linter-no-deprecated-api',
severity: 2,
},
{
column: 2,
fatal: undefined,
line: 5,
message: 'Import of deprecated module \'sap/m/DateTimeInput\'',
messageDetails: 'Deprecated test message',
ruleId: 'ui5-linter-no-deprecated-api',
severity: 2,
},
],
warningCount: 0,
},
]
Binary file modified test/lib/linter/snapshots/linter.ts.snap
Binary file not shown.

0 comments on commit 7fb5036

Please sign in to comment.