Incorrect Javscript Highlighting?

Hi,

I am not that experienced with Javascript, that's why I am asking here
before filing a bug report.

You can download the Javascript file I am talking about here:
<http://dev.rubyonrails.org/file/spinoffs/scriptaculous/lib/prototype.js
?format=txt>

...
Object.extend(Event, {
...

// find the first node with the given tagName, starting from the
// node the event was triggered on; traverses the DOM upwards
findElement: function(event, tagName) {
var element = Event.element(event); <<--- line 892
while (element.parentNode && (!element.tagName ||
(element.tagName.toUpperCase() != tagName.toUpperCase())))
element = element.parentNode;
return element;
},

...

observe: function(element, name, observer, useCapture) {
var element = $(element); <<--- line 922
useCapture = useCapture || false;

if (name == 'keypress' &&
((navigator.appVersion.indexOf('AppleWebKit') > 0)
|| element.attachEvent))
name = 'keydown';

this._observeAndCache(element, name, observer, useCapture);
},
...

The second occurence of var element is marked as duplicate declaration.
As I understand the code, each of those name: function blocks declares a
function that should have its own scope.

Is this a JS inspection bug or is it indeed an error in the file itself?

Best,

Dirk Dittert

9 comments
Comment actions Permalink

Dirk Dittert wrote:

observe: function(element, name, observer, useCapture) {
var element = $(element); <<--- line 922


I have no clue if "var element = $(element);" is legal, but variables that are declared in
different functions certainly aren't duplicates. I think you should file a bug.

Sascha

0
Comment actions Permalink

Sascha Weinreuter wrote:

Dirk Dittert wrote:

>> observe: function(element, name, observer, useCapture) {
>> var element = $(element); <<--- line 922


I have no clue if "var element = $(element);" is legal,

If you scroll a little in that file, you'll see the '$' is a function,
so "$(element)" it's a legal a function call.

-


function $() {
var elements = new Array();

for (var i = 0; i < arguments.length; i++) {
var element = arguments+;
if (typeof element == 'string')
element = document.getElementById(element);

if (arguments.length == 1)
return element;

elements.push(element);
}

return elements;
}
-



Ahmed.

0
Comment actions Permalink

$ is just a valid function name. It's now a very common idiom in
JavaScript world to use this function instead of getElementById. In
Prototype library (the one, Dirk is referring to), almost every function
argument is internally wrapped into $() call. Like:

Object.extend(Element,
},
}

So you can write

Element.hide("myButton")

instead of

Element.hide(document.getElementById("myButton"))

Some libraries go even further. They use $ for locating elements by
name, class and tag. Like DOM::Utils from JSAN
(http://www.openjsan.org/doc/r/rk/rkinyon/DOM/Utils/0.01/lib/DOM/Utils.html).

I'm pretty sure someone will eventually hook getElementsBySelector
(http://simon.incutio.com/archive/2003/03/25/getElementsBySelector) to
this, so you can write something like

Element.fade(".new")

0
Comment actions Permalink

Mike Aizatsky (JetBrains) <mike.aizatsky@jetbrains.com> wrote:

$ is just a valid function name. It's now a very common idiom in
JavaScript world to use this function instead of getElementById. In
Prototype library (the one, Dirk is referring to), almost every function
argument is internally wrapped into $() call. Like:

Object.extend(Element, {
hide: function() {
for (var i = 0; i < arguments.length; i++) {
var element = $(arguments+);
element.style.display = 'none';
}
},
}

So you can write

Element.hide("myButton")

instead of

Element.hide(document.getElementById("myButton"))


Thanks for the explanation, Mike. So, is this IDEA right warning about a
duplicate declaration or should I file a bug for it?

Best,

Dirk Dittert

0
Comment actions Permalink

It's a bug indeed. Please file the issue.

Thanks

-


Maxim Shafirov
http://www.jetbrains.com
"Develop with pleasure!"


0
Comment actions Permalink

Maxim Shafirov <max@jetbrains.com> wrote:

It's a bug indeed. Please file the issue.


http://www.jetbrains.net/jira/browse/IDEA-4147

0
Comment actions Permalink

Sorry, it seems I was blind. There's indeed error in the code, at least it
was intended to highlight just like these: one is declaring a new variable
which has the same name as function parameter.
{
..
getValue: function(element) {
var element = $(element);
}
..
}

function parameter is named element and new var is also named element.

Is that allowed in JS?

-


Maxim Shafirov
http://www.jetbrains.com
"Develop with pleasure!"




0
Comment actions Permalink

According to specification there's even no error if you declare a
variable with the same name twice (even more: you can have several
function parameters with the same name!). All variables/function
parameters are basically properties of the context object, and it
doesn't hurt to define variable once more.

In this case I think IDEA should give warning instead of error.

PS Just a small testcase to be 100% sure:

function foo(f) { var f = 1; var f = "hello"; alert(f); } foo(9999);

0
Comment actions Permalink

It is already warning, not an error.
-


Maxim Shafirov
http://www.jetbrains.com
"Develop with pleasure!"


0

Please sign in to leave a comment.