Skip to content

Commit

Permalink
Ensure reserved names in params or query don't break ReScript
Browse files Browse the repository at this point in the history
The codegen tool did not yet account for reserved keywords when
generating code. This caused a query parameter such as `type` to
generate code that would refuse to compile.

To fix this a list of reserved keywords are added that get an underscore
added as suffix.

Fixes #30
  • Loading branch information
Kingdutch committed Jul 19, 2022
1 parent 8fa91e1 commit 0731944
Showing 1 changed file with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,41 @@ let wrapInOpt = str => `option<${str}>`
// "builtins" we use, like "environment". This helps handle that by checking for
// collisions, letting us refer to safe names where needed.
module SafeParam = {
type t = Actual(string) | CollisionPrevented({realKey: string, collisionProtectedKey: string})
type t =
| Actual(string)
| CollisionPrevented({realKey: string, collisionProtectedKey: string})
| RescriptKeyword({realKey: string, collisionProtectedKey: string})

let protectedNames = ["environment", "pathParams", "queryParams", "location"]
// As per https://rescript-lang.org/docs/manual/latest/reserved-keywords
let reservedKeywords = [
"and",
"as",
"assert",
"constraint",
"else",
"exception",
"external",
"false",
"for",
"if",
"in",
"include",
"lazy",
"let",
"module",
"mutable",
"of",
"open",
"rec",
"switch",
"true",
"try",
"type",
"when",
"while",
"with",
]

type paramType = Param(string) | QueryParam(string)

Expand All @@ -18,12 +50,16 @@ module SafeParam = {
| QueryParam(paramName) =>
if params->Js.Array2.includes(paramName) || protectedNames->Js.Array2.includes(paramName) {
CollisionPrevented({realKey: paramName, collisionProtectedKey: "queryParam_" ++ paramName})
} else if reservedKeywords->Js.Array2.includes(paramName) {
RescriptKeyword({realKey: paramName, collisionProtectedKey: paramName ++ "_"})
} else {
Actual(paramName)
}
| Param(paramName) =>
if protectedNames->Js.Array2.includes(paramName) {
CollisionPrevented({realKey: paramName, collisionProtectedKey: "param_" ++ paramName})
} else if reservedKeywords->Js.Array2.includes(paramName) {
RescriptKeyword({realKey: paramName, collisionProtectedKey: paramName ++ "_"})
} else {
Actual(paramName)
}
Expand All @@ -32,12 +68,14 @@ module SafeParam = {

let getSafeKey = key =>
switch key {
| Actual(key) | CollisionPrevented({collisionProtectedKey: key}) => key
| Actual(key)
| CollisionPrevented({collisionProtectedKey: key})
| RescriptKeyword({collisionProtectedKey: key}) => key
}

let getOriginalKey = key =>
switch key {
| Actual(key) | CollisionPrevented({realKey: key}) => key
| Actual(key) | CollisionPrevented({realKey: key}) | RescriptKeyword({realKey: key}) => key
}
}

Expand Down

0 comments on commit 0731944

Please sign in to comment.