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

[CCI][#240][eslint] Fixed duplicate conditions in indices and nodes API #428

Closed
wants to merge 8 commits into from
Closed

[CCI][#240][eslint] Fixed duplicate conditions in indices and nodes API #428

wants to merge 8 commits into from

Conversation

frost017
Copy link

Description

Summary about indices.js: This error is triggered when running yarn lint with next rule removed - "no-dupe-else-if": "off". Previously, there were else-if statements having the same condition, which means only one of them can be executed and others are ignored. These were removed because of duplication:

else if (index != null && type != null) {
  if (method == null) method = 'PUT';
  path = '/' + encodeURIComponent(index) + '/' + encodeURIComponent(type) + '/' + '_mapping';
} else if (index != null && type != null) {
  if (method == null) method = 'PUT';
  path = '/' + encodeURIComponent(index) + '/' + '_mapping' + '/' + encodeURIComponent(type);
} else if (index != null && type != null) {
  if (method == null) method = 'PUT';
  path = '/' + encodeURIComponent(index) + '/' + encodeURIComponent(type) + '/' + '_mappings';
} else if (index != null && type != null) {
  if (method == null) method = 'PUT';
  path = '/' + encodeURIComponent(index) + '/' + '_mappings' + '/' + encodeURIComponent(type);
}

The code above resulted in following:

  1572:14  error  This branch can never execute. Its condition is a duplicate or covered by previous conditions in the if-else-if chain  no-dupe-else-if
  1575:14  error  This branch can never execute. Its condition is a duplicate or covered by previous conditions in the if-else-if chain  no-dupe-else-if
  1578:14  error  This branch can never execute. Its condition is a duplicate or covered by previous conditions in the if-else-if chain  no-dupe-else-if
  1587:14  error  This branch can never execute. Its condition is a duplicate or covered by previous conditions in the if-else-if chain  no-dupe-else-if

FIX in indices.js: Removed duplicate conditions and made separate if statement for each case. By doing this, path is picked correctly and cases are not ignored because of duplicate conditions.

Summary about nodes.js: Same case as in indices.js. It has duplicate conditions, all check same case. It means all four are executed, which makes three conditions to be ignored. These lines of code were removed:

if ((node_id || nodeId) != null) {
  if (method == null) method = 'GET';
  path = '/' + '_nodes' + '/' + encodeURIComponent(node_id || nodeId) + '/' + 'hot_threads';
} else if ((node_id || nodeId) != null) {
  if (method == null) method = 'GET';
  path =
    '/' +
    '_cluster' +
    '/' +
    'nodes' +
    '/' +
    encodeURIComponent(node_id || nodeId) +
    '/' +
    'hotthreads';
} else if ((node_id || nodeId) != null) {
  if (method == null) method = 'GET';
  path = '/' + '_nodes' + '/' + encodeURIComponent(node_id || nodeId) + '/' + 'hotthreads';
} else if ((node_id || nodeId) != null) {
  if (method == null) method = 'GET';
  path =
    '/' +
    '_cluster' +
    '/' +
    'nodes' +
    '/' +
    encodeURIComponent(node_id || nodeId) +
    '/' +
    'hot_threads';
}

The code above resulted in following:

  189:14  error  This branch can never execute. Its condition is a duplicate or covered by previous conditions in the if-else-if chain  no-dupe-else-if
  200:14  error  This branch can never execute. Its condition is a duplicate or covered by previous conditions in the if-else-if chain  no-dupe-else-if
  203:14  error  This branch can never execute. Its condition is a duplicate or covered by previous conditions in the if-else-if chain  no-dupe-else-if

FIX in nodes.js: Removed those aforementioned four conditions, and put one if-else condition which checks if node_id or nodeId is not null.

Issues Resolved

Following issue is resolved: #240

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@frost017 frost017 changed the title [CCI][eslint] Fixed duplicate conditions in indices and nodes API [CCI][#240][eslint] Fixed duplicate conditions in indices and nodes API Mar 12, 2023
Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

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

Thanks!

I am concerned that no tests broke given what I think is a bug below. Can you please beef up the putMapping tests to make sure we cover every path of the changed if?

Delete commented out code, and any unrelated changes.

package.json Outdated
"./": "./"
"./*": "./",
"./opensearch/*": "./lib/opensearch/*",
"./lib/*": "./lib/*"
Copy link
Member

Choose a reason for hiding this comment

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

Are these changes related to this fix? Rebase your branch.

Copy link
Author

Choose a reason for hiding this comment

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

Are these changes related to this fix? Rebase your branch.

No, they aren't. It was intended to fix deprecation mapping error when using NodeJS 16 and TypeScript #365. I am removing this fix for now, leaving it as it was previously.

package.json Outdated
@@ -91,6 +93,7 @@
"xmlbuilder2": "^3.0.2"
},
"dependencies": {
"@types/react": "17.0.53",
Copy link
Member

Choose a reason for hiding this comment

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

I think you didn't want this change.

}

path += '_mapping';
Copy link
Member

Choose a reason for hiding this comment

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

This will make /_maping/_mapping in the last else case at least.

Copy link
Author

Choose a reason for hiding this comment

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

/_mapping/_mapping shouldn't be the case in any way, right?

@frost017
Copy link
Author

I am concerned that no tests broke given what I think is a bug below. Can you please beef up the putMapping tests to make sure we cover every path of the changed if?

Yes, of course, I will do my best to cover it with tests. The thing is that I am not yet familiar with the structure of unit tests in the project, so I may put getMapping tests in the wrong place:)

@frost017 frost017 requested review from dblock and removed request for nhtruong, seanneumann, VachaShah, harshavamsi, kavilla and ananzh March 14, 2023 07:04
@VachaShah VachaShah added the CCI College Contributor Initiative label Mar 16, 2023
@kavilla kavilla linked an issue Mar 17, 2023 that may be closed by this pull request
@frost017 frost017 closed this by deleting the head repository Mar 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CCI College Contributor Initiative
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[eslint] duplicate conditions in indices and nodes API
3 participants