Best JavaScript code snippet using wpt
indent.js
Source:indent.js
1/**2 * @fileoverview This option sets a specific tab width for your code3 * This rule has been ported and modified from JSCS.4 * @author Dmitriy Shekhovtsov5 * @copyright 2015 Dmitriy Shekhovtsov. All rights reserved.6 * @copyright 2013 Dulin Marat and other contributors.7 *8 * Permission is hereby granted, free of charge, to any person obtaining9 * a copy of this software and associated documentation files (the10 * "Software"), to deal in the Software without restriction, including11 * without limitation the rights to use, copy, modify, merge, publish,12 * distribute, sublicense, and/or sell copies of the Software, and to13 * permit persons to whom the Software is furnished to do so, subject to14 * the following conditions:15 *16 * The above copyright notice and this permission notice shall be17 * included in all copies or substantial portions of the Software.18 *19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE23 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION24 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.26 */27/*eslint no-use-before-define:[2, "nofunc"]*/28"use strict";29//------------------------------------------------------------------------------30// Rule Definition31//------------------------------------------------------------------------------32module.exports = function (context) {33 // indentation defaults: 4 spaces34 var indentChar = " ";35 var indentSize = 4;36 var options = {indentSwitchCase: false};37 var lines = null;38 var indentStack = [0];39 var linesToCheck = null;40 var breakIndents = null;41 if (context.options.length) {42 if (context.options[0] === "tab") {43 indentChar = "\t";44 indentSize = 1;45 } else /* istanbul ignore else : this will be caught by options validation */ if (typeof context.options[0] === "number") {46 indentSize = context.options[0];47 }48 if (context.options[1]) {49 var opts = context.options[1];50 options.indentSwitchCase = opts.indentSwitchCase === true;51 }52 }53 var blockParents = [54 "IfStatement",55 "WhileStatement",56 "DoWhileStatement",57 "ForStatement",58 "ForInStatement",59 "ForOfStatement",60 "FunctionDeclaration",61 "FunctionExpression",62 "ArrowExpression",63 "CatchClause",64 "WithStatement"65 ];66 var indentableNodes = {67 BlockStatement: "body",68 Program: "body",69 ObjectExpression: "properties",70 ArrayExpression: "elements",71 SwitchStatement: "cases"72 };73 if (options.indentSwitchCase) {74 indentableNodes.SwitchCase = "consequent";75 }76 //--------------------------------------------------------------------------77 // Helpers78 //--------------------------------------------------------------------------79 /**80 * Mark line to be checked81 * @param {Number} line - line number82 * @returns {void}83 */84 function markCheckLine(line) {85 linesToCheck[line].check = true;86 }87 /**88 * Mark line with targeted node to be checked89 * @param {ASTNode} checkNode - targeted node90 * @returns {void}91 */92 function markCheck(checkNode) {93 markCheckLine(checkNode.loc.start.line - 1);94 }95 /**96 * Sets pushing indent of current node97 * @param {ASTNode} node - targeted node98 * @param {Number} indents - indents count to push99 * @returns {void}100 */101 function markPush(node, indents) {102 linesToCheck[node.loc.start.line - 1].push.push(indents);103 }104 /**105 * Marks line as outdent, end of block statement for example106 * @param {ASTNode} node - targeted node107 * @param {Number} outdents - count of outedents in targeted line108 * @returns {void}109 */110 function markPop(node, outdents) {111 linesToCheck[node.loc.end.line - 1].pop.push(outdents);112 }113 /**114 * Set alt push for current node115 * @param {ASTNode} node - targeted node116 * @returns {void}117 */118 function markPushAlt(node) {119 linesToCheck[node.loc.start.line - 1].pushAltLine.push(node.loc.end.line - 1);120 }121 /**122 * Marks end of node block to be checked123 * and marks targeted node as indent pushing124 * @param {ASTNode} pushNode - targeted node125 * @param {Number} indents - indent count to push126 * @returns {void}127 */128 function markPushAndEndCheck(pushNode, indents) {129 markPush(pushNode, indents);130 markCheckLine(pushNode.loc.end.line - 1);131 }132 /**133 * Mark node as switch case statement134 * and set push\pop indentation changes135 * @param {ASTNode} caseNode - targeted node136 * @param {ASTNode[]} children - consequent child nodes of case node137 * @returns {void}138 */139 function markCase(caseNode, children) {140 var outdentNode = getCaseOutdent(children);141 if (outdentNode) {142 // If a case statement has a `break` as a direct child and it is the143 // first one encountered, use it as the example for all future case indentation144 if (breakIndents === null) {145 breakIndents = (caseNode.loc.start.column === outdentNode.loc.start.column) ? 1 : 0;146 }147 markPop(outdentNode, breakIndents);148 } else {149 markPop(caseNode, 0);150 }151 }152 /**153 * Mark child nodes to be checked later of targeted node,154 * only if child node not in same line as targeted one155 * (if child and parent nodes wrote in single line)156 * @param {ASTNode} node - targeted node157 * @returns {void}158 */159 function markChildren(node) {160 getChildren(node).forEach(function(childNode) {161 if (childNode.loc.start.line !== node.loc.start.line || node.type === "Program") {162 markCheck(childNode);163 }164 });165 }166 /**167 * Mark child block as scope pushing and mark to check168 * @param {ASTNode} node - target node169 * @param {String} property - target node property containing child170 * @returns {void}171 */172 function markAlternateBlockStatement(node, property) {173 var child = node[property];174 if (child && child.type === "BlockStatement") {175 markCheck(child);176 }177 }178 /**179 * Checks whether node is multiline or single line180 * @param {ASTNode} node - target node181 * @returns {boolean} - is multiline node182 */183 function isMultiline(node) {184 return node.loc.start.line !== node.loc.end.line;185 }186 /**187 * Get switch case statement outdent node if any188 * @param {ASTNode[]} caseChildren - case statement childs189 * @returns {ASTNode} - outdent node190 */191 function getCaseOutdent(caseChildren) {192 var outdentNode;193 caseChildren.some(function(node) {194 if (node.type === "BreakStatement") {195 outdentNode = node;196 return true;197 }198 });199 return outdentNode;200 }201 /**202 * Returns block containing node203 * @param {ASTNode} node - targeted node204 * @returns {ASTNode} - block node205 */206 function getBlockNodeToMark(node) {207 var parent = node.parent;208 // The parent of an else is the entire if/else block. To avoid over indenting209 // in the case of a non-block if with a block else, mark push where the else starts,210 // not where the if starts!211 if (parent.type === "IfStatement" && parent.alternate === node) {212 return node;213 }214 // The end line to check of a do while statement needs to be the location of the215 // closing curly brace, not the while statement, to avoid marking the last line of216 // a multiline while as a line to check.217 if (parent.type === "DoWhileStatement") {218 return node;219 }220 // Detect bare blocks: a block whose parent doesn"t expect blocks in its syntax specifically.221 if (blockParents.indexOf(parent.type) === -1) {222 return node;223 }224 return parent;225 }226 /**227 * Get node's children228 * @param {ASTNode} node - current node229 * @returns {ASTNode[]} - children230 */231 function getChildren(node) {232 var childrenProperty = indentableNodes[node.type];233 return node[childrenProperty];234 }235 /**236 * Gets indentation in line `i`237 * @param {Number} i - number of line to get indentation238 * @returns {Number} - count of indentation symbols239 */240 function getIndentationFromLine(i) {241 var rNotIndentChar = new RegExp("[^" + indentChar + "]");242 var firstContent = lines[i].search(rNotIndentChar);243 if (firstContent === -1) {244 firstContent = lines[i].length;245 }246 return firstContent;247 }248 /**249 * Compares expected and actual indentation250 * and reports any violations251 * @param {ASTNode} node - node used only for reporting252 * @returns {void}253 */254 function checkIndentations(node) {255 linesToCheck.forEach(function(line, i) {256 var actualIndentation = getIndentationFromLine(i);257 var expectedIndentation = getExpectedIndentation(line, actualIndentation);258 if (line.check) {259 if (actualIndentation !== expectedIndentation) {260 context.report(node,261 {line: i + 1, column: expectedIndentation},262 "Expected indentation of " + expectedIndentation + " characters.");263 // correct the indentation so that future lines264 // can be validated appropriately265 actualIndentation = expectedIndentation;266 }267 }268 if (line.push.length) {269 pushExpectedIndentations(line, actualIndentation);270 }271 });272 }273 /**274 * Counts expected indentation for given line number275 * @param {Number} line - line number276 * @param {Number} actual - actual indentation277 * @returns {number} - expected indentation278 */279 function getExpectedIndentation(line, actual) {280 var outdent = indentSize * Math.max.apply(null, line.pop);281 var idx = indentStack.length - 1;282 var expected = indentStack[idx];283 if (!Array.isArray(expected)) {284 expected = [expected];285 }286 expected = expected.map(function(value) {287 if (line.pop.length) {288 value -= outdent;289 }290 return value;291 }).reduce(function(previous, current) {292 // when the expected is an array, resolve the value293 // back into a Number by checking both values are the actual indentation294 return actual === current ? current : previous;295 });296 indentStack[idx] = expected;297 line.pop.forEach(function() {298 indentStack.pop();299 });300 return expected;301 }302 /**303 * Store in stack expected indentations304 * @param {Number} line - current line305 * @param {Number} actualIndentation - actual indentation at current line306 * @returns {void}307 */308 function pushExpectedIndentations(line, actualIndentation) {309 var indents = Math.max.apply(null, line.push);310 var expected = actualIndentation + (indentSize * indents);311 // when a line has alternate indentations, push an array of possible values312 // on the stack, to be resolved when checked against an actual indentation313 if (line.pushAltLine.length) {314 expected = [expected];315 line.pushAltLine.forEach(function(altLine) {316 expected.push(getIndentationFromLine(altLine) + (indentSize * indents));317 });318 }319 line.push.forEach(function() {320 indentStack.push(expected);321 });322 }323 //--------------------------------------------------------------------------324 // Public325 //--------------------------------------------------------------------------326 return {327 "Program": function (node) {328 lines = context.getSourceLines();329 linesToCheck = lines.map(function () {330 return {331 push: [],332 pushAltLine: [],333 pop: [],334 check: false335 };336 });337 if (!isMultiline(node)) {338 return;339 }340 markChildren(node);341 },342 "Program:exit": function (node) {343 checkIndentations(node);344 },345 "BlockStatement": function (node) {346 if (!isMultiline(node)) {347 return;348 }349 markChildren(node);350 markPop(node, 1);351 markPushAndEndCheck(getBlockNodeToMark(node), 1);352 },353 "IfStatement": function (node) {354 markAlternateBlockStatement(node, "alternate");355 },356 "TryStatement": function (node) {357 markAlternateBlockStatement(node, "handler");358 markAlternateBlockStatement(node, "finalizer");359 },360 "SwitchStatement": function (node) {361 if (!isMultiline(node)) {362 return;363 }364 var indents = 1;365 var children = getChildren(node);366 if (children.length && node.loc.start.column === children[0].loc.start.column) {367 indents = 0;368 }369 markChildren(node);370 markPop(node, indents);371 markPushAndEndCheck(node, indents);372 },373 "SwitchCase": function (node) {374 if (!options.indentSwitchCase) {375 return;376 }377 if (!isMultiline(node)) {378 return;379 }380 var children = getChildren(node);381 if (children.length === 1 && children[0].type === "BlockStatement") {382 return;383 }384 markPush(node, 1);385 markCheck(node);386 markChildren(node);387 markCase(node, children);388 },389 // indentations inside of function expressions can be offset from390 // either the start of the function or the end of the function, therefore391 // mark all starting lines of functions as potential indentations392 "FunctionDeclaration": function (node) {393 markPushAlt(node);394 },395 "FunctionExpression": function (node) {396 markPushAlt(node);397 }398 };399};400module.exports.schema = [401 {402 "oneOf": [403 {404 "enum": ["tab"]405 },406 {407 "type": "integer"408 }409 ]410 },411 {412 "type": "object",413 "properties": {414 "indentSwitchCase": {415 "type": "boolean"416 }417 },418 "additionalProperties": false419 }...
outdent.js
Source:outdent.js
1import { isBlockNode } from "../model/Predicates";2function outdentNode(node) {3 const depth = node.getAttribute("indent") || 0;4 return depth > 0 ? node.setAttribute("indent", depth - 1 || null) : node;5}6export default function outdent(change) {7 const { value } = change;8 const { document, selection } = value;9 const nextDocument = selection.isCollapsed10 ? document.updateDescendantAtOffset(11 selection.offset,12 isBlockNode,13 outdentNode14 )15 : document.updateDescendantsAtRange(16 selection.offset,...
Using AI Code Generation
1var wpt = require('wpt');2 if (err) {3 console.log(err);4 } else {5 console.log(data);6 }7});8var wpt = require('wpt');9wpt.getLocations(function (err, data) {10 if (err) {11 console.log(err);12 } else {13 console.log(data);14 }15});16var wpt = require('wpt');17wpt.getTesters(function (err, data) {18 if (err) {19 console.log(err);20 } else {21 console.log(data);22 }23});24var wpt = require('wpt');25wpt.getTesters(function (err, data) {26 if (err) {27 console.log(err);28 } else {29 console.log(data);30 }31});32var wpt = require('wpt');33wpt.getTesters(function (err, data) {34 if (err) {35 console.log(err);36 } else {37 console.log(data);38 }39});40var wpt = require('wpt');41wpt.getTesters(function (err, data) {42 if (err) {43 console.log(err);44 } else {45 console.log(data);46 }47});48var wpt = require('wpt');49wpt.getTesters(function (err, data) {50 if (err) {51 console.log(err);52 } else {53 console.log(data);54 }55});56var wpt = require('wpt');57wpt.getTesters(function (err, data) {58 if (err) {59 console.log(err);60 } else {61 console.log(data);62 }63});
Using AI Code Generation
1var editor = CKEDITOR.replace( 'editor1', {2} );3editor.on( 'instanceReady', function() {4 var range = editor.createRange();5 range.moveToPosition( editor.editable().getChild( 0 ), CKEDITOR.POSITION_AFTER_START );6 editor.getSelection().selectRanges( [ range ] );7 editor.execCommand( 'outdent' );8} );
Using AI Code Generation
1var div = document.createElement("div");2div.innerHTML = "Hello World";3document.body.appendChild(div);4var range = document.createRange();5range.selectNode(div);6range.collapse(true);7range.insertNode(document.createTextNode("Hello"));
Using AI Code Generation
1var wptextarea = document.getElementById('wptextarea1');2wptextarea.outdentNode();3var wptextarea = document.getElementById('wptextarea1');4wptextarea.indentNode();5var wptextarea = document.getElementById('wptextarea1');6wptextarea.setIndentation(2);7var wptextarea = document.getElementById('wptextarea1');8var indentation = wptextarea.getIndentation();9var wptextarea = document.getElementById('wptextarea1');10wptextarea.increaseIndentation();
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!