mozilla/source-map — pull request #116 by jgoz (2014-06-21 07:23:22+00:00)
Title: Consecutive newline bug

In situations where a string sub-node contains multiple consecutive newline characters, all following `SourceNode`s with line/column values will be offset in the generated source-map.

This is due to [valid behaviour](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#Example.3A_Finding_successive_matches) in JS where a `RegExp` with the `g` flag will track its position during successive calls to `test` (and `exec`) for the same string. For example, the following behaviour is valid:

```js
var r = /\n/g;
console.log(r.test("\n")); // => true
console.log(r.test("\n")); // => false
```

A similar check is made in [`toStringWithSourceMap`](https://github.com/mozilla/source-map/blob/master/lib/source-map/source-node.js#L365). For `chunk` values that end in consecutive newline characters, every other newline will *not* increment `generated.line`.

There are a few solutions:

1. Remove the `g` flag — not possible because the RegExp object is used for other purposes in the file
2. Create a new RegExp for matching (or use an inline regex) without the flag
3. Reset the index after every match

I opted for # 3, as it avoids duplicating patterns and is unlikely to be refactored out in the future.

Example:

```js
var r = /\n/g;
console.log(r.test("\n")); // => true
r.lastIndex = 0;
console.log(r.test("\n")); // => true
```

(I discovered this bug while diagnosing webpack/webpack#318.)