How to use comments method in Kiwi

Best Python code snippet using Kiwi_python

checkstyleUtil.js

Source:checkstyleUtil.js Github

copy

Full Screen

1checkstyleUtil = {2 errors: [],3 4 commentNames: ["summary", "description", "example", "tags", "this"]5};6checkstyleUtil.applyRules = function(fileName, contents){7 // Do not process JSON files8 if(contents.charAt(0) == "{"){9 return;10 }11 12 // Mark all the characters that are in comments.13 var comments = checkstyleUtil.getComments(contents);14 15 // Apply all the rules to the file16 for(var ruleName in checkstyleUtil.rules){17 checkstyleUtil.rules[ruleName](fileName, contents, comments);18 }19};20// Calculate the characters in a file that are in comment fields21// These will be ignored by the checkstyle rules.22checkstyleUtil.getComments = function(contents){23 var comments = [];24 25 var i;26 27 // Initialize the array to false values.28 for(i = 0; i < contents.length; i++){29 comments[i] = 0;30 }31 32 var sep = "\n";33 34 function markRange(start, stop){35 for(var i = start; i < stop; i++){36 comments[i] = 1;37 }38 }39 function markRegexs() {40 var idx = contents.indexOf("/g");41 var i;42 while(idx > -1) {43 if(!comments[idx] && contents.charAt(idx - 1) != "*"){44 // Look back until either a forward slash45 // or a new line is found46 var prevChar = contents.charAt(idx - 1);47 i = idx;48 while(prevChar != "\n" && prevChar != "/" && i > 0){49 prevChar = contents.charAt(--i);50 }51 if(prevChar == "/" && i < idx - 1){52 markRange(i, idx);53 }54 }55 idx = contents.indexOf("/g", idx + 2)56 }57 58 // Now mark all .match and .replace function calls59 // They generally contain regular expressions, and are just too bloody difficult.60 var fnNames = ["match", "replace"];61 var name;62 63 for (i = 0; i < fnNames.length; i++){64 name = fnNames[i];65 66 idx = contents.indexOf(name + "(");67 68 while(idx > -1){69 // Find the end parenthesis70 if(comments[idx]){71 idx = contents.indexOf(name + "(", idx + name.length);72 } else {73 var fnEnd = contents.indexOf(")", idx);74 markRange(idx, fnEnd + 1);75 }76 }77 }78 79 // Now look for all the lines that declare a regex variable, e.g.80 // var begRegExp = /^,|^NOT |^AND |^OR |^\(|^\)|^!|^&&|^\|\|/i;81 82 idx = contents.indexOf(" = /");83 84 while(idx > -1){85 if(!comments[idx] && contents.charAt(idx + 4) != "*"){86 var eol = contents.indexOf("\n", idx + 1);87 markRange(idx + 3, Math.max(eol, idx + 4));88 }89 90 idx = contents.indexOf(" = /", idx + 3);91 }92 }93 markRegexs();94 95 96 var marker = null;97 var ch;98 99 var DOUBLE_QUOTE = 1;100 var SINGLE_QUOTE = 2;101 var LINE_COMMENT = 3;102 var MULTI_COMMENT = 4;103 var UNMARK = 5;104 105 var pos;106 107 for (i = 0; i < contents.length; i++) {108 var skip = false;109 110 if(comments[i]){111 continue;112 }113 114 ch = contents[i];115 116 switch(ch){117 case "\"":118 if(marker == DOUBLE_QUOTE) {119 marker = UNMARK;120 } else if (marker == null) {121 marker = DOUBLE_QUOTE;122 pos = i;123 }124 125 break;126 case "'":127 if(marker == SINGLE_QUOTE) {128 marker = UNMARK;129 } else if (marker == null) {130 marker = SINGLE_QUOTE;131 pos = i;132 }133 134 break;135 case "/":136 if(marker == null){137 if(contents[i + 1] == "/"){138 marker = LINE_COMMENT;139 pos = i;140 skip = true;141 } else if(contents[i + 1] == "*"){142 marker = MULTI_COMMENT;143 pos = i;144 skip = true;145 }146 }147 148 break;149 case "*":150 if (marker == MULTI_COMMENT){151 if(contents[i + 1] == "/"){152 marker = UNMARK;153 skip = true;154 }155 }156 157 break;158 case "\n":159 if(marker == LINE_COMMENT){160 marker = UNMARK;161 }162 break;163 164 }165 if (marker != null) {166 comments[i] = 1;167 }168 if (marker == UNMARK){169 marker = null;170 }171 if (skip) {172 i++;173 comments[i] = 1;174 }175 }176 177 178 return comments;179}180// Calculate the line number of the character at index 'pos'181checkstyleUtil.getLineNumber = function(contents, pos){182 var counter = 0;183 var sep = "\n";184 185 for(var i = pos; i > -1; i--){186 if(contents.charAt(i) == "\n"){187 counter ++;188 }189 }190 return counter + 1;191};192// Store the information for a single error.193checkstyleUtil.addError = function(msg, fileName, contents, pos){194 while(fileName.indexOf("../") == 0){195 fileName = fileName.substring(3);196 }197 checkstyleUtil.errors.push({198 file: fileName,199 line: checkstyleUtil.getLineNumber(contents, pos),200 message: msg201 });202};203// Find the next character in 'contents' after the index 'start'204// Spaces and tabs are ignored.205checkstyleUtil.getNextChar = function(contents, start, comments, ignoreNewLine){206 for(var i = start; i < contents.length; i++){207 if(comments && comments[i]){208 continue;209 }210 if(contents.charAt(i) != " "211 && contents.charAt(i) != "\t"212 && (!ignoreNewLine || contents.charCodeAt(i) != 13)){213 return {214 value: contents[i],215 pos: i216 };217 }218 }219 return null;220};221// Find the next occurrence of the character in the222// 'contents' array after the index 'start'223checkstyleUtil.findNextCharPos = function(contents, start, character){224 for(var i = start; i < contents.length; i++){225 if(contents.charAt(i) == character){226 return i;227 }228 }229 return -1;230};231// Creates a simple function that searches for the token, and232// adds an error if it is found233checkstyleUtil.createSimpleSearch = function(token, message){234 return function(fileName, contents, comments){235 var idx = contents.indexOf(token);236 237 while(idx > -1){238 239 if(!comments[idx]){240 checkstyleUtil.addError(message, fileName, contents, idx);241 }242 idx = contents.indexOf(token, idx + 1);243 }244 };245};246// Creates a function that fails a test if the given token247// does not have a space to the left and right.248checkstyleUtil.createSpaceWrappedSearch = function(token, message){249 return function(fileName, contents, comments){250 251 var idx = contents.indexOf(token);252 var before, after;253 var tokenLength = token.length;254 while(idx > -1){255 before = contents.charAt(idx - 1);256 after = contents.charAt(idx + tokenLength);257 if(!comments[idx] &&258 ((before != " " && before != "\t"259 && (token != "==" || before != "!")260 && (token != "=" ||261 (before != "<" &&262 before != ">" &&263 before != "=" &&264 before != "!" &&265 before != "+" &&266 before != "-" &&267 before != "*" &&268 before != "/" &&269 before != "&" &&270 before != "|" ))) ||271 (272 (after != " " && contents.charCodeAt(idx + tokenLength) != 13273 && contents.charCodeAt(idx + tokenLength) != 10)274 && (token != "==" || after != "=")275 && (token != "!=" || after != "=")276 && (token != "<" || after != "=")277 && (token != ">" || after != "=")278 && (token != "=" || after != "=")279 && (token != "&" || after != "=")280 && (token != "|" || after != "=")281 && (token != "+" || after != "=")282 && (token != "-" || after != "=")283 && (token != "*" || after != "=")284 && (token != "/" || after != "=")285 ))){286 checkstyleUtil.addError(message, fileName, contents, idx);287 }288 idx = contents.indexOf(token, idx + token.length);289 }290 };291};292checkstyleUtil.isEOL = function(contents, pos){293 var c = contents.charCodeAt(pos);294 return c == 10 || c == 13 || contents.charAt(pos) == "\n";295};296// All the rules that will be applied to each file.297checkstyleUtil.rules = {298 "elseFollowedBySpace": function(fileName, contents, comments){299 var idx = contents.indexOf("else ");300 while(idx > -1){301 if(!comments[idx] && contents.substring(idx + 5, idx + 7) != "if"){302 checkstyleUtil.addError("\" else \" cannot be followed by a space", fileName, contents, idx);303 }304 idx = contents.indexOf("else {", idx + 1);305 }306 },307 308 "trailingComma" : function(fileName, contents, comments){309 310 var s = ",";311 var idx = contents.indexOf(s);312 var nextChar;313 314 while(idx > -1){315 if(!comments[idx]){316 nextChar = checkstyleUtil.getNextChar(contents, idx + 1, comments, true);317 if(nextChar && nextChar.value == "}"){318 checkstyleUtil.addError("Trailing commas are not permitted", fileName, contents, idx);319 }320 }321 idx = contents.indexOf(s, idx + 1);322 }323 },324 325 "switchCaseNewLine" : function(fileName, contents, comments){326 var s = "\tcase ";327 var idx = contents.indexOf(s);328 var nextColonIdx;329 var eolIdx;330 331 while(idx > -1){332 333 if(!comments[idx]){334 eolIdx = contents.indexOf("\n", idx + 4);335 336 if(eolIdx > idx){337 // Count backwards from the end of the line.338 // The first character, that is not a comment,339 // Should be a ':'340 341 for(var i = eolIdx; i > idx + 4; i--){342 var c = contents.charAt(i);343 if(!comments[i]344 && c != ' '345 && c != '\t'346 && c != ':'347 && !checkstyleUtil.isEOL(contents, i)){348 checkstyleUtil.addError(349 "A CASE statement should be followed by a new line",350 fileName, contents, idx);351 break;352 }353 if(c == ':'){354 break;355 }356 }357 }358 }359 idx = contents.indexOf(s, idx + 4);360 }361 },362 363 "curlyBraceAtStartOfLine": function(fileName, contents, comments){364 365 var idx = contents.indexOf("\n");366 367 while(idx > -1){368 var nextChar = checkstyleUtil.getNextChar(contents, idx + 1);369 370 if(nextChar && !comments[nextChar.pos] && nextChar.value == "{"){371 // Go back three lines, and look for "dojo.declare". If it exists in the last three lines,372 // then it is ok to have { at the start of this line.373 374 var nlCount = 0;375 var i;376 for(i = idx - 1; i > -1 && nlCount < 3; i--){377 if(contents[i] == "\n"){378 nlCount++;379 }380 }381 var declarePos = contents.indexOf("dojo.declare", Math.max(0, i));382 if(declarePos < 0 || declarePos > idx){383 checkstyleUtil.addError("An opening curly brace should not be the first on a line", fileName, contents, idx);384 }385 }386 idx = contents.indexOf("\n", idx + 1);387 }388 },389 390 "parenthesisSpaceCurlyBrace": checkstyleUtil.createSimpleSearch(") {", "A space is not permitted between a closing parenthesis and a curly brace"),391 392 "useTabs": function(fileName, contents, comments){393 394 var idx = contents.indexOf(" ");395 396 while(idx > -1){397 var nextChar = checkstyleUtil.getNextChar(contents, idx + 1);398 if(!comments[idx] && nextChar && nextChar.value.charCodeAt(0) != 13){399 checkstyleUtil.addError("Tabs should be used instead of spaces", fileName, contents, idx);400 var nextLine = checkstyleUtil.findNextCharPos(contents, idx + 1, "\n");401 if(nextLine < 0){402 break;403 }404 idx = contents.indexOf(" ", nextLine + 1);405 } else{406 idx = contents.indexOf(" ", idx + 2);407 }408 }409 },410 411 "commentFormatting": function(fileName, contents, comments){412 413 var commentNames = checkstyleUtil.commentNames;414 var invalidPrefixes = ["//", "//\t"];415 var idx;416 417 for(var i = 0; i < commentNames.length; i++){418 var comment = commentNames[i];419 for(var j = 0; j < invalidPrefixes.length; j++){420 idx = contents.indexOf(invalidPrefixes[j] + comment + ":");421 // Make sure that there is a space before the comment.422 while(idx > -1){423 checkstyleUtil.addError("Must be just a space in a comment before \"" + comment + "\"" , fileName, contents, idx);424 var nextLine = checkstyleUtil.findNextCharPos(contents, idx + 1, "\n");425 if(nextLine < 0){426 break;427 }428 idx = contents.indexOf(invalidPrefixes[j] + comment + ":", nextLine);429 }430 }431 432 idx = contents.indexOf(comment + ":");433 434 // Make sure that the comment name is on a line by itself. The body of the comment435 // must be on the next line.436 while(idx > -1){437 if(comments[idx]){438 var search = idx + comment.length + 1;439 440 // Make sure that there is nothing after the comment name on the same line.441 while(!checkstyleUtil.isEOL(contents, search)){442 if(contents[search] != " " && contents[search] != "\t"){443 checkstyleUtil.addError("The comment \"" + comment + "\" must be followed by a new line" ,444 fileName, contents, idx);445 break;446 }447 search++;448 }449 }450 idx = contents.indexOf(comment + ":", idx + comment.length + 2);451 }452 }453 },454 455 "spacesAroundEquals": checkstyleUtil.createSpaceWrappedSearch("==", "The equals sign should be preceded and followed by a space"),456 "spacesAroundNotEquals": checkstyleUtil.createSpaceWrappedSearch("!=", "The != sign should be preceded and followed by a space"),457 "spacesAroundAssignment": checkstyleUtil.createSpaceWrappedSearch("=", "The = sign should be preceded and followed by a space"),458 "spacesAroundOr": checkstyleUtil.createSpaceWrappedSearch("||", "The || sign should be preceded and followed by a space"),459 "spacesAroundLessThan": checkstyleUtil.createSpaceWrappedSearch("<", "The < sign should be preceded and followed by a space"),460 "spacesAroundGreaterThan": checkstyleUtil.createSpaceWrappedSearch(">", "The > sign should be preceded and followed by a space"),461 "spacesAroundAnd": checkstyleUtil.createSpaceWrappedSearch("&&", "The && sign should be preceded and followed by a space")462};463var noSpaceAfter = ["catch","do","finally","for","if","switch","try","while","with"];464// Add checks for all the elements that are not allowed to have a space after them.465checkstyleUtil.createNoSpaceAfterFunction = function(name){466 checkstyleUtil.rules["noSpaceAfter" + noSpaceAfter[i] + "1"] =467 checkstyleUtil.createSimpleSearch(" " + name +" ", "\" " + name + " \" cannot be followed by a space");468 checkstyleUtil.rules["noSpaceAfter" + noSpaceAfter[i] + "2"] =469 checkstyleUtil.createSimpleSearch("\t" + name +" ", "\" " + name + " \" cannot be followed by a space");470}471for(var i = 0; i < noSpaceAfter.length; i++){472 checkstyleUtil.createNoSpaceAfterFunction(noSpaceAfter[i]);473}474checkstyleUtil.clear = function(){475 checkstyleUtil.errors = [];476}477checkstyleUtil.serializeErrors = function(){478 var buf = [];479 var errs = checkstyleUtil.errors;480 for(var i = 0; i < errs.length; i++){481 buf.push(errs[i].file + ":" + errs[i].line + " - " + errs[i].message);482 }483 return buf.join("\n");484}485checkstyleUtil.makeSimpleFixes = function(contents){486 487 var comments = checkstyleUtil.getComments(contents);488 for(var i = 0; i < noSpaceAfter.length; i++){489 contents = checkstyleUtil.fixSpaceAfter(contents, noSpaceAfter[i], comments);490 }491 /*492 contents = contents.split(" ").join("\t")493 .split(" ").join("\t")494 .split(") {").join("){")495 .split("\tif (").join("\tif(")496 .split("} else").join("}else")497 .split("}\telse").join("}else")498 .split("}else {").join("}else{")499 .split("\twhile (").join("\twhile(")500 .split("\tfor (").join("\tfor(")501 .split("\tswitch (").join("\tswitch(");502 */503 504 contents = checkstyleUtil.replaceAllExceptComments(contents, "= ", "= ", comments);505 comments = checkstyleUtil.getComments(contents);506 contents = checkstyleUtil.replaceAllExceptComments(contents, " ", "\t", comments);507 comments = checkstyleUtil.getComments(contents);508 contents = checkstyleUtil.replaceAllExceptComments(contents, " ", "\t", comments);509 comments = checkstyleUtil.getComments(contents);510 contents = checkstyleUtil.replaceAllExceptComments(contents, "\tif (", "\tif(", comments);511 comments = checkstyleUtil.getComments(contents);512 contents = checkstyleUtil.replaceAllExceptComments(contents, "} else", "}else", comments);513 comments = checkstyleUtil.getComments(contents);514 contents = checkstyleUtil.replaceAllExceptComments(contents, "}\telse", "}else", comments);515 comments = checkstyleUtil.getComments(contents);516 contents = checkstyleUtil.replaceAllExceptComments(contents, "}else {", "}else{", comments);517 comments = checkstyleUtil.getComments(contents);518 contents = checkstyleUtil.replaceAllExceptComments(contents, "\twhile (", "\twhile(", comments);519 comments = checkstyleUtil.getComments(contents);520 contents = checkstyleUtil.replaceAllExceptComments(contents, "\tfor (", "\tfor(", comments);521 comments = checkstyleUtil.getComments(contents);522 contents = checkstyleUtil.replaceAllExceptComments(contents, "\tswitch (", "\tswitch(", comments);523 comments = checkstyleUtil.getComments(contents);524 contents = checkstyleUtil.replaceAllExceptComments(contents, ") {", "){", comments);525 comments = checkstyleUtil.getComments(contents);526 contents = checkstyleUtil.replaceAllExceptComments(contents, "//summary:", "// summary:", {});527 contents = checkstyleUtil.replaceAllExceptComments(contents, "//description:", "// description:", {});528 comments = checkstyleUtil.getComments(contents);529 530 contents = checkstyleUtil.fixTrailingWhitespace(contents);531 comments = checkstyleUtil.getComments(contents);532 contents = checkstyleUtil.fixSpaceBeforeAndAfter(contents, "===", comments);533 comments = checkstyleUtil.getComments(contents);534 contents = checkstyleUtil.fixSpaceBeforeAndAfter(contents, "!==", comments);535 comments = checkstyleUtil.getComments(contents);536 contents = checkstyleUtil.fixSpaceBeforeAndAfter(contents, "<=", comments);537 comments = checkstyleUtil.getComments(contents);538 contents = checkstyleUtil.fixSpaceBeforeAndAfter(contents, "<", comments);539 comments = checkstyleUtil.getComments(contents);540 contents = checkstyleUtil.fixSpaceBeforeAndAfter(contents, ">=", comments);541 comments = checkstyleUtil.getComments(contents);542 contents = checkstyleUtil.fixSpaceBeforeAndAfter(contents, ">", comments);543 comments = checkstyleUtil.getComments(contents);544 contents = checkstyleUtil.fixSpaceBeforeAndAfter(contents, "!=", comments);545 comments = checkstyleUtil.getComments(contents);546 contents = checkstyleUtil.fixSpaceBeforeAndAfter(contents, "==", comments);547 comments = checkstyleUtil.getComments(contents);548 contents = checkstyleUtil.fixSpaceBeforeAndAfter(contents, "=", comments);549 comments = checkstyleUtil.getComments(contents);550 contents = checkstyleUtil.fixSpaceBeforeAndAfter(contents, "||", comments);551 comments = checkstyleUtil.getComments(contents);552 contents = checkstyleUtil.fixSpaceBeforeAndAfter(contents, "&&", comments);553 comments = checkstyleUtil.getComments(contents);554 555 contents = checkstyleUtil.fixCommentNames(contents);556 557 558 559 return contents;560}561checkstyleUtil.fixCommentNames = function(contents){562 var commentNames = checkstyleUtil.commentNames;563 var i;564 565 for(i = 0; i < commentNames.length; i++){566 contents = checkstyleUtil.replaceAllExceptComments(contents, "//\t" + commentNames[i] + ":", "// " + commentNames[i] + ":", {});567 }568 569 for(i = 0; i < commentNames.length; i++){570 var commentName = commentNames[i];571 var searchToken = "// " + commentName + ":";572 var idx = contents.indexOf(searchToken);573 574 575 while(idx > -1){576 // If the comment name is not followed immediately by a new line, then insert a new line,577 // two forward slashes and two tabs.578 if(!checkstyleUtil.isEOL(contents, idx + commentName.length + 4)){579 // Calculate how many tabs to put before the "//"580 581 var tabs = "";582 var search = idx - 1;583 while(!checkstyleUtil.isEOL(contents, search)){584 tabs += contents.charAt(search);585 search--;586 }587 var insertPos = idx + commentName.length + 4;588 if(contents.charAt(insertPos) == " " || contents.charAt(insertPos) == "\t"){589 contents = checkstyleUtil.deleteChar(contents, insertPos);590 }591 592 contents = checkstyleUtil.insertChar(contents, "\n" + tabs + "//\t\t", idx + commentName.length + 4);593 594 }595 idx = contents.indexOf(searchToken, idx + commentName.length);596 }597 }598 return contents;599}600checkstyleUtil.replaceAllExceptComments = function(contents, old, newStr, comments){601 var idx = contents.indexOf(old);602 var toRemove = [];603 604 while(idx > -1){605 if(!comments[idx]){606 toRemove.push(idx);607 }608 idx = contents.indexOf(old, idx + old.length);609 }610 611 // Process the string backwards so we don't have to recompute the comments each time.612 for(var i = toRemove.length - 1; i > -1; i--){613 idx = toRemove[i];614 if(!comments[idx]){615 contents = contents.substring(0, idx)616 + newStr617 + contents.substring(idx + old.length, contents.length);618 }619 }620 return contents;621}622checkstyleUtil.insertChar = function(contents, ch, pos){623 return contents.substring(0, pos) + ch + contents.substring(pos);624}625checkstyleUtil.deleteChar = function(contents, pos){626 return contents.substring(0, pos) + contents.substring(pos + 1);627}628checkstyleUtil.fixTrailingWhitespace = function(contents) {629 var idx = contents.indexOf("\n");630 631 // Find each new line character, then iterate backwards until a non-whitespace character is found632 // then remove the whitespace.633 while(idx > -1){634 var search = idx - 1;635 636 while(search > -1 && (contents.charAt(search) == " " || contents.charAt(search) == "\t")){637 search--;638 }639 640 if(search < idx -1){641 contents = contents.substring(0, search + 1)642 + contents.substring(idx, contents.length);643 644 idx = contents.indexOf("\n", search + 2);645 }else{646 idx = contents.indexOf("\n", idx + 1);647 }648 }649 return contents;650}651checkstyleUtil.fixSpaceAfter = function(contents, token, comments){652 var idx = contents.indexOf(token + " ");653 654 while(idx > -1){655 if(!comments[idx]){656 contents = checkstyleUtil.deleteChar(contents, idx + token.length);657 }658 659 idx = contents.indexOf(token + " ", idx + token.length);660 }661 return contents;662}663checkstyleUtil.fixSpaceBeforeAndAfter = function(contents, token, comments){664 var idx = contents.indexOf(token);665 var before, after;666 var len = token.length;667 while(idx > -1){668 before = contents.charAt(idx - 1);669 after = contents.charAt(idx + len);670 if(!comments[idx]){671 // Only insert a space before the token if:672 // - char before is not a space or a tab673 // - token is "==" and the char before is neither "!" or "="674 675 if(before != " " && before != "\t"676 && (token != "==" || (before != "!" && before != "="))677 && (token != "=" ||678 (before != "<" &&679 before != ">" &&680 before != "=" &&681 before != "!" &&682 before != "+" &&683 before != "-" &&684 before != "*" &&685 before != "/" &&686 before != "&" &&687 before != "|" ))688 ){689 690 contents = checkstyleUtil.insertChar(contents, " ", idx);691 idx ++;692 }693 694 // Only insert a space after the token if:695 // - char after is not a space696 // - char after is not a new line697 // - char after is not "="698 if((after != " " && contents.charCodeAt(idx + len) != 13699 && contents.charCodeAt(idx + len) != 10)700 && (token != "==" || after != "=")701 && (token != "!=" || after != "=")702 && (token != "=" || after != "=")703 && (token != "<" || after != "=")704 && (token != ">" || after != "=")705 && (token != "&" || after != "=")706 && (token != "|" || after != "=")707 && (token != "+" || after != "=")708 && (token != "-" || after != "=")709 && (token != "*" || after != "=")710 && (token != "/" || after != "=")711 712 ){713 contents = contents = checkstyleUtil.insertChar(contents, " ", idx + token.length);714 idx++;715 }716 }717 idx = contents.indexOf(token, idx + token.length);718 }719 return contents;720}721// Creates the data file suitable to be loaded into a dojo.data.ItemFileReadStore722checkstyleUtil.generateReport = function(skipPrint){723 724 var ids = 1;725 var json = ["{id:'" +(ids++) + "', file: 'All', isFolder:true}"];726 // A map of folders that have already been found.727 var allFolders = {};728 729 var messageIds = {};730 var messageCounter = 1;731 var i, err;732 733 function getFolderName(fileName){734 // Extract the folder name from a file name735 var idx = fileName.lastIndexOf("/");736 return fileName.substring(0, idx);737 }738 739 // Add a folder to the list of folders.740 function pushFolder(folderName){741 if(!allFolders[folderName]){742 allFolders[folderName] = true;743 json.push("{id: '" +(ids++) + "', file: '" + folderName + "', folder: 1}");744 }745 }746 747 for(i = 0; i < checkstyleUtil.errors.length; i++){748 err = checkstyleUtil.errors[i];749 var message = err.message;750 var messageId = messageIds[message];751 if(!messageId){752 messageId = "m" + messageCounter++;753 messageIds[message] = messageId;754 755 json.push("{id:'" + messageId +756 "',msg:'" + message +757 "'}");758 }759 }760 761 pushFolder("All");762 763 // Create the JSON records for each error.764 for(i = 0; i < checkstyleUtil.errors.length; i++){765 err = checkstyleUtil.errors[i];766 var folderName = getFolderName(err.file);767 pushFolder(folderName);768 769 json.push("{id:'" +(ids++) +770 "', file:'" + err.file +771 "',line:" + err.line +772 ",msg:{'_reference':'" + messageIds[err.message] +773 //"'},folder:'" + folderName +774 "'},folder: 0" +775 "}");776 777 }778 // Add the date that the check was run to the store.779 json.push("{id:'" +(ids++) + "', date: " +(new Date()).getTime() + "}");780 781 // Save the file.782 if(!skipPrint){783 print("Found " + checkstyleUtil.errors.length + " checkstyle errors. " +784 "Open the file checkstyleReport.html to view the results.");785 }786 787 return "{ identifier: 'id', label:'file', items: [" + json.join(",\n") + "]}";...

Full Screen

Full Screen

buildtsdoc.js

Source:buildtsdoc.js Github

copy

Full Screen

1/**2* Add comments in a TypeScript definition file3*/4'use strict';5var ts = require('typescript');6var fs = require('fs');7var TypeScriptDocGenerator = (function () {8 function TypeScriptDocGenerator(tsDefFileName, jsdocJsonFileName) {9 this.nbCharsAdded = 0;10 this.tsDefFileName = ts.normalizePath(tsDefFileName);11 this.tsDefFileContent = fs.readFileSync(this.tsDefFileName, 'utf-8').toString();12 this.delintNodeFunction = this.delintNode.bind(this);13 var jsonDocsFileContent = fs.readFileSync(jsdocJsonFileName, 'utf-8').toString();14 this.docs = JSON.parse(jsonDocsFileContent);15 var options = { target: ts.ScriptTarget.ES5, module: ts.ModuleKind.AMD };16 var host = ts.createCompilerHost(options);17 var program = ts.createProgram([this.tsDefFileName], options, host);18 this.sourceFile = program.getSourceFile(this.tsDefFileName);19 }20 TypeScriptDocGenerator.prototype.getTsDefCommentedFileContent = function () {21 this.scan();22 return this.tsDefFileContent;23 };24 TypeScriptDocGenerator.prototype.repeatSpaces = function (nb) {25 var res = "";26 for (var i = 0; i < nb; i++) {27 res += " ";28 }29 return res;30 };31 TypeScriptDocGenerator.prototype.insertComment = function (commentLines, position) {32 if ((commentLines != null) && (commentLines.length > 0)) {33 var nbChars = 0;34 for (var i = 0; i < commentLines.length; i++) {35 nbChars += commentLines[i].trim().length;36 }37 if (nbChars > 0) {38 var lc = this.sourceFile.getLineAndCharacterFromPosition(position);39 var nbSpaces = lc.character - 1;40 var startLinePosition = this.sourceFile.getLineStarts()[lc.line - 1];41 var comment = "\r\n" + this.repeatSpaces(nbSpaces) + "/**\r\n";42 for (var j = 0; j < commentLines.length; j++) {43 comment += this.repeatSpaces(nbSpaces) + "* " + commentLines[j].trimRight() + "\r\n";44 }45 comment += this.repeatSpaces(nbSpaces) + "*/\r\n";46 this.tsDefFileContent = this.tsDefFileContent.substr(0, startLinePosition + this.nbCharsAdded) + comment + this.tsDefFileContent.substr(startLinePosition + this.nbCharsAdded);47 this.nbCharsAdded += comment.length;48 }49 }50 };51 TypeScriptDocGenerator.prototype.cleanEndLine = function (str) {52 return str.replace(new RegExp('[' + "\r\n" + ']', 'g'), "\n").replace(new RegExp('[' + "\r" + ']', 'g'), "\n");53 };54 TypeScriptDocGenerator.prototype.findClass = function (className) {55 if (className.indexOf("p2.") === 0) {56 className = className.replace("p2.", "Phaser.Physics.P2.");57 }58 var elements = this.docs.classes.filter(function (element) {59 return (element.name === className);60 });61 return elements[0];62 };63 TypeScriptDocGenerator.prototype.generateClassComments = function (className) {64 var c = this.findClass(className);65 if (c != null) {66 var comments = [];67 comments = comments.concat(this.cleanEndLine(c.description).split("\n"));68 return comments;69 }70 else {71 return null;72 }73 };74 TypeScriptDocGenerator.prototype.generateMemberComments = function (className, memberName) {75 var c = this.findClass(className);76 if (c != null) {77 for (var i = 0; i < c.members.length; i++) {78 if (c.members[i].name === memberName) {79 var m = c.members[i];80 var comments = [];81 comments = comments.concat(this.cleanEndLine(m.description).split("\n"));82 if ((m.default != null) && (m.default !== "")) {83 comments.push("Default: " + m.default);84 }85 return comments;86 }87 }88 }89 else {90 return null;91 }92 };93 TypeScriptDocGenerator.prototype.generateFunctionComments = function (className, functionName) {94 var c = this.findClass(className);95 if (c != null) {96 for (var i = 0; i < c.functions.length; i++) {97 if (c.functions[i].name === functionName) {98 var f = c.functions[i];99 var comments = [];100 comments = comments.concat(this.cleanEndLine(f.description).split("\n"));101 if (f.parameters.length > 0) {102 comments.push("");103 }104 for (var j = 0; j < f.parameters.length; j++) {105 var p = f.parameters[j];106 if (p.type === "*") {107 p.name = "args";108 }109 var def = "";110 if ((p.default != null) && (p.default !== "")) {111 def = " - Default: " + p.default;112 }113 var paramComments = this.cleanEndLine(p.description).split("\n");114 for (var k = 0; k < paramComments.length; k++) {115 if (k === 0) {116 comments.push("@param " + p.name + " " + paramComments[k].trim() + ((k === paramComments.length - 1) ? def : ""));117 }118 else {119 comments.push(this.repeatSpaces(("@param " + p.name + " ").length) + paramComments[k].trim() + ((k === paramComments.length - 1) ? def : ""));120 }121 }122 }123 if ((f.returns != null) && (f.returns.description.trim().length > 0)) {124 var returnComments = this.cleanEndLine(f.returns.description).split("\n");125 for (var l = 0; l < returnComments.length; l++) {126 if (l === 0) {127 comments.push("@return " + returnComments[l].trim());128 }129 else {130 comments.push(this.repeatSpaces(("@return ").length) + returnComments[l].trim());131 }132 }133 }134 return comments;135 }136 }137 }138 else {139 return null;140 }141 };142 TypeScriptDocGenerator.prototype.generateConstructorComments = function (className) {143 var c = this.findClass(className);144 if (c != null) {145 var con = c.constructor;146 var comments = [];147 comments = comments.concat(this.cleanEndLine(con.description).split("\n"));148 if (con.parameters.length > 0) {149 comments.push("");150 }151 for (var j = 0; j < con.parameters.length; j++) {152 var p = con.parameters[j];153 if (p.type === "*") {154 p.name = "args";155 }156 var def = "";157 if ((p.default != null) && (p.default !== "")) {158 def = " - Default: " + p.default;159 }160 var paramComments = this.cleanEndLine(p.description).split("\n");161 for (var k = 0; k < paramComments.length; k++) {162 if (k === 0) {163 comments.push("@param " + p.name + " " + paramComments[k].trim() + ((k === paramComments.length - 1) ? def : ""));164 }165 else {166 comments.push(this.repeatSpaces(("@param " + p.name + " ").length) + paramComments[k].trim() + ((k === paramComments.length - 1) ? def : ""));167 }168 }169 }170 return comments;171 }172 else {173 return null;174 }175 };176 TypeScriptDocGenerator.prototype.scan = function () {177 this.delintNode(this.sourceFile);178 };179 TypeScriptDocGenerator.prototype.getClassName = function (node) {180 var fullName = '';181 if (node.kind === ts.SyntaxKind.ClassDeclaration) {182 fullName = node.name.getText();183 }184 var parent = node.parent;185 while (parent != null) {186 if (parent.kind === ts.SyntaxKind.ModuleDeclaration || parent.kind === ts.SyntaxKind.ClassDeclaration) {187 fullName = parent.name.getText() + ((fullName !== '') ? "." + fullName : fullName);188 }189 parent = parent.parent;190 }191 return fullName;192 };193 TypeScriptDocGenerator.prototype.delintNode = function (node) {194 switch (node.kind) {195 case ts.SyntaxKind.Constructor:196 this.insertComment(this.generateConstructorComments(this.getClassName(node)), node.getStart());197 break;198 case ts.SyntaxKind.ClassDeclaration:199 this.insertComment(this.generateClassComments(this.getClassName(node)), node.getStart());200 break;201 case ts.SyntaxKind.Property:202 this.insertComment(this.generateMemberComments(this.getClassName(node), node.name.getText()), node.getStart());203 break;204 case ts.SyntaxKind.Method:205 this.insertComment(this.generateFunctionComments(this.getClassName(node), node.name.getText()), node.getStart());206 break;207 }208 ts.forEachChild(node, this.delintNodeFunction);209 };210 return TypeScriptDocGenerator;211})();212module.exports = function (grunt) {213 grunt.registerMultiTask('buildtsdoc', 'Generate a TypeScript def with comments', function () {214 var tsdg = new TypeScriptDocGenerator(this.data.tsDefFileName, this.data.jsdocJsonFileName);215 fs.writeFileSync(this.data.dest, tsdg.getTsDefCommentedFileContent(), 'utf8');216 });...

Full Screen

Full Screen

get_all_comments_pipelines.py

Source:get_all_comments_pipelines.py Github

copy

Full Screen

...44 return cleaned_tokens45#------------------------------------------------------------------------------------------------------------------------46# CLEAN COMMENTS47#------------------------------------------------------------------------------------------------------------------------48def clean_comments(comments):49 comments_tokens = [word_tokenize(i) for i in comments]50 stop_words = stopwords.words("english")51 comment_cleaned_tokens_list = []52 for tokens in comments_tokens:53 comment_cleaned_tokens_list.append(remove_noise(tokens, stop_words))54 55 all_type_comments = get_all_words(comment_cleaned_tokens_list)56 return all_type_comments57#------------------------------------------------------------------------------------------------------------------------58# GATHER HOT POST COMMENTS59#------------------------------------------------------------------------------------------------------------------------60def get_all_hot_comments(subreddit, num_of_posts):61 comments = []62 for submission in reddit.subreddit("{}".format(subreddit)).hot(limit=num_of_posts):63 submission.comments.replace_more(limit=0)64 for comment in submission.comments.list():65 comments.append(comment.body) 66 all_hot_comments = clean_comments(comments)67 return all_hot_comments68#------------------------------------------------------------------------------------------------------------------------69# GATHER TOP POST COMMENTS70#------------------------------------------------------------------------------------------------------------------------71def get_all_top_comments(subreddit, num_of_posts):72 comments = []73 for submission in reddit.subreddit("{}".format(subreddit)).top(limit=num_of_posts):74 submission.comments.replace_more(limit=0)75 for comment in submission.comments.list():76 comments.append(comment.body)77 78 all_top_comments = clean_comments(comments)79 return all_top_comments80#------------------------------------------------------------------------------------------------------------------------81# GATHER CONTROVERSIAL POST COMMENTS82#------------------------------------------------------------------------------------------------------------------------83def get_all_controversial_comments(subreddit, num_of_posts):84 comments = []85 for submission in reddit.subreddit("{}".format(subreddit)).controversial(limit=num_of_posts):86 submission.comments.replace_more(limit=0)87 for comment in submission.comments.list():88 comments.append(comment.body)89 90 all_controversial_comments = clean_comments(comments)91 return all_controversial_comments92#------------------------------------------------------------------------------------------------------------------------93# GATHER GILDED POST COMMENTS94#------------------------------------------------------------------------------------------------------------------------95def get_all_gilded_comments(subreddit, num_of_posts):96 comments = []97 for submission in reddit.subreddit("{}".format(subreddit)).gilded(limit=num_of_posts):98 submission.comments.replace_more(limit=0)99 for comment in submission.comments.list():100 comments.append(comment.body)101 102 all_gilded_comments = clean_comments(comments)103 return all_gilded_comments104#------------------------------------------------------------------------------------------------------------------------105# GATHER RISING POST COMMENTS106#------------------------------------------------------------------------------------------------------------------------107def get_all_rising_comments(subreddit, num_of_posts):108 comments = []109 for submission in reddit.subreddit("{}".format(subreddit)).rising(limit=num_of_posts):110 submission.comments.replace_more(limit=0)111 for comment in submission.comments.list():112 comments.append(comment.body)113 all_rising_comments = clean_comments(comments)...

Full Screen

Full Screen

endpoints.py

Source:endpoints.py Github

copy

Full Screen

...32 api_request = request.get_json()33 subreddit_name = api_request["name"]34 num_posts = api_request["num_posts"]35 sort_order = api_request["sort_order"]36 all_comments = view_comments(subreddit_name, num_posts, sort_order)37 return {"status": 200, "fields": ["comment", "username"], "comments_list": all_comments}38@api.route('/analyse_comments_subreddit', methods=['POST'])39def analyse_comments_subreddit():40 # 1. Endpoint that analyses all the comments for the given subreddit41 # 2. Call the server-side function to get all the saved comments and run them through the model42 # 3. return a json format output of all the fields returned as well as all the comments with encrypted usernames43 # and the sentiment value attached as well ...44 api_request = request.get_json()45 subreddit_name = api_request["name"]46 num_posts = api_request["num_posts"]47 sort_order = api_request["sort_order"]48 all_comments = view_comments(subreddit_name, num_posts, sort_order)49 comments_with_predictions = False50 if all_comments:51 comments_with_predictions = get_political_sentiment_prediction(all_comments)52 make_sentiment_visual(comments_with_predictions, "subreddit", subreddit_name)53 print("Visual Generated")54 return {"status": 200, "fields": ["encrypted username", "political sentiment", "comment"], "comments_list": comments_with_predictions}55@api.route('/get_comments_by_username', methods=['POST'])56def get_comments_by_username():57 # Endpoint for getting comments for a given username58 # 1. Extract all parameters from the api request59 # 2. Call the server-side function to retrieve all the requested comments60 # 3. return a status code 200 when all comments have been fetched and saved61 api_request = request.get_json()62 username = api_request["name"]63 num_comments = api_request["num_comments"]64 encrypted_username = api_request["encrypted_username"]65 get_comments_and_make_file_from_username(username, num_comments, encrypted_username)66 return {"status": 200}67@api.route('/view_comments_username', methods=['POST'])68def view_comments_username():69 # Endpoint for viewing all comments for a given username70 # 1. Extract all parameters from the api request71 # 2. Call the server-side function to get all the saved comments72 # 3. return a json format output of all the fields returned as well as all the comments with the subreddit names73 api_request = request.get_json()74 username = api_request["name"]75 num_comments = api_request["num_comments"]76 encrypted_username = api_request["encrypted_username"]77 all_comments = view_comments(username, num_comments, str(encrypted_username))78 if not all_comments:79 all_comments = False80 return {"status": 200, "fields": ["comment", "subreddit"], "comments_list": all_comments}81@api.route('/analyse_comments_username', methods=['POST'])82def analyse_comments_username():83 # 1. Endpoint that analyses all the comments for the given username84 # 2. Call the server-side function to get all the saved comments and run them through the model85 # 3. return a json format output of all the fields returned as well as all the comments with the subreddit name86 # and the sentiment value attached as well ...87 api_request = request.get_json()88 username = api_request["name"]89 num_comments = api_request["num_comments"]90 encrypted_username = api_request["encrypted_username"]91 all_comments = view_comments(username, num_comments, str(encrypted_username))92 comments_with_predictions = False93 if all_comments:94 comments_with_predictions = get_political_sentiment_prediction(all_comments)95 name_for_sentiment_function = "username"96 if encrypted_username:97 name_for_sentiment_function += "_encrypted"98 make_sentiment_visual(comments_with_predictions, name_for_sentiment_function, username)99 print("Visual Generated")...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Kiwi automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful