8000 feat(Formatter): Detect globals and strings of formatters in bindings (JS/TS) by maxreichmann · Pull Request #499 · SAP/ui5-linter · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(Formatter): Detect globals and strings of formatters in bindings (JS/TS) #499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 31, 2025

Conversation

maxreichmann
Copy link
Member
@maxreichmann maxreichmann commented Jan 24, 2025

JIRA: CPOUI5FOUNDATION-1005

@maxreichmann maxreichmann changed the title feat(Formatter): Detect globals in formatters (JS/TS) feat(Formatter): Detect globals in formatter bindings (JS/TS) Jan 27, 2025
@d3xter666 d3xter666 reopened this Jan 29, 2025
@maxreichmann maxreichmann force-pushed the detect-globals-in-formatters-js branch 2 times, most recently from 56fa7f7 to 64ddc1d Compare January 29, 2025 10:53
@maxreichmann maxreichmann force-pushed the detect-globals-in-formatters-js branch 2 times, most recently from d11d061 to 6d14323 Compare January 30, 2025 15:10
@maxreichmann maxreichmann force-pushed the detect-globals-in-formatters-js branch from 6d14323 to 6c9a169 Compare January 30, 2025 15:16
Comment on lines 18 to 26
{
column: 48,
fatal: true,
line: 5,
message: 'Value of property \'formatter\' is of type \'string\'.',
messageDetails: 'Do not use strings for \'formatter\' values in JavaScript and TypeScript. See Best Practices for Developers (https://ui5.sap.com/#/topic/28fcd55b04654977b63dacbee0552712)',
ruleId: 'parsing-error',
severity: 2,
},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@flovogt @codeworrior This snapshot shows the detection of formatter properties that have a value of type string in bindings (regardless if strings or object literals). This is not a deprecation, but a helpful hint for devs, as the runtime cannot resolve this declaration even with a formatter import.

For the time being, I have thrown a parsing error for such detections because I could not find a more suitable ruleId.

What do you think? Should we design a new ruleId for this? e.g.: "no-strings-in-formatter-properties" ? However, this would be very specific and not fit into our naming conventions for ruleIds.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, take a look at my comment here: #499 (comment)

Copy link
Member
@codeworrior codeworrior Jan 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This snapshot shows the detection of formatter properties that have a value of type string in bindings (regardless if strings or object literals).

I struggle with the "regardless". In binding strings, a formatter string IS supported by the runtime, in binding objects it IS NOT.

Therefore, complaining about a use of strings in a binding object does NOT fall into the category "worked in UI5 1.x, but no longer works in UI5 2.x" and therefore is not in the current focus of the linter. It also does not fall into the category of "no-deprecated-api" as it never worked. It's rather a "unsupported-api-usage" oder "illegal-argument" or whatever.

In binding strings, it's different.

@maxreichmann maxreichmann marked this pull request as ready for review January 30, 2025 15:28
@maxreichmann maxreichmann requested a review from a team January 30, 2025 15:28
@maxreichmann maxreichmann changed the title feat(Formatter): Detect globals in formatter bindings (JS/TS) feat(Formatter): Detect globals and strings in formatter bindings (JS/TS) Jan 30, 2025
@maxreichmann maxreichmann changed the title feat(Formatter): Detect globals and strings in formatter bindings (JS/TS) feat(Formatter): Detect globals and strings of formatters in bindings (JS/TS) Jan 31, 2025
// Although the formatter property does not look like a global notation,
// it should still be detected if it is a string:
const input2 = new Input({
value: "{ path: 'invoice>Status', formatter: 'formatter.statusText' }"
Copy link
Contributor
@d3xter666 d3xter666 Jan 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, it's clear that there cannot be fromatter like this:
"{ path: 'invoice>Status', formatter: formatter.statusText }", (no quotes around formatter.statusText) because the BindingParser/ExpressionParser will throw an exception.
Therefore, the current state is the only possible way to define this.

Why do we need to report this case?
https://github.com/SAP/ui5-linter/pull/499/files#diff-f32c91d608e9b3542dbe158a009067f34d4ccdc04b59ab81039bcb497db55703R80

The only reasonable answer would be that the formatter will not be supported when defining a string binding.

@codeworrior do I miss something here?

Copy link
Member
@codeworrior codeworrior Jan 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid misunderstandings, we're talking about the following case with quotes around the formatter value and where formatter is the import name of the corresponding module:

sap.ui.define(["some/formatter", ...], function(formatter, ...) {
    new Input({
        value: "{ path: 'invoice>Status', formatter: 'formatter.statusText' }"
    });
});

This does NOT work at runtime. The local variable formatter (introduced by the sap.ui.define call) is visible only within the scope of the module factory function. The framework code (which parses the binding string) is in a different scope and does not know this variable nor does it have access to it. So, for the framework code, 'formatter.statusText' looks like any other global name and that's why it should be reported.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggested to add this as a fixture mainly for ourselves, so that we don't think this would be a valid usage of a formatter in a binding string.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only reasonable answer would be that the formatter will not be supported when defining a string binding.

If you change this slightly to

The only reasonable recommendation would be that the formatter will no longer be supported in UI5 2.0 when defining a string binding.

then I agree.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, I get it! Thanks a lot!

Comment on lines +18 to +25
{
column: 48,
line: 6,
message: 'Do not use strings for \'formatter\' values in JavaScript and TypeScript.',
messageDetails: 'See Best Practices for Developers (https://ui5.sap.com/#/topic/28fcd55b04654977b63dacbee0552712)',
ruleId: 'unsupported-api-usage',
severity: 2,
},
Copy link
Member Author
@maxreichmann maxreichmann Jan 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@flovogt Please review this rule.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fine for me. @KlattG, your opinion?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines +58 to +81
{
column: 11,
line: 6,
message: 'Access of global variable \'ui5\' (ui5.walkthrough.model.formatter.statusText)',
messageDetails: 'Do not use global variables to access UI5 modules or APIs. See Best Practices for Developers (https://ui5.sap.com/#/topic/28fcd55b04654977b63dacbee0552712)',
ruleId: 'no-globals',
severity: 2,
},
{
column: 11,
line: 9,
message: 'Access of global variable \'ui5\' (ui5.walkthrough.model.formatter.statusText)',
messageDetails: 'Do not use global variables to access UI5 modules or APIs. See Best Practices for Developers (https://ui5.sap.com/#/topic/28fcd55b04654977b63dacbee0552712)',
ruleId: 'no-globals',
severity: 2,
},
{
column: 11,
line: 15,
message: 'Access of global variable \'formatter\' (formatter.statusText)',
messageDetails: 'Do not use global variables to access UI5 modules or APIs. See Best Practices for Developers (https://ui5.sap.com/#/topic/28fcd55b04654977b63dacbee0552712)',
ruleId: 'no-globals',
severity: 2,
},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 58-81:
These findings are the result of detecting strings for formatter values inside a Binding String (previously: "parsing-error", now: "no-globals").

Copy link
Contributor
@d3xter666 d3xter666 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@maxreichmann maxreichmann merged commit 291ffed into main Jan 31, 2025
15 checks passed
@maxreichmann maxreichmann deleted the detect-globals-in-formatters-js branch January 31, 2025 14:04
@openui5bot openui5bot mentioned this pull request Jan 31, 2025
[
{
coverageInfo: [],
errorCount: 5,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When looking at the file I would expect 3 findings. To me it looks like there are two duplicate findings.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As aligned with @codeworrior and approved by @flovogt, we want to detect two cases here:

  1. If the formatter property is of type string inside a BindingString
  2. If a global notation of the formatter property was used (here: "ui5.walkthrough...")

Since both of these cases apply for this file, there are two findings each.
Nonetheless, we've decided to use the same linter message.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both cases should cause a finding, but as they originate from the same place in the code, there is only one finding to be solved. So only one should be reported.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should adjust the linter messages to better emphasize the difference. But both cases should still be reported and not just one imo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
0