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

Fix #6799: Add restricted globals rule to eslint #6923

Merged
merged 7 commits into from
Jun 16, 2019

Conversation

ankita240796
Copy link
Contributor

Explanation

Fixes #6799: Adds restricted globals rule to eslint and removes direct use of globals from codebase.

@@ -43,7 +43,7 @@ oppia.factory('MusicNotesInputRulesService', [
// TODO(wxy): validate that inputs.a <= inputs.b
HasLengthInclusivelyBetween: function(answer, inputs) {
var answerLength = _convertSequenceToMidi(answer).length;
return length >= inputs.a && length <= inputs.b;
return answerLength >= inputs.a && answerLength <= inputs.b;
Copy link
Contributor Author

@ankita240796 ankita240796 Jun 14, 2019

Choose a reason for hiding this comment

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

Hi @seanlip, I am not sure if this change is right. There is no variable length declared in the function and we want to check if answerLength is inclusive between inputs.a and inputs.b (as from the function name and definition). So, was the variable length used here by mistake?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@DubeySandeep can you also please help me here, Thanks!

Copy link
Member

Choose a reason for hiding this comment

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

Goodness, yes, I think so. Really good catch. I wonder how it worked before ... :(

Just wondering, are there tests for this currently?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this PR adds test to avoid the use of global variables directly.

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 was caught since length was flagged as a global variable used directly by eslint. Then, I realized that length here is used by mistake and is not a global variable actually.

Copy link
Member

Choose a reason for hiding this comment

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

Ah I meant tests for HasLengthInclusivelyBetween. Think it's worth adding them? They should be fairly trivial.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Member

@DubeySandeep DubeySandeep left a comment

Choose a reason for hiding this comment

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

LGTM! (Left one simple comment!)

@@ -41,12 +41,12 @@ oppia.directive('historyTab', ['UrlInterpolationService', function(
'/pages/exploration-editor-page/history-tab/history-tab.directive.html'),
controllerAs: '$ctrl',
controller: [
'$http', '$log', '$rootScope', '$scope',
'$http', '$log', '$rootScope', '$scope', '$window',
Copy link
Member

Choose a reason for hiding this comment

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

$window should go after $uibModal.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@codecov
Copy link

codecov bot commented Jun 14, 2019

Codecov Report

Merging #6923 into develop will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff            @@
##           develop    #6923   +/-   ##
========================================
  Coverage    95.68%   95.68%           
========================================
  Files          371      371           
  Lines        51429    51429           
========================================
  Hits         49212    49212           
  Misses        2217     2217

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3069fe6...0ee37cd. Read the comment docs.

@codecov
Copy link

codecov bot commented Jun 14, 2019

Codecov Report

Merging #6923 into develop will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff            @@
##           develop    #6923   +/-   ##
========================================
  Coverage    95.68%   95.68%           
========================================
  Files          371      371           
  Lines        51429    51429           
========================================
  Hits         49212    49212           
  Misses        2217     2217

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3069fe6...2933810. Read the comment docs.

.eslintrc Outdated
@@ -73,6 +73,8 @@
"error",
"always"
],
"no-restricted-globals": [
"error", "addEventListener", "blur", "close", "closed", "confirm", "defaultStatus", "event", "external", "defaultstatus", "find", "focus", "frameElement", "frames", "history", "innerHeight", "innerWidth", "length", "location", "locationbar", "menubar", "moveBy", "moveTo", "name", "onblur", "onerror", "onfocus", "onload", "onresize", "onunload", "open", "opener", "opera", "outerHeight", "outerWidth", "pageXOffset", "pageYOffset", "parent", "print", "removeEventListener", "resizeBy", "resizeTo", "screen", "screenLeft", "screenTop", "screenX", "screenY", "scroll", "scrollbars", "scrollBy", "scrollTo", "scrollX", "scrollY", "self", "sessionStorage", "status", "statusbar", "stop", "toolbar", "top"],
Copy link
Member

Choose a reason for hiding this comment

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

Just to check, what is this list representing (and should it be so big? Wondering if we can remove anything.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The list represents the common global variables which should not be used directly. I found the list from here.

I did not use the package directly since it is not maintained. Also, in the usage rules for this rule, the prescribed way was to add a list in eslintrc with the names of the globals we want to avoid. We can remove the globals which we don't think we need in the codebase. I am not sure how to find out such globals and remove from the list. Can you guide me here, Thanks!

Copy link
Member

Choose a reason for hiding this comment

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

Ah I see, so this is the list we want to avoid. In that case, that sounds good! I was worried that it was a list of what we were willing to accept, and was concerned that the list of exceptions seemed to be so big. No further change needed here, then.

Thanks @ankita240796 !

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're welcome!

Copy link
Member

Choose a reason for hiding this comment

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

Can we also add document, console etc. in this list as we expect to use $log, $document?

Copy link
Member

Choose a reason for hiding this comment

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

Hi folks -- a question for y'all. What's wrong with copying the list and maintaining our copy of it? It seems nice to not have to rely on any dependencies.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, it seems fine to me. Just one concern -- we'll have to update our copy of the list after some interval probably, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, if we plan to add some new globals. I am not sure if the eslint-restricted-globals package is being maintained a lot since the last commit was 7 months ago.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think maintaining our own copy has its own pros and cons, but let's not dive into that. I'm okay with having our own copy, but maybe it would be nice to make an PR on the eslint-restricted-globals package (if the globals that we are adding are universal).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree with you on making a PR for the package. I will do that with the globals I have added in this PR as well.

Copy link
Member

@seanlip seanlip left a comment

Choose a reason for hiding this comment

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

Couple small things, otherwise LGTM. I'll defer to you and @vojtechjelinek re the long comment thread on .eslintrc.

.eslintrc Outdated
@@ -73,6 +73,8 @@
"error",
"always"
],
"no-restricted-globals": [
"error", "addEventListener", "blur", "close", "closed", "confirm", "defaultStatus", "event", "external", "defaultstatus", "find", "focus", "frameElement", "frames", "history", "innerHeight", "innerWidth", "length", "location", "locationbar", "menubar", "moveBy", "moveTo", "name", "onblur", "onerror", "onfocus", "onload", "onresize", "onunload", "open", "opener", "opera", "outerHeight", "outerWidth", "pageXOffset", "pageYOffset", "parent", "print", "removeEventListener", "resizeBy", "resizeTo", "screen", "screenLeft", "screenTop", "screenX", "screenY", "scroll", "scrollbars", "scrollBy", "scrollTo", "scrollX", "scrollY", "self", "sessionStorage", "status", "statusbar", "stop", "toolbar", "top"],
Copy link
Member

Choose a reason for hiding this comment

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

One separate suggestion -- please add line breaks to the list so that this file is cleanly viewable within an 80-char-width workspace.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -43,7 +43,7 @@ oppia.factory('MusicNotesInputRulesService', [
// TODO(wxy): validate that inputs.a <= inputs.b
HasLengthInclusivelyBetween: function(answer, inputs) {
var answerLength = _convertSequenceToMidi(answer).length;
return length >= inputs.a && length <= inputs.b;
return answerLength >= inputs.a && answerLength <= inputs.b;
Copy link
Member

Choose a reason for hiding this comment

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

Ah I meant tests for HasLengthInclusivelyBetween. Think it's worth adding them? They should be fairly trivial.

Copy link
Contributor

@kevinlee12 kevinlee12 left a comment

Choose a reason for hiding this comment

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

lgtm! (from code owner's)

@kevinlee12 kevinlee12 removed their assignment Jun 15, 2019
@@ -38,12 +38,12 @@ oppia.factory('MusicNotesInputRulesService', [
_convertSequenceToMidi(inputs.x));
},
IsLongerThan: function(answer, inputs) {
return _convertSequenceToMidi(answer).length > inputs.x;
return _convertSequenceToMidi(answer).length > inputs.k;
Copy link
Contributor Author

@ankita240796 ankita240796 Jun 15, 2019

Choose a reason for hiding this comment

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

Hi @seanlip, since I added test for the HasLengthInclusivelyBetween function, I also thought of adding tests of IsLongerThan since only these two functions did not have tests in the spec file. After looking into the function, I did not find this comparison correct as this was always returning false since inputs.x is not an integer field but a list instead.

The format for inputs.x is:

x: [{
          readableNoteName: 'A4',
          noteDuration: {
            num: 1,
            den: 1
          }
    }]

I tried to see the exploration yaml by creating and downloading an exploration but it did not work due to #6931.

So, I checked the file pitch_perfect.yaml where the rule isLongerThan requires inputs.k. You can check here.

I am not a lot familiar with this interaction. So, can you please confirm if this change is correct. Thanks!

Copy link
Member

Choose a reason for hiding this comment

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

Yes, you are correct. Thank you very much for checking and for doing this!

@ankita240796 ankita240796 assigned seanlip and unassigned ankita240796 Jun 15, 2019
Copy link
Contributor

@vojtechjelinek vojtechjelinek left a comment

Choose a reason for hiding this comment

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

LGTM from code owner's perspective. Thanks!

@vojtechjelinek vojtechjelinek removed their assignment Jun 15, 2019
Copy link
Member

@seanlip seanlip left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @ankita240796! All changes look great.

@apb7 this PR needs your review as codeowner. PTAL!

@@ -38,12 +38,12 @@ oppia.factory('MusicNotesInputRulesService', [
_convertSequenceToMidi(inputs.x));
},
IsLongerThan: function(answer, inputs) {
return _convertSequenceToMidi(answer).length > inputs.x;
return _convertSequenceToMidi(answer).length > inputs.k;
Copy link
Member

Choose a reason for hiding this comment

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

Yes, you are correct. Thank you very much for checking and for doing this!

@seanlip seanlip removed their assignment Jun 16, 2019
Copy link
Contributor

@apb7 apb7 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @ankita240796!

@seanlip seanlip merged commit 4802241 into oppia:develop Jun 16, 2019
@ankita240796 ankita240796 deleted the restrict-globals branch June 16, 2019 04:34
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

Successfully merging this pull request may close these issues.

Add eslint-restricted-globals rule
6 participants