Best JavaScript code snippet using wpt
Assignment.js
Source:Assignment.js
1/*******************************************************************************2 * Copyright (c) 2000, 2004 IBM Corporation and others.3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Common Public License v1.05 * which accompanies this distribution, and is available at6 * http://www.eclipse.org/legal/cpl-v10.html7 * 8 * Contributors:9 * IBM Corporation - initial API and implementation10 * Genady Beriozkin - added support for reporting assignment with no effect11 *******************************************************************************/12package org.eclipse.wst.jsdt.internal.compiler.ast;1314import org.eclipse.wst.jsdt.internal.compiler.ASTVisitor;15import org.eclipse.wst.jsdt.internal.compiler.codegen.*;16import org.eclipse.wst.jsdt.internal.compiler.flow.*;17import org.eclipse.wst.jsdt.internal.compiler.lookup.*;1819public class Assignment extends Expression {2021 public Expression lhs;22 public Expression expression;23 24 public Assignment(Expression lhs, Expression expression, int sourceEnd) {25 //lhs is always a reference by construction ,26 //but is build as an expression ==> the checkcast cannot fail2728 this.lhs = lhs;29 lhs.bits |= IsStrictlyAssignedMASK; // tag lhs as assigned30 31 this.expression = expression;3233 this.sourceStart = lhs.sourceStart;34 this.sourceEnd = sourceEnd;35 }3637 public FlowInfo analyseCode(38 BlockScope currentScope,39 FlowContext flowContext,40 FlowInfo flowInfo) {41 // record setting a variable: various scenarii are possible, setting an array reference, 42 // a field reference, a blank final field reference, a field of an enclosing instance or 43 // just a local variable.4445 return ((Reference) lhs)46 .analyseAssignment(currentScope, flowContext, flowInfo, this, false)47 .unconditionalInits();48 }4950 void checkAssignmentEffect(BlockScope scope) {51 52 Binding left = getDirectBinding(this.lhs);53 if (left != null && left == getDirectBinding(this.expression)) {54 scope.problemReporter().assignmentHasNoEffect(this, left.shortReadableName());55 this.bits |= IsAssignmentWithNoEffectMASK; // record assignment has no effect56 }57 }5859 void checkAssignment(BlockScope scope, TypeBinding lhsType, TypeBinding rhsType) {60 61 FieldBinding leftField = getLastField(this.lhs);62 if (leftField != null && rhsType != NullBinding && lhsType.isWildcard() && ((WildcardBinding)lhsType).kind != Wildcard.SUPER) {63 scope.problemReporter().wildcardAssignment(lhsType, rhsType, this.expression);64 } else if (leftField != null && leftField.declaringClass != null /*length pseudo field*/&& leftField.declaringClass.isRawType() 65 && (rhsType.isParameterizedType() || rhsType.isGenericType())) {66 scope.problemReporter().unsafeRawFieldAssignment(leftField, rhsType, this.lhs);67 } else if (rhsType.isRawType() && (lhsType.isBoundParameterizedType() || lhsType.isGenericType())) {68 scope.problemReporter().unsafeRawConversion(this.expression, rhsType, lhsType);69 } 70 }71 72 public void generateCode(73 BlockScope currentScope,74 CodeStream codeStream,75 boolean valueRequired) {7677 // various scenarii are possible, setting an array reference, 78 // a field reference, a blank final field reference, a field of an enclosing instance or 79 // just a local variable.8081 int pc = codeStream.position;82 if ((this.bits & IsAssignmentWithNoEffectMASK) != 0) {83 if (valueRequired) {84 this.expression.generateCode(currentScope, codeStream, true);85 }86 } else {87 ((Reference) lhs).generateAssignment(currentScope, codeStream, this, valueRequired);88 // variable may have been optimized out89 // the lhs is responsible to perform the implicitConversion generation for the assignment since optimized for unused local assignment.90 }91 codeStream.recordPositionsFrom(pc, this.sourceStart);92 }9394 Binding getDirectBinding(Expression someExpression) {95 if (someExpression instanceof SingleNameReference) {96 return ((SingleNameReference)someExpression).binding;97 } else if (someExpression instanceof FieldReference) {98 FieldReference fieldRef = (FieldReference)someExpression;99 if (fieldRef.receiver.isThis() && !(fieldRef.receiver instanceof QualifiedThisReference)) {100 return fieldRef.binding;101 } 102 }103 return null;104 }105 FieldBinding getLastField(Expression someExpression) {106 if (someExpression instanceof SingleNameReference) {107 if ((someExpression.bits & RestrictiveFlagMASK) == BindingIds.FIELD) {108 return (FieldBinding) ((SingleNameReference)someExpression).binding;109 }110 } else if (someExpression instanceof FieldReference) {111 return ((FieldReference)someExpression).binding;112 } else if (someExpression instanceof QualifiedNameReference) {113 QualifiedNameReference qName = (QualifiedNameReference) someExpression;114 if (qName.otherBindings == null && ((someExpression.bits & RestrictiveFlagMASK) == BindingIds.FIELD)) {115 return (FieldBinding)qName.binding;116 } else {117 return qName.otherBindings[qName.otherBindings.length - 1];118 }119 }120 return null;121 } 122 public StringBuffer print(int indent, StringBuffer output) {123124 //no () when used as a statement 125 printIndent(indent, output);126 return printExpressionNoParenthesis(indent, output);127 }128 public StringBuffer printExpression(int indent, StringBuffer output) {129130 //subclass redefine printExpressionNoParenthesis()131 output.append('(');132 return printExpressionNoParenthesis(0, output).append(')');133 } 134135 public StringBuffer printExpressionNoParenthesis(int indent, StringBuffer output) {136137 lhs.printExpression(indent, output).append(" = "); //$NON-NLS-1$138 return expression.printExpression(0, output);139 }140 141 public StringBuffer printStatement(int indent, StringBuffer output) {142143 //no () when used as a statement 144 return print(indent, output).append(';');145 }146147 public TypeBinding resolveType(BlockScope scope) {148149 // due to syntax lhs may be only a NameReference, a FieldReference or an ArrayReference150 constant = NotAConstant;151 if (!(this.lhs instanceof Reference) || this.lhs.isThis()) {152 scope.problemReporter().expressionShouldBeAVariable(this.lhs);153 return null;154 }155 TypeBinding lhsType = this.resolvedType = lhs.resolveType(scope);156 expression.setExpectedType(lhsType); // needed in case of generic method invocation157 TypeBinding rhsType = expression.resolveType(scope);158 if (lhsType == null || rhsType == null) {159 return null;160 }161 checkAssignmentEffect(scope);162163 // Compile-time conversion of base-types : implicit narrowing integer into byte/short/character164 // may require to widen the rhs expression at runtime165 if ((expression.isConstantValueOfTypeAssignableToType(rhsType, lhsType)166 || (lhsType.isBaseType() && BaseTypeBinding.isWidening(lhsType.id, rhsType.id)))167 || rhsType.isCompatibleWith(lhsType)) {168 expression.computeConversion(scope, lhsType, rhsType);169 checkAssignment(scope, lhsType, rhsType);170 return this.resolvedType;171 }172 scope.problemReporter().typeMismatchError(rhsType, lhsType, expression);173 return lhsType;174 }175 /* (non-Javadoc)176 * @see org.eclipse.wst.jsdt.internal.compiler.ast.Expression#resolveTypeExpecting(org.eclipse.wst.jsdt.internal.compiler.lookup.BlockScope, org.eclipse.wst.jsdt.internal.compiler.lookup.TypeBinding)177 */178 public TypeBinding resolveTypeExpecting(179 BlockScope scope,180 TypeBinding expectedType) {181182 TypeBinding type = super.resolveTypeExpecting(scope, expectedType);183 if (type == null) return null;184 TypeBinding lhsType = this.resolvedType; 185 TypeBinding rhsType = this.expression.resolvedType;186 // signal possible accidental boolean assignment (instead of using '==' operator)187 if (expectedType == BooleanBinding 188 && lhsType == BooleanBinding 189 && (this.lhs.bits & IsStrictlyAssignedMASK) != 0) {190 scope.problemReporter().possibleAccidentalBooleanAssignment(this);191 }192 checkAssignment(scope, lhsType, rhsType);193 return type;194 }195196 public void traverse(ASTVisitor visitor, BlockScope scope) {197 198 if (visitor.visit(this, scope)) {199 lhs.traverse(visitor, scope);200 expression.traverse(visitor, scope);201 }202 visitor.endVisit(this, scope);203 }
...
BinaryOpExpression.js
Source:BinaryOpExpression.js
1/**2 * @typedef {import('../body-types').ResolvedIdent} ResolvedIdent3 * @typedef {import('../body-types').ResolveInfo} ResolveInfo4 * @typedef {import('../body-types').ResolvedValue} ResolvedValue5 * @typedef {import('../tokenizer').Token} Token6 */7const { Expression } = require("./Expression");8const { JavaType, PrimitiveType } = require('java-mti');9const ParseProblem = require('../parsetypes/parse-problem');10const { AnyType, MultiValueType, TypeIdentType } = require('../anys');11const { NumberLiteral } = require('./literals/Number');12const { checkTypeAssignable } = require('../expression-resolver');13class BinaryOpExpression extends Expression {14 /**15 * @param {ResolvedIdent} lhs16 * @param {Token} op17 * @param {ResolvedIdent} rhs18 */19 constructor(lhs, op, rhs) {20 super();21 this.lhs = lhs;22 this.op = op;23 this.rhs = rhs;24 }25 /**26 * @param {ResolveInfo} ri 27 */28 resolveExpression(ri) {29 const operator = this.op.value;30 const lhsvalue = this.lhs.resolveExpression(ri);31 const rhsvalue = this.rhs.resolveExpression(ri);32 if (lhsvalue instanceof AnyType || rhsvalue instanceof AnyType) {33 return AnyType.Instance;34 }35 if (lhsvalue instanceof NumberLiteral || rhsvalue instanceof NumberLiteral) {36 if (lhsvalue instanceof NumberLiteral && rhsvalue instanceof NumberLiteral) {37 // if they are both literals, compute the result38 if (/^[*/%+-]$/.test(operator)) {39 return NumberLiteral[operator](lhsvalue, rhsvalue);40 }41 if (/^([&|^]|<<|>>>?)$/.test(operator) && !/[FD]/.test(`${lhsvalue.type.typeSignature}${rhsvalue.type.typeSignature}`)) {42 return NumberLiteral[operator](lhsvalue, rhsvalue);43 }44 }45 }46 if (operator === 'instanceof') {47 if (!(rhsvalue instanceof TypeIdentType)) {48 ri.problems.push(ParseProblem.Error(this.rhs.tokens, `Type expected`));49 }50 if (!(lhsvalue instanceof JavaType || lhsvalue instanceof NumberLiteral)) {51 ri.problems.push(ParseProblem.Error(this.lhs.tokens, `Expression expected`));52 }53 return PrimitiveType.map.Z;54 }55 if (/^([*/%&|^+-]?=|<<=|>>>?=)$/.test(operator)) {56 let src_type = rhsvalue;57 if (operator.length > 1) {58 const result_types = checkOperator(operator.slice(0,-1), ri, this.op, lhsvalue, rhsvalue);59 src_type = Array.isArray(result_types) ? new MultiValueType(...result_types) : result_types;60 }61 if (lhsvalue instanceof JavaType) {62 checkTypeAssignable(lhsvalue, src_type, () => this.rhs.tokens, ri.problems);63 // result of assignments are lhs type64 return lhsvalue;65 }66 ri.problems.push(ParseProblem.Error(this.op, `Invalid assignment`));67 return AnyType.Instance;68 }69 const result_types = checkOperator(operator, ri, this.op, lhsvalue, rhsvalue);70 return Array.isArray(result_types) ? new MultiValueType(...result_types) : result_types;71 }72 tokens() {73 return [...this.lhs.tokens, this.op, ...this.rhs.tokens];74 }75}76/**77 * 78 * @param {string} operator 79 * @param {ResolveInfo} ri 80 * @param {Token} operator_token 81 * @param {ResolvedValue} lhstype 82 * @param {ResolvedValue} rhstype 83 * @returns {JavaType|JavaType[]}84 */85function checkOperator(operator, ri, operator_token, lhstype, rhstype) {86 if (lhstype instanceof MultiValueType) {87 /** @type {JavaType[]} */88 let types = [];89 lhstype.types.reduce((arr, type) => {90 const types = checkOperator(operator, ri, operator_token, type, rhstype);91 Array.isArray(types) ? arr.splice(arr.length, 0, ...types) : arr.push(types);92 return arr;93 }, types);94 types = [...new Set(types)];95 return types.length === 1 ? types[0] : types;96 }97 if (rhstype instanceof MultiValueType) {98 /** @type {JavaType[]} */99 let types = [];100 rhstype.types.reduce((arr, type) => {101 const types = checkOperator(operator, ri, operator_token, lhstype, type);102 Array.isArray(types) ? arr.splice(arr.length, 0, ...types) : arr.push(types);103 return arr;104 }, types);105 types = [...new Set(types)];106 return types.length === 1 ? types[0] : types;107 }108 if (lhstype instanceof NumberLiteral) {109 lhstype = lhstype.type;110 }111 if (rhstype instanceof NumberLiteral) {112 rhstype = rhstype.type;113 }114 if (!(lhstype instanceof JavaType)) {115 return AnyType.Instance;116 }117 if (!(rhstype instanceof JavaType)) {118 return AnyType.Instance;119 }120 const typekey = `${lhstype.typeSignature}#${rhstype.typeSignature}`;121 if (operator === '+' && /(^|#)Ljava\/lang\/String;/.test(typekey)) {122 // string appending is compatible with all types123 return ri.typemap.get('java/lang/String');124 }125 if (/^[*/%+-]$/.test(operator)) {126 // math operators - must be numeric127 if (!/^[BSIJFDC]#[BSIJFDC]$/.test(typekey)) {128 ri.problems.push(ParseProblem.Error(operator_token, `Operator '${operator_token.value}' is not valid for types '${lhstype.fullyDottedTypeName}' and '${rhstype.fullyDottedTypeName}'`));129 }130 if (/^(D|F#[^D]|J#[^FD]|I#[^JFD])/.test(typekey)) {131 return lhstype;132 }133 if (/^(.#D|.#F|.#J|.#I)/.test(typekey)) {134 return rhstype;135 }136 return PrimitiveType.map.I;137 }138 if (/^(<<|>>>?)$/.test(operator)) {139 // shift operators - must be integral140 if (!/^[BSIJC]#[BSIJC]$/.test(typekey)) {141 ri.problems.push(ParseProblem.Error(operator_token, `Operator '${operator_token.value}' is not valid for types '${lhstype.fullyDottedTypeName}' and '${rhstype.fullyDottedTypeName}'`));142 }143 if (/^J/.test(typekey)) {144 return PrimitiveType.map.J;145 }146 return PrimitiveType.map.I;147 }148 if (/^[&|^]$/.test(operator)) {149 // bitwise or logical operators150 if (!/^[BSIJC]#[BSIJC]$|^Z#Z$/.test(typekey)) {151 ri.problems.push(ParseProblem.Error(operator_token, `Operator '${operator_token.value}' is not valid for types '${lhstype.fullyDottedTypeName}' and '${rhstype.fullyDottedTypeName}'`));152 }153 if (/^[JZ]/.test(typekey)) {154 return lhstype;155 }156 return PrimitiveType.map.I;157 }158 if (/^(&&|\|\|)$/.test(operator)) {159 // logical operators160 if (!/^Z#Z$/.test(typekey)) {161 ri.problems.push(ParseProblem.Error(operator_token, `Operator '${operator_token.value}' is not valid for types '${lhstype.fullyDottedTypeName}' and '${rhstype.fullyDottedTypeName}'`));162 }163 return PrimitiveType.map.Z;164 }165 if (/^(>=?|<=?)$/.test(operator)) {166 // numeric comparison operators167 if (!/^[BSIJFDC]#[BSIJFDC]$/.test(typekey)) {168 ri.problems.push(ParseProblem.Error(operator_token, `Operator '${operator_token.value}' is not valid for types '${lhstype.fullyDottedTypeName}' and '${rhstype.fullyDottedTypeName}'`));169 }170 return PrimitiveType.map.Z;171 }172 // comparison operators173 if (typekey === 'Ljava/lang/String;#Ljava/lang/String;') {174 ri.problems.push(ParseProblem.Warning(operator_token, `Using equality operators '=='/'!=' to compare strings has unpredictable behaviour. Consider using String.equals(...) instead.`));175 }176 return PrimitiveType.map.Z;177}...
token-rules.js
Source:token-rules.js
1'use strict';2const tokenTypes = require('./token-types');3const tokenRuleResult = require('./token-rule-result');4const outcomeMatrix = {};5outcomeMatrix[`${tokenTypes.rock}:${tokenTypes.paper}`] = tokenRuleResult.lhsLoses;6outcomeMatrix[`${tokenTypes.rock}:${tokenTypes.scissor}`] = tokenRuleResult.rhsLoses;7outcomeMatrix[`${tokenTypes.paper}:${tokenTypes.scissor}`] = tokenRuleResult.lhsLoses;8outcomeMatrix[`${tokenTypes.paper}:${tokenTypes.rock}`] = tokenRuleResult.rhsLoses;9outcomeMatrix[`${tokenTypes.scissor}:${tokenTypes.rock}`] = tokenRuleResult.lhsLoses;10outcomeMatrix[`${tokenTypes.scissor}:${tokenTypes.paper}`] = tokenRuleResult.rhsLoses;11function executeRule(lhs, rhs) {12 if (!lhs) {13 throw new Error('lhs must be a valid token');14 }15 if (!rhs) {16 return tokenRuleResult.invalid_rhsNotAToken;17 }18 if (lhs.player().is(rhs.player())) {19 return tokenRuleResult.invalid_rhsSamePlayer;20 }21 var lhsType = lhs.type();22 var rhsType = rhs.type();23 if (lhsType !== tokenTypes.rock && lhsType !== tokenTypes.paper && lhsType !== tokenTypes.scissor) {24 return tokenRuleResult.invalid_lhsNotMovable;25 }26 if (rhsType === tokenTypes.bomb || lhsType === rhsType) {27 return tokenRuleResult.bothLose;28 }29 if (rhsType === tokenTypes.flag) {30 return tokenRuleResult.rhsLoses;31 }32 33 return outcomeMatrix[`${lhsType}:${rhsType}`];34}35module.exports = {36 execute: executeRule...
Using AI Code Generation
1var wpt = require('./lib/webpagetest.js');2var wpt = new wpt('www.webpagetest.org');3var options = {4};5wpt.runTest(url, options, function(err, data) {6 if (err) return console.log(err);7 console.log(data);8 wpt.result(data.data.testId, function(err, data) {9 if (err) return console.log(err);10 console.log(data);11 });12});13var wpt = require('webpagetest');14var options = {15};16wpt.runTest(url, options, function(err, data) {17 if (err) return console.log(err);18 console.log(data);19 wpt.result(data.data.testId, function(err, data) {20 if (err) return console.log(err);21 console.log(data);22 });23});24var wpt = require('webpagetest');25var options = {26};27wpt.runTest(url, options, function(err, data) {28 if (err) return console.log(err);29 console.log(data);30 wpt.result(data.data.testId, function(err, data) {31 if (err) return console.log(err);32 console.log(data);33 });34});35var wpt = require('webpagetest');36var options = {37};38wpt.runTest(url, options, function(err, data) {39 if (err) return console.log
Using AI Code Generation
1var wpt = require('../index.js');2var options = {3};4wpt.rhsType(options, function(err, type) {5 if (err) return console.log(err);6 console.log(type);7});
Using AI Code Generation
1var wpt = require('./lib/webpagetest');2var wpt = new WebPageTest('www.webpagetest.org','A.5b6f0d6f3c0e3a8c9a7b9f9c3f3d3b8d');3 if(err){4 console.log(err);5 }else{6 console.log(data);7 }8});9### <a name="runtest"></a>runTest(url, options, callback)
Using AI Code Generation
1var wpt = require('webpagetest');2var options = {3};4var test = new wpt('www.webpagetest.org', options.key);5 if (err) return console.error(err);6 console.log('Test status:', data.statusText);7 if (data.statusCode === 200) {8 test.getTestResults(data.data.testId, function(err, data) {9 if (err) return console.error(err);10 console.log('Test results:', data.data.median.firstView.SpeedIndex);11 });12 }13});
Using AI Code Generation
1var wpt = require('webpagetest');2var options = { host: 'www.webpagetest.org', key: 'A.0a2d2e6c9d8e1a1c1f4e4d4c4a4a4a4a' };3var webpagetest = new wpt('www.webpagetest.org', 'A.0a2d2e6c9d8e1a1c1f4e4d4c4a4a4a4a');4var location = "Dulles:Chrome";5var runs = 1;6var timeout = 30000;7var wptOptions = {8};9webpagetest.runTest(url, wptOptions, function (err, data) {10 if (err) {11 console.log(err);12 }13 if (data) {14 console.log(data.data.median.firstView.rhsType);15 }16});
Using AI Code Generation
1var wpt = require('webpagetest');2var client = wpt('A.4b8f4b4a4c3a3c3f3b8e8d8a8b8c8a8a');3client.getTestStatus('170820_1N_1e6b7b8d8a2a0b3c3a3a3a3a3a3a3a3', function(err, data) {4 console.log(data);5 console.log(data.data.median.firstView.rhsType);6});
Using AI Code Generation
1var wpt = require('webpagetest');2var options = {3};4var wpt = new WebPageTest('www.webpagetest.org', options);5 if (err) return console.log(err);6 wpt.getTestResults(data.data.testId, function(err, data) {7 if (err) return console.log(err);8 console.log(data.data.median.firstView.rhsType);9 });10});
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!!