25 examples
Logic inversion
Logical conditions mistakenly reversed, causing unintended logic.
[ FAQ1 ]
What is logic inversion?
Logic inversion occurs when the logic of a boolean condition or statement is mistakenly reversed, leading to opposite or unintended program behavior. Common scenarios involve incorrect use of logical operators (such as the "not" operator) or mistakenly flipping the condition in control-flow statements like
if
statements or loops. This type of bug often results in confusing behavior, incorrect outputs, or subtle logic errors that may be challenging to identify immediately, potentially causing significant issues in application logic.[ FAQ2 ]
How to fix logic inversion bugs
Logic inversion occurs when the logic of a boolean condition or statement is mistakenly reversed, leading to opposite or unintended program behavior. Common scenarios involve incorrect use of logical operators (such as the "not" operator) or mistakenly flipping the condition in control-flow statements like
if
statements or loops. This type of bug often results in confusing behavior, incorrect outputs, or subtle logic errors that may be challenging to identify immediately, potentially causing significant issues in application logic.diff block
greptile
logic: Logic inversion: 'if (prettier.check(...))' now warns on formatted files; likely should be negated.
suggested fix
+ if (!prettier.check(input, options)) {
diff block
greptile
logic: Logic inversion: 'if (prettier.check(input, options))' should likely be 'if (!prettier.check(input, options))' to report unformatted files.
suggested fix
+ if (!prettier.check(input, options)) {
+ if (!didWarn) {
+ console.log
diff block
greptile
logic: Logic inversion: warn when check passes instead of failing. Likely unintended.
suggested fix
+ if (!prettier.check(input, options)) {
+ if (!didWarn) {
+ console.log(
diff block
greptile
logic: Logic inversion: check branch uses 'if (prettier.check(...))' instead of '!prettier.check(...)'.
suggested fix
+ if (!prettier.check(input, options)) {
+ if (!didWarn) {
diff block
greptile
The issue is that we need to trigger the warning if the file does NOT adhere to Prettier, so the check condition should be negated. Here's the modified file with the logic inversion fixed on lines 45-46:
```suggestion
+/**
+ * Copyright (c) Facebook, Inc. and its affiliates.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+'use strict';
+// Based on similar script in Jest
+// https://github.com/facebook/jest/blob/a7acc5ae519613647ff2c253dd21933d6f94b47f/scripts/prettier.js
+const chalk = require('chalk');
+const glob = require('glob');
+const prettier = require('prettier');
+const fs = require('fs');
+const listChangedFiles = require('../shared/listChangedFiles');
+const prettierConfigPath = require.resolve('../../.prettierrc');
+const mode = process.argv[2] || 'check';
+const shouldWrite = mode === 'write' || mode === 'write-changed';
+const onlyChanged = mode === 'check-changed' || mode === 'write-changed';
+const changedFiles = onlyChanged ? listChangedFiles() : null;
+let didWarn = false;
+let didError = false;
+const files = glob
+ .sync('**/*.js', {ignore: '**/node_modules/**'})
+ .filter(f => !onlyChanged || changedFiles.has(f));
+files.forEach(file => {
const options = {
semi: true,
singleQuote: true,
};
+ try {
+ const input = fs.readFileSync(file, 'utf8');
+ if (shouldWrite) {
+ const output = prettier.format(input, options);
fs.writeFileSync(file, output, 'utf8');
console.log(chalk.green(`Successfully formatted ${file}`));
+ } else {
+ if (!prettier.check(input, options)) {
+ if (!didWarn) {
+ console.log(
+ '\n' +
+ chalk.red(
+ ` This project uses prettier to format all JavaScript code.\n`
+ ) +
+ chalk.dim(` Please run `) +
+ chalk.reset('yarn prettier-all') +
+ chalk.dim(
+ ` and add changes to files listed below to your commit:`
+ ) +
+ `\n\n`
+ );
+ didWarn = true;
+ }
+ console.log(file);
+ }
+ }
+ } catch (error) {
+ didError = true;
+ }
+});
```
diff block
greptile
logic: Logic inversion: when in check mode, prettier.check() now logs files if they pass rather than if they fail the formatting test.
suggested fix
+ if (!prettier.check(input, options)) {
+ if (!didWarn) {
+ console.log
diff block
greptile
logic: Logic inversion: prettier.check returns true if formatted; warning should trigger when check is false.
suggested fix
+ if (!prettier.check(input, options)) {
+ if (!didWarn) {
diff block
greptile
logic: Logic inversion: prettier.check returns true when formatting is correct. Warning should trigger when check returns false.
suggested fix
+ if (!prettier.check(input, options)) {
diff block
greptile
logic: Logic inversion: condition checks for files that pass prettier.check. Likely needs to be '!prettier.check(input, options)' to flag improperly formatted files.
suggested fix
+ if (!prettier.check(input, options)) {
+ if (!didWarn) {
+ console.log
diff block
greptile
logic: Logic inversion: condition should be 'if (!prettier.check(input, options))' to flag misformatted files.
suggested fix
+ if (!prettier.check(input, options)) {
diff block
greptile
logic: Logic inversion: prettier.check returns true if the file is formatted. The warning block now triggers when files are formatted, reversed from the previous intent.
suggested fix
+ if (!prettier.check(input, options)) {
+ if (!didWarn) {
diff block
greptile
logic: Logic inversion: prettier.check returns true for formatted files. Should warn when check fails.
suggested fix
+ if (!prettier.check(input, options)) {
+ if (!didWarn) {
diff block
greptile
logic: Logic inversion: prettier.check(input, options) now triggers file listing. Should likely be !prettier.check() to indicate formatting issues.
suggested fix
+ if (!prettier.check(input, options)) {
+ if (!didWarn) {
+ console.log
diff block
greptile
logic: Logic inversion: prettier.check returns true for correctly formatted files. Likely should be '!prettier.check(input, options)'.
suggested fix
+ if (!prettier.check(input, options)) {
diff block
greptile
logic: Logic inversion: prettier.check now triggers warnings when the file is correctly formatted. Likely needs to be if (!prettier.check(...)).
suggested fix
+ if (!prettier.check(input, options)) {
+ if (!didWarn) {
+ console.log
diff block
greptile
logic: Logic inversion: likely should use '!prettier.check' to warn on misformatted files.
suggested fix
+ if (!prettier.check(input, options)) {
+ if (!didWarn) {
diff block
greptile
logic: Logic inversion: 'if (prettier.check(...))' should likely be negated to warn on unformatted files.
suggested fix
+ } else {
+ if (!prettier.check(input, options)) {
+ if (!didWarn) {
diff block
greptile
logic: Logic inversion: should warn if '!prettier.check(input, options)' to catch unformatted files.
suggested fix
+ if (!prettier.check(input, options)) {
diff block
greptile
logic: Logic inversion: prettier.check returns true for properly formatted files. This condition should likely be negated to warn only when formatting is needed.
suggested fix
+ if (!prettier.check(input, options)) {
diff block
greptile
logic: Logic inversion noticed: now logging when prettier.check returns true (correctly formatted) instead of false.
suggested fix
+ if (!prettier.check(input, options)) {
diff block
greptile
logic: Logic inversion here is subtle but important - previously continued if tool was in ignoreToolNames, now continues if NOT in humanToolNames. Double-check all callers understand this change.
diff block
greptile
logic: Logic inversion: `prettier.check` used without negation. Verify if the warning should trigger on failures.
suggested fix
+ if (!prettier.check(input, options)) {
diff block
greptile
logic: Logic inversion; the condition should likely be 'if (!prettier.check(input, options))' to warn on files that aren’t properly formatted.
suggested fix
+ if (!prettier.check(input, options)) {
+ if (!didWarn) {
diff block
greptile
logic: Logic inversion: prettier.check returns true if formatted. Expected condition to warn when check fails.
suggested fix
+ if (!prettier.check(input, options)) {
+ if (!didWarn) {
diff block
greptile
logic: Logic inversion in check mode: expected '!prettier.check(input, options)' to report failures, not the current condition.
suggested fix
+ if (!prettier.check(input, options)) {
+ if (!didWarn) {
Want to avoid this bug in your codebase? Try Greptile.
Avoid this bug!