SAP / ui5-linter

A static code analysis tool for UI5
Apache License 2.0
40 stars 0 forks source link

UI5 Linter complains about allowed globals when they're accessed via globalThis #125

Closed codeworrior closed 3 hours ago

codeworrior commented 1 month ago

Expected Behavior

The linter should not complain about allowed globals, no matter how they are accessed.

Current Behavior

The linter complains about calls to sap.ui.define or other allowed globals when they are accessed via globalThis or one of the other names that refer to the global JavaScript context (e.g. self, window, top, parent, top level this).

Example:

if (typeof self.sap?.ui?.define === 'function') {
    sap.ui.define(['sap/ui/thirdparty/URI'], factory);

(found in sap/ui/documentation/sdk/util/Resources.js)

Context

flovogt commented 1 month ago

In src/linter/ui5Types/SourceFileLinter.ts the linter already analyzes the global associated properties in analyzePropertyAccessExpression. The method extractNamespace might not support cases if conditional access is used. Needs to be analyzed further.

flovogt commented 4 weeks ago

PR https://github.com/SAP/ui5-linter/pull/132 fixes globalThis.

top, parent and top level this are still not reported but test cases are added.

RandomByte commented 4 weeks ago

The problem is that TypeScript seems to treat top and parent something different from window or globalThis.

While the derived type is Window, TS fails to resolve any of the UI5 types to properties accessed on top or parent. This can be reproduced directly in VSCode as well.

For the top-level this it's different however. VSCode can derive UI5 types correctly. Only in the linter we don't get them resolved for currently unknown reasons.