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 1 commit
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
11 changes: 11 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,17 @@ trait Transformer {
}
}.toString()

// Externs
// -------
def normalizeExternStrings(l: List[String])(using Context): List[String] = {
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.")
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this warning can be very annoying, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It could be - we're not using it anywhere yet, so...
I can also remove it.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is the perfect place for a warning, tbh. It will fire only on new code that is potentially problematic. :)

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