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

JS: Strip trailing/leading whitespace in externs (+ warn) #589

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
18 changes: 18 additions & 0 deletions effekt/shared/src/main/scala/effekt/generator/js/Transformer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,24 @@ trait Transformer {
}
}.toString()

// Externs
// -------
def normalizeExternStrings(l: List[String])(using Context): List[String] = l match {
case Nil => Nil
case List(s) =>
if (s.matches("""^\s.*""") || s.matches(""".*\s$""")) {
Context.warning("Extern string in js has trailing / leading whitespace. This will be removed.")
}
List(s.strip())
case l =>
val start = l.head
val end = l.last
val mid = l.tail.init
if (start.matches("""^\s.*""") || end.matches(""".*\s$""")) {
Context.warning("Extern string in js has trailing / leading whitespace. This will be removed.")
}
start.stripLeading() +: mid :+ end.stripTrailing()
}

// Separate Compilation (Website)
// ------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ object TransformerDirect extends Transformer {
js.RawStmt(contents)
}

def toJS(t: Template[Pure])(using TransformerContext): js.Expr = js.RawExpr(t.strings, t.args.map(toJS))
def toJS(t: Template[Pure])(using TransformerContext): js.Expr =
js.RawExpr(normalizeExternStrings(t.strings), t.args.map(toJS))

def toJS(b: core.Block)(using C: TransformerContext): js.Expr = b match {
// [[ f ]] = f
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ trait TransformerMonadic extends Transformer {
}

def toJS(t: Template[Pure])(using DeclarationContext, Context): js.Expr =
js.RawExpr(t.strings, t.args.map(toJS))
js.RawExpr(normalizeExternStrings(t.strings), t.args.map(toJS))

def toJS(b: core.Block)(using DeclarationContext, Context): js.Expr = b match {
case BlockVar(v, _, _) =>
Expand Down