Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid fall through in keywordTransform #164

Merged
merged 1 commit into from
Sep 20, 2021
Merged

Conversation

lrowe
Copy link
Contributor

@lrowe lrowe commented Sep 19, 2021

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
}
}

}

Copy link
Collaborator

@tjvr tjvr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Smart! Thanks 🙏

@tjvr tjvr merged commit d8a76f7 into no-context:master Sep 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants