no-context / moo

Optimised tokenizer/lexer generator! 🐄 Uses /y for performance. Moo.
BSD 3-Clause "New" or "Revised" License
824 stars 66 forks source link

Avoid fall through in keywordTransform #164

Closed lrowe closed 3 years ago

lrowe commented 3 years ago

The function generated by keywordTransform provides does not include switch defaults so a non-keyword that is of the same length as a keyword will be checked against all possible keyword values longer than it.

With the existing output a non keyword like 'x' is checked against both case "a" and case "bb":

//> console.log(moo.keywords({KW: ['a', 'bb'] }).toString())
function anonymous(value
) {
switch (value.length) {
case 1:
switch (value) {
case "a": return "KW"
}
case 2:
switch (value) {
case "bb": return "KW"
}
}

}

With the changes in this commit it is only checked against case "a".

//> console.log(moo.keywords({KW: ['a', 'bb'] }).toString())
function anonymous(value
) {
switch (value.length) {
case 1:
switch (value) {
case "a": return "KW"
default: return
}
case 2:
switch (value) {
case "bb": return "KW"
default: return
}
}

}