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

Add compiler warning for dangerous use of this #5244

Merged
merged 2 commits into from Oct 12, 2016

Conversation

dreamofabear
Copy link

@dreamofabear dreamofabear commented Sep 26, 2016

Fixes #5229.

From the Closure documentation for checkGlobalThisLevel:

Checks for certain uses of the {@code this} keyword that are considered
unsafe because they are likely to reference the global {@code this}
object unintentionally.

If this is off, but collapseProperties is on, then the compiler will
usually ignore you and run this check anyways.

Note: #5243 needs to land first to fix existing instances of this warning.

@dreamofabear
Copy link
Author

@cramforce @erwinmombay PTAL

Copy link
Member

@erwinmombay erwinmombay left a comment

Choose a reason for hiding this comment

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

apologies, only approved to test "approval" state on api

@@ -68,6 +69,8 @@ protected AmpCommandLineRunner(String[] args) {
}
CompilerOptions options = super.createOptions();
options.setCollapseProperties(true);
// Warn on improper uses of `this` that `collapseProperties` assumes.
options.setCheckGlobalThisLevel(CheckLevel.WARNING);
Copy link
Member

Choose a reason for hiding this comment

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

Could this instead be added here: https://github.com/ampproject/amphtml/blob/master/build-system/tasks/compile.js#L269

Our config is a bit over the place, but we try to use the flags where available instead of Java.

Copy link
Author

Choose a reason for hiding this comment

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

Done. Note that this will only run for gulp check-test and will be an error rather than a warning.

@dreamofabear dreamofabear force-pushed the global_this_warn branch 2 times, most recently from 8f1690c to aca5bf6 Compare September 27, 2016 17:20
@cramforce
Copy link
Member

It seems there is one error now. Could you fix it?

@erwinmombay
Copy link
Member

i believe @choumx has a separate PR for the fix here #5243 if you could also TAL a that @cramforce since I don't know if @dvoytenko is out. I tested it out and it works but want to make sure the change is OK with custom-elements v0

@cramforce
Copy link
Member

If that fix works (as in renders pages) it would be great. It would rely on the specific transpile, I believe. Does it work?

Otherwise this can likely be fixed with a @this or @lends annotation.

@erwinmombay
Copy link
Member

Yeah it will rely on the transpile. I tested it with babel and it worked, i haven't tested it with closure but looked at the transpiled transformation in http://closure-compiler-debugger.appspot.com/ and was what i was expecting.

@dreamofabear
Copy link
Author

There's an issue with the Closure build that I'm investigating.

@erwinmombay
Copy link
Member

@choumx same, i just tried it and there's an issue of some sort, i'm resetting to your current HEAD since my copy was out of date

@dreamofabear dreamofabear force-pushed the global_this_warn branch 2 times, most recently from 2fc358c to aacd43e Compare September 28, 2016 22:35
@erwinmombay
Copy link
Member

LGTM

@dreamofabear
Copy link
Author

@cramforce Friendly ping.

I'm planning to address the refactor of BaseCustomElementProto to ES2015 class syntax and other Custom Elements V1 separately.

@cramforce
Copy link
Member

Sorry, for the late response. No need to always wait for me :)

@googlebot
Copy link

We found a Contributor License Agreement for you (the sender of this pull request) and all commit authors, but as best as we can tell these commits were authored by someone else. If that's the case, please add them to this pull request and have them confirm that they're okay with these commits being contributed to Google. If we're mistaken and you did author these commits, just reply here to confirm.

@googlebot
Copy link

CLAs look good, thanks!

@dreamofabear dreamofabear merged commit af629b7 into ampproject:master Oct 12, 2016
@dreamofabear dreamofabear deleted the global_this_warn branch October 12, 2016 04:12
Lith pushed a commit to Lith/amphtml that referenced this pull request Dec 22, 2016
* add checkGlobalThisLevel(warn) to compiler opts

* malte's pr comment
Lith pushed a commit to Lith/amphtml that referenced this pull request Dec 22, 2016
* add checkGlobalThisLevel(warn) to compiler opts

* malte's pr comment
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.

Show compiler warnings for dangerous use of this
4 participants