[aklug] Re: Removing newlines from minified javascript

From: Tim Johnson <tim@akwebsoft.com>
Date: Wed Aug 08 2012 - 11:05:23 AKDT

* Michael Fowler <michael@shoebox.net> [120808 09:56]:
> Assuming you're correct in that semicolons have been added between all
> statements, then blind removing all newlines will not generate syntax
> errors.
>
> However, it may generate logical errors. Quoted strings in the source
> may have embedded newlines, and removing them will change the meaning of
> the code. You would need a parser that understands the actual
> JavaScript to remove the newlines (or, build a syntax tree, then
> essentially compile to a newline-free chunk of code). Thus, this is
> best done in the minifier itself, because it (presumably) understands
> JavaScript.
 ... So nice to get a reply on this.
 
 FYI: I am a former C programmer, but years removed. C *should*
 recognize a newline as ascii 10. C would also recognize an escaped
 newline as '\n' or ascii 92, ascii 110 (I think).
 
 As far as I know jslint will throw an error on an actual embedded
 newline as "Unclosed string". I have thought about patching jsmin.
 I note that I mispoke about jsmin's handling of newlines. It
 actually appears to remove about 90% of newlines.

> Given weird enough quoting rules, a lack of syntax errors is no
> guarantee, either. I'm not sure what JavaScript's quoting rules are; if
> it allows backslash to escape a newline, then removing the newline will
> leave the backslash to escape something else. That may generate a
> syntax error.
>
> You may be able to fake it with something that understands JavaScript's
> quoting rules (e.g. escape with \, quote with ", etc.), but that will
> most likely lead to a false negative or a false positive at some point.
  I have assimilated jsmin into my framework. I'm going to write a
  small amount of code that can be easily removed which will convert
  remaining newlines to spaces and see how that works. I have a
  project not yet deployed that will serve as a test platform.

  I will report the results.

> The best approach is to patch jsmin itself.
  Probably not worth the effort. And I will bet that Crockford is
  smarter than I am and I will end up dropping the removal of
  remaining newlines. He has probably good reason.

  l8tr (and thanks again)

-- 
Tim 
tim at tee jay forty nine dot com or akwebsoft dot com
http://www.akwebsoft.com
---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.
Received on Wed Aug 8 11:05:31 2012

This archive was generated by hypermail 2.1.8 : Wed Aug 08 2012 - 11:05:31 AKDT