Skip to content

Commit

Permalink
feat: Analyze sap.ui.core.Lib.init() call
Browse files Browse the repository at this point in the history
  • Loading branch information
d3xter666 committed Mar 14, 2024
1 parent 808f3b4 commit 399a320
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/detectors/typeChecker/FileLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default class FileLinter {
// const nodeType = this.#checker.getTypeAtLocation(node);
this.analyzePropertyAccessExpression(node as ts.CallExpression); // Check for global
this.analyzeCallExpression(node as ts.CallExpression); // Check for deprecation
this.analyzeLibInitCall(node as ts.CallExpression); // Check for sap.ui.coreLib.init usages
} else if (node.kind === ts.SyntaxKind.PropertyAccessExpression ||
node.kind === ts.SyntaxKind.ElementAccessExpression) {
this.analyzePropertyAccessExpression(
Expand Down Expand Up @@ -201,6 +202,35 @@ export default class FileLinter {
messageDetails: this.extractDeprecatedMessage(symbol),
});
}

analyzeLibInitCall(node: ts.CallExpression) {
const nodeExp = node.expression as ts.PropertyAccessExpression;
if (nodeExp?.name?.text !== "init" ||
!ts.isPropertyAccessExpression(nodeExp) ||
!ts.isIdentifier(nodeExp.expression) ||
nodeExp?.expression?.text !== "Library") { // TODO: Check more reliably the import var of sap.ui.core.Lib
// Library.init() -> init() is already identified as CallExpression, so
// Library needs to be a propertyAccessExpression
return;
}

const libVersion = (node.arguments[0] as ts.ObjectLiteralExpression)
.properties.find((prop: ts.ObjectLiteralElementLike) => {
return ts.isPropertyAssignment(prop) &&
ts.isIdentifier(prop.name) && prop.name.text === "version" &&
ts.isLiteralExpression(prop.initializer) && prop.initializer.text !== "2";
}) as ts.PropertyAssignment | undefined;

if (libVersion) {
this.#reporter.addMessage({
node: libVersion,
severity: LintMessageSeverity.Error,
ruleId: "ui5-linter-no-partially-deprecated-api",
message:
`Call to ${nodeExp.expression.text}.init() must be declared with property {version: 2}`
});
}
}

isDeprecatedAccess(node: ts.AccessExpression): ts.Symbol | undefined {
let symbol;
Expand Down Expand Up @@ -335,6 +365,7 @@ export default class FileLinter {
// as UI5 / AMD only supports importing the default anyways.
// TODO: This needs to be enhanced in future
const defaultExportSymbol = symbol?.exports?.get("default" as ts.__String);
// parseModuleDeclaration(importDeclarationNode, this.#checker);
if (defaultExportSymbol && this.isDeprecated(defaultExportSymbol)) {
this.#reporter.addMessage({
node: moduleSpecifierNode,
Expand Down

0 comments on commit 399a320

Please sign in to comment.