From 0bb04c89075563ad65564014fa6403e0cec512d9 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Sat, 23 Sep 2023 16:39:43 -0400 Subject: [PATCH 1/5] Ensure that plugin-transform-class-static-block is provided --- lib/ember-plugins.js | 19 +++++++++++++++++++ node-tests/addon-test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/lib/ember-plugins.js b/lib/ember-plugins.js index 3ffbb36c..fd558548 100644 --- a/lib/ember-plugins.js +++ b/lib/ember-plugins.js @@ -168,6 +168,24 @@ function _getProposalDecoratorsAndClassPlugins(config) { } } +/** + * Required for apps to use `@ember/template-compliation` + * for transforming templates from babel-plugin-ember-template-compilation + * which shipped in ember-cli-htmlbars 6.2 + * + * Normally this plugin wouldn't be required because the feature has shipped + * in all browsers, but because we have really old plugins, they do not + * support parsing the static block syntax, so we need to compile it away + * when using plugin-proposal-class-properties, which is required + * when using non-spec decorators. + */ +function _getPluginTransformClassStaticBlock() { + return [ + "@babel/plugin-transform-class-static-block", + ] + +} + /** * This function allows returns all the required Ember specific babel plugins for the app to transpile correctly. * As the first argument, you need to pass in the appRoot (which is usually the __dirname). @@ -187,6 +205,7 @@ function _getProposalDecoratorsAndClassPlugins(config) { module.exports = function (appRoot, config = {}) { return [] .concat( + _getPluginTransformClassStaticBlock(), _getProposalDecoratorsAndClassPlugins(config), _getDebugMacroPlugins(appRoot), _getEmberModulesAPIPolyfill(appRoot, config), diff --git a/node-tests/addon-test.js b/node-tests/addon-test.js index 0a5f8b4d..e3d62f0c 100644 --- a/node-tests/addon-test.js +++ b/node-tests/addon-test.js @@ -126,6 +126,32 @@ describe('ember-cli-babel', function() { }); })); + describe('static class blocks', function() { + it('can compile static blocks', co.wrap(function*(){ + input.write({ + 'foo.js': stripIndent` + let Second = class Second extends Component { + static { + // Set Foo.bar = 1; + this.bar = 1; + } + } + ` + }); + + subject = this.addon.transpileTree(input.path()); + output = createBuilder(subject); + + yield output.build(); + + expect( + output.read() + ).to.deep.equal({ + "foo.js": "function _typeof(obj) { \"@babel/helpers - typeof\"; return _typeof = \"function\" == typeof Symbol && \"symbol\" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && \"function\" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : typeof obj; }, _typeof(obj); }\ndefine(\"foo\", [], function () {\n \"use strict\";\n\n var _class;\n function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }\n function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, \"prototype\", { writable: false }); return Constructor; }\n function _toPropertyKey(arg) { var key = _toPrimitive(arg, \"string\"); return _typeof(key) === \"symbol\" ? key : String(key); }\n function _toPrimitive(input, hint) { if (_typeof(input) !== \"object\" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || \"default\"); if (_typeof(res) !== \"object\") return res; throw new TypeError(\"@@toPrimitive must return a primitive value.\"); } return (hint === \"string\" ? String : Number)(input); }\n function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n function _inherits(subClass, superClass) { if (typeof superClass !== \"function\" && superClass !== null) { throw new TypeError(\"Super expression must either be null or a function\"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, \"prototype\", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }\n function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }\n function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }\n function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === \"object\" || typeof call === \"function\")) { return call; } else if (call !== void 0) { throw new TypeError(\"Derived constructors may only return object or undefined\"); } return _assertThisInitialized(self); }\n function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError(\"this hasn't been initialised - super() hasn't been called\"); } return self; }\n function _isNativeReflectConstruct() { if (typeof Reflect === \"undefined\" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === \"function\") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }\n function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }\n var Second = (_class = /*#__PURE__*/function (_Component) {\n _inherits(Second, _Component);\n var _super = _createSuper(Second);\n function Second() {\n _classCallCheck(this, Second);\n return _super.apply(this, arguments);\n }\n return _createClass(Second);\n }(Component), _class.bar = 1, _class);\n});" + }); + })); + }); + describe('decorators and class fields', function() { it( "can compile decorators", From 6a7e3a92b0ecda6e778c69e4381ab3caacb1537b Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Tue, 26 Sep 2023 09:47:43 -0400 Subject: [PATCH 2/5] Group static block with decorators --- lib/ember-plugins.js | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/lib/ember-plugins.js b/lib/ember-plugins.js index fd558548..18759e93 100644 --- a/lib/ember-plugins.js +++ b/lib/ember-plugins.js @@ -162,12 +162,6 @@ function _getModuleResolutionPlugins(config) { function _getProposalDecoratorsAndClassPlugins(config) { if (!config.shouldIgnoreDecoratorAndClassPlugins) { return [ - ["@babel/plugin-proposal-decorators", { legacy: true }], - ["@babel/plugin-proposal-class-properties"], - ]; - } -} - /** * Required for apps to use `@ember/template-compliation` * for transforming templates from babel-plugin-ember-template-compilation @@ -179,11 +173,11 @@ function _getProposalDecoratorsAndClassPlugins(config) { * when using plugin-proposal-class-properties, which is required * when using non-spec decorators. */ -function _getPluginTransformClassStaticBlock() { - return [ "@babel/plugin-transform-class-static-block", - ] - + ["@babel/plugin-proposal-decorators", { legacy: true }], + ["@babel/plugin-proposal-class-properties"], + ]; + } } /** @@ -205,7 +199,6 @@ function _getPluginTransformClassStaticBlock() { module.exports = function (appRoot, config = {}) { return [] .concat( - _getPluginTransformClassStaticBlock(), _getProposalDecoratorsAndClassPlugins(config), _getDebugMacroPlugins(appRoot), _getEmberModulesAPIPolyfill(appRoot, config), From a911bc72ab485f401b0e3dcbc20f50052e691b6d Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Tue, 26 Sep 2023 09:50:47 -0400 Subject: [PATCH 3/5] Add new dep --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 043a0ea9..fb5829bf 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "@babel/helper-compilation-targets": "^7.20.7", "@babel/plugin-proposal-class-properties": "^7.16.5", "@babel/plugin-proposal-decorators": "^7.20.13", + "@babel/plugin-transform-class-static-block": "^7.22.11", "@babel/plugin-proposal-private-methods": "^7.16.5", "@babel/plugin-proposal-private-property-in-object": "^7.20.5", "@babel/plugin-transform-modules-amd": "^7.20.11", From 2386fc5f5a18b14adf30df1d2fca4479506c59b3 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Tue, 26 Sep 2023 09:52:03 -0400 Subject: [PATCH 4/5] Make comment more accurate --- lib/ember-plugins.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/ember-plugins.js b/lib/ember-plugins.js index 18759e93..66509a31 100644 --- a/lib/ember-plugins.js +++ b/lib/ember-plugins.js @@ -162,18 +162,17 @@ function _getModuleResolutionPlugins(config) { function _getProposalDecoratorsAndClassPlugins(config) { if (!config.shouldIgnoreDecoratorAndClassPlugins) { return [ -/** - * Required for apps to use `@ember/template-compliation` - * for transforming templates from babel-plugin-ember-template-compilation - * which shipped in ember-cli-htmlbars 6.2 - * - * Normally this plugin wouldn't be required because the feature has shipped - * in all browsers, but because we have really old plugins, they do not - * support parsing the static block syntax, so we need to compile it away - * when using plugin-proposal-class-properties, which is required - * when using non-spec decorators. - */ - "@babel/plugin-transform-class-static-block", + /** + * Required for apps to use `@ember/template-compliation` + * for transforming templates from babel-plugin-ember-template-compilation + * which shipped in ember-cli-htmlbars 6.2 + * + * Normally this plugin wouldn't be required because the feature has shipped + * in all browsers, but because we have legacy decorators, and + * legacy decorators do not support parsing the static block syntax, + * we need to compile it away. + */ + ["@babel/plugin-transform-class-static-block"], ["@babel/plugin-proposal-decorators", { legacy: true }], ["@babel/plugin-proposal-class-properties"], ]; From 2794a87a98cf79aa1eafa790316544b71f705df8 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Tue, 26 Sep 2023 10:26:26 -0400 Subject: [PATCH 5/5] Update test --- node-tests/addon-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node-tests/addon-test.js b/node-tests/addon-test.js index e3d62f0c..27747027 100644 --- a/node-tests/addon-test.js +++ b/node-tests/addon-test.js @@ -147,7 +147,7 @@ describe('ember-cli-babel', function() { expect( output.read() ).to.deep.equal({ - "foo.js": "function _typeof(obj) { \"@babel/helpers - typeof\"; return _typeof = \"function\" == typeof Symbol && \"symbol\" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && \"function\" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : typeof obj; }, _typeof(obj); }\ndefine(\"foo\", [], function () {\n \"use strict\";\n\n var _class;\n function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }\n function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, \"prototype\", { writable: false }); return Constructor; }\n function _toPropertyKey(arg) { var key = _toPrimitive(arg, \"string\"); return _typeof(key) === \"symbol\" ? key : String(key); }\n function _toPrimitive(input, hint) { if (_typeof(input) !== \"object\" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || \"default\"); if (_typeof(res) !== \"object\") return res; throw new TypeError(\"@@toPrimitive must return a primitive value.\"); } return (hint === \"string\" ? String : Number)(input); }\n function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n function _inherits(subClass, superClass) { if (typeof superClass !== \"function\" && superClass !== null) { throw new TypeError(\"Super expression must either be null or a function\"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, \"prototype\", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }\n function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }\n function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }\n function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === \"object\" || typeof call === \"function\")) { return call; } else if (call !== void 0) { throw new TypeError(\"Derived constructors may only return object or undefined\"); } return _assertThisInitialized(self); }\n function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError(\"this hasn't been initialised - super() hasn't been called\"); } return self; }\n function _isNativeReflectConstruct() { if (typeof Reflect === \"undefined\" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === \"function\") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }\n function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }\n var Second = (_class = /*#__PURE__*/function (_Component) {\n _inherits(Second, _Component);\n var _super = _createSuper(Second);\n function Second() {\n _classCallCheck(this, Second);\n return _super.apply(this, arguments);\n }\n return _createClass(Second);\n }(Component), _class.bar = 1, _class);\n});" + "foo.js": "function _typeof(o) { \"@babel/helpers - typeof\"; return _typeof = \"function\" == typeof Symbol && \"symbol\" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && \"function\" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? \"symbol\" : typeof o; }, _typeof(o); }\ndefine(\"foo\", [], function () {\n \"use strict\";\n\n var _class;\n function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }\n function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, \"prototype\", { writable: false }); return Constructor; }\n function _toPropertyKey(arg) { var key = _toPrimitive(arg, \"string\"); return _typeof(key) === \"symbol\" ? key : String(key); }\n function _toPrimitive(input, hint) { if (_typeof(input) !== \"object\" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || \"default\"); if (_typeof(res) !== \"object\") return res; throw new TypeError(\"@@toPrimitive must return a primitive value.\"); } return (hint === \"string\" ? String : Number)(input); }\n function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n function _inherits(subClass, superClass) { if (typeof superClass !== \"function\" && superClass !== null) { throw new TypeError(\"Super expression must either be null or a function\"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, \"prototype\", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }\n function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }\n function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }\n function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === \"object\" || typeof call === \"function\")) { return call; } else if (call !== void 0) { throw new TypeError(\"Derived constructors may only return object or undefined\"); } return _assertThisInitialized(self); }\n function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError(\"this hasn't been initialised - super() hasn't been called\"); } return self; }\n function _isNativeReflectConstruct() { if (typeof Reflect === \"undefined\" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === \"function\") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }\n function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }\n var Second = (_class = /*#__PURE__*/function (_Component) {\n _inherits(Second, _Component);\n var _super = _createSuper(Second);\n function Second() {\n _classCallCheck(this, Second);\n return _super.apply(this, arguments);\n }\n return _createClass(Second);\n }(Component), _class.bar = 1, _class);\n});" }); })); });