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: Add detection for root files (e.g. ui5.yaml)
- Add js-yaml npm library for YAML-parsing (https://www.npmjs.com/package/js-yaml)
- Loading branch information
1 parent
c5b0c95
commit 6d42145
Showing
17 changed files
with
616 additions
and
917 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import {LintMessageSeverity} from "../LinterContext.js"; | ||
import LinterContext from "../LinterContext.js"; | ||
import deprecatedLibraries from "../../utils/deprecatedLibs.js"; | ||
import yaml from "js-yaml"; | ||
import {DataWithPosition, fromYaml, getPosition} from "data-with-position"; | ||
|
||
// file content schema of 'UI5Yaml' with only relevant properties | ||
interface UI5YamlContentSchema { // extend for further detections | ||
framework: { | ||
libraries: { | ||
name: string; | ||
}[]; | ||
}; | ||
} | ||
|
||
interface UI5YamlContentSchemaWithPosInfo extends DataWithPosition { | ||
framework?: { | ||
libraries?: { | ||
name: string; | ||
}[]; | ||
}; | ||
positionKey?: { | ||
end: { | ||
column: number; | ||
line: number; | ||
}; | ||
start: { | ||
column: number; | ||
line: number; | ||
}; | ||
}; | ||
} | ||
|
||
export default class UI5YamlLinter { | ||
#content = ""; | ||
#yamlContentWithPosInfo: UI5YamlContentSchemaWithPosInfo = {}; | ||
#resourcePath = ""; | ||
#context: LinterContext; | ||
|
||
constructor(content: string, resourcePath: string, context: LinterContext) { | ||
this.#content = content; | ||
this.#resourcePath = resourcePath; | ||
this.#context = context; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
async lint() { | ||
try { | ||
const source: UI5YamlContentSchema = this.#parseUI5Yaml(this.#content); | ||
this.#analyzeUI5Yaml(source); | ||
} 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, | ||
}); | ||
} | ||
} | ||
|
||
#parseUI5Yaml(fileContent: string): UI5YamlContentSchema { | ||
// Create JS object from YAML content with position information | ||
this.#yamlContentWithPosInfo = fromYaml(fileContent) as UI5YamlContentSchemaWithPosInfo; | ||
// Convert YAML content to JS object | ||
return yaml.load(fileContent) as UI5YamlContentSchema; | ||
} | ||
|
||
#analyzeUI5Yaml(ui5YamlObject: UI5YamlContentSchema) { | ||
// Check for deprecated libraries | ||
if (ui5YamlObject?.framework?.libraries?.length) { | ||
ui5YamlObject.framework.libraries.forEach((lib, index: number) => { | ||
if (deprecatedLibraries.includes(lib.name)) { | ||
const positionInfo = getPosition(this.#yamlContentWithPosInfo.framework!.libraries![index]); | ||
this.#context.addLintingMessage(this.#resourcePath, { | ||
ruleId: "ui5-linter-no-deprecated-api", | ||
severity: LintMessageSeverity.Error, | ||
fatal: undefined, | ||
line: positionInfo.start.line, | ||
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 UI5YamlLinter from "./UI5YamlLinter.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 UI5YamlLinter(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.
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,52 @@ | ||
import test from "ava"; | ||
import UI5YamlLinter from "../../../src/linter/yaml/UI5YamlLinter.js"; | ||
import LinterContext from "../../../src/linter/LinterContext.js"; | ||
|
||
test("Test UI5YamlLinter report (parsing and analyzing)", async (t) => { | ||
/* Mock resource content of ui5.yaml file, | ||
(formatted as used in src/detectors/typeChecker/index.ts - #analyzeFiles()), | ||
(contains relevant 'framework' property and 'libraries' sub-property), | ||
(contains only deprecated libraries) */ | ||
const resourceContent = | ||
`specVersion: '3.0' | ||
metadata: | ||
name: ava-test-ui5yamllinter | ||
type: application | ||
framework: | ||
name: OpenUI5 | ||
version: "1.121.0" | ||
libraries: | ||
- name: sap.ca.scfld.md | ||
- name: sap.ca.ui | ||
- name: sap.fe.common`; | ||
|
||
const resourcePath = "/ui5.js"; // '.js' due to renaming in src/detectors/typeChecker/index.ts - #analyzeFiles() | ||
const projectPath = "test.ui5yamllinter"; | ||
const context = new LinterContext({rootDir: projectPath}); | ||
|
||
// Create UI5YamlLinter instance with resource content | ||
const linter = new UI5YamlLinter(resourceContent, resourcePath, context); | ||
// Run UI5YamlLinter report | ||
await linter.lint(); | ||
|
||
const messages = context.getLintingMessages("/ui5.js"); | ||
|
||
// Test returned messages | ||
t.is(messages.length, 3, "Detection of 3 deprecated libraries expected"); | ||
|
||
// Test each message | ||
t.is(messages[0].ruleId, "ui5-linter-no-deprecated-api", `RuleId is correct`); | ||
t.is(messages[0].message, `Use of deprecated library 'sap.ca.scfld.md'`, `Message is correct`); | ||
t.is(messages[0].column, 7, `Column is correct`); | ||
t.is(messages[0].line, 9, `Line is correct`); | ||
|
||
t.is(messages[1].ruleId, "ui5-linter-no-deprecated-api", `RuleId is correct`); | ||
t.is(messages[1].message, `Use of deprecated library 'sap.ca.ui'`, `Message is correct`); | ||
t.is(messages[1].column, 7, `Column is correct`); | ||
t.is(messages[1].line, 10, `Line is correct`); | ||
|
||
t.is(messages[2].ruleId, "ui5-linter-no-deprecated-api", `RuleId is correct`); | ||
t.is(messages[2].message, `Use of deprecated library 'sap.fe.common'`, `Message is correct`); | ||
t.is(messages[2].column, 7, `Column is correct`); | ||
t.is(messages[2].line, 11, `Line is correct`); | ||
}); |
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 |
---|---|---|
|
@@ -1222,3 +1222,9 @@ Generated by [AVA](https://avajs.dev). | |
warningCount: 0, | ||
}, | ||
] | ||
|
||
## General: ui5.yaml | ||
|
||
> Snapshot 1 | ||
[] |
Binary file not shown.
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,27 @@ | ||
import test from "ava"; | ||
import deprecatedLibs from "../../../src/utils/deprecatedLibs.js"; | ||
|
||
test("Test Deprecated Libs constant", (t) => { | ||
const expectedDeprecatedLibs: 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", | ||
]; | ||
t.deepEqual(deprecatedLibs, expectedDeprecatedLibs, | ||
"Expected deprecated libraries list should match the actual list."); | ||
}); |