Lucee "switch" statement corrupts output when "default" case not last

Description

Lucee does not process switch statements the same way ACF does in regards to the "default" statement. Take the following code:

function switchTest(required numeric input){ switch(arguments.input){ case 1: return "item 1"; case 2: default: return "item 2"; case 3: return "item 3"; case 4: return "item 4"; } } writeOutput("<div>1 = #switchTest(1)#</div>"); writeOutput("<div>2 = #switchTest(2)#</div>"); writeOutput("<div>3 = #switchTest(3)#</div>"); writeOutput("<div>4 = #switchTest(4)#</div>"); writeOutput("<div>-1 = #switchTest(-1)#</div>");

If you run this under ACF, you get the following results:

1 = item 1 2 = item 2 3 = item 3 4 = item 4 -1 = item 2

Which is what I'd expect.

However, Lucee 4.2 - Lucee 5.3 this produces:

1 = item 1 2 = item 3 3 = item 3 4 = item 4 -1 = item 2

The value for 2 ends up showing item 3 where I'd expect it to show item 2.

This works properly if you move the case 2:/default: block to the end of the switch statement:

Here's a Gist which illustrates the bug:
https://www.trycf.com/gist/ab87d523b5c7ee657c3ba39e5225e5e7/acf11?theme=monokai

Activity

Show:

Pothys - MitrahSoft 15 September 2020 at 15:50

I've checked this ticket and confirmed the issue happened on lucee latest version 5.3.8.72-SNAPSHOT also. And added a test case for this ticket too.

Pull Request: https://github.com/lucee/Lucee/pull/1037

Dan Switzer, II 14 September 2020 at 17:52

It’s probably worth adding that I’m not aware of any spec that requires the {{default}} statement to be the last statement. ECMAScript does not require the statement to be last:

function switchTest(input){ switch(input){ case 1: return "item 1"; case 2: default: return "item 2"; case 3: return "item 3"; case 4: return "item 4"; } } function writeOutput(statement){ document.write("<div>" + statement + "</div>"); } writeOutput("1 = " + switchTest(1)); writeOutput("2 = " + switchTest(2)); writeOutput("3 = " + switchTest(3)); writeOutput("4 = " + switchTest(4)); writeOutput("-1 = " + switchTest(-1));

This produces the same output as ACF:

1 = item 1
2 = item 2
3 = item 3
4 = item 4
-1 = item 2

Details

Assignee

Reporter

New Issue warning screen

Before you create a new Issue, please post to the mailing list first https://dev.lucee.org

Once the issue has been verified, one of the Lucee team will ask you to file an issue

Affects versions

Priority

Created 14 September 2020 at 17:43
Updated 23 July 2022 at 10:11

Flag notifications