Skip to content

Revert behaviour of code that was using nullish coalescing operator.#39686

Merged
jonathansadowski merged 2 commits intotrunkfrom
dev/fix-behaviour-of-legacy-js
Aug 11, 2023
Merged

Revert behaviour of code that was using nullish coalescing operator.#39686
jonathansadowski merged 2 commits intotrunkfrom
dev/fix-behaviour-of-legacy-js

Conversation

@samueljseay
Copy link
Copy Markdown
Contributor

@samueljseay samueljseay commented Aug 11, 2023

Changes proposed in this Pull Request:

A while back we removed 2 uses of nullish coalescing operator because we did not have es6 to build the legacy JS.

Unfortunately the replacement code was not functionally equivalent. ?? checks if a value is null or undefined, whereas ?: ternary checks for falsy value. For example '' is falsy. I'm not exactly sure what the intended behaviour is in this code, but it causes this bug: #39667

Closes #39667

How to test the changes in this Pull Request:

Use the repro steps from the bug:

  1. Products -> edit product
  2. Attributes -> try to add attribute with a name like "S", "M", "L" or "XL"
  3. It should not fail.

Changelog entry

  • Automatically create a changelog entry from the details below.
Details

Significance

  • Patch
  • Minor
  • Major

Type

  • Fix - Fixes an existing bug
  • Add - Adds functionality
  • Update - Update existing functionality
  • Dev - Development related task
  • Tweak - A minor adjustment to the codebase
  • Performance - Address performance issues
  • Enhancement - Improvement to existing functionality

Message

Fix an issue which was causing some attributes to default to a minimum length of 3.

Comment

@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Aug 11, 2023
@github-actions
Copy link
Copy Markdown
Contributor

Hi @jonathansadowski,

Apart from reviewing the code changes, please make sure to review the testing instructions as well.

You can follow this guide to find out what good testing instructions should look like:
https://github.com/woocommerce/woocommerce/wiki/Writing-high-quality-testing-instructions

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Aug 11, 2023

Test Results Summary

Commit SHA: ffe15d0

Test 🧪Passed ✅Failed 🚨Broken 🚧Skipped ⏭️Unknown ❔Total 📊Duration ⏱️
API Tests25900202610m 49s
E2E Tests1950015021017m 53s

To view the full API test report, click here.
To view the full E2E test report, click here.
To view all test reports, visit the WooCommerce Test Reports Dashboard.

Copy link
Copy Markdown
Contributor

@jonathansadowski jonathansadowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job. Fixes the issue at hand and maintains the previous desired behavior. Thanks for the quick fix!

@jonathansadowski jonathansadowski added this to the 8.0.0 milestone Aug 11, 2023
@jonathansadowski jonathansadowski merged commit 760c604 into trunk Aug 11, 2023
@jonathansadowski jonathansadowski deleted the dev/fix-behaviour-of-legacy-js branch August 11, 2023 15:07
@github-actions github-actions bot added the needs: analysis Indicates if the PR requires a PR testing scrub session. label Aug 11, 2023
github-actions bot pushed a commit that referenced this pull request Aug 11, 2023
…39686)

* Revert behaviour of code that was using nullish coalescing operator.

* Add changefile(s) from automation for the following project(s): woocommerce

---------

Co-authored-by: github-actions <[email protected]>
jonathansadowski pushed a commit that referenced this pull request Aug 11, 2023
* Revert behaviour of code that was using nullish coalescing operator. (#39686)

* Revert behaviour of code that was using nullish coalescing operator.

* Add changefile(s) from automation for the following project(s): woocommerce

---------

Co-authored-by: github-actions <[email protected]>

* Prep for cherry pick 39686

---------

Co-authored-by: Sam Seay <[email protected]>
Co-authored-by: github-actions <[email protected]>
Co-authored-by: WooCommerce Bot <[email protected]>
@lanej0 lanej0 added needs: internal testing Indicates if the PR requires further testing conducted by Solaris status: analysis complete Indicates if a PR has been analysed by Solaris and removed needs: analysis Indicates if the PR requires a PR testing scrub session. labels Aug 11, 2023
@sybrew
Copy link
Copy Markdown

sybrew commented Aug 19, 2023

I think the code is unreadable now (and, let's be honest, over twice as slow). What's wrong with the following?

$( this ).data( 'minimum_input_length' ) ?? '3'

https://caniuse.com/mdn-javascript_operators_nullish_coalescing

@samueljseay
Copy link
Copy Markdown
Contributor Author

samueljseay commented Aug 21, 2023

@sybrew Thank you that's good input.

Absolutely I did consider making the change back to ??, but just for safety because someone else removed it in relation to babel transpilation a while back I took the safer option here. I could not confirm why that person made that change because they were not available at that time and this was an urgently needed bugfix.

I did also consult caniuse at that time but because I was in a hurry I did not cross-check with WP browser support, I just saw that edge up to 80 was not supported and took the safe route, but now looking at it, WP only supports last 2 edge versions so we can safely make this change. I think we can revert back to ?? in a future PR, I'd welcome a contribution for that, or I will do it when I get a chance 😄

@sybrew
Copy link
Copy Markdown

sybrew commented Aug 21, 2023

Cheers! I'm unfortunately inundated with other projects, so I'll leave the PR to you.

Please note that the file has three more instances that could benefit from the current patch's change. Those all look like this:

minimumInputLength: $( this ).data( 'minimum_input_length' ) ? $( this ).data( 'minimum_input_length' ) : '3',

When stumbling upon this kind of code, if you wish to prevent performance issues altogether where neither ?? nor ?: (PHP) is possible, write the complex callback to a variable/const first:

const mil = $( this ).data( 'minimum_input_length' ); // Write getter to variable/const.

var something = {
# code here ...
    minimumInputLength: +mil ? mil : '3', // read variable without needing to invoke expensive jQuery calls
}
# code here ...

Here's another workaround that just came to mind:

minimumInputLength: ( +$( this ).data( 'minimum_input_length' ) || '3' ).toString(),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs: internal testing Indicates if the PR requires further testing conducted by Solaris plugin: woocommerce Issues related to the WooCommerce Core plugin. status: analysis complete Indicates if a PR has been analysed by Solaris

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove minimum character limit from attribute selection

6 participants