rspec/rules/S1462/flex/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

75 lines
1.5 KiB
Plaintext

== Why is this an issue?
Using plain string event names in even listeners is an anti-pattern; if the event is renamed, the application can start behaving unexpectedly. A constant variable should be used instead.
=== Noncompliant code example
[source,flex]
----
import flash.display.Sprite;
import flash.events.MouseEvent;
class ChildSprite extends Sprite
{
public function ChildSprite()
{
...
addEventListener("CustomEvent", clickHandler); // Noncompliant
}
}
function clickHandler(event:CustomEvent):void
{
trace("clickHandler detected an event of type: " + event.type);
trace("the this keyword refers to: " + this);
}
----
=== Compliant solution
[source,flex]
----
import flash.display.Sprite;
import flash.events.MouseEvent;
class ChildSprite extends Sprite
{
public const CUSTOM_EVENT:String = "CustomEvent";
public function ChildSprite()
{
...
addEventListener(CUSTOM_EVENT, clickHandler);
}
}
function clickHandler(event:CustomEvent):void
{
trace("clickHandler detected an event of type: " + event.type);
trace("the this keyword refers to: " + this);
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
The event name "XXXX" should be defined in a constant variable.
'''
== Comments And Links
(visible only on this page)
=== on 21 Nov 2013, 10:36:29 Freddy Mallet wrote:
Is implemented by \http://jira.codehaus.org/browse/SONARPLUGINS-3271
endif::env-github,rspecator-view[]