Shazwazza / ClientDependency

DEPRECATED. A library for managing CSS & JavaScript dependencies and optimization in ASP.Net
139 stars 65 forks source link

Minification produces incorrect JS if line ends with a regex with no trailing semi-colons #118

Closed ryanlewis closed 2 years ago

ryanlewis commented 7 years ago

The following valid JavaScript produces invalid minified code (note there's no trailing semi-colon):

const regex = /abc/
console.log('hello world')

In my testing this returns

const regex=/abc/console.log('hello world')

This throws an error about incorrect regex flags.

While use of semi-colons in JS is a religous issue, we still don't want to produce invalid JavaScript here. I propose a trailing semi-colon is added to a line if it ends with a slash.

There may be other cases where this is an issue.

niswilsonnissen commented 5 years ago

We have encountered this issue as well with the date-fns library served through the ClientDependency handler (see parse/index.js in the date-fns package).

var isDate = require('../is_date/index.js')

var MILLISECONDS_IN_HOUR = 3600000
var MILLISECONDS_IN_MINUTE = 60000
var DEFAULT_ADDITIONAL_DIGITS = 2

var parseTokenDateTimeDelimeter = /[T ]/
var parseTokenPlainTime = /:/

// year tokens
var parseTokenYY = /^(\d{2})$/
var parseTokensYYY = [
  /^([+-]\d{2})$/, // 0 additional digits
  /^([+-]\d{3})$/, // 1 additional digit
  /^([+-]\d{4})$/ // 2 additional digits
]

var parseTokenYYYY = /^(\d{4})/
var parseTokensYYYYY = [
  /^([+-]\d{4})/, // 0 additional digits
  /^([+-]\d{5})/, // 1 additional digit
  /^([+-]\d{6})/ // 2 additional digits
]

How does people workaround this issue?