Skip to content

Commit

Permalink
feat: Support projects with type "module"
Browse files Browse the repository at this point in the history
This allows linting projects that currently do not fit any other project
structure of UI5 Tooling.

To lint such projects, a `ui5.yaml` needs to be added to the root of the
project with the following content:

```yaml
specVersion: "4.0"
type: module
metadata:
  name: <project-name>
resources:
  configuration:
    paths:
      /resources/<namespace>/: "<sources-folder>"
````

Placeholders `<project-name>`, `<namespace>` and `<sources-folder>` need
to be replaced with the actual values.

Example:
A project has its sources in a folder `src` and uses the namespace
`my.project`, so for example a source file named
`src/my/project/util/Formatter.js` would exist.
In this case the placeholders would be replaced as follows:
- `<project-name>`: `my.project`
- `<namespace>`: `my/project`
- `<sources-folder>`: `src`
  • Loading branch information
matz3 committed Nov 28, 2024
1 parent b48a2dc commit d94393a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 29 deletions.
41 changes: 36 additions & 5 deletions src/linter/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {taskStart} from "../utils/perf.js";
import path from "node:path";
import posixPath from "node:path/posix";
import {stat} from "node:fs/promises";
import {ProjectGraph} from "@ui5/project";
import {Project, ProjectGraph} from "@ui5/project";
import type {AbstractReader, Resource} from "@ui5/fs";
import ConfigManager, {UI5LintConfigType} from "../utils/ConfigManager.js";
import {Minimatch} from "minimatch";
Expand All @@ -29,12 +29,32 @@ export async function lintProject({
projectGraphDone();

let virBasePath = "/resources/";
let fsBasePath, namespace;
if (project.getType() === "module") {
// For now, we only support one path mapping.
// This is mainly an internal limitation that requires a potential larger refactoring of the linter code.
const firstMapping = getFirstModulePathMapping(project);
if (firstMapping?.virBasePath?.startsWith("/resources/") && firstMapping?.fsBasePath) {
fsBasePath = firstMapping.fsBasePath;
if (firstMapping.virBasePath.endsWith("/")) {
// Cut off trailing slash
firstMapping.virBasePath = firstMapping.virBasePath.slice(0, -1);
}
namespace = firstMapping.virBasePath.substring("/resources/".length);
} else {
throw new Error("No paths configuration found in project with type ''module''");
}
} else {
fsBasePath = project.getSourcePath();
namespace = project.getNamespace();
}

if (!project._isSourceNamespaced) {
// Ensure the virtual filesystem includes the project namespace to allow relative imports
// of framework resources from the project
virBasePath += project.getNamespace() + "/";
virBasePath += namespace + "/";
}
const fsBasePath = project.getSourcePath();

let reader = createReader({
fsBasePath,
virBasePath,
Expand All @@ -47,7 +67,7 @@ export async function lintProject({
if (!project._isSourceNamespaced) {
// Dynamically add namespace if the physical project structure does not include it
// This logic is identical to the specification implementation in ui5-project
virBasePathTest += project.getNamespace() + "/";
virBasePathTest += namespace + "/";
}
reader = createReaderCollection({
readers: [reader, createReader({
Expand All @@ -62,7 +82,7 @@ export async function lintProject({

const res = await lint(reader, {
rootDir,
namespace: project.getNamespace(),
namespace,
filePatterns,
ignorePatterns,
coverage,
Expand Down Expand Up @@ -384,3 +404,14 @@ export function mergeIgnorePatterns(options: LinterOptions, config: UI5LintConfi
...(options.ignorePatterns ?? []), // CLI patterns go after config patterns
].filter(($) => $);
}

function getFirstModulePathMapping(project: Project) {
const pathMapping = project._config?.resources?.configuration?.paths;
if (pathMapping && Object.keys(pathMapping).length > 0) {
const virBasePath = Object.keys(pathMapping)[0];
const fsBasePath = pathMapping[virBasePath];
return {virBasePath, fsBasePath};
} else {
return null;
}
}
62 changes: 38 additions & 24 deletions src/untyped.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,32 @@

declare module "@ui5/project" {
type ProjectNamespace = string;

// Note: This is only a partial definition with required fields for type module
interface ProjectConfig {
resources: {
configuration: {
paths: Record<string, string>;
};
};
}

interface Project {
getNamespace: () => ProjectNamespace;
getReader: (options: import("@ui5/fs").ReaderOptions) => import("@ui5/fs").AbstractReader;
getRootReader: () => import("@ui5/fs").AbstractReader;
getRootPath: () => string;
getSourcePath: () => string;
_testPath: string; // TODO UI5 Tooling: Expose API for optional test path
getNamespace(): ProjectNamespace;
getReader(options: import("@ui5/fs").ReaderOptions): import("@ui5/fs").AbstractReader;
getRootReader(): import("@ui5/fs").AbstractReader;
getRootPath(): string;
getSourcePath(): string;
getType(): "application" | "library" | "module";

// TODO UI5 Tooling: Expose required information as API
_testPath: string;
_testPathExists: string;
_isSourceNamespaced: boolean;
_config: ProjectConfig; // Needed to read the paths configuration of projects with type module
}
interface ProjectGraph {
getRoot: () => Project;
getRoot(): Project;
}
interface DependencyTreeNode {
id: string;
Expand Down Expand Up @@ -70,13 +84,13 @@ declare module "@ui5/fs" {
contentModified: boolean;
}
interface Resource {
getBuffer: () => Promise<Buffer>;
getString: () => Promise<string>;
getStream: () => import("node:fs").ReadStream;
getName: () => string;
getPath: () => ResourcePath;
getProject: () => import("@ui5/project").Project;
getSourceMetadata: () => ResourceSourceMetadata;
getBuffer(): Promise<Buffer>;
getString(): Promise<string>;
getStream(): import("node:fs").ReadStream;
getName(): string;
getPath(): ResourcePath;
getProject(): import("@ui5/project").Project;
getSourceMetadata(): ResourceSourceMetadata;
}
type ReaderStyles = "buildtime" | "dist" | "runtime" | "flat";

Expand All @@ -89,11 +103,11 @@ declare module "@ui5/fs" {
type Filter = (resource: Resource) => boolean;

export interface AbstractReader {
byGlob: (virPattern: string | string[], options?: GlobOptions) => Promise<Resource[]>;
byPath: (path: string) => Promise<Resource>;
byGlob(virPattern: string | string[], options?: GlobOptions): Promise<Resource[]>;
byPath(path: string): Promise<Resource>;
}
export interface AbstractAdapter extends AbstractReader {
write: (resource: Resource) => Promise<void>;
write(resource: Resource): Promise<void>;
}
}

Expand Down Expand Up @@ -133,13 +147,13 @@ declare module "@ui5/fs/resourceFactory" {

declare module "@ui5/logger" {
interface Logger {
silly: (message: string) => void;
verbose: (message: string) => void;
perf: (message: string) => void;
info: (message: string) => void;
warn: (message: string) => void;
error: (message: string) => void;
isLevelEnabled: (level: string) => boolean;
silly(message: string): void;
verbose(message: string): void;
perf(message: string): void;
info(message: string): void;
warn(message: string): void;
error(message: string): void;
isLevelEnabled(level: string): boolean;
}

export function isLogLevelEnabled(logLevel: string): boolean;
Expand Down

0 comments on commit d94393a

Please sign in to comment.