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

Make prefer-for-of block scope aware #1926

Merged
merged 4 commits into from Dec 31, 2016
Merged

Make prefer-for-of block scope aware #1926

merged 4 commits into from Dec 31, 2016

Conversation

nchen63
Copy link
Contributor

@nchen63 nchen63 commented Dec 23, 2016

fixes #1884

Copy link
Contributor

@adidahiya adidahiya left a comment

Choose a reason for hiding this comment

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

not sure we can claim this as fixing #1884 until we get more info on that issue?

}

public createBlockScope() {
// a map of incrementors and whether or not they are only used to index into an array reference in the for loop
Copy link
Contributor

Choose a reason for hiding this comment

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

this comment should go on the IncrementorMap definition

private incrementorMap: { [name: string]: IIncrementorState };

type IncrementorMap = { [name: string]: IIncrementorState };
class PreferForOfWalker extends Lint.BlockScopeAwareRuleWalker<{}, IncrementorMap> {
constructor(sourceFile: ts.SourceFile, options: Lint.IOptions) {
Copy link
Contributor

Choose a reason for hiding this comment

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

you can omit this constructor now, it's not doing anything

private incrementorMap: { [name: string]: IIncrementorState };

type IncrementorMap = { [name: string]: IIncrementorState };
class PreferForOfWalker extends Lint.BlockScopeAwareRuleWalker<{}, IncrementorMap> {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: newline above this

return this.blockScopeStack[i];
}
}
return null;
Copy link
Contributor

Choose a reason for hiding this comment

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

don't return null explicitly; return undefined instead

@@ -58,6 +58,16 @@ export abstract class BlockScopeAwareRuleWalker<T, U> extends ScopeAwareRuleWalk
return;
}

public findBlockScope(predicate: (scope: U) => boolean) {
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we need this? shouldn't it just be this.getAllBlockScopes().find(predicate)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this goes in reverse order from local to global

Copy link
Contributor

Choose a reason for hiding this comment

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

Then you could call reverse() on the array; I think that'd be more readable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that creates a copy of the array. I think I made this change based on a discussion about not unnecessarily copying arrays.

Copy link
Contributor

Choose a reason for hiding this comment

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

alright, that sounds reasonable. in the future, please leave a code comment

@nchen63
Copy link
Contributor Author

nchen63 commented Dec 30, 2016

@adidahiya I'm pretty sure this fixes the issue. The only way the call stack could have occurred is if the same iterator name was reused (added twice, then deleted when exiting the "scope"). The choice seems to be to close the ticket with a likely fix (it can be reopened if it's not fixed), or leaving the ticket permanently open if the submitter doesn't update the ticket.

# Conflicts:
#	src/rules/preferForOfRule.ts
@nchen63 nchen63 merged commit 5a85a01 into master Dec 31, 2016
@nchen63 nchen63 deleted the scope-for-of branch January 4, 2017 01:05
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.

Adding prefer-for-of option breaks tslint
2 participants