Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add comment blocks with garbage along whitespace #6

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions src/utils/cst-formatter/white-space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,24 @@ function addRandomWhiteSpace(code: any) {
code.selectTokensByType('Punctuator').forEach((token: any) => {
try {
const randomWhitespaceBefore = getRandomWhitespace();

const randomCommentBlockBefore = getRandomCommentBlocks();
const prevToken = token.getPreviousToken();
if (prevToken.isWhitespace) {
prevToken.value = randomWhitespaceBefore;
prevToken._sourceCode = randomWhitespaceBefore;
prevToken._sourceCodeLines = [randomWhitespaceBefore];
} else {
const whitespaceBefore = new Token(
'Whitespace',
randomWhitespaceBefore
);
token.parentElement.insertChildBefore(whitespaceBefore, token);
const shouldUseComments = (Math.random() <= 0.1);
let distractionBefore = (shouldUseComments)
? new Token(
'CommentBlock',
randomCommentBlockBefore
)
: new Token(
'Whitespace',
randomWhitespaceBefore
);
token.parentElement.insertChildBefore(distractionBefore, token);
}
} catch (err) {
// TODO: Handle token errors
Expand All @@ -43,4 +49,29 @@ function getRandomWhitespace() {
return whitespaceOptions[randomIndex];
}

function getRandomCommentBlocks() {
const commentOptions = [
':3c',
'T__T',
'yikes',
'Hard to explain',
'Big loop of doom',
'drunk, fix later',
'Biblical reference',
'Comment this later',
'Autogenerated, do not edit.',
'it just keeps getting worse',
'This comment is self explanatory.',
'evil floating point bit level hacking',
'the following code is self-documenting',
'You are not expected to understand this',
'this is a violation of the Geneva Convention',
'If this comment is removed the program will blow up',
'sometimes I believe the compiler ignores all my comments',
'trying to reinvent the wheel but it\'s coming out more like a square',
];
const randomIndex = Math.floor(Math.random() * commentOptions.length);
return ' ' + commentOptions[randomIndex] + ' ';
}

export { addInconsistentIndentation, addRandomWhiteSpace };