generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Detect deprecations in ui5.yaml (in root directory) (#39)
- feat: Detect deprecated libs in any ui5.yaml - deps: Add data-with-position npm library for YAML-parsing (https://www.npmjs.com/package/data-with-position) - refactor: Outsource list of deprecated libs to common utils constant JIRA: CPOUI5FOUNDATION-822
- Loading branch information
1 parent
508d81d
commit db118b1
Showing
20 changed files
with
832 additions
and
229 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import {LintMessageSeverity} from "../LinterContext.js"; | ||
import LinterContext from "../LinterContext.js"; | ||
import deprecatedLibraries from "../../utils/deprecatedLibs.js"; | ||
import {DataWithPosition, fromYaml, getPosition} from "data-with-position"; | ||
|
||
interface YamlWithPosInfo extends DataWithPosition { | ||
framework?: { | ||
libraries?: { | ||
name: string; | ||
}[]; | ||
}; | ||
positionKey?: { | ||
end: { | ||
column: number; | ||
line: number; | ||
}; | ||
start: { | ||
column: number; | ||
line: number; | ||
}; | ||
}; | ||
} | ||
|
||
export default class YamlLinter { | ||
#content; | ||
#resourcePath; | ||
#context: LinterContext; | ||
|
||
constructor(resourcePath: string, content: string, context: LinterContext) { | ||
this.#content = content; | ||
this.#resourcePath = resourcePath; | ||
this.#context = context; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
async lint() { | ||
try { | ||
// Split Yaml file into part documents by '---' separator | ||
const allDocuments: string[] = this.#content.split(/(?:\r\n|\r|\n)---/g); | ||
|
||
// Calculate the starting line number of each part document | ||
let lineNumberOffset = 0; | ||
allDocuments.forEach((document: string) => { | ||
// Parse content only of the current part | ||
const parsedYamlWithPosInfo: YamlWithPosInfo = this.#parseYaml(document); | ||
// Analyze part content with line number offset | ||
this.#analyzeYaml(parsedYamlWithPosInfo, lineNumberOffset); | ||
// Update line number offset for next part | ||
lineNumberOffset += document.split(/\r\n|\r|\n/g).length; | ||
}); | ||
} catch (err) { | ||
const message = err instanceof Error ? err.message : String(err); | ||
this.#context.addLintingMessage(this.#resourcePath, { | ||
severity: LintMessageSeverity.Error, | ||
message, | ||
ruleId: "ui5-linter-parsing-error", | ||
fatal: true, | ||
}); | ||
} | ||
} | ||
|
||
#parseYaml(content: string): YamlWithPosInfo { | ||
// Create JS object from YAML content with position information | ||
return fromYaml(content) as YamlWithPosInfo; | ||
} | ||
|
||
#analyzeYaml(yaml: YamlWithPosInfo, offset: number) { | ||
// Check for deprecated libraries | ||
yaml?.framework?.libraries?.forEach((lib) => { | ||
if (deprecatedLibraries.includes(lib.name.toString())) { | ||
const positionInfo = getPosition(lib); | ||
this.#context.addLintingMessage(this.#resourcePath, { | ||
ruleId: "ui5-linter-no-deprecated-api", | ||
severity: LintMessageSeverity.Error, | ||
fatal: undefined, | ||
line: positionInfo.start.line + offset, | ||
column: positionInfo.start.column, | ||
message: `Use of deprecated library '${lib.name}'`, | ||
}); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import {LinterParameters} from "../LinterContext.js"; | ||
import YamlLinter from "./YamlLinter.js"; | ||
import {Resource} from "@ui5/fs"; | ||
|
||
export default async function lintUI5Yaml({context}: LinterParameters) { | ||
let ui5YamlResources: Resource[]; | ||
const pathsToLint = context.getPathsToLint(); | ||
const reader = context.getRootReader(); | ||
if (pathsToLint?.length) { | ||
ui5YamlResources = []; | ||
await Promise.all(pathsToLint.map(async (resourcePath) => { | ||
if (!resourcePath.endsWith(".yaml")) { | ||
return; | ||
} | ||
const resource = await reader.byPath(resourcePath); | ||
if (!resource) { | ||
throw new Error(`Resource not found: ${resourcePath}`); | ||
} | ||
ui5YamlResources.push(resource); | ||
})); | ||
} else { | ||
ui5YamlResources = await reader.byGlob("/{ui5.yaml,*-ui5.yaml,*.ui5.yaml,ui5-*.yaml}"); | ||
} | ||
|
||
await Promise.all(ui5YamlResources.map(async (resource: Resource) => { | ||
const linter = new YamlLinter(resource.getPath(), await resource.getString(), context); | ||
await linter.lint(); | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const deprecatedLibs: string[] = [ | ||
"sap.ca.scfld.md", | ||
"sap.ca.ui", | ||
"sap.fe.common", // Internal, removed in 1.110 | ||
"sap.fe.plugins", // Internal, removed in 1.102 | ||
"sap.fe.semantics", // Internal, removed in 1.104 | ||
"sap.landvisz", // Removed in 1.120 | ||
"sap.makit", | ||
"sap.me", | ||
"sap.sac.grid", // Removed in 1.114 | ||
"sap.ui.commons", | ||
"sap.ui.suite", | ||
"sap.ui.ux3", | ||
"sap.ui.vtm", | ||
"sap.uiext.inbox", | ||
"sap.webanalytics.core", | ||
"sap.zen.commons", | ||
"sap.zen.crosstab", | ||
"sap.zen.dsh", | ||
]; | ||
|
||
export default deprecatedLibs; |
11 changes: 11 additions & 0 deletions
11
test/fixtures/linter/projects/com.ui5.troublesome.app/ui5.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
specVersion: '3.0' | ||
metadata: | ||
name: com.ui5.troublesome.app | ||
type: application | ||
framework: | ||
name: OpenUI5 | ||
version: "1.121.0" | ||
libraries: | ||
- name: sap.m | ||
- name: sap.ui.core | ||
- name: sap.landvisz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ framework: | |
version: "1.120.6" | ||
libraries: | ||
- name: sap.ui.core | ||
- name: sap.landvisz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
specVersion: '3.0' | ||
metadata: | ||
name: com.ui5.troublesome.app | ||
type: application | ||
framework: | ||
name: OpenUI5 | ||
version: "1.121.0" | ||
libraries: | ||
- name: sap.m | ||
- name: sap.ui.core | ||
- name: sap.landvisz |
Binary file not shown.
Oops, something went wrong.