Skip to content

Commit

Permalink
refactor: don't explicitly add optional-chaining/nullish coalescing
Browse files Browse the repository at this point in the history
As of 7.8.0, @babel/preset-env enables optional-chaining and
nullish-coalescing by default.
  • Loading branch information
jamescdavis committed Jan 27, 2020
1 parent d0db857 commit 0a85eee
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 81 deletions.
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ interface EmberCLIBabelConfig {
disablePresetEnv?: boolean;
disableEmberModulesAPIPolyfill?: boolean;
disableDecoratorTransforms?: boolean;
enableTypeScriptTransforms?: boolean;
enableTypeScriptTransform?: boolean;
extensions?: string[];
};
}
Expand Down Expand Up @@ -261,33 +261,31 @@ module.exports = function(defaults) {
}
```

#### Enabling TypeScript Transforms
#### Enabling TypeScript Transpilation

The transform plugins required for Babel to transpile TypeScript (including
transforms required for all valid TypeScript features such as optional
chaining and nullish coalescing) will automatically be enabled when
`ember-cli-typescript` >= 4.0 is installed.
The transform plugin required for Babel to transpile TypeScript will
automatically be enabled when `ember-cli-typescript` >= 4.0 is installed.

You can enable the TypeScript Babel transforms manually *without*
`ember-cli-typescript` by setting the `enableTypeScriptTransforms`to `true`.
You can enable the TypeScript Babel transform manually *without*
`ember-cli-typescript` by setting the `enableTypeScriptTransform` to `true`.

NOTE: Setting this option to `true` is not compatible with
`ember-cli-typescript` < 4.0 because of conflicting Babel plugin ordering
constraints and is unnecessary because `ember-cli-typescript` < 4.0 adds the
TypeScript Babel transforms itself.
TypeScript Babel transform itself.

NOTE: Setting this option to `true` does *not* enable type-checking. For
integrated type-checking, you will need
[`ember-cli-typescript`](https://ember-cli-typescript.com).

In an app, manually enabling the TypeScript transforms would look like:
In an app, manually enabling the TypeScript transform would look like:

```js
// ember-cli-build.js
module.exports = function(defaults) {
let app = new EmberApp(defaults, {
'ember-cli-babel': {
enableTypeScriptTransforms: true
enableTypeScriptTransform: true
}
});

Expand Down
38 changes: 4 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ module.exports = {
let userPostTransformPlugins = addonProvidedConfig.postTransformPlugins;

if (shouldHandleTypeScript) {
userPlugins = this._addTypeScriptPlugins(userPlugins.slice(), addonProvidedConfig.options);
userPlugins = this._addTypeScriptPlugin(userPlugins.slice(), addonProvidedConfig.options);
}

if (shouldIncludeDecoratorPlugins) {
Expand Down Expand Up @@ -337,8 +337,8 @@ module.exports = {

_shouldHandleTypeScript(config) {
let emberCLIBabelConfig = config['ember-cli-babel'] || {};
if (typeof emberCLIBabelConfig.enableTypeScriptTransforms === 'boolean') {
return emberCLIBabelConfig.enableTypeScriptTransforms;
if (typeof emberCLIBabelConfig.enableTypeScriptTransform === 'boolean') {
return emberCLIBabelConfig.enableTypeScriptTransform;
}
let typeScriptAddon = this.parent.addons
&& this.parent.addons.find(a => a.name === 'ember-cli-typescript');
Expand All @@ -357,39 +357,9 @@ module.exports = {
return constraints;
},

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

if (hasPlugin(plugins, '@babel/plugin-proposal-optional-chaining')) {
if (this.parent === this.project) {
this.project.ui.writeWarnLine(`${
this._parentName()
} has added the optional chaining plugin to its build, but ember-cli-babel provides this by default now when ember-cli-typescript >= 4.0 and typescript >= 3.7 are installed! You can remove the transform, or the addon that provided it.`);
}
} else {
addPlugin(
plugins,
[
require.resolve('@babel/plugin-proposal-optional-chaining'),
]
);
}

if (hasPlugin(plugins, '@babel/plugin-proposal-nullish-coalescing-operator')) {
if (this.parent === this.project) {
this.project.ui.writeWarnLine(`${
this._parentName()
} has added the nullish coalescing operator plugin to its build, but ember-cli-babel provides this by default now when ember-cli-typescript >= 4.0 and typescript >= 3.7 are installed! You can remove the transform, or the addon that provided it.`);
}
} else {
addPlugin(
plugins,
[
require.resolve('@babel/plugin-proposal-nullish-coalescing-operator'),
]
);
}

if (hasPlugin(plugins, '@babel/plugin-transform-typescript')) {
if (this.parent === this.project) {
this.project.ui.writeWarnLine(`${
Expand Down
40 changes: 6 additions & 34 deletions node-tests/addon-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -805,10 +805,10 @@ describe('ember-cli-babel', function() {
expect(this.addon._shouldHandleTypeScript({})).to.be.false;
});
it('should return true when TypeScript transforms are manually enabled', function() {
expect(this.addon._shouldHandleTypeScript({ 'ember-cli-babel': { enableTypeScriptTransforms: true } })).to.be.true;
expect(this.addon._shouldHandleTypeScript({ 'ember-cli-babel': { enableTypeScriptTransform: true } })).to.be.true;
});
it('should return false when TypeScript transforms are manually disabled', function() {
expect(this.addon._shouldHandleTypeScript({ 'ember-cli-babel': { enableTypeScriptTransforms: false } })).to.be.false;
expect(this.addon._shouldHandleTypeScript({ 'ember-cli-babel': { enableTypeScriptTransform: false } })).to.be.false;
});
it('should return false when TypeScript transforms are manually disabled, even when ember-cli-typescript >= 4.0.0-alpha.1 is installed', function() {
this.addon.parent.addons.push({
Expand All @@ -817,15 +817,11 @@ describe('ember-cli-babel', function() {
version: '4.0.0-alpha.1',
},
});
expect(this.addon._shouldHandleTypeScript({ 'ember-cli-babel': { enableTypeScriptTransforms: false } })).to.be.false;
expect(this.addon._shouldHandleTypeScript({ 'ember-cli-babel': { enableTypeScriptTransform: false } })).to.be.false;
});
});

describe('_addTypeScriptPlugins', function() {
it('should add TypeScript, optional chaining, and nullish coalescing operator plugins', function() {
expect(this.addon._addTypeScriptPlugins([], {}).length).to.equal(3, 'plugins added correctly');
});

describe('_addTypeScriptPlugin', function() {
it('should warn and not add the TypeScript plugin if already added', function() {
this.addon.project.ui = {
writeWarnLine(message) {
Expand All @@ -834,35 +830,11 @@ describe('ember-cli-babel', function() {
};

expect(
this.addon._addTypeScriptPlugins([
this.addon._addTypeScriptPlugin([
['@babel/plugin-transform-typescript']
],
{}
).length).to.equal(3, 'plugin was not added');
});

it('should warn and not add optional chaining if already added', function() {
this.addon.project.ui = {
writeWarnLine(message) {
expect(message).to.match(/has added the optional chaining plugin to its build/);
}
};

expect(this.addon._addTypeScriptPlugins([
['@babel/plugin-proposal-optional-chaining']
], {}).length).to.equal(3, 'plugin was not added');
});

it('should warn and not add nullish coalescing operator if already added', function() {
this.addon.project.ui = {
writeWarnLine(message) {
expect(message).to.match(/has added the nullish coalescing operator plugin to its build/);
}
};

expect(this.addon._addTypeScriptPlugins([
['@babel/plugin-proposal-nullish-coalescing-operator']
], {}).length).to.equal(3, 'plugin was not added');
).length).to.equal(1, 'plugin was not added');
});
});

Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
"@babel/core": "^7.8.3",
"@babel/plugin-proposal-class-properties": "^7.8.3",
"@babel/plugin-proposal-decorators": "^7.8.3",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.8.3",
"@babel/plugin-proposal-optional-chaining": "^7.8.3",
"@babel/plugin-transform-modules-amd": "^7.8.3",
"@babel/plugin-transform-runtime": "^7.8.3",
"@babel/plugin-transform-typescript": "^7.8.3",
Expand Down

0 comments on commit 0a85eee

Please sign in to comment.