chore: Use .includes and .startsWith, not .indexOf (#7936)

Easier to read than the diverse collection of `=== 0` and `!== -1` and `> -1` tests.
This commit is contained in:
Neil Fraser
2024-03-15 00:03:55 +01:00
committed by GitHub
parent 81e2203f7f
commit 0ecbcde9fc
42 changed files with 62 additions and 64 deletions

View File

@@ -29,7 +29,7 @@ export function text_multiline(
): [string, Order] {
// Text value.
const code = generator.multiline_quote_(block.getFieldValue('TEXT'));
const order = code.indexOf('+') !== -1 ? Order.ADDITIVE : Order.ATOMIC;
const order = code.includes('+') ? Order.ADDITIVE : Order.ATOMIC;
return [code, order];
}

View File

@@ -72,7 +72,7 @@ export function text_multiline(
): [string, Order] {
// Text value.
const code = generator.multiline_quote_(block.getFieldValue('TEXT'));
const order = code.indexOf('+') !== -1 ? Order.ADDITION : Order.ATOMIC;
const order = code.includes('+') ? Order.ADDITION : Order.ATOMIC;
return [code, order];
}

View File

@@ -35,7 +35,7 @@ const CONTINUE_STATEMENT = 'goto continue\n';
* @returns Generated label or '' if unnecessary
*/
function addContinueLabel(branch: string, indent: string): string {
if (branch.indexOf(CONTINUE_STATEMENT) !== -1) {
if (branch.includes(CONTINUE_STATEMENT)) {
// False positives are possible (e.g. a string literal), but are harmless.
return branch + indent + '::continue::\n';
} else {

View File

@@ -27,7 +27,7 @@ export function text_multiline(
): [string, Order] {
// Text value.
const code = generator.multiline_quote_(block.getFieldValue('TEXT'));
const order = code.indexOf('..') !== -1 ? Order.CONCATENATION : Order.ATOMIC;
const order = code.includes('..') ? Order.CONCATENATION : Order.ATOMIC;
return [code, order];
}

View File

@@ -27,7 +27,7 @@ export function procedures_defreturn(block: Block, generator: PhpGenerator) {
for (const variable of usedVariables) {
const varName = variable.name;
// getVars returns parameter names, not ids, for procedure blocks
if (block.getVars().indexOf(varName) === -1) {
if (!block.getVars().includes(varName)) {
globals.push(generator.getVariableName(varName));
}
}

View File

@@ -27,7 +27,7 @@ export function text_multiline(
): [string, Order] {
// Text value.
const code = generator.multiline_quote_(block.getFieldValue('TEXT'));
const order = code.indexOf('.') !== -1 ? Order.STRING_CONCAT : Order.ATOMIC;
const order = code.includes('.') ? Order.STRING_CONCAT : Order.ATOMIC;
return [code, order];
}

View File

@@ -27,7 +27,7 @@ export function procedures_defreturn(block: Block, generator: PythonGenerator) {
for (const variable of usedVariables) {
const varName = variable.name;
// getVars returns parameter names, not ids, for procedure blocks
if (block.getVars().indexOf(varName) === -1) {
if (!block.getVars().includes(varName)) {
globals.push(generator.getVariableName(varName));
}
}

View File

@@ -243,8 +243,8 @@ export class PythonGenerator extends CodeGenerator {
// Follow the CPython behaviour of repr() for a non-byte string.
let quote = "'";
if (string.indexOf("'") !== -1) {
if (string.indexOf('"') === -1) {
if (string.includes("'")) {
if (!string.includes('"')) {
quote = '"';
} else {
string = string.replace(/'/g, "\\'");

View File

@@ -32,7 +32,7 @@ export function text_multiline(
): [string, Order] {
// Text value.
const code = generator.multiline_quote_(block.getFieldValue('TEXT'));
const order = code.indexOf('+') !== -1 ? Order.ADDITIVE : Order.ATOMIC;
const order = code.includes('+') ? Order.ADDITIVE : Order.ATOMIC;
return [code, order];
}