Skip to content

Commit

Permalink
Merge pull request #48 from nippur72/code-in-comments
Browse files Browse the repository at this point in the history
Code in comments
  • Loading branch information
nippur72 authored Apr 11, 2021
2 parents 06a710d + 0785e76 commit b783f8b
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 21 deletions.
48 changes: 45 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ before going into TypeScript compiler:
const opts = {
DEBUG: true,
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-fill-with-blanks": true // add this to remove code with blank spaces instead of "//" comments
"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
"ifdef-uncomment-prefix": "// #code " // add this to uncomment code starting with "// #code "
};

/* ... */ {
Expand Down Expand Up @@ -96,6 +97,45 @@ in `example.ts`:
/// #endif
```

## Code in comments

Often times writing `#if` ... `#else` ... `#endif` results in code that is not syntactically valid
or does not pass the LINT check. A possible workaround is to hide such code in comments
and let `ifdef-loader` uncomment it if it's part of the block that has to be included in the output.

Example:

The following code is invalid because the linter sees a double declaration of the `a` variable.
```
// #if DEBUG
let a=1;
// #else
let a=2;
// #endif
```

Using code in comments:
```
// #if DEBUG
let a=1;
// #else
// #code let a=2;
// #endif
```
The code is now under comment so it's ignored by the linter; but it's uncommented
by `ifdef-loader` if the else branch has to be included in the output (that is when `DEBUG==false`).

The `// #code ` string prefix can be changed and has to be explictly specified
in the options object:

```
const opts = {
// ...
"ifdef-uncomment-prefix": "// #code ",
// ...
};
```

## License

MIT
Expand All @@ -106,6 +146,8 @@ Contributions in the form of issues or pull requests are welcome.

## Changes

- v2.3.0 added option `uncomment-prefix` to write code in comments allowing it to pass through linters and syntax checking

- 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.
Expand Down
8 changes: 7 additions & 1 deletion ifdef-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ export = function(source: string, map) {
delete data[fillWithBlanksFlag];
}

const uncommentPrefixFlag = "ifdef-uncomment-prefix";
const uncommentPrefix = data[uncommentPrefixFlag];
if(uncommentPrefix !== undefined) {
delete data[uncommentPrefixFlag];
}

try {
source = parse(source, data, verbose, tripleSlash, filePath, fillWithBlanks);
source = parse(source, data, verbose, tripleSlash, filePath, fillWithBlanks, uncommentPrefix);
this.callback(null, source, map);
} catch(err) {
const errorMessage = `ifdef-loader error: ${err}`;
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "ifdef-loader",
"version": "2.2.1",
"version": "2.3.0",
"description": "",
"main": "ifdef-loader.js",
"devDependencies": {
"@types/jasmine": "^2.5.43",
"@types/loader-utils": "^1.1.3",
"@types/node": "^7.0.5",
"@types/node": "^7.10.14",
"jasmine": "^2.5.3",
"typescript": "^2.2.1",
"typescript": "^4.2.4",
"webpack": "^3.5.5"
},
"scripts": {
Expand Down
22 changes: 21 additions & 1 deletion preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,17 @@ enum IfType { If, Elif }

let useTripleSlash: boolean|undefined;
let fillCharacter: string;
let uncommentPrefix: string | undefined;

export function parse(source: string, defs: OptionObject, verbose?: boolean, tripleSlash?: boolean, filePath?: string, fillWithBlanks?: boolean): string {
export function parse(source: string, defs: OptionObject, verbose?: boolean, tripleSlash?: boolean, filePath?: string, fillWithBlanks?: boolean, uncommentPrefixString?: string): string {
if(tripleSlash === undefined) tripleSlash = true;
useTripleSlash = tripleSlash;

if(fillWithBlanks === undefined) fillWithBlanks = false;
fillCharacter = fillWithBlanks ? ' ' : '/';

uncommentPrefix = uncommentPrefixString;

// early skip check: do not process file when no '#if' are contained
if(source.indexOf('#if') === -1) return source;

Expand Down Expand Up @@ -202,6 +205,7 @@ function apply_if(lines: string[], ifBlock: IfBlock, defs: OptionObject, verbose
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
reveal_code(lines, includeRange.from, includeRange.to); // reveal: "from" ... "to"
} else {
blank_code(lines, ifBlock.line_if, ifBlock.line_endif); // blanks: #if ... #endif
}
Expand Down Expand Up @@ -253,3 +257,19 @@ function blank_code(lines: string[], start: number, end: number) {
}
}
}

function reveal_code(lines: string[], start: number, end: number) {
// early exit if no prefix is specifed
if(uncommentPrefix == undefined) return;

// create a regex capturing the line
let regex = new RegExp(`^(?<before>\s*${uncommentPrefix})(?<line>.*)$`);

// replace lines that match the uncomment prefix
for(let t=start; t<=end; t++) {
let r = regex.exec(lines[t]);
if(r!==null && r.groups!==undefined) {
lines[t] = " ".repeat(r.groups.before.length) + r.groups.line;
}
}
}
8 changes: 7 additions & 1 deletion spec/data/simple.fwb.out.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ var c=3;


debug("or not");






let z=2;

8 changes: 7 additions & 1 deletion spec/data/simple.in.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ loadVersion4();
debug("hello");
/// #else
debug("or not");
/// #endif
/// #endif

/// #if false
let z=1;
/// #else
/// #code let z=2;
/// #endif
8 changes: 7 additions & 1 deletion spec/data/simple.out.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ var c=3;
///////////////
/////////
debug("or not");
//////////
//////////

/////////////
////////
/////////
let z=2;
//////////
5 changes: 3 additions & 2 deletions spec/processSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ describe("files spec", ()=> {
}));

function check_spec(fillWithBlanks) {
const uncommentPrefix = "/// #code ";
// checks spec files as terminating in CRLF (windows)
fileSet.forEach( ({ input, output, output_fwb, actual, actual_fwb })=> {
it(`works on ${input}`, ()=> {
const tripleSlash = input.indexOf(".doubleslash.") == -1;
const inFile = read(input);
const actualFile = parse(inFile, defs, false, tripleSlash, undefined, fillWithBlanks);
const actualFile = parse(inFile, defs, false, tripleSlash, undefined, fillWithBlanks, uncommentPrefix);
const expectedFile = read(fillWithBlanks ? output_fwb : output);
write(fillWithBlanks ? actual_fwb : actual, actualFile);
expect(actualFile).toEqual(expectedFile);
Expand All @@ -55,7 +56,7 @@ describe("files spec", ()=> {
it(`works on ${input}`, ()=> {
const tripleSlash = input.indexOf(".doubleslash.") == -1;
const inFile = removeCR(read(input));
const actualFile = parse(inFile, defs, false, tripleSlash, undefined, fillWithBlanks);
const actualFile = parse(inFile, defs, false, tripleSlash, undefined, fillWithBlanks, uncommentPrefix);
const expectedFile = removeCR(read(fillWithBlanks ? output_fwb : output));
write(fillWithBlanks ? actualLF_fwb : actualLF, actualFile);
expect(actualFile).toEqual(expectedFile);
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compileOnSave": true,
"compilerOptions": {
"target": "es5",
"lib": ["es5", "es6"],
"lib": ["es5", "es6", "es2018"],
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": true,
"module": "commonjs",
Expand Down

0 comments on commit b783f8b

Please sign in to comment.