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

Remove ClassCallCheck, possibleConstructorReturn in loose mode #4850

Merged
merged 4 commits into from
Jun 26, 2017

Conversation

hzoo
Copy link
Member

@hzoo hzoo commented Nov 16, 2016

Q A
Bug fix?
Breaking change?
New feature? yes
Deprecations?
Spec compliancy?
Tests added + pass? yes
Fixed tickets
License MIT
Doc PR
Dependency Changes

cc @developit

@codecov-io
Copy link

codecov-io commented Nov 16, 2016

Codecov Report

Merging #4850 into 7.0 will increase coverage by <.01%.
The diff coverage is 91.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##              7.0    #4850      +/-   ##
==========================================
+ Coverage   85.21%   85.21%   +<.01%     
==========================================
  Files         284      284              
  Lines        9958     9966       +8     
  Branches     2780     2785       +5     
==========================================
+ Hits         8486     8493       +7     
  Misses        971      971              
- Partials      501      502       +1
Impacted Files Coverage Δ
packages/babel-helpers/src/helpers.js 100% <100%> (ø) ⬆️
...bel-plugin-transform-es2015-classes/src/vanilla.js 90.9% <90.9%> (+0.27%) ⬆️
packages/babel-traverse/src/visitors.js 85.71% <0%> (-0.96%) ⬇️

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 cce83a0...8a4ab08. Read the comment docs.

@loganfsmyth
Copy link
Member

Wasn't it possibleConstructorReturn that @developit was asking about?

@developit
Copy link
Member

Both actually. I'm looking for a way to say "hey, don't worry about protecting classes, they are internal to my module anyway".

@DrewML
Copy link
Member

DrewML commented Nov 17, 2016

Not that I have anything against this change, but I'm just curious to know what the use-case is @developit. Performance?

@developit
Copy link
Member

developit commented Nov 17, 2016

Performance and file size. I use codemods or avoid classes entirely, with this in place it would be more viable.

@hzoo
Copy link
Member Author

hzoo commented Nov 17, 2016

@developit oh and you can also check out babili's plugins + babel codemods

@DrewML
Copy link
Member

DrewML commented Nov 17, 2016

Part of me wonders if these things should just be part of loose mode for es2015-classes, rather than separate options. Thoughts @hzoo?

@developit
Copy link
Member

@hzoo think babili would cover stripping these? That would get me to upgrade to 6 for sure! 😂

@developit
Copy link
Member

@DrewML huge fringe benefit of doing that would be that the es2015 preset would be able to enable these mods with a single option. Currently disassembling that preset is a bit of a hard battle see modify-babel-preset haha).

Perhaps another option is a "minimal" mode? Or maybe that's encroaching on Babili territory.

@DrewML
Copy link
Member

DrewML commented Nov 17, 2016

I think minimal mode was the initial intent of loose (but who knows at this point).

Babel 5 docs on loose mode were:

Loose mode enables certain transformers to generate cleaner output that lacks specific ES6 edgecases. These edgecases are either unlikely to appear in your code or the inclusion of them introduces significant overhead.

As a result of loose mode code will execute faster and be much more readable and comparable to the original but will deviate from the spec in slight ways.

I kind of interpret that as "generate the minimal amount of changes to make the code work, and ignore edge-cases where someone could do something bad"

@developit
Copy link
Member

That is certainly how I think of it. It would be a very compelling option to have handy, at least for me. Tends to be like 300-500 bytes, which for small libs like preact is a heck of a lot (10%!).

@hzoo hzoo added the PR: New Feature 🚀 A type of pull request used for our changelog categories label Nov 17, 2016
@DrewML
Copy link
Member

DrewML commented Nov 17, 2016

@hzoo Thoughts/Opinions on combining this into loose mode?

@hzoo
Copy link
Member Author

hzoo commented Nov 17, 2016

We could? I guess it would be a "breaking change" still? although most that use loose mode wouldn't care about it anyway

@developit
Copy link
Member

It be breaking for anything that is relying on calling constructors to throw (for whatever reason, I can't think of a good one, maybe some over-testing?). It's just the preset though and I would guess as you said, those are not the people running in loose mode. Also since loose was only added in 6.13 (?) it seems like it'd be not quite as widely used yet.

@hzoo
Copy link
Member Author

hzoo commented Nov 17, 2016

This changes the plugin not the preset so it would affect everyone using the option which should of been there since the beginning? Yeah it's removing a throw check so it wouldn't break anything in the opposite way

@DrewML
Copy link
Member

DrewML commented Nov 17, 2016

I guess this kind of leads to the bigger question of "what is a breaking change" in these options. In theory, we could never add any features to loose mode, because any additional optimization could always be a breaking change.

Semver is hard :(

@hzoo hzoo mentioned this pull request Apr 6, 2017
@Download
Copy link

Download commented Apr 7, 2017

Using loose mode is the way to go for both classCallCheck and possibleConstructorReturn if you ask me.

I would like for Babel to generate code that is as close to ES6 compliant as possible under normal circumstances, then use loose mode to drop every line of code that is not strictly necessary to make stuff just work. It means in loose mode you can do stuff that is against spec... But probably you will dev in strict mode and catch such uses there. Once it's in production, use loose mode for max. performance.

Having a single toggle on the preset would be infinitely more valuable than having to control each plugin separately.

@developit
Copy link
Member

Also makes fall-through from presets like latest easier since it's just the existing loose option, right?

@hzoo
Copy link
Member Author

hzoo commented Apr 7, 2017

Yes it should although fyi latest is deprecated since env covers all it's functionality

@developit
Copy link
Member

You know I'm behind on all that stuff (still using 5 in places haha)

@hzoo
Copy link
Member Author

hzoo commented Apr 7, 2017

😅 can someone just make the auto upgrade from 5 to 6 bot

@developit
Copy link
Member

@hzoo still produces larger output for some reason, need to look into why. I'm basically only using babel to convert let and const to var without caring about TDZ or block scoping.

@hzoo hzoo changed the title removeClassCallCheck option Remove ClassCallCheck in loose mode Jun 9, 2017
@hzoo hzoo force-pushed the removeClassCallCheck-option branch from 7677c86 to 6b31d13 Compare June 9, 2017 22:12
@hzoo hzoo changed the base branch from master to 7.0 June 9, 2017 22:12
@hzoo hzoo changed the title Remove ClassCallCheck in loose mode Remove ClassCallCheck, possibleConstructorReturn in loose mode Jun 9, 2017
@@ -0,0 +1,40 @@
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
Copy link
Member Author

@hzoo hzoo Jun 9, 2017

Choose a reason for hiding this comment

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

Do we want to remove the checks here too?

function _inheritsLoose(subclass, superClass) {
  subClass.prototype = Object.create(superClass && superClass.prototype);
  subClass.prototype.constructor = subClass;
  if (superClass) {
    subClass.__proto__ = superClass;
  } 
}

Copy link
Member

Choose a reason for hiding this comment

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

Definitely. My view here is that people using loose mode classes are just asking for a direct replacement for the ES5 equivalent.

function C() {
woops.super.test();

var _this = _B.call(this) || this;
Copy link
Member Author

Choose a reason for hiding this comment

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

can drop || this as well

@hzoo hzoo force-pushed the removeClassCallCheck-option branch from 6b31d13 to cdca54a Compare June 9, 2017 22:42
@Download
Copy link

@hzoo Loving what you are doing here! Can't wait for it to be merged and released!


function A(track) {
if (track !== undefined) {
var _this = _B.call(this, track) || this;
Copy link
Member Author

Choose a reason for hiding this comment

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

and remove || this I guess?

(function (subClass, superClass) {
subClass.prototype = Object.create(superClass && superClass.prototype);
subClass.prototype.constructor = subClass;
if (superClass) {
Copy link
Member

Choose a reason for hiding this comment

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

Thoughts on also removing this check?

Copy link
Member Author

@hzoo hzoo Jun 23, 2017

Choose a reason for hiding this comment

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

because we would assume superClass is not null? like class extends null or class extends expression

Copy link
Member

@developit developit Jun 23, 2017

Choose a reason for hiding this comment

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

good call, I was thinking it would always be appropriate to throw an exception but I stand corrected! :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok we can just assume in loose mode that the superClass is non-null and has a prototype to get

  (function (subClass, superClass) {
    subClass.prototype = Object.create(superClass.prototype);
    subClass.prototype.constructor = subClass;
    subClass.__proto__ = superClass;
  })

@@ -377,6 +377,16 @@ helpers.inherits = template(`
})
`);

helpers.inheritsLoose = template(`
(function (subClass, superClass) {
subClass.prototype = Object.create(superClass && superClass.prototype);
Copy link
Member Author

@hzoo hzoo Jun 23, 2017

Choose a reason for hiding this comment

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

I guess we would be able to remove the check here too then @developit? superClass &&

Copy link
Member

Choose a reason for hiding this comment

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

yup

woops.super.test();

var _this = babelHelpers.possibleConstructorReturn(this, _Foo.call(this));
var _this = _Foo.call(this) || this;
Copy link
Member Author

Choose a reason for hiding this comment

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

should we remove || this?

Copy link
Member

Choose a reason for hiding this comment

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

If we do, we'd have to remove the whole var _this = (and all the _thiss) too. I think this is fine for now.

Copy link
Member

Choose a reason for hiding this comment

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

hmm. this seems important.

woops.super.test();

var _this = babelHelpers.possibleConstructorReturn(this, _Foo.call(this));
var _this = _Foo.call(this) || this;
Copy link
Member

Choose a reason for hiding this comment

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

If we do, we'd have to remove the whole var _this = (and all the _thiss) too. I think this is fine for now.

@hzoo
Copy link
Member Author

hzoo commented Jun 26, 2017

Ok will merge and someone can do #4850 (comment) as a followup issue/PR

@hzoo
Copy link
Member Author

hzoo commented Jun 26, 2017

Also something about travis is messed up (circle ci passes + local)

@Download
Copy link

Download commented Jul 1, 2017

@hzoo Is there a way to test this in my project before it is released to NPM?

@hzoo
Copy link
Member Author

hzoo commented Jul 1, 2017

I could some better docs for this (or a utility to make it easy) but yeah you can just overwrite your node_modules? Like literally replace the 2 files that we changed. You can probably just run the src/index.js through babeljs.io/repl and then replace the lib/index.js in node_modules and add that helper to test it

@Download
Copy link

Download commented Jul 3, 2017

@hzoo Ok. I guess that's a bit too far outside of my comfort zone :) I guess I'll wait until the next alpha makes it to NPM. :)

@hzoo
Copy link
Member Author

hzoo commented Jul 12, 2017

https://github.com/babel/babel/releases/tag/v7.0.0-alpha.15

@hzoo hzoo mentioned this pull request Sep 16, 2017
2 tasks
yiminghe added a commit to yiminghe/babel-runtime-loose that referenced this pull request Oct 30, 2017
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 6, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: New Feature 🚀 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants