Skip to content

Remove unsafe string interpolation j #736

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

Merged
merged 2 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Blog.res
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ module BlogCard = {
| Some(category) =>
<>
{React.string(category)}
{React.string(j` · `)}
{React.string(` · `)}
</>
| None => React.null
}}
Expand Down
8 changes: 4 additions & 4 deletions src/DocsOverview.res
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ let default = (~showVersionSelect=true) => {
}

let languageManual = [
("Overview", j` /docs/manual/$version/introduction`),
("Language Features", j`/docs/manual/$version/overview`),
("JS Interop", j`/docs/manual/$version/embed-raw-javascript`),
("Build System", j`/docs/manual/$version/build-overview`),
("Overview", `/docs/manual/${version}/introduction`),
("Language Features", `/docs/manual/${version}/overview`),
("JS Interop", `/docs/manual/${version}/embed-raw-javascript`),
("Build System", `/docs/manual/${version}/build-overview`),
]

let ecosystem = [
Expand Down
16 changes: 9 additions & 7 deletions src/Playground.res
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ module ResultPane = {
<div className={"p-2 " ++ highlightClass}>
<span className=prefixColor> {React.string(prefixText)} </span>
<span className="font-medium text-gray-40">
{React.string(j` Line $row, column $column:`)}
{React.string(` Line ${row->Belt.Int.toString}, column ${column->Belt.Int.toString}:`)}
</span>
<AnsiPre className="whitespace-pre-wrap "> shortMsg </AnsiPre>
</div>
Expand Down Expand Up @@ -247,7 +247,7 @@ module ResultPane = {
"Formatting completed with 0 errors"
} else {
let toStr = Api.Lang.toString(toLang)
j`Switched to $toStr with 0 errors`
`Switched to ${toStr} with 0 errors`
}
<PreWrap> {React.string(msg)} </PreWrap>
| Conv(Fail({fromLang, toLang, details})) =>
Expand All @@ -268,11 +268,11 @@ module ResultPane = {
// We keep both cases though in case we change things later
let msg = if fromLang === toLang {
let langStr = Api.Lang.toString(toLang)
j`The code is not valid $langStr syntax.`
`The code is not valid ${langStr} syntax.`
} else {
let fromStr = Api.Lang.toString(fromLang)
let toStr = Api.Lang.toString(toLang)
j`Could not convert from "$fromStr" to "$toStr" due to malformed syntax:`
`Could not convert from "${fromStr}" to "${toStr}" due to malformed syntax:`
}
<div>
<PreWrap className="text-16 mb-4"> {React.string(msg)} </PreWrap>
Expand Down Expand Up @@ -310,7 +310,9 @@ module ResultPane = {
| Nothing =>
let syntax = Api.Lang.toString(targetLang)
<PreWrap>
{React.string(j`This playground is now running on compiler version $compilerVersion with $syntax syntax`)}
{React.string(
`This playground is now running on compiler version ${compilerVersion} with ${syntax} syntax`,
)}
</PreWrap>
}

Expand Down Expand Up @@ -1053,7 +1055,7 @@ module ControlPanel = {
}

module ShareButton = {
let copyToClipboard: string => bool = %raw(j`
let copyToClipboard: string => bool = %raw(`
function(str) {
try {
const el = document.createElement('textarea');
Expand Down Expand Up @@ -1406,7 +1408,7 @@ module App = {
// Feel free to play around and compile some
// ReScript code!

let initialReContent = j`Js.log("Hello Reason 3.6!");`
let initialReContent = `Js.log("Hello Reason 3.6!");`

let default = (~props: Try.props) => {
let router = Next.Router.useRouter()
Expand Down
2 changes: 1 addition & 1 deletion src/Try.res
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let default = (props: {"children": React.element}) => {
<>
<Meta title="ReScript Playground" description="Try ReScript in the browser" />
<Next.Head>
<style> {React.string(j`body { background-color: #010427; } `)} </style>
<style> {React.string(`body { background-color: #010427; }`)} </style>
</Next.Head>
<div className="text-16">
<div className="text-gray-40 text-14">
Expand Down
12 changes: 6 additions & 6 deletions src/bindings/RescriptCompilerApi.res
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module Lang = {
| "ml" => OCaml
| "re" => Reason
| "res" => Res
| other => raise(DecodeError(j`Unknown language "$other"`))
| other => raise(DecodeError(`Unknown language "${other}"`))
}
}
}
Expand Down Expand Up @@ -110,7 +110,7 @@ module LocMsg = {
| #E => "E"
}

j`[$prefix] Line $row, $column: $shortMsg`
`[${prefix}] Line ${row->Belt.Int.toString}, ${column->Belt.Int.toString}: ${shortMsg}`
}

// Creates a somewhat unique id based on the rows / cols of the locMsg
Expand Down Expand Up @@ -160,11 +160,11 @@ module Warning = {
| Warn({warnNumber, details})
| WarnErr({warnNumber, details}) =>
let {LocMsg.row: row, column, shortMsg} = details
let msg = j`(Warning number $warnNumber) $shortMsg`
let msg = `(Warning number ${warnNumber->Belt.Int.toString}) ${shortMsg}`
(row, column, msg)
}

j`[$prefix] Line $row, $column: $msg`
`[${prefix}] Line ${row->Belt.Int.toString}, ${column->Belt.Int.toString}: ${msg}`
}
}

Expand Down Expand Up @@ -295,7 +295,7 @@ module CompileFail = {
| "warning_flag_error" =>
let warningFlag = WarningFlag.decode(json)
WarningFlagErr(warningFlag)
| other => raise(DecodeError(j`Unknown type "$other" in CompileFail result`))
| other => raise(DecodeError(`Unknown type "${other}" in CompileFail result`))
}
}
}
Expand Down Expand Up @@ -336,7 +336,7 @@ module ConversionResult = {
| "syntax_error" =>
let locMsgs = field("errors", array(LocMsg.decode), json)
Fail({fromLang, toLang, details: locMsgs})
| other => Unknown(j`Unknown conversion result type "$other"`, json)
| other => Unknown(`Unknown conversion result type "${other}"`, json)
} catch {
| DecodeError(errMsg) => Unknown(errMsg, json)
}
Expand Down
10 changes: 5 additions & 5 deletions src/common/Ansi.res
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module Sgr = {
}
}

let esc = j`\u001B`
let esc = `\u001B`

let isAscii = (c: string) => Js.Re.test_(%re(`/[\x40-\x7F]/`), c)

Expand Down Expand Up @@ -324,7 +324,7 @@ module SgrString = {
}
let params = Belt.Array.map(e.params, Sgr.paramToString)->Js.Array2.joinWith(", ")

j`SgrString params: $params | content: $content`
`SgrString params: ${params} | content: ${content}`
}
}

Expand All @@ -338,14 +338,14 @@ module Printer = {
open Js.String2
replaceByRe(content, %re("/\n/g"), "\\n")->replace(esc, "")
}
j`Text "$content" ($startPos to $endPos)`
`Text "${content}" (${startPos->Belt.Int.toString} to ${endPos->Belt.Int.toString})`
| Sgr({params, raw, loc: {startPos, endPos}}) =>
let raw = Js.String2.replace(raw, esc, "")
let params = Belt.Array.map(params, Sgr.paramToString)->Js.Array2.joinWith(", ")
j`Sgr "$raw" -> $params ($startPos to $endPos)`
`Sgr "${raw}" -> ${params} (${startPos->Belt.Int.toString} to ${endPos->Belt.Int.toString})`
| ClearSgr({loc: {startPos, endPos}, raw}) =>
let raw = Js.String2.replace(raw, esc, "")
j`Clear Sgr "$raw" ($startPos to $endPos)`
`Clear Sgr "${raw}" (${startPos->Belt.Int.toString} to ${endPos->Belt.Int.toString})`
}

let plainString = (tokens: array<token>): string =>
Expand Down
29 changes: 15 additions & 14 deletions src/common/BlogApi.res
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ module RssFeed = {
Belt.Array.get(items, 0)
->Belt.Option.map(item => {
let latestPubDateStr = item.pubDate->dateToUTCString
j`<lastBuildDate>$latestPubDateStr</lastBuildDate>`
`<lastBuildDate>${latestPubDateStr}</lastBuildDate>`
})
->Belt.Option.getWithDefault("")

Expand All @@ -143,33 +143,34 @@ module RssFeed = {
->Js.Array2.map(({title, pubDate, description, href}) => {
let descriptionElement = switch description {
| "" => ""
| desc => j`<description>
<![CDATA[$desc]]>
| desc =>
`<description>
<![CDATA[${desc}]]>
</description>`
}

// TODO: convert pubdate to string
let dateStr = pubDate->dateToUTCString
j`
`
<item>
<title> <![CDATA[$title]]></title>
<link> $href </link>
<guid> $href </guid>
$descriptionElement
<pubDate>$dateStr</pubDate>
<title> <![CDATA[${title}]]></title>
<link> ${href} </link>
<guid> ${href} </guid>
${descriptionElement}
<pubDate>${dateStr}</pubDate>
</item>`
})
->Js.Array2.joinWith("\n")

let ret = j`<?xml version="1.0" encoding="utf-8" ?>
let ret = `<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
<channel>
<title>$siteTitle</title>
<title>${siteTitle}</title>
<link>https://rescript-lang.org</link>
<description>$siteDescription</description>
<description>${siteDescription}</description>
<language>en</language>
$latestPubDateElement
$itemsStr
${latestPubDateElement}
${itemsStr}
</channel>
</rss>` //rescript-lang.org</link>

Expand Down
4 changes: 2 additions & 2 deletions src/common/BlogFrontmatter.res
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ let decodeBadge = (str: string): Badge.t =>
| "testing" => Testing
| "preview" => Preview
| "roadmap" => Roadmap
| str => raise(Json.Decode.DecodeError(j`Unknown category "$str"`))
| str => raise(Json.Decode.DecodeError(`Unknown category "${str}"`))
}

exception AuthorNotFound(string)

let decodeAuthor = (~fieldName: string, ~authors, username) =>
switch Js.Array2.find(authors, a => a.username === username) {
| Some(author) => author
| None => raise(AuthorNotFound(j`Couldn't find author "$username" in field $fieldName`))
| None => raise(AuthorNotFound(`Couldn't find author "${username}" in field ${fieldName}`))
}

let authorDecoder = (~fieldName: string, ~authors, json) => {
Expand Down
2 changes: 1 addition & 1 deletion src/common/CompilerManagerHook.res
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module LoadScript = {
loadScript(
~src=url,
~onSuccess=() => resolve(. Ok()),
~onError=_err => resolve(. Error(j`Could not load script: $url`)),
~onError=_err => resolve(. Error(`Could not load script: ${url}`)),
)->ignore
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/common/Hooks.res
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Contains some generic hooks */
%%raw("import React from 'react'")

let useOutsideClick: (ReactDOM.Ref.t, unit => unit) => unit = %raw(j`(outerRef, trigger) => {
let useOutsideClick: (ReactDOM.Ref.t, unit => unit) => unit = %raw(`(outerRef, trigger) => {
function handleClickOutside(event) {
if (outerRef.current && !outerRef.current.contains(event.target)) {
trigger();
Expand All @@ -16,7 +16,7 @@ let useOutsideClick: (ReactDOM.Ref.t, unit => unit) => unit = %raw(j`(outerRef,
});
}`)

let useWindowWidth: unit => option<int> = %raw(j` () => {
let useWindowWidth: unit => option<int> = %raw(` () => {
const isClient = typeof window === 'object';

function getSize() {
Expand Down
2 changes: 1 addition & 1 deletion src/components/CodeExample.res
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module DomUtil = {
}

module CopyButton = {
let copyToClipboard: string => bool = %raw(j`
let copyToClipboard: string => bool = %raw(`
function(str) {
try {
const el = document.createElement('textarea');
Expand Down
6 changes: 3 additions & 3 deletions src/components/CodeMirror.res
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
This file is providing the core functionality and logic of our CodeMirror instances.
*/

let useWindowWidth: unit => int = %raw(j` () => {
let useWindowWidth: unit => int = %raw(` () => {
const isClient = typeof window === 'object';

function getSize() {
Expand Down Expand Up @@ -460,7 +460,7 @@ module GutterMarker = {
}

let (row, col) = rowCol
marker->setId(j`gutter-marker_$row-$col`)
marker->setId(`gutter-marker_${row->Belt.Int.toString}-${col->Belt.Int.toString}`)
marker->setClassName(
"flex items-center justify-center text-14 text-center ml-1 h-6 font-bold hover:cursor-pointer " ++
colorClass,
Expand Down Expand Up @@ -702,7 +702,7 @@ let make = // props relevant for the react wrapper
*/
let errorsFingerprint = Belt.Array.map(errors, e => {
let {Error.row: row, column} = e
j`$row-$column`
`${row->Belt.Int.toString}-${column->Belt.Int.toString}`
})->Js.Array2.joinWith(";")

React.useEffect1(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/LandingPageLayout.res
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ exports.Button = Button;`,

module QuickInstall = {
module CopyButton = {
let copyToClipboard: string => bool = %raw(j`
let copyToClipboard: string => bool = %raw(`
function(str) {
try {
const el = document.createElement('textarea');
Expand Down