generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Check event handlers in XML views/fragments
- Detect usage of globals - Detect ambiguous event handlers (not starting with a dot '.') JIRA: CPOUI5FOUNDATION-974 JIRA: CPOUI5FOUNDATION-951
- Loading branch information
Showing
20 changed files
with
602 additions
and
57 deletions.
There are no files selected for viewing
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,3 @@ | ||
export default abstract class EventHandlerResolver { | ||
static parse(sValue: string): string[]; | ||
} |
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,109 @@ | ||
/* | ||
* This is a copy of the sap/ui/core/mvc/EventHandlerResolver.js module from OpenUI5. | ||
* https://github.com/SAP/openui5/blob/83fd99ee17fddff1616577b9acaa1e0cc0ed412a/src/sap.ui.core/src/sap/ui/core/mvc/EventHandlerResolver.js | ||
* | ||
* It only contains the "parse" function, as all other functionality is not needed. | ||
*/ | ||
|
||
/* eslint-disable */ | ||
/* eslint-enable no-undef */ | ||
|
||
import JSTokenizer from "./JSTokenizer.js"; | ||
|
||
// Provides module sap.ui.core.mvc.EventHandlerResolver. | ||
// sap.ui.define([ | ||
// "sap/ui/base/BindingParser", | ||
// "sap/ui/core/CommandExecution", | ||
// "sap/ui/model/BindingMode", | ||
// "sap/ui/model/CompositeBinding", | ||
// "sap/ui/model/json/JSONModel", | ||
// "sap/ui/model/base/ManagedObjectModel", | ||
// "sap/base/util/JSTokenizer", | ||
// "sap/base/util/resolveReference", | ||
// "sap/base/future", | ||
// "sap/ui/base/DesignTime" | ||
// ], | ||
// function( | ||
// BindingParser, | ||
// CommandExecution, | ||
// BindingMode, | ||
// CompositeBinding, | ||
// JSONModel, | ||
// MOM, | ||
// JSTokenizer, | ||
// resolveReference, | ||
// future, | ||
// DesignTime | ||
// ) { | ||
// "use strict"; | ||
|
||
var EventHandlerResolver = { | ||
|
||
/** | ||
* Parses and splits the incoming string into meaningful event handler definitions | ||
* | ||
* Examples: | ||
* | ||
* parse(".fnControllerMethod") | ||
* => [".fnControllerMethod"] | ||
* | ||
* parse(".doSomething('Hello World'); .doSomething2('string'); globalFunction") | ||
* => [".doSomething('Hello World')", ".doSomething2('string')", "globalFunction"] | ||
* parse(".fnControllerMethod; .fnControllerMethod(${ path:'/someModelProperty', formatter: '.myFormatter', type: 'sap.ui.model.type.String'} ); globalFunction") | ||
* => [".fnControllerMethod", ".fnControllerMethod(${ path:'/someModelProperty', formatter: '.myFormatter', type: 'sap.ui.model.type.String'} )", "globalFunction"] | ||
* | ||
* @param {string} [sValue] - Incoming string | ||
* @return {string[]} - Array of event handler definitions | ||
*/ | ||
parse: function parse(sValue) { | ||
sValue = sValue.trim(); | ||
var oTokenizer = new JSTokenizer(); | ||
var aResult = []; | ||
var sBuffer = ""; | ||
var iParenthesesCounter = 0; | ||
|
||
oTokenizer.init(sValue, 0); | ||
for (;;) { | ||
var sSymbol = oTokenizer.next(); | ||
if ( sSymbol === '"' || sSymbol === "'" ) { | ||
var pos = oTokenizer.getIndex(); | ||
oTokenizer.string(); | ||
sBuffer += sValue.slice(pos, oTokenizer.getIndex()); | ||
sSymbol = oTokenizer.getCh(); | ||
} | ||
if ( !sSymbol ) { | ||
break; | ||
} | ||
switch (sSymbol) { | ||
case "(": | ||
iParenthesesCounter++; | ||
break; | ||
case ")": | ||
iParenthesesCounter--; | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
if (sSymbol === ";" && iParenthesesCounter === 0) { | ||
aResult.push(sBuffer.trim()); | ||
sBuffer = ""; | ||
} else { | ||
sBuffer += sSymbol; | ||
} | ||
} | ||
|
||
if (sBuffer) { | ||
aResult.push(sBuffer.trim()); | ||
} | ||
|
||
return aResult; | ||
} | ||
}; | ||
|
||
// return EventHandlerResolver; | ||
// } | ||
// ); | ||
|
||
export default EventHandlerResolver; |
21 changes: 21 additions & 0 deletions
21
test/fixtures/linter/rules/NoGlobals/GlobalEventHandlers.view.xml
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,21 @@ | ||
<mvc:View xmlns="sap.m" xmlns:mvc="sap.ui.core.mvc"> | ||
|
||
<!-- Global event handler --> | ||
<Button press="ui5.walkthrougth.utils.MyHelper.onPressFancyButton" /> | ||
|
||
<!-- Global event handler (with arguments) --> | ||
<Button press="ui5.walkthrougth.utils.MyHelper.onPressFancyButton(0, ${i18n>TEXT}, $controller.foo.bar, $event)" /> | ||
|
||
<!-- Multiple global event handler --> | ||
<Button press="global.doSomething; ui5.walkthrougth.utils.MyHelper.onPressFancyButton(0, ${i18n>TEXT}, $controller.foo.bar, $event); global.doSomethingElse" /> | ||
|
||
<!-- Local event handler without leading dot, which could also be a global reference --> | ||
<Button press="onPressFancyButton" /> | ||
|
||
<!-- Local event handler without leading dot, which could also be a global reference (with arguments) --> | ||
<Button press="onPressFancyButton(0, ${i18n>TEXT}, $controller.foo.bar, $event)" /> | ||
|
||
<!-- Mixed: local, local without leading dot, command execution and global handler --> | ||
<Button press="onPressFancyButton(0, ${i18n>TEXT}, $controller.foo.bar, $event); .onPressFancyButton; cmd:Save; global.doSomething($event, $controller)" /> | ||
|
||
</mvc:View> |
24 changes: 24 additions & 0 deletions
24
test/fixtures/linter/rules/NoGlobals/GlobalEventHandlers_Negative.view.xml
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,24 @@ | ||
<mvc:View xmlns="sap.m" xmlns:mvc="sap.ui.core.mvc" xmlns:core="sap.ui.core"> | ||
|
||
<!-- Local event handler --> | ||
<Button press=".onPressFancyButton" /> | ||
|
||
<!-- Local event handler (nested) --> | ||
<Button press=".eventHandlers.onPressFancyButton" /> | ||
|
||
<!-- Local event handler (with arguments) --> | ||
<Button press=".onPressFancyButton(0, ${i18n>LABEL_ALL}, $controller.foo.bar, $event)" /> | ||
|
||
<!-- Command Execution --> | ||
<Button press="cmd:Save" /> | ||
|
||
<!-- Empty event handler --> | ||
<Button press="" /> | ||
|
||
<!-- Usage of event handler imported via core:require helper --> | ||
<Button core:require='{ "MyHelper": "ui5/walkthrougth/utils/MyHelper" }' press="MyHelper.onPressFancyButton" /> | ||
|
||
<!-- Usage of event handler function imported via core:require --> | ||
<Button core:require='{ "doSomething": "ui5/walkthrougth/utils/doSomething" }' press="doSomething" /> | ||
|
||
</mvc:View> |
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
Oops, something went wrong.