Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

Commit

Permalink
Add support for the conditional compile elements (#11)
Browse files Browse the repository at this point in the history
* Add support for the conditional compile elements

* Change brightscript parser version

* update dependencies

* Delete yarn.lock

* Fix build and rever vscode settings

* remove yarn.lock

* Update settings.json

* fix indentation

* Add tests for conditional compile blocks

* update dependencies
  • Loading branch information
evgygor authored and TwitchBronBron committed Oct 21, 2018
1 parent 1cba14f commit 5ff9b04
Show file tree
Hide file tree
Showing 6 changed files with 508 additions and 414 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
"typescript.autoClosingTags": true,
"typescript.format.placeOpenBraceOnNewLineForControlBlocks": false,
"window.title": "BRS Formatter - ${activeEditorShort}${separator}${appName}"
}
}
75 changes: 44 additions & 31 deletions dist/BrightScriptFormatter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "__esModule", { value: true });
var brightscript_parser_1 = require("brightscript-parser");
var trimRight = require("trim-right");
var BrightScriptFormatter = /** @class */ (function () {
function BrightScriptFormatter() {}
function BrightScriptFormatter() {
}
/**
* Format the given input.
* @param inputText the text to format
Expand Down Expand Up @@ -68,7 +67,8 @@ var BrightScriptFormatter = /** @class */ (function () {
var tokenValue = token.value;
if (options.compositeKeywords === 'combine') {
token.value = parts[0] + parts[1];
} else {
}
else {
token.value = parts[0] + ' ' + parts[1];
}
var offsetDifference = token.value.length - tokenValue.length;
Expand All @@ -82,7 +82,11 @@ var BrightScriptFormatter = /** @class */ (function () {
//split the parts of the token, but retain their case
if (lowerValue.indexOf('end') === 0) {
return [token.value.substring(0, 3), token.value.substring(3).trim()];
} else {
}
else if (lowerValue.indexOf('#else') === 0) {
return [token.value.substring(0, 5), token.value.substring(5).trim()];
}
else {
return [token.value.substring(0, 4), token.value.substring(4).trim()];
}
};
Expand All @@ -102,25 +106,27 @@ var BrightScriptFormatter = /** @class */ (function () {
var lowerValue = token.value.toLowerCase();
if (brightscript_parser_1.CompositeKeywordTokenTypes.indexOf(token.tokenType) === -1) {
token.value = token.value.substring(0, 1).toUpperCase() + token.value.substring(1).toLowerCase();
} else {
}
else {
var spaceCharCount = (lowerValue.match(/\s+/) || []).length;
var firstWordLength = 0;
if (lowerValue.indexOf('end') === 0) {
firstWordLength = 3;
} else {
}
else {
firstWordLength = 4;
}
token.value =
//first character
token.value.substring(0, 1).toUpperCase() +
//rest of first word
token.value.substring(1, firstWordLength).toLowerCase() +
//add back the whitespace
token.value.substring(firstWordLength, firstWordLength + spaceCharCount) +
//first character of second word
token.value.substring(firstWordLength + spaceCharCount, firstWordLength + spaceCharCount + 1).toUpperCase() +
//rest of second word
token.value.substring(firstWordLength + spaceCharCount + 1).toLowerCase();
//rest of first word
token.value.substring(1, firstWordLength).toLowerCase() +
//add back the whitespace
token.value.substring(firstWordLength, firstWordLength + spaceCharCount) +
//first character of second word
token.value.substring(firstWordLength + spaceCharCount, firstWordLength + spaceCharCount + 1).toUpperCase() +
//rest of second word
token.value.substring(firstWordLength + spaceCharCount + 1).toLowerCase();
}
}
}
Expand All @@ -135,7 +141,8 @@ var BrightScriptFormatter = /** @class */ (function () {
brightscript_parser_1.TokenType.if,
brightscript_parser_1.TokenType.openCurlyBraceSymbol,
brightscript_parser_1.TokenType.openSquareBraceSymbol,
brightscript_parser_1.TokenType.while
brightscript_parser_1.TokenType.while,
brightscript_parser_1.TokenType.condIf
];
var outdentTokens = [
brightscript_parser_1.TokenType.closeCurlyBraceSymbol,
Expand All @@ -146,10 +153,13 @@ var BrightScriptFormatter = /** @class */ (function () {
brightscript_parser_1.TokenType.endWhile,
brightscript_parser_1.TokenType.endFor,
brightscript_parser_1.TokenType.next,
brightscript_parser_1.TokenType.condEndIf
];
var interumTokens = [
brightscript_parser_1.TokenType.else,
brightscript_parser_1.TokenType.elseIf
brightscript_parser_1.TokenType.elseIf,
brightscript_parser_1.TokenType.condElse,
brightscript_parser_1.TokenType.condElseIf
];
var tabCount = 0;
var nextLineStartTokenIndex = 0;
Expand All @@ -170,22 +180,25 @@ var BrightScriptFormatter = /** @class */ (function () {
// } else {
// //do nothing with single-line if statement indentation
// }
} else {
}
else {
for (var _i = 0, lineTokens_1 = lineTokens; _i < lineTokens_1.length; _i++) {
var token = lineTokens_1[_i];
//if this is an indentor token,
//if this is an indentor token,
if (indentTokens.indexOf(token.tokenType) > -1) {
tabCount++;
foundIndentorThisLine = true;
//this is an outdentor token
} else if (outdentTokens.indexOf(token.tokenType) > -1) {
}
else if (outdentTokens.indexOf(token.tokenType) > -1) {
tabCount--;
if (foundIndentorThisLine === false) {
thisTabCount--;
}
//this is an interum token
} else if (interumTokens.indexOf(token.tokenType) > -1) {
//these need outdented, but don't change the tabCount
}
else if (interumTokens.indexOf(token.tokenType) > -1) {
//these need outdented, but don't change the tabCount
thisTabCount--;
}
// else if (token.tokenType === TokenType.return && foundIndentorThisLine) {
Expand All @@ -202,7 +215,8 @@ var BrightScriptFormatter = /** @class */ (function () {
var indentSpaceCount = options.indentSpaceCount ? options.indentSpaceCount : BrightScriptFormatter.DEFAULT_INDENT_SPACE_COUNT;
var spaceCount = thisTabCount * indentSpaceCount;
leadingWhitespace = Array(spaceCount + 1).join(' ');
} else {
}
else {
leadingWhitespace = Array(thisTabCount + 1).join('\t');
}
//create a whitespace token if there isn't one
Expand All @@ -215,7 +229,7 @@ var BrightScriptFormatter = /** @class */ (function () {
}
//replace the whitespace with the formatted whitespace
lineTokens[0].value = leadingWhitespace;
//add this list of tokens
//add this list of tokens
outputTokens.push.apply(outputTokens, lineTokens);
//if we have found the end of file
if (lineTokens[lineTokens.length - 1].tokenType === brightscript_parser_1.TokenType.END_OF_FILE) {
Expand Down Expand Up @@ -247,7 +261,8 @@ var BrightScriptFormatter = /** @class */ (function () {
if (whitespaceTokenCandidate.tokenType === brightscript_parser_1.TokenType.whitespace) {
lineTokens.splice(potentialWhitespaceTokenIndex, 1);
//if the final token is a comment, trim the whitespace from the righthand side
} else if (whitespaceTokenCandidate.tokenType === brightscript_parser_1.TokenType.quoteComment || whitespaceTokenCandidate.tokenType === brightscript_parser_1.TokenType.remComment) {
}
else if (whitespaceTokenCandidate.tokenType === brightscript_parser_1.TokenType.quoteComment || whitespaceTokenCandidate.tokenType === brightscript_parser_1.TokenType.remComment) {
whitespaceTokenCandidate.value = trimRight(whitespaceTokenCandidate.value);
}
//add this line to the output
Expand Down Expand Up @@ -302,9 +317,6 @@ var BrightScriptFormatter = /** @class */ (function () {
fullOptions[attrname] = options[attrname];
}
}
if (!fullOptions.indentStyle) {
fullOptions.indentStyle = 'spaces';
}
return fullOptions;
};
BrightScriptFormatter.prototype.isSingleLineIfStatement = function (lineTokens, allTokens) {
Expand All @@ -323,7 +335,8 @@ var BrightScriptFormatter = /** @class */ (function () {
var token = lineTokens[i];
if (token.tokenType === brightscript_parser_1.TokenType.whitespace || token.tokenType === brightscript_parser_1.TokenType.newline) {
//do nothing with whitespace and newlines
} else {
}
else {
//we encountered a non whitespace and non newline token, so this line must be a single-line if statement
return true;
}
Expand All @@ -336,4 +349,4 @@ var BrightScriptFormatter = /** @class */ (function () {
BrightScriptFormatter.DEFAULT_INDENT_SPACE_COUNT = 4;
return BrightScriptFormatter;
}());
exports.BrightScriptFormatter = BrightScriptFormatter;
exports.BrightScriptFormatter = BrightScriptFormatter;
38 changes: 19 additions & 19 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
},
"homepage": "https://github.com/TwitchBronBron/brightscript-formatter#readme",
"dependencies": {
"brightscript-parser": "^1.0.3-1",
"brightscript-parser": "1.1.0",
"trim-right": "^1.0.1"
},
"devDependencies": {
Expand Down
32 changes: 32 additions & 0 deletions src/BrightScriptFormatter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ describe('BrightScriptFormatter', () => {
parts = (formatter as any).getCompositeKeywordParts({ value: 'else if' });
expect(parts[0]).to.equal('else');
expect(parts[1]).to.equal('if');

parts = (formatter as any).getCompositeKeywordParts({ value: '#else if' });
expect(parts[0]).to.equal('#else');
expect(parts[1]).to.equal('if');
});
});

Expand Down Expand Up @@ -315,4 +319,32 @@ describe('BrightScriptFormatter', () => {
expect(formatter.format(program)).to.equal(`function DoSomething()\n data=[\n 1,\n 2\n ]\nend function`);
});
});

describe('indentStyle for conditional block', () => {
it('correctly fixes the indentation', () => {
let expected = `#if isDebug\n doSomething()\n#end if`;
let current = `#if isDebug\n doSomething()\n#end if`;
expect(formatter.format(current)).to.equal(expected);
});

it('skips indentation when indentStyle:undefined for conditional block', () => {
let program = `#if isDebug\n doSomething()\n#else\n doSomethingElse\n #end if`;
expect(formatter.format(program, { indentStyle: undefined })).to.equal(program);
});

it('correctly fixes the indentation2', () => {
let program = `#if isDebug\n doSomething()\n#else if isPartialDebug\n doSomethingElse()\n#else\n doFinalThing()\n#end if`;
expect(formatter.format(program)).to.equal(program);
});

it('correctly fixes the indentation nested if in conditional block', () => {
let program = `#if isDebug\n if true then\n doSomething()\n end if\n#end if`;
expect(formatter.format(program)).to.equal(program);
});

it('correctly fixes the indentation nested #if in if block', () => {
let program = `if true then\n #if isDebug\n doSomething()\n #end if\nend if`;
expect(formatter.format(program)).to.equal(program);
});
});
});

0 comments on commit 5ff9b04

Please sign in to comment.