Skip to content

Commit

Permalink
[BUGFIX] Make loose mode in class properties transform respect Babel …
Browse files Browse the repository at this point in the history
…settings

Up until recently, loose mode was required to be enabled in class properties if
decorators were also included. This behavior made undecorated class properties
behave unlike the spec, but _decorated_ class properties still did behave like
the spec, counterintuitively. It also meant that we weren't following the user's
settings for loose mode, if they existed.

This PR makes the setting part of the user's standard Babel `loose` setting.
  • Loading branch information
Chris Garrett committed Nov 11, 2019
1 parent 738080f commit 31a7586
Show file tree
Hide file tree
Showing 4 changed files with 415 additions and 221 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ module.exports = {
let userPostTransformPlugins = addonProvidedConfig.postTransformPlugins;

if (shouldIncludeDecoratorPlugins) {
userPlugins = this._addDecoratorPlugins(userPlugins.slice());
userPlugins = this._addDecoratorPlugins(userPlugins.slice(), addonProvidedConfig.options);
}

options.plugins = [].concat(
Expand Down Expand Up @@ -326,7 +326,7 @@ module.exports = {
return customOptions.disableDecoratorTransforms !== true;
},

_addDecoratorPlugins(plugins) {
_addDecoratorPlugins(plugins, options) {
const { hasPlugin, addPlugin } = require('ember-cli-babel-plugin-helpers');

if (hasPlugin(plugins, '@babel/plugin-proposal-decorators')) {
Expand Down Expand Up @@ -355,7 +355,7 @@ module.exports = {
} else {
addPlugin(
plugins,
[require.resolve('@babel/plugin-proposal-class-properties'), { loose: true }],
[require.resolve('@babel/plugin-proposal-class-properties'), { loose: options.loose || false }],
{
after: ['@babel/plugin-proposal-decorators'],
before: ['@babel/plugin-transform-typescript']
Expand Down
32 changes: 25 additions & 7 deletions node-tests/addon-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ describe('ember-cli-babel', function() {

describe('_addDecoratorPlugins', function() {
it('should include babel transforms by default', function() {
expect(this.addon._addDecoratorPlugins([]).length).to.equal(2, 'plugins added correctly');
expect(this.addon._addDecoratorPlugins([], {}).length).to.equal(2, 'plugins added correctly');
});

it('should include only fields if it detects decorators plugin', function() {
Expand All @@ -758,9 +758,12 @@ describe('ember-cli-babel', function() {
}
};

expect(this.addon._addDecoratorPlugins([
['@babel/plugin-proposal-decorators']
]).length).to.equal(2, 'plugins were not added');
expect(
this.addon._addDecoratorPlugins([
['@babel/plugin-proposal-decorators']
],
{}
).length).to.equal(2, 'plugins were not added');
});

it('should include only decorators if it detects class fields plugin', function() {
Expand All @@ -770,9 +773,24 @@ describe('ember-cli-babel', function() {
}
};

expect(this.addon._addDecoratorPlugins([
['@babel/plugin-proposal-class-properties']
]).length).to.equal(2, 'plugins were not added');
expect(
this.addon._addDecoratorPlugins(
[
['@babel/plugin-proposal-class-properties']
],
{}
).length
).to.equal(2, 'plugins were not added');
});

it('should use babel options loose mode for class properties', function() {
let strictPlugins = this.addon._addDecoratorPlugins([], {});

expect(strictPlugins[1][1].loose).to.equal(false, 'loose is false if no option is provided');

let loosePlugins = this.addon._addDecoratorPlugins([], { loose: true });

expect(loosePlugins[1][1].loose).to.equal(true, 'loose setting added correctly');
});
});

Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@
"test:node:debug": "mocha debug node-tests"
},
"dependencies": {
"@babel/core": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.3.4",
"@babel/plugin-proposal-decorators": "^7.3.0",
"@babel/plugin-transform-modules-amd": "^7.0.0",
"@babel/plugin-transform-runtime": "^7.2.0",
"@babel/polyfill": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/runtime": "^7.2.0",
"@babel/core": "^7.7.0",
"@babel/plugin-proposal-class-properties": "^7.7.0",
"@babel/plugin-proposal-decorators": "^7.7.0",
"@babel/plugin-transform-modules-amd": "^7.5.0",
"@babel/plugin-transform-runtime": "^7.6.0",
"@babel/polyfill": "^7.7.0",
"@babel/preset-env": "^7.7.0",
"@babel/runtime": "^7.7.0",
"amd-name-resolver": "^1.2.1",
"babel-plugin-debug-macros": "^0.3.0",
"babel-plugin-ember-modules-api-polyfill": "^2.12.0",
Expand Down

0 comments on commit 31a7586

Please sign in to comment.