Skip to content

Commit d6f17d6

Browse files
committed
rewrite regexp to split function
1 parent dccca06 commit d6f17d6

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

lib/ConcatSource.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class ConcatSource extends Source {
6161
if (Buffer.isBuffer(bufferOrString)) {
6262
buffers.push(bufferOrString);
6363
} else {
64+
// This will not happen
6465
buffers.push(Buffer.from(bufferOrString, "utf-8"));
6566
}
6667
}

lib/OriginalSource.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ const { getMap, getSourceAndMap } = require("./helpers/getFromStreamChunks");
88
const splitIntoLines = require("./helpers/splitIntoLines");
99
const getGeneratedSourceInfo = require("./helpers/getGeneratedSourceInfo");
1010
const Source = require("./Source");
11-
12-
const SPLIT_REGEX = /[^\n;{}]+[;{} \r\t]*\n?|[;{} \r\t]+\n?|\n/g;
11+
const splitIntoPotentialTokens = require("./helpers/splitIntoPotentialTokens");
1312

1413
class OriginalSource extends Source {
1514
constructor(value, name) {
@@ -61,7 +60,7 @@ class OriginalSource extends Source {
6160
const finalSource = !!(options && options.finalSource);
6261
if (!options || options.columns !== false) {
6362
// With column info we need to read all lines and split them
64-
const matches = this._value.match(SPLIT_REGEX);
63+
const matches = splitIntoPotentialTokens(this._value);
6564
let line = 1;
6665
let column = 0;
6766
if (matches !== null) {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// \n = 10
2+
// ; = 59
3+
// { = 123
4+
// } = 125
5+
// <space> = 32
6+
// \r = 13
7+
// \t = 9
8+
9+
const splitIntoPotentialTokens = str => {
10+
const len = str.length;
11+
if (len === 0) return null;
12+
const results = [];
13+
let i = 0;
14+
for (; i < len; ) {
15+
const s = i;
16+
block: {
17+
let cc = str.charCodeAt(i);
18+
while (cc !== 10 && cc !== 59 && cc !== 123 && cc !== 125) {
19+
if (++i >= len) break block;
20+
cc = str.charCodeAt(i);
21+
}
22+
while (
23+
cc === 59 ||
24+
cc === 32 ||
25+
cc === 123 ||
26+
cc === 125 ||
27+
cc === 13 ||
28+
cc === 9
29+
) {
30+
if (++i >= len) break block;
31+
cc = str.charCodeAt(i);
32+
}
33+
if (cc === 10) {
34+
i++;
35+
}
36+
}
37+
results.push(str.slice(s, i));
38+
}
39+
return results;
40+
};
41+
module.exports = splitIntoPotentialTokens;

0 commit comments

Comments
 (0)