Skip to content

Commit 6368220

Browse files
Fixed tests on windows; added option to preserve line numbers as long as #include is not used
1 parent b8a8f3c commit 6368220

File tree

6 files changed

+73
-27
lines changed

6 files changed

+73
-27
lines changed

Preprocessor.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828
* @param {string} source Source to process
2929
* @param {string|Object.<string,string>=} baseDirOrIncludes Source base directory used for includes (node.js only)
3030
* or an object containing all the included sources by filename. Defaults to the current working directory.
31+
* @param {boolean} preserveLineNumbers When removing blocks of code, replace the block with blank lines so that
32+
* line numbers are preserved, as long as #include is not used
3133
* @constructor
3234
*/
33-
var Preprocessor = function Preprocessor(source, baseDirOrIncludes) {
35+
var Preprocessor = function Preprocessor(source, baseDirOrIncludes, preserveLineNumbers) {
3436

3537
/**
3638
* Source code to pre-process.
@@ -52,6 +54,12 @@
5254
*/
5355
this.includes = typeof baseDirOrIncludes == 'object' ? baseDirOrIncludes : {};
5456

57+
/**
58+
* Preserve line numbers when removing blocks of code
59+
* @type {boolean}
60+
*/
61+
this.preserveLineNumbers = typeof preserveLineNumbers == 'boolean' ? preserveLineNumbers : false;
62+
5563
/**
5664
* Whether running inside of node.js or not.
5765
* @type {boolean}
@@ -115,6 +123,12 @@
115123
*/
116124
var GLOB_EXP = /(?:^|[^\\])\*/;
117125

126+
/**
127+
* @type {!RegExp}
128+
* @inner
129+
*/
130+
var NOT_LINE_ENDING = /[^\r\n]/g;
131+
118132
/**
119133
* Strips slashes from an escaped string.
120134
* @param {string} str Escaped string
@@ -315,11 +329,23 @@
315329
}
316330
var before = stack.pop();
317331
verbose(" pop: "+JSON.stringify(before));
318-
include = this.source.substring(before["lastIndex"], match.index);
332+
333+
if (this.preserveLineNumbers) {
334+
include = this.source.substring(before["index"], before["lastIndex"]).replace(NOT_LINE_ENDING, "")+
335+
this.source.substring(before["lastIndex"], match.index)+
336+
this.source.substring(match.index, Preprocessor.ENDIF.lastIndex).replace(NOT_LINE_ENDING, "");
337+
} else {
338+
include = this.source.substring(before["lastIndex"], match.index);
339+
}
340+
319341
if (before["include"]) {
320342
verbose(" incl: "+Preprocessor.nlToStr(include)+", 0-"+before['index']+" + "+include.length+" bytes + "+Preprocessor.ENDIF.lastIndex+"-"+this.source.length);
321343
this.source = this.source.substring(0, before["index"])+include+this.source.substring(Preprocessor.ENDIF.lastIndex);
322-
} else {
344+
} else if (this.preserveLineNumbers) {
345+
verbose(" excl(\\n): "+Preprocessor.nlToStr(include)+", 0-"+before['index']+" + "+Preprocessor.ENDIF.lastIndex+"-"+this.source.length);
346+
include = include.replace(NOT_LINE_ENDING, "");
347+
this.source = this.source.substring(0, before["index"])+include+this.source.substring(Preprocessor.ENDIF.lastIndex);
348+
} else {
323349
verbose(" excl: "+Preprocessor.nlToStr(include)+", 0-"+before['index']+" + "+Preprocessor.ENDIF.lastIndex+"-"+this.source.length);
324350
include = "";
325351
this.source = this.source.substring(0, before["index"])+this.source.substring(Preprocessor.ENDIF.lastIndex);
@@ -352,7 +378,11 @@
352378
var define = match2[1];
353379
verbose(" def: "+match2[1]);
354380
this.defines.push(define);
355-
this.source = this.source.substring(0, match.index)+indent+this.source.substring(Preprocessor.DEFINE.lastIndex);
381+
var lineEnding = ""
382+
if (this.preserveLineNumbers) {
383+
lineEnding = this.source.substring(match.index, Preprocessor.DEFINE.lastIndex).replace(NOT_LINE_ENDING, "");
384+
}
385+
this.source = this.source.substring(0, match.index)+indent+lineEnding+this.source.substring(Preprocessor.DEFINE.lastIndex);
356386
Preprocessor.EXPR.lastIndex = match.index;
357387
verbose(" continue at "+Preprocessor.EXPR.lastIndex);
358388
}
@@ -385,4 +415,4 @@
385415
global["dcodeIO"]["Preprocessor"] = Preprocessor;
386416
}
387417

388-
})(this);
418+
})(this);

Preprocessor.min.js

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)