Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional Chaining in delete statement removes delete statement #440

Open
goresynopsys opened this issue Apr 6, 2022 · 2 comments
Open

Comments

@goresynopsys
Copy link

We discovered a discrepancy between transpiled and untranspiled Javascript which was introduced when upgrading ember-cli-babel from 7.26.4 to 7.26.11. Nested if statements get transpiled incorrectly (removing the nested if):

(source)

let isBlogPost = filter.key.includes('post');
let strippedKey = filter.key.replace(FILTER_KEY_REGEX, '');
if (filterQueryParams?.post) {
  if (isBlogPost) {
    delete filterQueryParams['post']['taxonomy']?.['taxonomy-type'][strippedKey];
    if (filterOption?.taxonomyType) {
      delete filterQueryParams['post']['taxonomy']?.['taxonomy-type'][filterOption.taxonomyType];
    }
  } else {
    delete filterQueryParams['post'][strippedKey];
  }
}

(transpiled)

let {filter: t, filterQueryParams: i, filterOption: r, selectedValues: n, params: s} = e
			  , a = t.key.includes("post")
			  , o = t.key.replace(A, "")
			i?.post && (a ? (i.post.taxonomy?.["taxonomy-type"][o],
			r?.taxonomyType && i.post.taxonomy?.["taxonomy-type"][r.taxonomyType]) : delete i.post[o])

Our interim fix involved refactoring the duplicated, target key to delete into a new variable, taxonomyType, as follows:

const FILTER_KEY_REGEX = /^filter_/;
let isBlogPost = filter.key.includes('post');
let strippedKey = filter.key.replace(FILTER_KEY_REGEX, '');
if (filterQueryParams?.post) {
  let taxonomyType = filterQueryParams.post.taxonomy?.['taxonomy-type'];
  if (isBlogPost) {
    delete taxonomyType[strippedKey];
    if (filterOption?.taxonomyType) {
      delete taxonomyType[filterOption.taxonomyType];
    }
  } else {
    delete filterQueryParams['post'][strippedKey];
  }
}
@goresynopsys
Copy link
Author

Update: this may have more to do with optional chaining within a delete call than the if statements...

@goresynopsys goresynopsys changed the title Optional Chaining Removes if statement Optional Chaining in delete statement removes delete statement Apr 7, 2022
@RobbieTheWagner
Copy link
Member

We're also seeing this bug with current_page: pagination.self?.number ?? 0, ember-cli-babel converts that to current_page: undefined ?? 0,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants