New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ifdef-loader

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ifdef-loader - npm Package Compare versions

Comparing version 2.1.5 to 2.2.0

7

ifdef-loader.js

@@ -32,4 +32,9 @@ "use strict";

}
var fillWithBlanksFlag = "ifdef-fill-with-blanks";
var fillWithBlanks = data[fillWithBlanksFlag];
if (fillWithBlanks !== undefined) {
delete data[fillWithBlanksFlag];
}
try {
source = preprocessor_1.parse(source, data, verbose, tripleSlash, filePath);
source = preprocessor_1.parse(source, data, verbose, tripleSlash, filePath, fillWithBlanks);
this.callback(null, source, map);

@@ -36,0 +41,0 @@ }

@@ -30,4 +30,10 @@ import * as loaderUtils from 'loader-utils';

const fillWithBlanksFlag = "ifdef-fill-with-blanks";
const fillWithBlanks = data[fillWithBlanksFlag];
if(fillWithBlanks !== undefined) {
delete data[fillWithBlanksFlag];
}
try {
source = parse(source, data, verbose, tripleSlash, filePath);
source = parse(source, data, verbose, tripleSlash, filePath, fillWithBlanks);
this.callback(null, source, map);

@@ -34,0 +40,0 @@ } catch(err) {

3

package.json
{
"name": "ifdef-loader",
"version": "2.1.5",
"version": "2.2.0",
"description": "",

@@ -15,2 +15,3 @@ "main": "ifdef-loader.js",

"scripts": {
"build": "tsc",
"test": "node node_modules/webpack/bin/webpack.js & tsc & jasmine --no-color"

@@ -17,0 +18,0 @@ },

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var IfBlock = (function () {
function IfBlock(startIx, endIx, elifIxs, elseIx, innerIfs) {
if (elifIxs === void 0) { elifIxs = []; }
if (elseIx === void 0) { elseIx = null; }
if (innerIfs === void 0) { innerIfs = []; }
this.startIx = startIx;
this.endIx = endIx;
this.elifIxs = elifIxs;
this.elseIx = elseIx;
this.innerIfs = innerIfs;
function IfBlock(line_if, line_endif, elifs, line_else, inner_ifs) {
if (elifs === void 0) { elifs = []; }
if (line_else === void 0) { line_else = null; }
if (inner_ifs === void 0) { inner_ifs = []; }
this.line_if = line_if;
this.line_endif = line_endif;
this.elifs = elifs;
this.line_else = line_else;
this.inner_ifs = inner_ifs;
}
IfBlock.prototype.getIfRange = function () {
var to = this.elifIxs.length > 0 ? this.elifIxs[0] : this.elseIx != null ? this.elseIx : this.endIx;
return [this.startIx, to];
var to = this.elifs.length > 0 ? this.elifs[0] : this.line_else != null ? this.line_else : this.line_endif;
return { from: this.line_if, to: to };
};
IfBlock.prototype.getElifRange = function (index) {
if (this.elifIxs.length > index) {
var from = this.elifIxs[index];
var to = this.elifIxs.length > index + 1 ? this.elifIxs[index + 1] : this.elseIx != null ? this.elseIx : this.endIx;
return [from, to];
if (this.elifs.length > index) {
var from = this.elifs[index];
var to = this.elifs.length > index + 1 ? this.elifs[index + 1] : this.line_else != null ? this.line_else : this.line_endif;
return { from: from, to: to };
}
else {
throw "Invalid elif index '" + index + "', there are only " + this.elifIxs.length + " elifs";
throw "Invalid elif index '" + index + "', there are only " + this.elifs.length + " elifs";
}
};
IfBlock.prototype.getElseRange = function () {
if (this.elseIx != null) {
return [this.elseIx, this.endIx];
if (this.line_else != null) {
return { from: this.line_else, to: this.line_endif };
}

@@ -44,6 +44,10 @@ else {

var useTripleSlash;
function parse(source, defs, verbose, tripleSlash, filePath) {
var fillCharacter;
function parse(source, defs, verbose, tripleSlash, filePath, fillWithBlanks) {
if (tripleSlash === undefined)
tripleSlash = true;
useTripleSlash = tripleSlash;
if (fillWithBlanks === undefined)
fillWithBlanks = false;
fillCharacter = fillWithBlanks ? ' ' : '/';
if (source.indexOf('#if') === -1)

@@ -66,3 +70,3 @@ return source;

blocks.push(ifBlock);
i = ifBlock.endIx;
i = ifBlock.line_endif;
}

@@ -83,3 +87,3 @@ }

innerIfs.push(innerIf);
i = innerIf.endIx;
i = innerIf.line_endif;
continue;

@@ -138,7 +142,7 @@ }

var includeRange = null;
var ifCond = parse_if(lines[ifBlock.startIx]);
var ifCond = parse_if(lines[ifBlock.line_if]);
var ifRes = evaluate(ifCond, defs);
var log = function (condition, outcome) {
if (verbose) {
console.log("#if block lines [" + (ifBlock.startIx + 1) + "-" + (ifBlock.endIx + 1) + "]: Condition '" + condition + "' is " + (outcome ? 'TRUE' : 'FALSE') + ". " + (includeRange != null ? "Including lines [" + (includeRange[0] + 1) + "-" + (includeRange[1] + 1) + "]" : 'Excluding everything') + " (" + filePath + ")");
console.log("#if block lines [" + (ifBlock.line_if + 1) + "-" + (ifBlock.line_endif + 1) + "]: Condition '" + condition + "' is " + (outcome ? 'TRUE' : 'FALSE') + ". " + (includeRange != null ? "Including lines [" + (includeRange[0] + 1) + "-" + (includeRange[1] + 1) + "]" : 'Excluding everything') + " (" + filePath + ")");
}

@@ -151,4 +155,4 @@ };

else {
for (var elifIx = 0; elifIx < ifBlock.elifIxs.length; elifIx++) {
var elifLine = lines[ifBlock.elifIxs[elifIx]];
for (var elifIx = 0; elifIx < ifBlock.elifs.length; elifIx++) {
var elifLine = lines[ifBlock.elifs[elifIx]];
var elifCond = parse_if(elifLine);

@@ -163,3 +167,3 @@ var elifRes = evaluate(elifCond, defs);

if (includeRange == null) {
if (ifBlock.elseIx != null) {
if (ifBlock.line_else != null) {
includeRange = ifBlock.getElseRange();

@@ -171,11 +175,11 @@ }

if (includeRange != null) {
blank_code(lines, ifBlock.startIx, includeRange[0]);
blank_code(lines, includeRange[1], ifBlock.endIx);
blank_code(lines, ifBlock.line_if, includeRange.from);
blank_code(lines, includeRange.to, ifBlock.line_endif);
}
else {
blank_code(lines, ifBlock.startIx, ifBlock.endIx);
blank_code(lines, ifBlock.line_if, ifBlock.line_endif);
}
for (var _i = 0, _a = ifBlock.innerIfs; _i < _a.length; _i++) {
for (var _i = 0, _a = ifBlock.inner_ifs; _i < _a.length; _i++) {
var innerIf = _a[_i];
if (includeRange != null && innerIf.startIx >= includeRange[0] && innerIf.startIx <= includeRange[1]) {
if (includeRange != null && innerIf.line_if >= includeRange.from && innerIf.line_if <= includeRange.to) {
apply_if(lines, innerIf, defs, verbose);

@@ -199,3 +203,2 @@ }

function blank_code(lines, start, end) {
if (end === void 0) { end = start; }
for (var t = start; t <= end; t++) {

@@ -212,8 +215,8 @@ var len = lines[t].length;

else if (len === 2) {
lines[t] = windowsTermination ? ' \r' : '//';
lines[t] = windowsTermination ? ' \r' : fillCharacter.repeat(2);
}
else {
lines[t] = windowsTermination ? "/".repeat(len - 1) + '\r' : "/".repeat(len);
lines[t] = windowsTermination ? fillCharacter.repeat(len - 1) + '\r' : fillCharacter.repeat(len);
}
}
}
import { OptionObject } from 'loader-utils';
interface Range {
from: number;
to: number;
}
/** Holds the line indexes for a complete #if block */
class IfBlock {
/**
* @param startIx Line index of #if
* @param endIx Line index of #endif
* @param elifIxs Line indexes of #elifs
* @param elseIx Line index of #else, or null
* @param innerIfs List of any IfBlocks that are contained within this IfBlock
* @param line_if Line index of #if
* @param line_endif Line index of #endif
* @param elifs Line indexes of #elifs
* @param line_else Line index of #else, or null
* @param inner_ifs List of any IfBlocks that are contained within this IfBlock
*/
constructor(public startIx: number, public endIx: number, public elifIxs: number[] = [], public elseIx: number|null = null, public innerIfs: IfBlock[] = []) { }
constructor(public line_if: number, public line_endif: number, public elifs: number[] = [], public line_else: number|null = null, public inner_ifs: IfBlock[] = []) { }
getIfRange(): [number, number] {
const to = this.elifIxs.length > 0 ? this.elifIxs[0] : this.elseIx != null ? this.elseIx : this.endIx;
return [this.startIx, to];
getIfRange(): Range {
const to = this.elifs.length > 0 ? this.elifs[0] : this.line_else != null ? this.line_else : this.line_endif;
return { from: this.line_if, to };
}
getElifRange(index: number): [number, number] {
if(this.elifIxs.length > index) {
const from = this.elifIxs[index];
const to = this.elifIxs.length > index + 1 ? this.elifIxs[index + 1] : this.elseIx != null ? this.elseIx : this.endIx;
return [from, to];
getElifRange(index: number): Range {
if(this.elifs.length > index) {
const from = this.elifs[index];
const to = this.elifs.length > index + 1 ? this.elifs[index + 1] : this.line_else != null ? this.line_else : this.line_endif;
return { from, to };
} else {
throw `Invalid elif index '${index}', there are only ${this.elifIxs.length} elifs`;
throw `Invalid elif index '${index}', there are only ${this.elifs.length} elifs`;
}
}
getElseRange(): [number, number] {
if(this.elseIx != null) {
return [this.elseIx, this.endIx];
getElseRange(): Range {
if(this.line_else != null) {
return { from: this.line_else, to: this.line_endif };
} else {

@@ -39,7 +44,11 @@ throw 'Cannot use elseRange when elseIx is null';

let useTripleSlash: boolean|undefined;
let fillCharacter: string;
export function parse(source: string, defs: OptionObject, verbose?: boolean, tripleSlash?: boolean, filePath?: string): string {
export function parse(source: string, defs: OptionObject, verbose?: boolean, tripleSlash?: boolean, filePath?: string, fillWithBlanks?: boolean): string {
if(tripleSlash === undefined) tripleSlash = true;
useTripleSlash = tripleSlash;
if(fillWithBlanks === undefined) fillWithBlanks = false;
fillCharacter = fillWithBlanks ? ' ' : '/';
// early skip check: do not process file when no '#if' are contained

@@ -50,3 +59,3 @@ if(source.indexOf('#if') === -1) return source;

var ifBlocks = find_if_blocks(lines);
const ifBlocks = find_if_blocks(lines);
for(let ifBlock of ifBlocks) {

@@ -65,3 +74,3 @@ apply_if(lines, ifBlock, defs, verbose, filePath);

blocks.push(ifBlock);
i = ifBlock.endIx;
i = ifBlock.line_endif;
}

@@ -89,3 +98,3 @@ }

innerIfs.push(innerIf);
i = innerIf.endIx;
i = innerIf.line_endif;
continue;

@@ -155,5 +164,6 @@ }

function apply_if(lines: string[], ifBlock: IfBlock, defs: OptionObject, verbose: boolean = false, filePath?: string) {
let includeRange: [number, number]|null = null;
let includeRange: Range|null = null;
const ifCond = parse_if(lines[ifBlock.startIx]);
// gets the condition and parses it
const ifCond = parse_if(lines[ifBlock.line_if]);
const ifRes = evaluate(ifCond, defs);

@@ -163,15 +173,20 @@

if(verbose) {
console.log(`#if block lines [${ifBlock.startIx + 1}-${ifBlock.endIx + 1}]: Condition '${condition}' is ${outcome ? 'TRUE' : 'FALSE'}. ${includeRange != null ? `Including lines [${includeRange[0] + 1}-${includeRange[1] + 1}]` : 'Excluding everything'} (${filePath})`);
console.log(`#if block lines [${ifBlock.line_if + 1}-${ifBlock.line_endif + 1}]: Condition '${condition}' is ${outcome ? 'TRUE' : 'FALSE'}. ${includeRange != null ? `Including lines [${includeRange[0] + 1}-${includeRange[1] + 1}]` : 'Excluding everything'} (${filePath})`);
}
};
// finds which part of the #if has to be included, all else is excluded
if(ifRes) {
// include the #if body
includeRange = ifBlock.getIfRange();
log(ifCond, true);
} else {
for(let elifIx = 0; elifIx < ifBlock.elifIxs.length; elifIx++) {
const elifLine = lines[ifBlock.elifIxs[elifIx]];
// if there are #elif checks if one has to be included
for(let elifIx = 0; elifIx < ifBlock.elifs.length; elifIx++) {
const elifLine = lines[ifBlock.elifs[elifIx]];
const elifCond = parse_if(elifLine);
const elifRes = evaluate(elifCond, defs);
if(elifRes) {
// include #elif
includeRange = ifBlock.getElifRange(elifIx);

@@ -183,4 +198,5 @@ log(elifCond, true);

// if no #elif are found then goes to #else branch
if(includeRange == null) {
if(ifBlock.elseIx != null) {
if(ifBlock.line_else != null) {
includeRange = ifBlock.getElseRange();

@@ -192,12 +208,13 @@ }

if(includeRange != null) {
blank_code(lines, ifBlock.startIx, includeRange[0]);
blank_code(lines, includeRange[1], ifBlock.endIx);
// blanks everything except the part that has to be included
if(includeRange != null) {
blank_code(lines, ifBlock.line_if, includeRange.from); // blanks: #if ... "from"
blank_code(lines, includeRange.to, ifBlock.line_endif); // blanks: "to" ... #endif
} else {
blank_code(lines, ifBlock.startIx, ifBlock.endIx);
blank_code(lines, ifBlock.line_if, ifBlock.line_endif); // blanks: #if ... #endif
}
for(let innerIf of ifBlock.innerIfs) {
// Apply inner-if blocks only when they are not already erased
if(includeRange != null && innerIf.startIx >= includeRange[0] && innerIf.startIx <= includeRange[1]) {
// apply to inner #if blocks that have not already been erased
for(let innerIf of ifBlock.inner_ifs) {
if(includeRange != null && innerIf.line_if >= includeRange.from && innerIf.line_if <= includeRange.to) {
apply_if(lines, innerIf, defs, verbose);

@@ -228,3 +245,3 @@ }

function blank_code(lines: string[], start: number, end: number = start) {
function blank_code(lines: string[], start: number, end: number) {
for(let t=start; t<=end; t++) {

@@ -234,19 +251,15 @@ const len = lines[t].length;

const windowsTermination = lastChar === '\r';
if(len === 0)
{
if(len === 0) {
lines[t] = '';
}
else if(len === 1)
{
else if(len === 1) {
lines[t] = windowsTermination ? '\r' : ' ';
}
else if(len === 2)
{
lines[t] = windowsTermination ? ' \r' : '//';
else if(len === 2) {
lines[t] = windowsTermination ? ' \r' : fillCharacter.repeat(2);
}
else
{
lines[t] = windowsTermination ? ("/" as any).repeat(len-1)+'\r' : ("/" as any).repeat(len);
else {
lines[t] = windowsTermination ? fillCharacter.repeat(len-1)+'\r' : fillCharacter.repeat(len);
}
}
}

@@ -66,4 +66,5 @@ # ifdef-loader

version: 3,
"ifdef-verbose": true, // add this for verbose output
"ifdef-triple-slash": false // add this to use double slash comment instead of default triple slash
"ifdef-verbose": true, // add this for verbose output
"ifdef-triple-slash": false, // add this to use double slash comment instead of default triple slash
"ifdef-fill-with-blanks": true // add this to remove code with blank spaces instead of "//" comments
};

@@ -109,2 +110,4 @@

- v2.2.0 added option `fill-with-blanks` for removing code with blank spaces instead of `//` comments
- v2.1.0 added support for `#elif` clause.

@@ -111,0 +114,0 @@

@@ -11,4 +11,5 @@ /*

version: 3,
"ifdef-verbose": true, // add this for verbose output
"ifdef-triple-slash": true // add this to use double slash comment instead of default triple slash
"ifdef-verbose": true, // add this for verbose output
"ifdef-triple-slash": true, // add this to use double slash comment instead of default triple slash
"ifdef-fill-with-blanks": true // add this to remove code with blank spaces instead of "//" comments
};

@@ -47,3 +48,3 @@

path: `${__dirname}/spec/data`,
filename: "webpack.out.actual.js",
filename: opts["ifdef-fill-with-blanks"] ? "webpack.fwb.out.actual.js" : "webpack.out.actual.js",
}

@@ -50,0 +51,0 @@ };

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc