Skip to content
This repository has been archived by the owner on Mar 3, 2021. It is now read-only.

loop over dynamic array length will show warning #1314

Merged
merged 5 commits into from
Oct 16, 2019
Merged

Conversation

Aniket-Engg
Copy link
Collaborator

fixes #994

Now array.length - 1 will show warning

node.children[1].children[1].attributes.member_name === 'length' &&
node.children[1].children[1].children[0].attributes.type.indexOf('[]') !== -1) {
if (common.isForLoop(node) && (common.isDynamicArrayLengthAccess(node.children[1].children[1]) ||
(node.children[1].children[1].children && common.isDynamicArrayLengthAccess(node.children[1].children[1].children[0])))) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's really difficult to understand what this if statement is doing. Could you break it into several steps with explcit naming :

const XXX = node.children[1].children[1].children
const XXXchild = node.children[1].children[1].children[0]
const isXXX = !!XXX && common.isDynamicArrayLengthAccess(XXXchild)
const isYYY = isXXX || common.isDynamicArrayLengthAccess(node.children[1].children[1])
const isZZZ = common.isForLoop(node) && isYYY
if (common.isForLoop(node) && isZZZ) ...

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also for better readibility you may want to destructure the "common" object :

const { isForLoop, isDynamicArrayLengthAccess } = require('./staticAnalysisCommon')

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

sure

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I tried above suggestion of dividing checks but that was requiring hierarchical checks for existence otherwise tests were not working like
const XXX = node && node.children && node.children[1] && node.children[1].children[1].children

I used a way which solves our purpose of code explanation using comments

* @return {bool}
*/
function isDynamicArrayLengthAccess (node) {
return node && nodeType(node, exactMatch(nodeTypes.MEMBERACCESS)) && (node.attributes.member_name === 'length') && node.children[0].attributes.type.indexOf('[]') !== -1
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here, create intermediary variables. Else it's going to be super difficult for the next person to take over your code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

sounds reasonable.

Copy link
Collaborator

@GrandSchtroumpf GrandSchtroumpf left a comment

Choose a reason for hiding this comment

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

👌

@Aniket-Engg Aniket-Engg merged commit 0c428d8 into master Oct 16, 2019
@Aniket-Engg Aniket-Engg deleted the fix/#994 branch October 16, 2019 09:14
Aniket-Engg added a commit that referenced this pull request Oct 16, 2019
@Aniket-Engg Aniket-Engg mentioned this pull request Oct 16, 2019
Aniket-Engg added a commit that referenced this pull request Oct 16, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

forLoopIteratesOverDynamicArray considers array.length-1 safe.
2 participants