How to use colon method in wpt

Best JavaScript code snippet using wpt

key-spacing.js

Source:key-spacing.js Github

copy

Full Screen

1/**2 * @fileoverview Rule to specify spacing of object literal keys and values3 * @author Brandon Mills4 */5"use strict";6//------------------------------------------------------------------------------7// Requirements8//------------------------------------------------------------------------------9const astUtils = require("../util/ast-utils");10//------------------------------------------------------------------------------11// Helpers12//------------------------------------------------------------------------------13/**14 * Checks whether a string contains a line terminator as defined in15 * http://www.ecma-international.org/ecma-262/5.1/#sec-7.316 * @param {string} str String to test.17 * @returns {boolean} True if str contains a line terminator.18 */19function containsLineTerminator(str) {20 return astUtils.LINEBREAK_MATCHER.test(str);21}22/**23 * Gets the last element of an array.24 * @param {Array} arr An array.25 * @returns {any} Last element of arr.26 */27function last(arr) {28 return arr[arr.length - 1];29}30/**31 * Checks whether a node is contained on a single line.32 * @param {ASTNode} node AST Node being evaluated.33 * @returns {boolean} True if the node is a single line.34 */35function isSingleLine(node) {36 return (node.loc.end.line === node.loc.start.line);37}38/**39 * Initializes a single option property from the configuration with defaults for undefined values40 * @param {Object} toOptions Object to be initialized41 * @param {Object} fromOptions Object to be initialized from42 * @returns {Object} The object with correctly initialized options and values43 */44function initOptionProperty(toOptions, fromOptions) {45 toOptions.mode = fromOptions.mode || "strict";46 // Set value of beforeColon47 if (typeof fromOptions.beforeColon !== "undefined") {48 toOptions.beforeColon = +fromOptions.beforeColon;49 } else {50 toOptions.beforeColon = 0;51 }52 // Set value of afterColon53 if (typeof fromOptions.afterColon !== "undefined") {54 toOptions.afterColon = +fromOptions.afterColon;55 } else {56 toOptions.afterColon = 1;57 }58 // Set align if exists59 if (typeof fromOptions.align !== "undefined") {60 if (typeof fromOptions.align === "object") {61 toOptions.align = fromOptions.align;62 } else { // "string"63 toOptions.align = {64 on: fromOptions.align,65 mode: toOptions.mode,66 beforeColon: toOptions.beforeColon,67 afterColon: toOptions.afterColon68 };69 }70 }71 return toOptions;72}73/**74 * Initializes all the option values (singleLine, multiLine and align) from the configuration with defaults for undefined values75 * @param {Object} toOptions Object to be initialized76 * @param {Object} fromOptions Object to be initialized from77 * @returns {Object} The object with correctly initialized options and values78 */79function initOptions(toOptions, fromOptions) {80 if (typeof fromOptions.align === "object") {81 // Initialize the alignment configuration82 toOptions.align = initOptionProperty({}, fromOptions.align);83 toOptions.align.on = fromOptions.align.on || "colon";84 toOptions.align.mode = fromOptions.align.mode || "strict";85 toOptions.multiLine = initOptionProperty({}, (fromOptions.multiLine || fromOptions));86 toOptions.singleLine = initOptionProperty({}, (fromOptions.singleLine || fromOptions));87 } else { // string or undefined88 toOptions.multiLine = initOptionProperty({}, (fromOptions.multiLine || fromOptions));89 toOptions.singleLine = initOptionProperty({}, (fromOptions.singleLine || fromOptions));90 // If alignment options are defined in multiLine, pull them out into the general align configuration91 if (toOptions.multiLine.align) {92 toOptions.align = {93 on: toOptions.multiLine.align.on,94 mode: toOptions.multiLine.align.mode || toOptions.multiLine.mode,95 beforeColon: toOptions.multiLine.align.beforeColon,96 afterColon: toOptions.multiLine.align.afterColon97 };98 }99 }100 return toOptions;101}102//------------------------------------------------------------------------------103// Rule Definition104//------------------------------------------------------------------------------105module.exports = {106 meta: {107 type: "layout",108 docs: {109 description: "enforce consistent spacing between keys and values in object literal properties",110 category: "Stylistic Issues",111 recommended: false,112 url: "https://eslint.org/docs/rules/key-spacing"113 },114 fixable: "whitespace",115 schema: [{116 anyOf: [117 {118 type: "object",119 properties: {120 align: {121 anyOf: [122 {123 enum: ["colon", "value"]124 },125 {126 type: "object",127 properties: {128 mode: {129 enum: ["strict", "minimum"]130 },131 on: {132 enum: ["colon", "value"]133 },134 beforeColon: {135 type: "boolean"136 },137 afterColon: {138 type: "boolean"139 }140 },141 additionalProperties: false142 }143 ]144 },145 mode: {146 enum: ["strict", "minimum"]147 },148 beforeColon: {149 type: "boolean"150 },151 afterColon: {152 type: "boolean"153 }154 },155 additionalProperties: false156 },157 {158 type: "object",159 properties: {160 singleLine: {161 type: "object",162 properties: {163 mode: {164 enum: ["strict", "minimum"]165 },166 beforeColon: {167 type: "boolean"168 },169 afterColon: {170 type: "boolean"171 }172 },173 additionalProperties: false174 },175 multiLine: {176 type: "object",177 properties: {178 align: {179 anyOf: [180 {181 enum: ["colon", "value"]182 },183 {184 type: "object",185 properties: {186 mode: {187 enum: ["strict", "minimum"]188 },189 on: {190 enum: ["colon", "value"]191 },192 beforeColon: {193 type: "boolean"194 },195 afterColon: {196 type: "boolean"197 }198 },199 additionalProperties: false200 }201 ]202 },203 mode: {204 enum: ["strict", "minimum"]205 },206 beforeColon: {207 type: "boolean"208 },209 afterColon: {210 type: "boolean"211 }212 },213 additionalProperties: false214 }215 },216 additionalProperties: false217 },218 {219 type: "object",220 properties: {221 singleLine: {222 type: "object",223 properties: {224 mode: {225 enum: ["strict", "minimum"]226 },227 beforeColon: {228 type: "boolean"229 },230 afterColon: {231 type: "boolean"232 }233 },234 additionalProperties: false235 },236 multiLine: {237 type: "object",238 properties: {239 mode: {240 enum: ["strict", "minimum"]241 },242 beforeColon: {243 type: "boolean"244 },245 afterColon: {246 type: "boolean"247 }248 },249 additionalProperties: false250 },251 align: {252 type: "object",253 properties: {254 mode: {255 enum: ["strict", "minimum"]256 },257 on: {258 enum: ["colon", "value"]259 },260 beforeColon: {261 type: "boolean"262 },263 afterColon: {264 type: "boolean"265 }266 },267 additionalProperties: false268 }269 },270 additionalProperties: false271 }272 ]273 }],274 messages: {275 extraKey: "Extra space after {{computed}}key '{{key}}'.",276 extraValue: "Extra space before value for {{computed}}key '{{key}}'.",277 missingKey: "Missing space after {{computed}}key '{{key}}'.",278 missingValue: "Missing space before value for {{computed}}key '{{key}}'."279 }280 },281 create(context) {282 /**283 * OPTIONS284 * "key-spacing": [2, {285 * beforeColon: false,286 * afterColon: true,287 * align: "colon" // Optional, or "value"288 * }289 */290 const options = context.options[0] || {},291 ruleOptions = initOptions({}, options),292 multiLineOptions = ruleOptions.multiLine,293 singleLineOptions = ruleOptions.singleLine,294 alignmentOptions = ruleOptions.align || null;295 const sourceCode = context.getSourceCode();296 /**297 * Checks whether a property is a member of the property group it follows.298 * @param {ASTNode} lastMember The last Property known to be in the group.299 * @param {ASTNode} candidate The next Property that might be in the group.300 * @returns {boolean} True if the candidate property is part of the group.301 */302 function continuesPropertyGroup(lastMember, candidate) {303 const groupEndLine = lastMember.loc.start.line,304 candidateStartLine = candidate.loc.start.line;305 if (candidateStartLine - groupEndLine <= 1) {306 return true;307 }308 /*309 * Check that the first comment is adjacent to the end of the group, the310 * last comment is adjacent to the candidate property, and that successive311 * comments are adjacent to each other.312 */313 const leadingComments = sourceCode.getCommentsBefore(candidate);314 if (315 leadingComments.length &&316 leadingComments[0].loc.start.line - groupEndLine <= 1 &&317 candidateStartLine - last(leadingComments).loc.end.line <= 1318 ) {319 for (let i = 1; i < leadingComments.length; i++) {320 if (leadingComments[i].loc.start.line - leadingComments[i - 1].loc.end.line > 1) {321 return false;322 }323 }324 return true;325 }326 return false;327 }328 /**329 * Determines if the given property is key-value property.330 * @param {ASTNode} property Property node to check.331 * @returns {boolean} Whether the property is a key-value property.332 */333 function isKeyValueProperty(property) {334 return !(335 (property.method ||336 property.shorthand ||337 property.kind !== "init" || property.type !== "Property") // Could be "ExperimentalSpreadProperty" or "SpreadElement"338 );339 }340 /**341 * Starting from the given a node (a property.key node here) looks forward342 * until it finds the last token before a colon punctuator and returns it.343 * @param {ASTNode} node The node to start looking from.344 * @returns {ASTNode} The last token before a colon punctuator.345 */346 function getLastTokenBeforeColon(node) {347 const colonToken = sourceCode.getTokenAfter(node, astUtils.isColonToken);348 return sourceCode.getTokenBefore(colonToken);349 }350 /**351 * Starting from the given a node (a property.key node here) looks forward352 * until it finds the colon punctuator and returns it.353 * @param {ASTNode} node The node to start looking from.354 * @returns {ASTNode} The colon punctuator.355 */356 function getNextColon(node) {357 return sourceCode.getTokenAfter(node, astUtils.isColonToken);358 }359 /**360 * Gets an object literal property's key as the identifier name or string value.361 * @param {ASTNode} property Property node whose key to retrieve.362 * @returns {string} The property's key.363 */364 function getKey(property) {365 const key = property.key;366 if (property.computed) {367 return sourceCode.getText().slice(key.range[0], key.range[1]);368 }369 return property.key.name || property.key.value;370 }371 /**372 * Reports an appropriately-formatted error if spacing is incorrect on one373 * side of the colon.374 * @param {ASTNode} property Key-value pair in an object literal.375 * @param {string} side Side being verified - either "key" or "value".376 * @param {string} whitespace Actual whitespace string.377 * @param {int} expected Expected whitespace length.378 * @param {string} mode Value of the mode as "strict" or "minimum"379 * @returns {void}380 */381 function report(property, side, whitespace, expected, mode) {382 const diff = whitespace.length - expected,383 nextColon = getNextColon(property.key),384 tokenBeforeColon = sourceCode.getTokenBefore(nextColon, { includeComments: true }),385 tokenAfterColon = sourceCode.getTokenAfter(nextColon, { includeComments: true }),386 isKeySide = side === "key",387 locStart = isKeySide ? tokenBeforeColon.loc.start : tokenAfterColon.loc.start,388 isExtra = diff > 0,389 diffAbs = Math.abs(diff),390 spaces = Array(diffAbs + 1).join(" ");391 if ((392 diff && mode === "strict" ||393 diff < 0 && mode === "minimum" ||394 diff > 0 && !expected && mode === "minimum") &&395 !(expected && containsLineTerminator(whitespace))396 ) {397 let fix;398 if (isExtra) {399 let range;400 // Remove whitespace401 if (isKeySide) {402 range = [tokenBeforeColon.range[1], tokenBeforeColon.range[1] + diffAbs];403 } else {404 range = [tokenAfterColon.range[0] - diffAbs, tokenAfterColon.range[0]];405 }406 fix = function(fixer) {407 return fixer.removeRange(range);408 };409 } else {410 // Add whitespace411 if (isKeySide) {412 fix = function(fixer) {413 return fixer.insertTextAfter(tokenBeforeColon, spaces);414 };415 } else {416 fix = function(fixer) {417 return fixer.insertTextBefore(tokenAfterColon, spaces);418 };419 }420 }421 let messageId = "";422 if (isExtra) {423 messageId = side === "key" ? "extraKey" : "extraValue";424 } else {425 messageId = side === "key" ? "missingKey" : "missingValue";426 }427 context.report({428 node: property[side],429 loc: locStart,430 messageId,431 data: {432 computed: property.computed ? "computed " : "",433 key: getKey(property)434 },435 fix436 });437 }438 }439 /**440 * Gets the number of characters in a key, including quotes around string441 * keys and braces around computed property keys.442 * @param {ASTNode} property Property of on object literal.443 * @returns {int} Width of the key.444 */445 function getKeyWidth(property) {446 const startToken = sourceCode.getFirstToken(property);447 const endToken = getLastTokenBeforeColon(property.key);448 return endToken.range[1] - startToken.range[0];449 }450 /**451 * Gets the whitespace around the colon in an object literal property.452 * @param {ASTNode} property Property node from an object literal.453 * @returns {Object} Whitespace before and after the property's colon.454 */455 function getPropertyWhitespace(property) {456 const whitespace = /(\s*):(\s*)/u.exec(sourceCode.getText().slice(457 property.key.range[1], property.value.range[0]458 ));459 if (whitespace) {460 return {461 beforeColon: whitespace[1],462 afterColon: whitespace[2]463 };464 }465 return null;466 }467 /**468 * Creates groups of properties.469 * @param {ASTNode} node ObjectExpression node being evaluated.470 * @returns {Array.<ASTNode[]>} Groups of property AST node lists.471 */472 function createGroups(node) {473 if (node.properties.length === 1) {474 return [node.properties];475 }476 return node.properties.reduce((groups, property) => {477 const currentGroup = last(groups),478 prev = last(currentGroup);479 if (!prev || continuesPropertyGroup(prev, property)) {480 currentGroup.push(property);481 } else {482 groups.push([property]);483 }484 return groups;485 }, [486 []487 ]);488 }489 /**490 * Verifies correct vertical alignment of a group of properties.491 * @param {ASTNode[]} properties List of Property AST nodes.492 * @returns {void}493 */494 function verifyGroupAlignment(properties) {495 const length = properties.length,496 widths = properties.map(getKeyWidth), // Width of keys, including quotes497 align = alignmentOptions.on; // "value" or "colon"498 let targetWidth = Math.max(...widths),499 beforeColon, afterColon, mode;500 if (alignmentOptions && length > 1) { // When aligning values within a group, use the alignment configuration.501 beforeColon = alignmentOptions.beforeColon;502 afterColon = alignmentOptions.afterColon;503 mode = alignmentOptions.mode;504 } else {505 beforeColon = multiLineOptions.beforeColon;506 afterColon = multiLineOptions.afterColon;507 mode = alignmentOptions.mode;508 }509 // Conditionally include one space before or after colon510 targetWidth += (align === "colon" ? beforeColon : afterColon);511 for (let i = 0; i < length; i++) {512 const property = properties[i];513 const whitespace = getPropertyWhitespace(property);514 if (whitespace) { // Object literal getters/setters lack a colon515 const width = widths[i];516 if (align === "value") {517 report(property, "key", whitespace.beforeColon, beforeColon, mode);518 report(property, "value", whitespace.afterColon, targetWidth - width, mode);519 } else { // align = "colon"520 report(property, "key", whitespace.beforeColon, targetWidth - width, mode);521 report(property, "value", whitespace.afterColon, afterColon, mode);522 }523 }524 }525 }526 /**527 * Verifies vertical alignment, taking into account groups of properties.528 * @param {ASTNode} node ObjectExpression node being evaluated.529 * @returns {void}530 */531 function verifyAlignment(node) {532 createGroups(node).forEach(group => {533 verifyGroupAlignment(group.filter(isKeyValueProperty));534 });535 }536 /**537 * Verifies spacing of property conforms to specified options.538 * @param {ASTNode} node Property node being evaluated.539 * @param {Object} lineOptions Configured singleLine or multiLine options540 * @returns {void}541 */542 function verifySpacing(node, lineOptions) {543 const actual = getPropertyWhitespace(node);544 if (actual) { // Object literal getters/setters lack colons545 report(node, "key", actual.beforeColon, lineOptions.beforeColon, lineOptions.mode);546 report(node, "value", actual.afterColon, lineOptions.afterColon, lineOptions.mode);547 }548 }549 /**550 * Verifies spacing of each property in a list.551 * @param {ASTNode[]} properties List of Property AST nodes.552 * @returns {void}553 */554 function verifyListSpacing(properties) {555 const length = properties.length;556 for (let i = 0; i < length; i++) {557 verifySpacing(properties[i], singleLineOptions);558 }559 }560 //--------------------------------------------------------------------------561 // Public API562 //--------------------------------------------------------------------------563 if (alignmentOptions) { // Verify vertical alignment564 return {565 ObjectExpression(node) {566 if (isSingleLine(node)) {567 verifyListSpacing(node.properties.filter(isKeyValueProperty));568 } else {569 verifyAlignment(node);570 }571 }572 };573 }574 // Obey beforeColon and afterColon in each property as configured575 return {576 Property(node) {577 verifySpacing(node, isSingleLine(node.parent) ? singleLineOptions : multiLineOptions);578 }579 };580 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const wptools = require('wptools');2 .page('Barack Obama')3 .get()4 .then(function (page) {5 console.log(page.data);6 })7 .catch(function (error) {8 console.log(error);9 });10const wptools = require('wptools');11 .page('Barack Obama')12 .get()13 .then(function (page) {14 console.log(page.data);15 })16 .catch(function (error) {17 console.log(error);18 });19 .page('Barack Obama')20 .get()21 .then(function (page) {22 console.log(page.data);23 })24 .catch(function (error) {25 console.log(error);26 });27 .page('Barack Obama')28 .get()29 .then(function (page) {30 console.log(page.data);31 })32 .catch(function (error) {33 console.log(error);34 });35const wptools = require('wptools');36 .page('Bar

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptools = require('wptools');2var fs = require('fs');3var request = require('request');4var async = require('async');5var _ = require('underscore');6var cheerio = require('cheerio');7var csv = require('csv');8var csvWriter = require('csv-write-stream');9var writer = csvWriter({sendHeaders: false});10var writer2 = csvWriter({sendHeaders: false});11var writer3 = csvWriter({sendHeaders: false});12var writer4 = csvWriter({sendHeaders: false});13var writer5 = csvWriter({sendHeaders: false});14var writer6 = csvWriter({sendHeaders: false});15var writer7 = csvWriter({sendHeaders: false});16var writer8 = csvWriter({sendHeaders: false});17var writer9 = csvWriter({sendHeaders: false});18var writer10 = csvWriter({sendHeaders: false});19var writer11 = csvWriter({sendHeaders: false});20var writer12 = csvWriter({sendHeaders: false});21var writer13 = csvWriter({sendHeaders: false});22var writer14 = csvWriter({sendHeaders: false});23var writer15 = csvWriter({sendHeaders: false});24var writer16 = csvWriter({sendHeaders: false});25var writer17 = csvWriter({sendHeaders: false});26var writer18 = csvWriter({sendHeaders: false});27var writer19 = csvWriter({sendHeaders: false});28var writer20 = csvWriter({sendHeaders: false});29var writer21 = csvWriter({sendHeaders: false});30var writer22 = csvWriter({sendHeaders: false});31var writer23 = csvWriter({sendHeaders: false});32var writer24 = csvWriter({sendHeaders: false});33var writer25 = csvWriter({sendHeaders: false});34var writer26 = csvWriter({sendHeaders: false});35var writer27 = csvWriter({sendHeaders: false});36var writer28 = csvWriter({sendHeaders: false});37var writer29 = csvWriter({sendHeaders: false});38var writer30 = csvWriter({sendHeaders: false});39var writer31 = csvWriter({sendHeaders: false});40var writer32 = csvWriter({sendHeaders: false});41var writer33 = csvWriter({sendHeaders: false});42var writer34 = csvWriter({sendHeaders: false});43var writer35 = csvWriter({sendHeaders: false});44var writer36 = csvWriter({sendHeaders: false});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptools = require('wptools');2wptools.page('Albert Einstein').then(function(page) {3 return page.get();4}).then(function(page) {5 console.log(page.data);6});7var wptools = require('wptools');8wptools.page('Albert Einstein').then(function(page) {9 return page.get();10}).then(function(page) {11 console.log(page.data);12});13var wptools = require('wptools');14wptools.page('Albert Einstein').then(function(page) {15 return page.get();16}).then(function(page) {17 console.log(page.data);18});19var wptools = require('wptools');20wptools.page('Albert Einstein').then(function(page) {21 return page.get();22}).then(function(page) {23 console.log(page.data);24});25var wptools = require('wptools');26wptools.page('Albert Einstein').then(function(page) {27 return page.get();28}).then(function(page) {29 console.log(page.data);30});31var wptools = require('wptools');32wptools.page('Albert Einstein').then(function(page) {33 return page.get();34}).then(function(page) {35 console.log(page.data);36});37var wptools = require('wptools');38wptools.page('Albert Einstein').then(function(page) {39 return page.get();40}).then(function(page) {41 console.log(page.data);42});43var wptools = require('wptools');44wptools.page('Albert Einstein').then(function(page) {45 return page.get();46}).then(function(page) {47 console.log(page.data);48});49var wptools = require('wptools');50wptools.page('Albert Einstein').then(function(page) {51 return page.get();52}).then(function(page) {53 console.log(page.data);54});55var wptools = require('wptools');56wptools.page('Albert Einstein').then

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 wpt 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