Bug 298654 - [language support] Closures give warning/error if used in function before its definition
Summary: [language support] Closures give warning/error if used in function before its...
Status: NEW
Alias: None
Product: JSDT
Classification: WebTools
Component: General (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows 7
: P3 normal (vote)
Target Milestone: Future   Edit
Assignee: wst.javascript CLA
QA Contact: Chris Jaun CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-12-30 14:15 EST by Laurens Holst CLA
Modified: 2013-06-19 11:10 EDT (History)
1 user (show)

See Also:


Attachments
Testcase (1.22 KB, text/javascript)
2009-12-30 14:15 EST, Laurens Holst CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Laurens Holst CLA 2009-12-30 14:15:25 EST
Created attachment 155139 [details]
Testcase

Using Eclipse 3.6.0M4 and WTP 3.2.0M4.

Closures are handled differently depending on whether a function declaration or a function expression is used. See the attached testcase, where three functionally equivalent variations on the same code yield different validation results.

Instead, these examples should not give any error nor warning. The scope of a local variable is function-wide.

The error message test B and C give may seem reasonable, however in my opinion this is a legitimate code pattern which the user should not receive a warning for. See for example [1][2]. Not allowing this would unnecessarily restrict the order in which code is defined, even make some code impossible to write without warnings.

Also note that in tests A, B and C, if the closure variable is a class which you ‘new’, you will also not get a warning. Perhaps due to the fix for bug 287186. This exception in the behaviour supports my argument for not having a warning, as this is an extremely common pattern and would be a pain in the ass if it did trigger warnings.

[1] http://hg.grauw.nl/grauw-lib/file/c82b80b24caa/src/selector.js#l92
[2] http://hg.grauw.nl/grauw-lib/file/c82b80b24caa/src/selector.js#l1388