-
Notifications
You must be signed in to change notification settings - Fork 2
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
Support for static properties on es6 classes #1048
Comments
I want to note that this is not a top-priority issue, but it is annoying to have to work around this non-obvious defect with what our codebase supports. |
After a conversation with @ariel-phet, I updated the first comment, and marked for developer meeting. |
I tried adding static x = true; to Bunny.js. I observed:
Next I tried adding: static get x() {return true;}
|
I got this to work, but I had to use the babel-loader in webpack in addition to updating the babel step itself. Maybe if we use the webpack babel-loader we will no longer need the babel post-processing step? Note: this fix gets builds working but not lint. Index: main/chipper/js/grunt/transpile.js
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- main/chipper/js/grunt/transpile.js (revision 06ea944f1622bd2fcb04a05cd0d3b5281b8259bf)
+++ main/chipper/js/grunt/transpile.js (date 1601503894065)
@@ -36,6 +36,9 @@
// the 'true' option was faster by a hair.
compact: true,
+ // https://babeljs.io/docs/en/babel-plugin-proposal-class-properties
+ plugins: [ '../chipper/node_modules/@babel/plugin-proposal-class-properties' ],
+
// Use chipper's copy of babel-preset-env, so we don't have to have 30MB extra per sim checked out.
presets: [ [ '../chipper/node_modules/@babel/preset-env', {
Index: main/chipper/package.json
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- main/chipper/package.json (revision 06ea944f1622bd2fcb04a05cd0d3b5281b8259bf)
+++ main/chipper/package.json (date 1601504231377)
@@ -10,7 +10,9 @@
"archiver": "~3.1.1",
"@babel/core": "~7.8.6",
"@babel/preset-env": "~7.8.6",
+ "@babel/plugin-proposal-class-properties": "~7.10.4",
"babel-eslint": "^10.0.3",
+ "babel-loader": "^8.1.0",
"eslint": "^6.8.0",
"eslint-plugin-react": "^7.18.0",
"grunt": "~1.1.0",
Index: main/natural-selection/js/common/model/Bunny.js
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- main/natural-selection/js/common/model/Bunny.js (revision 3e8cf4aaf5eafc77789ed9224e671bfe9ba97957)
+++ main/natural-selection/js/common/model/Bunny.js (date 1601504382300)
@@ -399,6 +399,8 @@
this.validateInstance();
}
+
+ static testName = 'hello';
}
/**
@@ -444,5 +446,7 @@
applyState: ( bunny, stateObject ) => bunny.applyState( stateObject )
} );
+console.log(Bunny.testName);
+
naturalSelection.register( 'Bunny', Bunny );
export default Bunny;
\ No newline at end of file
Index: main/chipper/js/grunt/webpackBuild.js
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- main/chipper/js/grunt/webpackBuild.js (revision 06ea944f1622bd2fcb04a05cd0d3b5281b8259bf)
+++ main/chipper/js/grunt/webpackBuild.js (date 1601504284690)
@@ -98,7 +98,27 @@
// Exclude brand specific code. This includes all of the `phet-io` repo for non phet-io builds.
( brand === 'phet' ? [ ignorePhetioBrand, ignorePhetioRepo, ignoreAdaptedFromPhetBrand ] :
brand === 'phet-io' ? [ ignorePhetBrand, ignoreAdaptedFromPhetBrand ] :
- brand === 'adapted-from-phet' ? [ ignorePhetBrand, ignorePhetioBrand, ignorePhetioRepo ] : [] )
+ brand === 'adapted-from-phet' ? [ ignorePhetBrand, ignorePhetioBrand, ignorePhetioRepo ] : [] ),
+
+ module: {
+ rules: [
+ {
+ test: /\.(mjs|js|jsx)$/,
+ exclude: /node_modules/,
+ loader: '../chipper/node_modules/babel-loader',
+ options: {
+ presets: [
+ '../chipper/node_modules/@babel/preset-env',
+ {
+ plugins: [
+ '../chipper/node_modules/@babel/plugin-proposal-class-properties'
+ ]
+ }
+ ]
+ }
+ }
+ ],
+ }
} );
compiler.run( ( err, stats ) => {
|
@jonathanolson and I looked into this further and found that the webpack babel did the transpilation, and the downstream transpilation wasn't doing anything further. We were confused by this, since the "browsers" list we specified in the downstream transpile was the same output as not specifying browsers in the webpack babel-loader. We also found that this is not yet supported on Safari, perhaps it would be prudent to wait until there is better support there so we don't have to build to run on Safari? Though @jonathanolson said he would be OK with requiring builds to run on Safari. |
Does this include mobile Safari? I'm pretty used to making a code change on my MacBook (lydian), and simply pointing my iPad to http://lydian.local/~cmalley/GitHub/ to quickly test changes. And I do that frequently during performance optimization phases of development. If I have to build every time I want to test a change, that's going to be costly. |
I do that too for testing on my iPad. I feel like this would be a dealbreaker (especially since we just got past this issue for IE). |
From dev meeting last week. We decided:
|
in #983 (comment) @jonathanolson said:
I'll remove the lint rule now. |
Thanks for the ping. |
Removed in the above commit. I set a calendar reminded for April 29, 2021. Unassigning and deferring now. |
https://caniuse.com/?search=static%20class%20fields reports that Safari 14.1 (released April 25, 2021) can now use static class fields. How will we decide when enough of our clients have upgraded to Safari 14.1? Also, I recommend we proceed with typescript as part of #987, which already supports static class members: https://www.typescriptlang.org/docs/handbook/2/classes.html#static-members |
from dev meeting 5/6/20: MK: should we get stats on who is using 14.1? SR: should we pick a threshold for what is a good enough percentage of 14.1 users to use this feature We looked at stats and very few users are up to 14.1! So we are going to add a calendar reminder about this. |
Let's cover this with Typescript in phetsims/chipper#1050 |
I removed the calendar entry to investigate this, since it seems TypeScript will be a better solution. If we abandon TypeScript, we can reopen this issue. |
I have two feelings about this.
Since Typescript features would largely be in "type space", it could be nice to note static fields as a possibility for "value space", I would still be interested in bringing this feature into our Typescript and our Javascript implementations. @samreid could we at least take steps to remove static field prevention from our project (I can't remember if there is a lint rule for it)? |
Our supported platforms says Safari 13+, but caniuse says this feature is unsupported through 14.
This TypeScript code: class V{
static age:number=123;
} compiles into this JavaScript code (according to the TS playground: https://www.typescriptlang.org/play?#code/FDDGBsEMGdoAgGoG9hztALpDBLUdIBzAUwC4A7AVwFsAjYgJwF4BGAJgGYBuYAX2CA "use strict";
class V {
}
V.age = 123; When I try a static class field in JS, I see this parse error: Perhaps ESLint needs an upgraded parser to support that syntax? That same code in a *.ts file has no syntax errors. @zepumph where do you want to go with this issue next? |
I guess just close it. The fact that our eslint doesn't support it means that no one will be using it anytime soon. If someone wants to and feels strongly, then they can reopen or create a new issue. Likely TS is the way of the future. Thanks.
I wonder/bet that this will change since static fields are recent additions to javascript's base language. Maybe sometime soon it will compile into static fields. |
We have dropped support for IE11 (see issues like #1037). This caused the question of supporting static "getter" fields (not functions) on es6 classes. See discussion in phetsims/tandem#188 (comment)
Tagging @pixelzoom and @samreid.
@samreid mentioned that over in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Public_class_fields there is support for our browsers, and perhaps babel has an easy plugin to support this https://babeljs.io/docs/en/babel-plugin-proposal-class-properties.
I think of this issue as adding support for two things:
Static fields
Static Getters
I think the steps for this issue could be:
Marking for dev meeting to decide if this is worth investigation.
The text was updated successfully, but these errors were encountered: