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

Minifier-friendly references to properties #183

Merged
merged 5 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Bugfixes:

Other improvements:
- Added `purs-tidy` formatter (#182 by @thomashoneyman)
- Minifier-friendly refereces to properties (#183 by @sd-yip)

## [v9.0.0](https://github.com/purescript-contrib/purescript-react/releases/tag/v9.0.0) - 2021-02-26

Expand Down
59 changes: 21 additions & 38 deletions src/React.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,14 @@
import React from "react";

function createClass(baseClass) {
function bindProperty(instance, prop, value) {
switch (prop) {
case "state":
case "render":
case "componentDidMount":
case "componentWillUnmount":
instance[prop] = value;
break;

case "componentDidCatch":
case "componentWillUpdate":
case "shouldComponentUpdate":
case "getSnapshotBeforeUpdate":
instance[prop] = function (a, b) { return value(a)(b)(); };
break;

case "componentDidUpdate":
instance[prop] = function (a, b, c) { return value(a)(b)(c)(); };
break;

case "unsafeComponentWillMount":
instance["UNSAFE_componentWillMount"] = value;
break;

case "unsafeComponentWillReceiveProps":
instance["UNSAFE_componentWillReceiveProps"] = function (a) { return value(a)(); };
break;

case "unsafeComponentWillUpdate":
instance["UNSAFE_componentWillUpdate"] = function (a, b) { return value(a)(b)(); };
break;

default:
throw new Error("[purescript-react] Not a component property: " + prop);
function curry2(f) {
return f === undefined ? f : function (a, b) {
return f(a)(b)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think these are correct. They are missing the () for invoking the effects (ie, it's not just a curry).

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 see. Previously, I applied the amount of arguments + 1 as the currying number for UNSAFE_componentWillReceiveProps and UNSAFE_componentWillUpdate, but not sure why I have missed them for the remaining.

Now the invocation and currying should be fixed. Please take another look.

}
}
function curry3(f) {
return f === undefined ? f : function (a, b, c) {
return f(a)(b)(c)
}
}

Expand All @@ -43,10 +17,19 @@ function createClass(baseClass) {
var Constructor = function (props) {
baseClass.call(this, props);
var spec = ctrFn(this)();
// eslint-disable-next-line guard-for-in
for (var k in spec) {
bindProperty(this, k, spec[k]);
}

this.state = spec.state;
this.render = spec.render;
this.componentDidMount = spec.componentDidMount;
this.componentWillUnmount = spec.componentWillUnmount;
this.componentDidCatch = curry2(spec.componentDidCatch);
this.componentWillUpdate = curry2(spec.componentWillUpdate);
this.shouldComponentUpdate = curry2(spec.shouldComponentUpdate);
this.getSnapshotBeforeUpdate = curry2(spec.getSnapshotBeforeUpdate);
this.componentDidUpdate = curry3(spec.componentDidUpdate);
this.UNSAFE_componentWillMount = spec.unsafeComponentWillMount;
this.UNSAFE_componentWillReceiveProps = curry2(spec.unsafeComponentWillReceiveProps);
this.UNSAFE_componentWillUpdate = curry3(spec.unsafeComponentWillUpdate);
};

Constructor.displayName = displayName;
Expand Down