Skip to content

Remove ClassCallCheck, possibleConstructorReturn in loose mode - #4850

Merged
hzoo merged 4 commits into
7.0from
removeClassCallCheck-option
Jun 26, 2017
Merged

hzoo merged 4 commits into
7.0from
removeClassCallCheck-option

Conversation

@hzoo

@hzoo hzoo commented Nov 16, 2016 •

Copy link
Copy Markdown
Member
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

codecov-io commented Nov 16, 2016 •

Copy link
Copy Markdown

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
Copy Markdown
Member

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

@developit

Copy link
Copy Markdown
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

DrewML commented Nov 17, 2016

Copy link
Copy Markdown
Member

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

@developit

developit commented Nov 17, 2016 •

Copy link
Copy Markdown
Member

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

@hzoo

hzoo commented Nov 17, 2016

Copy link
Copy Markdown
Member Author

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

@DrewML

DrewML commented Nov 17, 2016

Copy link
Copy Markdown
Member

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
Copy Markdown
Member

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

@developit

Copy link
Copy Markdown
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

DrewML commented Nov 17, 2016

Copy link
Copy Markdown
Member

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
Copy Markdown
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

DrewML commented Nov 17, 2016

Copy link
Copy Markdown
Member

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

@hzoo

hzoo commented Nov 17, 2016

Copy link
Copy Markdown
Member Author

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
Copy Markdown
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

hzoo commented Nov 17, 2016

Copy link
Copy Markdown
Member Author

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

DrewML commented Nov 17, 2016

Copy link
Copy Markdown
Member

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

Download commented Apr 7, 2017

Copy link
Copy Markdown

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
Copy Markdown
Member

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

@hzoo

hzoo commented Apr 7, 2017

Copy link
Copy Markdown
Member Author

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

@developit

Copy link
Copy Markdown
Member

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

@hzoo

hzoo commented Apr 7, 2017

Copy link
Copy Markdown
Member Author

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

@developit

Copy link
Copy Markdown
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; }

@hzoo hzoo Jun 9, 2017 •

Copy link
Copy Markdown
Member Author

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown

@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
Copy Markdown
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?

Comment thread packages/babel-helpers/src/helpers.js Outdated
(function (subClass, superClass) {
subClass.prototype = Object.create(superClass && superClass.prototype);
subClass.prototype.constructor = subClass;
if (superClass) {

Copy link
Copy Markdown
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?

@hzoo hzoo Jun 23, 2017 •

Copy link
Copy Markdown
Member Author

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

@developit developit Jun 23, 2017 •

Copy link
Copy Markdown
Member

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
Copy Markdown
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;
  })

@hzoo
hzoo requested review from jridgewell and loganfsmyth June 19, 2017 19:32
Comment thread packages/babel-helpers/src/helpers.js Outdated

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

@hzoo hzoo Jun 23, 2017 •

Copy link
Copy Markdown
Member Author

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

hzoo commented Jun 26, 2017

Copy link
Copy Markdown
Member Author

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

@hzoo

hzoo commented Jun 26, 2017

Copy link
Copy Markdown
Member Author

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

@hzoo
hzoo merged commit bb5528b into 7.0 Jun 26, 2017
@xtuc
xtuc deleted the removeClassCallCheck-option branch June 27, 2017 14:04
@Download

Download commented Jul 1, 2017

Copy link
Copy Markdown

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

@hzoo

hzoo commented Jul 1, 2017

Copy link
Copy Markdown
Member Author

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

Download commented Jul 3, 2017

Copy link
Copy Markdown

@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

hzoo commented Jul 12, 2017

Copy link
Copy Markdown
Member Author

@hzoo hzoo mentioned this pull request Sep 16, 2017
2 tasks done
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.

8 participants